From e1b56f855187b77282b4a751ea553fda8644751b Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 16 Oct 2020 13:50:29 +0200 Subject: [PATCH] Part: expose BRepFeat_MakePrism to Python --- src/Mod/Part/App/AppPart.cpp | 5 + src/Mod/Part/App/AppPartPy.cpp | 13 + src/Mod/Part/App/BRepFeat/MakePrismPy.xml | 100 ++++++ src/Mod/Part/App/BRepFeat/MakePrismPyImp.cpp | 331 +++++++++++++++++++ src/Mod/Part/App/CMakeLists.txt | 12 + 5 files changed, 461 insertions(+) create mode 100644 src/Mod/Part/App/BRepFeat/MakePrismPy.xml create mode 100644 src/Mod/Part/App/BRepFeat/MakePrismPyImp.cpp diff --git a/src/Mod/Part/App/AppPart.cpp b/src/Mod/Part/App/AppPart.cpp index 1406ea900f..ba5ae7d84d 100644 --- a/src/Mod/Part/App/AppPart.cpp +++ b/src/Mod/Part/App/AppPart.cpp @@ -108,6 +108,7 @@ #include "Mod/Part/App/BRepOffsetAPI_MakeFillingPy.h" #include "Mod/Part/App/PartFeaturePy.h" #include "Mod/Part/App/AttachEnginePy.h" +#include #include #include #include @@ -376,6 +377,10 @@ PyMOD_INIT_FUNC(Part) Base::Interpreter().addType(&Part::GeometryBoolExtensionPy ::Type,partModule,"GeometryBoolExtension"); Base::Interpreter().addType(&Part::GeometryDoubleExtensionPy ::Type,partModule,"GeometryDoubleExtension"); + // BRepFeat package + PyObject* brepfeatModule(module.getAttr("BRepFeat").ptr()); + Base::Interpreter().addType(&Part::MakePrismPy::Type,brepfeatModule,"MakePrism"); + // BRepOffsetAPI package PyObject* brepOffsetApiModule(module.getAttr("BRepOffsetAPI").ptr()); Base::Interpreter().addType(&Part::BRepOffsetAPI_MakePipeShellPy::Type,brepOffsetApiModule,"MakePipeShell"); diff --git a/src/Mod/Part/App/AppPartPy.cpp b/src/Mod/Part/App/AppPartPy.cpp index a258c33892..8bafa61b1d 100644 --- a/src/Mod/Part/App/AppPartPy.cpp +++ b/src/Mod/Part/App/AppPartPy.cpp @@ -271,6 +271,17 @@ PartExport std::list sort_Edges(double tol3d, std::list +{ +public: + BRepFeatModule() : Py::ExtensionModule("BRepFeat") + { + initialize("This is a module working with the BRepFeat package."); // register with Python + } + + virtual ~BRepFeatModule() {} +}; + class BRepOffsetAPIModule : public Py::ExtensionModule { public: @@ -317,6 +328,7 @@ public: class Module : public Py::ExtensionModule { + BRepFeatModule brepFeat; BRepOffsetAPIModule brepOffsetApi; Geom2dModule geom2d; GeomPlateModule geomPlate; @@ -537,6 +549,7 @@ public: ); initialize("This is a module working with shapes."); // register with Python + PyModule_AddObject(m_module, "BRepFeat", brepFeat.module().ptr()); PyModule_AddObject(m_module, "BRepOffsetAPI", brepOffsetApi.module().ptr()); PyModule_AddObject(m_module, "Geom2d", geom2d.module().ptr()); PyModule_AddObject(m_module, "GeomPlate", geomPlate.module().ptr()); diff --git a/src/Mod/Part/App/BRepFeat/MakePrismPy.xml b/src/Mod/Part/App/BRepFeat/MakePrismPy.xml new file mode 100644 index 0000000000..a56b19cee3 --- /dev/null +++ b/src/Mod/Part/App/BRepFeat/MakePrismPy.xml @@ -0,0 +1,100 @@ + + + + + + Describes functions to build prism features. + + + + +Initializes this algorithm for building prisms along surfaces. +A face Pbase is selected in the shape Sbase +to serve as the basis for the prism. The orientation +of the prism will be defined by the vector Direction. + +Fuse offers a choice between: +- removing matter with a Boolean cut using the setting 0 +- adding matter with Boolean fusion using the setting 1. +The sketch face Skface serves to determine +the type of operation. If it is inside the basis +shape, a local operation such as glueing can be performed. + + + + + + +Indicates that the edge will slide on the face. +Raises ConstructionError if the face does not belong to the +basis shape, or the edge to the prismed shape. + + + + + + + + + + + +Realizes a semi-infinite prism, limited by the +position of the prism base. All other faces extend infinitely. + + + + + + +Realizes a semi-infinite prism, limited by the face Funtil. + + + + + + +Builds an infinite prism. The infinite descendants will not be kept in the result. + + + + + + +Assigns both a limiting shape, Until from TopoDS_Shape +and a height, Length at which to stop generation of the prism feature. + + + + + + +Returns the list of curves S parallel to the axis of the prism. + + + + + + +Generates a curve along the center of mass of the primitive. + + + + + + Returns a shape built by the shape construction algorithm. + + + + diff --git a/src/Mod/Part/App/BRepFeat/MakePrismPyImp.cpp b/src/Mod/Part/App/BRepFeat/MakePrismPyImp.cpp new file mode 100644 index 0000000000..b8935e085e --- /dev/null +++ b/src/Mod/Part/App/BRepFeat/MakePrismPyImp.cpp @@ -0,0 +1,331 @@ +/*************************************************************************** + * Copyright (c) 2020 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#include "PreCompiled.h" +#ifndef _PreComp_ +# include +# include +# include +#endif + +#include "BRepFeat/MakePrismPy.h" +#include "BRepFeat/MakePrismPy.cpp" +#include "Geometry.h" +#include "TopoShapeEdgePy.h" +#include "TopoShapeFacePy.h" +#include + +using namespace Part; + + +PyObject *MakePrismPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper +{ + // create a new instance of MakePrismPy + return new MakePrismPy(nullptr); +} + +// constructor method +int MakePrismPy::PyInit(PyObject* args, PyObject* kwds) +{ + PyObject* Sbase; + PyObject* Pbase; + PyObject* Skface; + PyObject* Direction; + int Fuse; + PyObject* Modify; + static char* keywords[] = {"Sbase", "Pbase", "Skface", "Direction", "Fuse", "Modifiy", nullptr}; + if (PyArg_ParseTupleAndKeywords(args, kwds, "O!O!O!O!iO!", keywords, + &(TopoShapePy::Type), &Sbase, + &(TopoShapePy::Type), &Pbase, + &(TopoShapeFacePy::Type), &Skface, + &(Base::VectorPy::Type), &Direction, &Fuse, + &(PyBool_Type), &Modify)) { + try { + TopoDS_Shape sbase = static_cast(Sbase)->getTopoShapePtr()->getShape(); + TopoDS_Shape pbase = static_cast(Pbase)->getTopoShapePtr()->getShape(); + TopoDS_Face skface = TopoDS::Face(static_cast(Skface)->getTopoShapePtr()->getShape()); + Base::Vector3d dir = static_cast(Direction)->value(); + std::unique_ptr ptr(new BRepFeat_MakePrism(sbase, pbase, skface, gp_Dir(dir.x, dir.y, dir.z), Fuse, + PyObject_IsTrue(Modify) ? Standard_True : Standard_False)); + + setTwinPointer(ptr.release()); + return 0; + } + catch (const Standard_Failure& e) { + PyErr_SetString(PyExc_RuntimeError, e.GetMessageString()); + return -1; + } + } + + PyErr_Clear(); + if (PyArg_ParseTuple(args, "")) { + try { + std::unique_ptr ptr(new BRepFeat_MakePrism()); + setTwinPointer(ptr.release()); + return 0; + } + catch (const Standard_Failure& e) { + PyErr_SetString(PyExc_RuntimeError, e.GetMessageString()); + return -1; + } + } + return -1; +} + +// returns a string which represents the object e.g. when printed in python +std::string MakePrismPy::representation(void) const +{ + return std::string(""); +} + +PyObject* MakePrismPy::init(PyObject *args, PyObject* kwds) +{ + PyObject* Sbase; + PyObject* Pbase; + PyObject* Skface; + PyObject* Direction; + int Fuse; + PyObject* Modify; + static char* keywords[] = {"Sbase", "Pbase", "Skface", "Direction", "Fuse", "Modifiy", nullptr}; + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O!O!O!iO!", keywords, + &(TopoShapePy::Type), &Sbase, + &(TopoShapePy::Type), &Pbase, + &(TopoShapeFacePy::Type), &Skface, + &(Base::VectorPy::Type), &Direction, &Fuse, + &(PyBool_Type), &Modify)) + return nullptr; + + + try { + TopoDS_Shape sbase = static_cast(Sbase)->getTopoShapePtr()->getShape(); + TopoDS_Shape pbase = static_cast(Pbase)->getTopoShapePtr()->getShape(); + TopoDS_Face skface = TopoDS::Face(static_cast(Skface)->getTopoShapePtr()->getShape()); + Base::Vector3d dir = static_cast(Direction)->value(); + getBRepFeat_MakePrismPtr()->Init(sbase, pbase, skface, gp_Dir(dir.x, dir.y, dir.z), Fuse, + PyObject_IsTrue(Modify) ? Standard_True : Standard_False); + + Py_Return; + } + catch (const Standard_Failure& e) { + PyErr_SetString(PyExc_RuntimeError, e.GetMessageString()); + return nullptr; + } +} + +PyObject* MakePrismPy::add(PyObject *args, PyObject* kwds) +{ + PyObject* Edge; + PyObject* Face; + static char* keywords[] = {"Edge", "Face", nullptr}; + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O!", keywords, + &(TopoShapeEdgePy::Type), &Edge, + &(TopoShapeFacePy::Type), &Face)) + return nullptr; + + + try { + TopoDS_Edge edge = TopoDS::Edge(static_cast(Edge)->getTopoShapePtr()->getShape()); + TopoDS_Face face = TopoDS::Face(static_cast(Face)->getTopoShapePtr()->getShape()); + getBRepFeat_MakePrismPtr()->Add(edge, face); + + Py_Return; + } + catch (const Standard_Failure& e) { + PyErr_SetString(PyExc_RuntimeError, e.GetMessageString()); + return nullptr; + } +} + +PyObject* MakePrismPy::perform(PyObject *args, PyObject* kwds) +{ + PyObject* From; + PyObject* Until; + static char* keywords_fu[] = {"From", "Until", nullptr}; + if (PyArg_ParseTupleAndKeywords(args, kwds, "O!O!", keywords_fu, + &(TopoShapePy::Type), &From, + &(TopoShapePy::Type), &Until)) { + try { + TopoDS_Shape from = static_cast(From)->getTopoShapePtr()->getShape(); + TopoDS_Shape until = static_cast(Until)->getTopoShapePtr()->getShape(); + getBRepFeat_MakePrismPtr()->Perform(from, until); + Py_Return; + } + catch (const Standard_Failure& e) { + PyErr_SetString(PyExc_RuntimeError, e.GetMessageString()); + return nullptr; + } + } + + PyErr_Clear(); + static char* keywords_u[] = {"Until", nullptr}; + if (PyArg_ParseTupleAndKeywords(args, kwds, "O!", keywords_u, + &(TopoShapePy::Type), &Until)) { + try { + TopoDS_Shape until = static_cast(Until)->getTopoShapePtr()->getShape(); + getBRepFeat_MakePrismPtr()->Perform(until); + Py_Return; + } + catch (const Standard_Failure& e) { + PyErr_SetString(PyExc_RuntimeError, e.GetMessageString()); + return nullptr; + } + } + + PyErr_Clear(); + double length; + static char* keywords_l[] = {"Length", nullptr}; + if (PyArg_ParseTupleAndKeywords(args, kwds, "d", keywords_l, &length)) { + try { + getBRepFeat_MakePrismPtr()->Perform(length); + Py_Return; + } + catch (const Standard_Failure& e) { + PyErr_SetString(PyExc_RuntimeError, e.GetMessageString()); + return nullptr; + } + } + + PyErr_SetString(PyExc_TypeError, "supported signatures:\n" + "perform(From [shape], Until [shape])\n" + "perform(Until [shape])\n" + "perform(Length [float])\n"); + return nullptr; +} + +PyObject* MakePrismPy::performUntilEnd(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + try { + getBRepFeat_MakePrismPtr()->PerformUntilEnd(); + Py_Return; + } + catch (const Standard_Failure& e) { + PyErr_SetString(PyExc_RuntimeError, e.GetMessageString()); + return nullptr; + } +} + +PyObject* MakePrismPy::performFromEnd(PyObject *args) +{ + PyObject* Until; + if (!PyArg_ParseTuple(args, "O!", &(TopoShapePy::Type), &Until)) + return nullptr; + + try { + TopoDS_Shape until = static_cast(Until)->getTopoShapePtr()->getShape(); + getBRepFeat_MakePrismPtr()->PerformFromEnd(until); + Py_Return; + } + catch (const Standard_Failure& e) { + PyErr_SetString(PyExc_RuntimeError, e.GetMessageString()); + return nullptr; + } +} + +PyObject* MakePrismPy::performThruAll(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + try { + getBRepFeat_MakePrismPtr()->PerformThruAll(); + Py_Return; + } + catch (const Standard_Failure& e) { + PyErr_SetString(PyExc_RuntimeError, e.GetMessageString()); + return nullptr; + } +} + +PyObject* MakePrismPy::performUntilHeight(PyObject *args) +{ + PyObject* Until; + double length; + if (!PyArg_ParseTuple(args, "O!d", &(TopoShapePy::Type), &Until, &length)) + return nullptr; + + try { + TopoDS_Shape until = static_cast(Until)->getTopoShapePtr()->getShape(); + getBRepFeat_MakePrismPtr()->PerformUntilHeight(until, length); + Py_Return; + } + catch (const Standard_Failure& e) { + PyErr_SetString(PyExc_RuntimeError, e.GetMessageString()); + return nullptr; + } +} + +PyObject* MakePrismPy::curves(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + TColGeom_SequenceOfCurve S; + getBRepFeat_MakePrismPtr()->Curves(S); + + Py::Tuple tuple(S.Length()); + for (int i = S.Lower(); i <= S.Upper(); ++i) { + Handle(Geom_Curve) hC = S.Value(i); + std::unique_ptr gc(Part::makeFromCurve(hC)); + tuple.setItem(i, Py::asObject(gc->getPyObject())); + } + + return Py::new_reference_to(tuple); +} + +PyObject* MakePrismPy::barycCurve(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + Handle(Geom_Curve) hC = getBRepFeat_MakePrismPtr()->BarycCurve(); + std::unique_ptr gc(Part::makeFromCurve(hC)); + return gc->getPyObject(); +} + +PyObject* MakePrismPy::shape(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + try { + TopoShape shape(getBRepFeat_MakePrismPtr()->Shape()); + return shape.getPyObject(); + } + catch (const Standard_Failure& e) { + PyErr_SetString(PyExc_RuntimeError, e.GetMessageString()); + return nullptr; + } +} + +PyObject *MakePrismPy::getCustomAttributes(const char* /*attr*/) const +{ + return 0; +} + +int MakePrismPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) +{ + return 0; +} diff --git a/src/Mod/Part/App/CMakeLists.txt b/src/Mod/Part/App/CMakeLists.txt index b6aed42172..d291c20e45 100644 --- a/src/Mod/Part/App/CMakeLists.txt +++ b/src/Mod/Part/App/CMakeLists.txt @@ -94,10 +94,13 @@ generate_from_xml(BRepOffsetAPI_MakePipeShellPy) generate_from_xml(BRepOffsetAPI_MakeFillingPy) # make sure to create the directory at configure time +file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/BRepFeat) file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/Geom2d) file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/GeomPlate) file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ShapeUpgrade) +generate_from_xml(BRepFeat/MakePrismPy) + generate_from_xml(Geom2d/ArcOfCircle2dPy) generate_from_xml(Geom2d/ArcOfConic2dPy) generate_from_xml(Geom2d/ArcOfEllipse2dPy) @@ -312,6 +315,14 @@ SET(Python_SRCS ) SOURCE_GROUP("Python" FILES ${Python_SRCS}) +# BRepFeat wrappers +SET(BRepFeatPy_SRCS + BRepFeat/MakePrismPy.xml + BRepFeat/MakePrismPyImp.cpp +) + +SOURCE_GROUP("ShapeUpgrade" FILES ${ShapeUpgradePy_SRCS}) + # Geom2d wrappers SET(Geom2dPy_SRCS Geom2d/ArcOfCircle2dPy.xml @@ -376,6 +387,7 @@ SET(Part_SRCS ${Features_SRCS} ${Properties_SRCS} ${Python_SRCS} + ${BRepFeatPy_SRCS} ${Geom2dPy_SRCS} ${GeomPlatePy_SRCS} ${ShapeUpgradePy_SRCS}