Skip to content

Commit

Permalink
Part: TopoShapePy: made Solid constructor accept CompSolid
Browse files Browse the repository at this point in the history
Part.Solid(shape) now accepts compsolid as input, and creates a solid by
joining the compsolid.

Same done to Part.makeSolid().

+ change exception handling to expose the error message.
  • Loading branch information
DeepSOIC committed Jul 4, 2016
1 parent 885fecb commit ca0a640
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 27 deletions.
50 changes: 36 additions & 14 deletions src/Mod/Part/App/AppPartPy.cpp
Expand Up @@ -256,7 +256,7 @@ class Module : public Py::ExtensionModule<Module>
"makeFilledFace(list) -- Create a face out of a list of edges."
);
add_varargs_method("makeSolid",&Module::makeSolid,
"makeSolid(shape) -- Create a solid out of the shells inside a shape."
"makeSolid(shape): Create a solid out of shells of shape. If shape is a compsolid, the overall volume solid is created."
);
add_varargs_method("makePlane",&Module::makePlane,
"makePlane(length,width,[pnt,dirZ,dirX]) -- Make a plane\n"
Expand Down Expand Up @@ -693,25 +693,47 @@ class Module : public Py::ExtensionModule<Module>
throw Py::Exception();

try {
BRepBuilderAPI_MakeSolid mkSolid;
const TopoDS_Shape& shape = static_cast<TopoShapePy*>(obj)
->getTopoShapePtr()->_Shape;
TopExp_Explorer anExp (shape, TopAbs_SHELL);
//first, if we were given a compsolid, try making a solid out of it
TopExp_Explorer CSExp (shape, TopAbs_COMPSOLID);
TopoDS_CompSolid compsolid;
int count=0;
for (; anExp.More(); anExp.Next()) {
for (; CSExp.More(); CSExp.Next()) {
++count;
mkSolid.Add(TopoDS::Shell(anExp.Current()));
compsolid = TopoDS::CompSolid(CSExp.Current());
if (count > 1)
break;
}
if (count == 0) {
//no compsolids. Get shells...
BRepBuilderAPI_MakeSolid mkSolid;
TopExp_Explorer anExp (shape, TopAbs_SHELL);
count=0;
for (; anExp.More(); anExp.Next()) {
++count;
mkSolid.Add(TopoDS::Shell(anExp.Current()));
}

if (count == 0)
Standard_Failure::Raise("No shells found in shape");

TopoDS_Solid solid = mkSolid.Solid();
BRepLib::OrientClosedSolid(solid);
return Py::asObject(new TopoShapeSolidPy(new TopoShape(solid)));
}
catch (Standard_Failure) {
throw Py::Exception(PartExceptionOCCError, "creation of solid failed");
if (count == 0)//no shells?
Standard_Failure::Raise("No shells or compsolids found in shape");

TopoDS_Solid solid = mkSolid.Solid();
BRepLib::OrientClosedSolid(solid);
return Py::asObject(new TopoShapeSolidPy(new TopoShape(solid)));
} else if (count == 1) {
BRepBuilderAPI_MakeSolid mkSolid(compsolid);
TopoDS_Solid solid = mkSolid.Solid();
return Py::asObject(new TopoShapeSolidPy(new TopoShape(solid)));
} else { // if (count > 1)
Standard_Failure::Raise("Only one compsolid can be accepted. Provided shape has more than one compsolid.");
return Py::None(); //prevents compiler warning
}
}
catch (Standard_Failure err) {
std::stringstream errmsg;
errmsg << "Creation of solid failed: " << err.GetMessageString();
throw Py::Exception(PartExceptionOCCError, errmsg.str().c_str());
}
}
Py::Object makePlane(const Py::Tuple& args)
Expand Down
4 changes: 2 additions & 2 deletions src/Mod/Part/App/TopoShapeSolidPy.xml
Expand Up @@ -12,7 +12,7 @@
Constructor="true">
<Documentation>
<Author Licence="LGPL" Name="Juergen Riegel" EMail="Juergen.Riegel@web.de" />
<UserDocu>Create a solid out of the shells of a shape</UserDocu>
<UserDocu>Part.Solid(shape): Create a solid out of shells of shape. If shape is a compsolid, the overall volume solid is created.</UserDocu>
</Documentation>
<Attribute Name="Mass" ReadOnly="true">
<Documentation>
Expand Down Expand Up @@ -101,4 +101,4 @@ solid.offsetFaces((solid.Faces[0],solid.Faces[1]), 1.5)
</Documentation>
</Methode>
</PythonExport>
</GenerateModel>
</GenerateModel>
45 changes: 34 additions & 11 deletions src/Mod/Part/App/TopoShapeSolidPyImp.cpp
Expand Up @@ -39,6 +39,7 @@
#include <TopoDS.hxx>
#include <TopoDS_Solid.hxx>
#include <TopoDS_Shell.hxx>
#include <TopoDS_CompSolid.hxx>
#include <gp_Ax1.hxx>
#include <gp_Pnt.hxx>
#include <gp_Dir.hxx>
Expand Down Expand Up @@ -81,25 +82,47 @@ int TopoShapeSolidPy::PyInit(PyObject* args, PyObject* /*kwd*/)
return -1;

try {
BRepBuilderAPI_MakeSolid mkSolid;
const TopoDS_Shape& shape = static_cast<TopoShapePy*>(obj)
->getTopoShapePtr()->_Shape;
TopExp_Explorer anExp (shape, TopAbs_SHELL);
//first, if we were given a compsolid, try making a solid out of it
TopExp_Explorer CSExp (shape, TopAbs_COMPSOLID);
TopoDS_CompSolid compsolid;
int count=0;
for (; anExp.More(); anExp.Next()) {
for (; CSExp.More(); CSExp.Next()) {
++count;
mkSolid.Add(TopoDS::Shell(anExp.Current()));
compsolid = TopoDS::CompSolid(CSExp.Current());
if (count > 1)
break;
}
if (count == 0) {
//no compsolids. Get shells...
BRepBuilderAPI_MakeSolid mkSolid;
TopExp_Explorer anExp (shape, TopAbs_SHELL);
count=0;
for (; anExp.More(); anExp.Next()) {
++count;
mkSolid.Add(TopoDS::Shell(anExp.Current()));
}

if (count == 0)
Standard_Failure::Raise("No shells found in shape");
if (count == 0)//no shells?
Standard_Failure::Raise("No shells or compsolids found in shape");

TopoDS_Solid solid = mkSolid.Solid();
BRepLib::OrientClosedSolid(solid);
getTopoShapePtr()->_Shape = solid;
} else if (count == 1) {
BRepBuilderAPI_MakeSolid mkSolid(compsolid);
TopoDS_Solid solid = mkSolid.Solid();
getTopoShapePtr()->_Shape = solid;
} else if (count > 1) {
Standard_Failure::Raise("Only one compsolid can be accepted. Provided shape has more than one compsolid.");
}

TopoDS_Solid solid = mkSolid.Solid();
BRepLib::OrientClosedSolid(solid);
getTopoShapePtr()->_Shape = solid;
}
catch (Standard_Failure) {
PyErr_SetString(PartExceptionOCCError, "creation of solid failed");
catch (Standard_Failure err) {
std::stringstream errmsg;
errmsg << "Creation of solid failed: " << err.GetMessageString();
PyErr_SetString(PartExceptionOCCError, errmsg.str().c_str());
return -1;
}

Expand Down

0 comments on commit ca0a640

Please sign in to comment.