[Part::CheckGeometry] add edwilliams16's code for some global properties to the advanced shape content
This commit is contained in:
81
src/Mod/Part/BasicShapes/ShapeContent.py
Normal file
81
src/Mod/Part/BasicShapes/ShapeContent.py
Normal file
@@ -0,0 +1,81 @@
|
||||
#! python
|
||||
# -*- coding: utf-8 -*-
|
||||
# ShapeContent.py
|
||||
# 2021, by Mark Ganson <TheMarkster>
|
||||
# LGPL 2.1 or later
|
||||
|
||||
# this file is called from c++ in TaskCheckGeometry.cpp
|
||||
# from buildShapeContent()
|
||||
|
||||
import FreeCAD as App
|
||||
|
||||
def roundVector(v,dec):
|
||||
return str([round(v[0],dec), round(v[1],dec), round(v[2],dec)])
|
||||
|
||||
def buildShapeContent(basename, decimals=2, advancedShapeContent=True):
|
||||
obj = basename[basename.index('.')+1:] #note: obj is a string, so is doc
|
||||
doc = basename[:basename.index(obj)-1]
|
||||
shp = App.getDocument(doc).getObject(obj).Shape
|
||||
typeStr = str(shp.ShapeType)
|
||||
result = doc+'.'+App.getDocument(doc).getObject(obj).Label+' ('+obj+'):\n'
|
||||
result += 'Shape type: '+typeStr+'\n'
|
||||
result += 'Vertices: '+str(len(shp.Vertexes))+'\n'
|
||||
result += 'Edges: '+str(len(shp.Edges))+'\n'
|
||||
result += 'Wires: '+str(len(shp.Wires))+'\n'
|
||||
result += 'Faces: '+str(len(shp.Faces))+'\n'
|
||||
result += 'Shells: '+str(len(shp.Shells))+'\n'
|
||||
result += 'Solids: '+str(len(shp.Solids))+'\n'
|
||||
result += 'CompSolids: '+str(len(shp.CompSolids))+'\n'
|
||||
result += 'Compounds: '+str(len(shp.Compounds))+'\n'
|
||||
result += 'Shapes: '+str(len(shp.Vertexes+shp.Edges+shp.Wires+shp.Faces+shp.Shells+shp.Solids+shp.CompSolids+shp.Compounds))+'\n'
|
||||
if advancedShapeContent:
|
||||
result += '----------\n'
|
||||
if hasattr(shp,'Area') and not 'Wire' in typeStr and not 'Edge' in typeStr and not 'Vertex' in typeStr:
|
||||
result += 'Area: '+str(round(shp.Area, decimals))+'\n'
|
||||
if hasattr(shp,'Volume') and not 'Wire' in typeStr and not 'Edge' in typeStr and not 'Vertex' in typeStr and not 'Face' in typeStr:
|
||||
result += 'Volume: '+str(round(shp.Volume, decimals))+'\n'
|
||||
if hasattr(shp,'Mass'):
|
||||
result += 'Mass: '+str(round(shp.Mass, decimals))+'\n'
|
||||
if hasattr(shp,'Length'):
|
||||
result += 'Length: '+str(round(shp.Length, decimals))+'\n'
|
||||
if hasattr(shp,'Curve') and hasattr(shp.Curve,'Radius'):
|
||||
result += 'Radius: '+str(round(shp.Curve.Radius, decimals))+'\n'
|
||||
if hasattr(shp,'Curve') and hasattr(shp.Curve,'Center'):
|
||||
result += 'Curve center: '+str([round(vv,decimals) for vv in shp.Curve.Center])+'\n'
|
||||
if hasattr(shp,'Curve') and hasattr(shp.Curve,'Continuity'):
|
||||
result += 'Continuity: '+str(shp.Curve.Continuity)+'\n'
|
||||
if hasattr(shp,'CenterOfMass'):
|
||||
result += 'CenterOfMass: '+roundVector(shp.CenterOfMass,decimals)+'\n'
|
||||
if hasattr(shp,'normalAt'):
|
||||
try:
|
||||
result += 'normalAt(0): '+str([round(vv,decimals) for vv in shp.normalAt(0)]) +'\n'
|
||||
except Exception:
|
||||
try:
|
||||
result += 'normalAt(0,0): '+str([round(vv,decimals) for vv in shp.normalAt(0,0)]) +'\n'
|
||||
except Exception:
|
||||
pass
|
||||
if hasattr(shp, 'isClosed') and ('Wire' in typeStr or 'Edge' in typeStr):
|
||||
result += 'isClosed: '+str(shp.isClosed())+'\n'
|
||||
if hasattr(shp, 'Orientation'):
|
||||
result += 'Orientation: '+str(shp.Orientation)+'\n'
|
||||
if hasattr(shp, 'PrincipalProperties'):
|
||||
props = shp.PrincipalProperties
|
||||
for p in props:
|
||||
if isinstance(props[p], App.Vector) or isinstance(props[p], tuple):
|
||||
result += str(p)+': '+roundVector(props[p],decimals) +'\n'
|
||||
else:
|
||||
result += str(p)+': '+str(props[p])+'\n'
|
||||
object = App.getDocument(doc).getObject(obj)
|
||||
if object.getGlobalPlacement() != object.Placement:
|
||||
rpl = object.getGlobalPlacement() * object.Placement.inverse()
|
||||
rot = rpl.Rotation
|
||||
if hasattr(shp, 'CenterOfMass'):
|
||||
result += 'Global CenterOfMass: '+roundVector(rpl.multVec(shp.CenterOfMass),decimals)+'\n'
|
||||
if hasattr(shp, 'PrincipalProperties'):
|
||||
props = shp.PrincipalProperties
|
||||
for p in props:
|
||||
if 'AxisOfInertia' in p:
|
||||
result += 'Global ' + str(p)+': '+roundVector(rot.multVec(props[p]),decimals) +'\n'
|
||||
else:
|
||||
result += 'Global Placement = Placement'
|
||||
return result
|
||||
@@ -34,6 +34,7 @@ set(BasicShapes_Scripts
|
||||
|
||||
if(BUILD_GUI)
|
||||
list (APPEND BasicShapes_Scripts
|
||||
BasicShapes/ShapeContent.py
|
||||
BasicShapes/CommandShapes.py
|
||||
BasicShapes/ViewProviderShapes.py
|
||||
BasicShapes/TaskTube.ui
|
||||
|
||||
@@ -64,6 +64,7 @@
|
||||
#endif //_PreComp_
|
||||
|
||||
#include "../App/PartFeature.h"
|
||||
#include "../App/TopoShapePy.h"
|
||||
#include <Base/Interpreter.h>
|
||||
#include <Gui/BitmapFactory.h>
|
||||
#include <Gui/Selection.h>
|
||||
@@ -586,79 +587,32 @@ void TaskCheckGeometryResults::checkSub(const BRepCheck_Analyzer &shapeCheck, co
|
||||
|
||||
void TaskCheckGeometryResults::buildShapeContent(const QString &baseName, const TopoDS_Shape &shape)
|
||||
{
|
||||
ParameterGrp::handle group = App::GetApplication().GetUserParameter().
|
||||
GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Units");
|
||||
int decimals = group->GetInt("Decimals", 2);
|
||||
group = App::GetApplication().GetUserParameter().
|
||||
GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod")->GetGroup("Part")->GetGroup("CheckGeometry");
|
||||
bool advancedShapeContent = group->GetBool("AdvancedShapeContent", true);
|
||||
|
||||
bool advancedShapeContent = App::GetApplication().GetUserParameter().GetGroup("BaseApp")->GetGroup("Preferences")->
|
||||
GetGroup("Mod")->GetGroup("Part")->GetGroup("CheckGeometry")->GetBool("AdvancedShapeContent", true);
|
||||
int decimals = App::GetApplication().GetUserParameter().
|
||||
GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Units")->GetInt("Decimals", 2);
|
||||
std::ostringstream stream;
|
||||
if (!shapeContentString.empty())
|
||||
stream << std::endl << std::endl;
|
||||
stream << "Checked object: ";
|
||||
std::ostringstream cmdstream;
|
||||
cmdstream << "_basename = '" << baseName.toStdString().c_str() << "'" << std::endl;
|
||||
cmdstream << "_obj = _basename[_basename.index('.')+1:]" << std::endl;
|
||||
cmdstream << "_doc = _basename[:_basename.index(_obj)-1]" << std::endl;
|
||||
cmdstream << "_shp = App.ActiveDocument.getObject(_obj).Shape" << std::endl;
|
||||
cmdstream << "_type = str(_shp.ShapeType)" << std::endl;
|
||||
cmdstream << "_result = _doc+'.'+App.ActiveDocument.getObject(_obj).Label+' ('+_obj+'):\\n'" << std::endl;
|
||||
cmdstream << "_result += 'Shape type: '+_type+'\\n'" << std::endl;
|
||||
cmdstream << "_result += 'Vertices: '+str(len(_shp.Vertexes))+'\\n'" << std::endl;
|
||||
cmdstream << "_result += 'Edges: '+str(len(_shp.Edges))+'\\n'" << std::endl;
|
||||
cmdstream << "_result += 'Wires: '+str(len(_shp.Wires))+'\\n'" << std::endl;
|
||||
cmdstream << "_result += 'Faces: '+str(len(_shp.Faces))+'\\n'" << std::endl;
|
||||
cmdstream << "_result += 'Shells: '+str(len(_shp.Shells))+'\\n'" << std::endl;
|
||||
cmdstream << "_result += 'Solids: '+str(len(_shp.Solids))+'\\n'" << std::endl;
|
||||
cmdstream << "_result += 'CompSolids: '+str(len(_shp.CompSolids))+'\\n'" << std::endl;
|
||||
cmdstream << "_result += 'Compounds: '+str(len(_shp.Compounds))+'\\n'" << std::endl;
|
||||
cmdstream << "_result += 'Shapes: '+str(len(_shp.Vertexes+_shp.Edges+_shp.Wires+_shp.Faces+_shp.Shells+_shp.Solids+_shp.CompSolids+_shp.Compounds))+'\\n'" << std::endl;
|
||||
if (advancedShapeContent){
|
||||
cmdstream << "_result += '----------\\n'" << std::endl;
|
||||
cmdstream << "if hasattr(_shp,'Area') and not 'Wire' in _type and not 'Edge' in _type and not 'Vertex' in _type:" << std::endl;
|
||||
cmdstream << " _result += 'Area: '+str(round(_shp.Area, " << decimals << "))+'\\n'" << std::endl;
|
||||
cmdstream << "if hasattr(_shp,'Volume') and not 'Wire' in _type and not 'Edge' in _type and not 'Vertex' in _type and not 'Face' in _type:" << std::endl;
|
||||
cmdstream << " _result += 'Volume: '+str(round(_shp.Volume, " << decimals << "))+'\\n'" << std::endl;
|
||||
cmdstream << "if hasattr(_shp,'Mass'):" << std::endl;
|
||||
cmdstream << " _result += 'Mass: '+str(round(_shp.Mass, " << decimals << "))+'\\n'" << std::endl;
|
||||
cmdstream << "if hasattr(_shp,'Length'):" << std::endl;
|
||||
cmdstream << " _result += 'Length: '+str(round(_shp.Length, " << decimals << "))+'\\n'" << std::endl;
|
||||
cmdstream << "if hasattr(_shp,'Curve') and hasattr(_shp.Curve,'Radius'):" << std::endl;
|
||||
cmdstream << " _result += 'Radius: '+str(round(_shp.Curve.Radius, " << decimals << "))+'\\n'" << std::endl;
|
||||
cmdstream << "if hasattr(_shp,'Curve') and hasattr(_shp.Curve,'Center'):" << std::endl;
|
||||
cmdstream << " _result += 'Curve center: '+str([round(vv," << decimals << ") for vv in _shp.Curve.Center])+'\\n'" << std::endl;
|
||||
cmdstream << "if hasattr(_shp,'Curve') and hasattr(_shp.Curve,'Continuity'):" << std::endl;
|
||||
cmdstream << " _result += 'Continuity: '+str(_shp.Curve.Continuity)+'\\n'" << std::endl;
|
||||
cmdstream << "if hasattr(_shp,'CenterOfMass'):" << std::endl;
|
||||
cmdstream << " _result += 'CenterOfMass: '+str([round(vv," << decimals << ") for vv in _shp.CenterOfMass])+'\\n'" << std::endl;
|
||||
cmdstream << "if hasattr(_shp,'normalAt'):" << std::endl;
|
||||
cmdstream << " try:" << std::endl;
|
||||
cmdstream << " _result += 'normalAt(0): '+str([round(vv," << decimals << ") for vv in _shp.normalAt(0)]) +'\\n'" << std::endl;
|
||||
cmdstream << " except Exception:" << std::endl;
|
||||
cmdstream << " try:" << std::endl;
|
||||
cmdstream << " _result += 'normalAt(0,0): '+str([round(vv," << decimals << ") for vv in _shp.normalAt(0,0)]) +'\\n'" << std::endl;
|
||||
cmdstream << " except Exception:" << std::endl;
|
||||
cmdstream << " pass" << std::endl;
|
||||
cmdstream << "if hasattr(_shp, 'isClosed') and ('Wire' in _type or 'Edge' in _type):" << std::endl;
|
||||
cmdstream << " _result += 'isClosed: '+str(_shp.isClosed())+'\\n'" << std::endl;
|
||||
cmdstream << "if hasattr(_shp, 'Orientation'):" << std::endl;
|
||||
cmdstream << " _result += 'Orientation: '+str(_shp.Orientation)+'\\n'" << std::endl;
|
||||
cmdstream << "if hasattr(_shp, 'PrincipalProperties'):" << std::endl;
|
||||
cmdstream << " _props = _shp.PrincipalProperties" << std::endl;
|
||||
cmdstream << " for _p in _props:" << std::endl;
|
||||
cmdstream << " if 'Base.Vector' in str(type(_props[_p])) or 'tuple' in str(type(_props[_p])):" << std::endl;
|
||||
cmdstream << " _result += str(_p)+': '+str([round(vv," << decimals << ") for vv in _props[_p]]) +'\\n'" << std::endl;
|
||||
cmdstream << " else:" << std::endl;
|
||||
cmdstream << " _result += str(_p)+': '+str(_props[_p])+'\\n'" << std::endl;
|
||||
}
|
||||
|
||||
std::string cmd = cmdstream.str();
|
||||
|
||||
Base::PyGILStateLocker lock;
|
||||
try {
|
||||
std::string result = Base::Interpreter().runStringWithKey(cmd.c_str(),"_result");
|
||||
stream << result;
|
||||
PyObject* module = PyImport_ImportModule("BasicShapes.ShapeContent");
|
||||
if (!module) {
|
||||
throw Py::Exception();
|
||||
}
|
||||
Py::Tuple args(3);
|
||||
args.setItem(0, Py::String(baseName.toStdString().c_str()));
|
||||
args.setItem(1, Py::Long(decimals));
|
||||
args.setItem(2, Py::Boolean(advancedShapeContent));
|
||||
Py::Module shapecontent(module, true);
|
||||
Py::String result(shapecontent.callMemberFunction("buildShapeContent", args));
|
||||
stream << result.as_std_string("ascii");
|
||||
}
|
||||
catch (Base::PyException&) { //script had runtime error so fall back on OCCT method
|
||||
catch (Py::Exception&) {
|
||||
Base::PyException e;
|
||||
e.ReportException();
|
||||
stream << baseName.toLatin1().data() << std::endl;
|
||||
BRepTools_ShapeSet set;
|
||||
set.Add(shape);
|
||||
|
||||
Reference in New Issue
Block a user