diff --git a/src/Mod/Part/App/AppPartPy.cpp b/src/Mod/Part/App/AppPartPy.cpp index a80c06b244..ecd206c314 100644 --- a/src/Mod/Part/App/AppPartPy.cpp +++ b/src/Mod/Part/App/AppPartPy.cpp @@ -24,6 +24,7 @@ #ifndef _PreComp_ # include # include +# include # include # include # include @@ -125,6 +126,7 @@ #include "ImportStep.h" #include "edgecluster.h" #include "FaceMaker.h" +#include "PartPyCXX.h" #ifdef FCUseFreeType # include "FT2FC.h" @@ -132,6 +134,10 @@ extern const char* BRepBuilderAPI_FaceErrorText(BRepBuilderAPI_FaceError fe); +namespace Part { +extern Py::Object shape2pyshape(const TopoDS_Shape &shape); +} + #ifndef M_PI #define M_PI 3.14159265358979323846 /* pi */ #endif @@ -368,6 +374,26 @@ public: add_varargs_method("makeWireString",&Module::makeWireString, "makeWireString(string,fontdir,fontfile,height,[track]) -- Make list of wires in the form of a string's characters." ); + add_varargs_method("makeSplitShape",&Module::makeSplitShape, + "makeSplitShape(shape, list of shape pairs,[check Interior=True]) -> two lists of shapes.\n" + "The following shape pairs are supported:\n" + "* Wire, Face\n" + "* Edge, Face\n" + "* Compound, Face\n" + "* Edge, Edge\n" + "* The face must be part of the specified shape and the edge, wire or compound must\n" + "lie on the face.\n" + "Output:\n" + "The first list contains the faces that are the left of the projected wires.\n" + "The second list contains the left part on the shape.\n\n" + "Example:\n" + "face = ...\n" + "edges = ...\n" + "split = [(edges[0],face),(edges[1],face)]\n" + "r = Part.makeSplitShape(face, split)\n" + "Part.show(r[0][0])\n" + "Part.show(r[1][0])\n" + ); add_varargs_method("exportUnits",&Module::exportUnits, "exportUnits([string=MM|M|IN]) -- Set units for exporting STEP/IGES files and returns the units." ); @@ -1545,6 +1571,80 @@ private: return Py::asObject(new TopoShapePy(new TopoShape(aResult))); #endif } + Py::Object makeSplitShape(const Py::Tuple& args) + { + PyObject* shape; + PyObject* list; + PyObject* checkInterior = Py_True; + if (!PyArg_ParseTuple(args.ptr(), "O!O|O!", &(TopoShapePy::Type), &shape, &list, + &PyBool_Type, &checkInterior)) + throw Py::Exception(); + + try { + TopoDS_Shape initShape = static_cast + (shape)->getTopoShapePtr()->getShape(); + BRepFeat_SplitShape splitShape(initShape); + splitShape.SetCheckInterior(PyObject_IsTrue(checkInterior) ? Standard_True : Standard_False); + + Py::Sequence seq(list); + for (Py::Sequence::iterator it = seq.begin(); it != seq.end(); ++it) { + Py::Tuple tuple(*it); + Py::TopoShape sh1(tuple[0]); + Py::TopoShape sh2(tuple[1]); + const TopoDS_Shape& shape1= sh1.extensionObject()->getTopoShapePtr()->getShape(); + const TopoDS_Shape& shape2= sh2.extensionObject()->getTopoShapePtr()->getShape(); + if (shape1.IsNull() || shape2.IsNull()) + throw Py::RuntimeError("Cannot add null shape"); + if (shape2.ShapeType() == TopAbs_FACE) { + if (shape1.ShapeType() == TopAbs_EDGE) { + splitShape.Add(TopoDS::Edge(shape1), TopoDS::Face(shape2)); + } + else if (shape1.ShapeType() == TopAbs_WIRE) { + splitShape.Add(TopoDS::Wire(shape1), TopoDS::Face(shape2)); + } + else if (shape1.ShapeType() == TopAbs_COMPOUND) { + splitShape.Add(TopoDS::Compound(shape1), TopoDS::Face(shape2)); + } + else { + throw Py::TypeError("First item in tuple must be Edge, Wire or Compound"); + } + } + else if (shape2.ShapeType() == TopAbs_EDGE) { + if (shape1.ShapeType() == TopAbs_EDGE) { + splitShape.Add(TopoDS::Edge(shape1), TopoDS::Edge(shape2)); + } + else { + throw Py::TypeError("First item in tuple must be Edge"); + } + } + else { + throw Py::TypeError("Second item in tuple must be Face or Edge"); + } + } + + splitShape.Build(); + const TopTools_ListOfShape& d = splitShape.DirectLeft(); + const TopTools_ListOfShape& l = splitShape.Left(); + + Py::List list1; + for (TopTools_ListIteratorOfListOfShape it(d); it.More(); it.Next()) { + list1.append(shape2pyshape(it.Value())); + } + + Py::List list2; + for (TopTools_ListIteratorOfListOfShape it(l); it.More(); it.Next()) { + list2.append(shape2pyshape(it.Value())); + } + + Py::Tuple tuple(2); + tuple.setItem(0, list1); + tuple.setItem(1, list2); + return tuple; + } + catch (Standard_Failure& e) { + throw Py::Exception(PartExceptionOCCError, e.GetMessageString()); + } + } Py::Object makeWireString(const Py::Tuple& args) { #ifdef FCUseFreeType