From ca0a640aed4e86c0f03d35213d029b0bbcd5150e Mon Sep 17 00:00:00 2001 From: DeepSOIC Date: Mon, 4 Jul 2016 14:58:53 +0300 Subject: [PATCH] Part: TopoShapePy: made Solid constructor accept CompSolid 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. --- src/Mod/Part/App/AppPartPy.cpp | 50 +++++++++++++++++------- src/Mod/Part/App/TopoShapeSolidPy.xml | 4 +- src/Mod/Part/App/TopoShapeSolidPyImp.cpp | 45 +++++++++++++++------ 3 files changed, 72 insertions(+), 27 deletions(-) diff --git a/src/Mod/Part/App/AppPartPy.cpp b/src/Mod/Part/App/AppPartPy.cpp index 7572d4b1c28f..32aa8a33d2a1 100644 --- a/src/Mod/Part/App/AppPartPy.cpp +++ b/src/Mod/Part/App/AppPartPy.cpp @@ -256,7 +256,7 @@ class Module : public Py::ExtensionModule "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" @@ -693,25 +693,47 @@ class Module : public Py::ExtensionModule throw Py::Exception(); try { - BRepBuilderAPI_MakeSolid mkSolid; const TopoDS_Shape& shape = static_cast(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) diff --git a/src/Mod/Part/App/TopoShapeSolidPy.xml b/src/Mod/Part/App/TopoShapeSolidPy.xml index 4f9d2a3e3dfa..3b4203aea5dd 100644 --- a/src/Mod/Part/App/TopoShapeSolidPy.xml +++ b/src/Mod/Part/App/TopoShapeSolidPy.xml @@ -12,7 +12,7 @@ Constructor="true"> - Create a solid out of the shells of a shape + Part.Solid(shape): Create a solid out of shells of shape. If shape is a compsolid, the overall volume solid is created. @@ -101,4 +101,4 @@ solid.offsetFaces((solid.Faces[0],solid.Faces[1]), 1.5) - + diff --git a/src/Mod/Part/App/TopoShapeSolidPyImp.cpp b/src/Mod/Part/App/TopoShapeSolidPyImp.cpp index cf4e123ababb..a8421674c137 100644 --- a/src/Mod/Part/App/TopoShapeSolidPyImp.cpp +++ b/src/Mod/Part/App/TopoShapeSolidPyImp.cpp @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -81,25 +82,47 @@ int TopoShapeSolidPy::PyInit(PyObject* args, PyObject* /*kwd*/) return -1; try { - BRepBuilderAPI_MakeSolid mkSolid; const TopoDS_Shape& shape = static_cast(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; }