From f3e941a382e63f35c5a5376a91dd299ef47852da Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 27 Apr 2022 14:30:16 +0200 Subject: [PATCH] Part: start to expose ShapeFix framework to Python --- src/Mod/Part/App/AppPart.cpp | 9 +- src/Mod/Part/App/AppPartPy.cpp | 13 +++ src/Mod/Part/App/CMakeLists.txt | 15 +++ src/Mod/Part/App/ShapeFix/ShapeFix_RootPy.xml | 43 +++++++ .../Part/App/ShapeFix/ShapeFix_RootPyImp.cpp | 101 ++++++++++++++++ .../Part/App/ShapeFix/ShapeFix_ShellPy.xml | 39 +++++++ .../Part/App/ShapeFix/ShapeFix_ShellPyImp.cpp | 108 ++++++++++++++++++ 7 files changed, 327 insertions(+), 1 deletion(-) create mode 100644 src/Mod/Part/App/ShapeFix/ShapeFix_RootPy.xml create mode 100644 src/Mod/Part/App/ShapeFix/ShapeFix_RootPyImp.cpp create mode 100644 src/Mod/Part/App/ShapeFix/ShapeFix_ShellPy.xml create mode 100644 src/Mod/Part/App/ShapeFix/ShapeFix_ShellPyImp.cpp diff --git a/src/Mod/Part/App/AppPart.cpp b/src/Mod/Part/App/AppPart.cpp index 342a67bb65..7814792d6e 100644 --- a/src/Mod/Part/App/AppPart.cpp +++ b/src/Mod/Part/App/AppPart.cpp @@ -99,7 +99,9 @@ #include "Mod/Part/App/PlanePy.h" #include "Mod/Part/App/PointPy.h" #include -#include "Mod/Part/App/RectangularTrimmedSurfacePy.h" +#include +#include +#include #include #include "Mod/Part/App/SpherePy.h" #include "Mod/Part/App/SurfaceOfExtrusionPy.h" @@ -337,6 +339,11 @@ PyMOD_INIT_FUNC(Part) Base::Interpreter().addType(&Part::CurveConstraintPy::Type, geomPlate, "CurveConstraint"); Base::Interpreter().addType(&Part::PointConstraintPy::Type, geomPlate, "PointConstraint"); + // ShapeFix sub-module + PyObject* shapeFix(module.getAttr("ShapeFix").ptr()); + Base::Interpreter().addType(&Part::ShapeFix_RootPy::Type, shapeFix, "Root"); + Base::Interpreter().addType(&Part::ShapeFix_ShellPy::Type, shapeFix, "Shell"); + // ShapeUpgrade sub-module PyObject* shapeUpgrade(module.getAttr("ShapeUpgrade").ptr()); Base::Interpreter().addType(&Part::UnifySameDomainPy::Type, shapeUpgrade, "UnifySameDomain"); diff --git a/src/Mod/Part/App/AppPartPy.cpp b/src/Mod/Part/App/AppPartPy.cpp index 82686cdd4b..5b97f5d9d0 100644 --- a/src/Mod/Part/App/AppPartPy.cpp +++ b/src/Mod/Part/App/AppPartPy.cpp @@ -303,6 +303,17 @@ public: virtual ~HLRBRepModule() {} }; +class ShapeFixModule : public Py::ExtensionModule +{ +public: + ShapeFixModule() : Py::ExtensionModule("ShapeFix") + { + initialize("This is a module working with the ShapeFix framework."); // register with Python + } + + virtual ~ShapeFixModule() {} +}; + class ShapeUpgradeModule : public Py::ExtensionModule { public: @@ -332,6 +343,7 @@ class Module : public Py::ExtensionModule Geom2dModule geom2d; GeomPlateModule geomPlate; HLRBRepModule HLRBRep; + ShapeFixModule shapeFix; ShapeUpgradeModule shapeUpgrade; ChFi2dModule chFi2d; public: @@ -559,6 +571,7 @@ public: PyModule_AddObject(m_module, "Geom2d", geom2d.module().ptr()); PyModule_AddObject(m_module, "GeomPlate", geomPlate.module().ptr()); PyModule_AddObject(m_module, "HLRBRep", HLRBRep.module().ptr()); + PyModule_AddObject(m_module, "ShapeFix", shapeFix.module().ptr()); PyModule_AddObject(m_module, "ShapeUpgrade", shapeUpgrade.module().ptr()); PyModule_AddObject(m_module, "ChFi2d", chFi2d.module().ptr()); } diff --git a/src/Mod/Part/App/CMakeLists.txt b/src/Mod/Part/App/CMakeLists.txt index 9eef362d42..390cb39bf6 100644 --- a/src/Mod/Part/App/CMakeLists.txt +++ b/src/Mod/Part/App/CMakeLists.txt @@ -99,6 +99,7 @@ file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ChFi2d) file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/Geom2d) file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/GeomPlate) file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/HLRBRep) +file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ShapeFix) file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ShapeUpgrade) generate_from_xml(BRepFeat/MakePrismPy) @@ -135,6 +136,9 @@ generate_from_xml(HLRBRep/HLRToShapePy) generate_from_xml(HLRBRep/HLRBRep_PolyAlgoPy) generate_from_xml(HLRBRep/PolyHLRToShapePy) +generate_from_xml(ShapeFix/ShapeFix_RootPy) +generate_from_xml(ShapeFix/ShapeFix_ShellPy) + generate_from_xml(ShapeUpgrade/UnifySameDomainPy) SET(Features_SRCS @@ -417,6 +421,16 @@ SET(HLRBRepPy_SRCS ) SOURCE_GROUP("HLRBRep" FILES ${HLRBRepPy_SRCS}) +# ShapeFix wrappers +SET(ShapeFixPy_SRCS + ShapeFix/ShapeFix_RootPy.xml + ShapeFix/ShapeFix_RootPyImp.cpp + ShapeFix/ShapeFix_ShellPy.xml + ShapeFix/ShapeFix_ShellPyImp.cpp +) + +SOURCE_GROUP("ShapeFix" FILES ${ShapeFixPy_SRCS}) + # ShapeUpgrade wrappers SET(ShapeUpgradePy_SRCS ShapeUpgrade/UnifySameDomainPy.xml @@ -434,6 +448,7 @@ SET(Part_SRCS ${Geom2dPy_SRCS} ${GeomPlatePy_SRCS} ${HLRBRepPy_SRCS} + ${ShapeFixPy_SRCS} ${ShapeUpgradePy_SRCS} Attacher.cpp Attacher.h diff --git a/src/Mod/Part/App/ShapeFix/ShapeFix_RootPy.xml b/src/Mod/Part/App/ShapeFix/ShapeFix_RootPy.xml new file mode 100644 index 0000000000..a1464ebf69 --- /dev/null +++ b/src/Mod/Part/App/ShapeFix/ShapeFix_RootPy.xml @@ -0,0 +1,43 @@ + + + + + + Root class for fixing operations + + + + Returns tolerance limited by [MinTolerance,MaxTolerance] + + + + + Basic precision value + + + + + + Minimal allowed tolerance + + + + + + Maximal allowed tolerance + + + + + diff --git a/src/Mod/Part/App/ShapeFix/ShapeFix_RootPyImp.cpp b/src/Mod/Part/App/ShapeFix/ShapeFix_RootPyImp.cpp new file mode 100644 index 0000000000..7f582a779f --- /dev/null +++ b/src/Mod/Part/App/ShapeFix/ShapeFix_RootPyImp.cpp @@ -0,0 +1,101 @@ +/*************************************************************************** + * Copyright (c) 2022 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 +#endif + +#include "ShapeFix/ShapeFix_RootPy.h" +#include "ShapeFix/ShapeFix_RootPy.cpp" + +using namespace Part; + +// returns a string which represents the object e.g. when printed in python +std::string ShapeFix_RootPy::representation() const +{ + return ""; +} + +PyObject *ShapeFix_RootPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper +{ + // create a new instance of ShapeFix_RootPy + return new ShapeFix_RootPy(new ShapeFix_Root); +} + +// constructor method +int ShapeFix_RootPy::PyInit(PyObject* /*args*/, PyObject* /*kwds*/) +{ + return 0; +} + +PyObject* ShapeFix_RootPy::limitTolerance(PyObject *args) +{ + double tol; + if (!PyArg_ParseTuple(args, "d", &tol)) + return nullptr; + + tol = getShapeFix_RootPtr()->LimitTolerance(tol); + return Py::new_reference_to(Py::Float(tol)); +} + +Py::Float ShapeFix_RootPy::getPrecision() const +{ + return Py::Float(getShapeFix_RootPtr()->Precision()); +} + +void ShapeFix_RootPy::setPrecision(Py::Float arg) +{ + getShapeFix_RootPtr()->SetPrecision(arg); +} + +Py::Float ShapeFix_RootPy::getMinTolerance() const +{ + return Py::Float(getShapeFix_RootPtr()->MinTolerance()); +} + +void ShapeFix_RootPy::setMinTolerance(Py::Float arg) +{ + getShapeFix_RootPtr()->SetMinTolerance(arg); +} + +Py::Float ShapeFix_RootPy::getMaxTolerance() const +{ + return Py::Float(getShapeFix_RootPtr()->MaxTolerance()); +} + +void ShapeFix_RootPy::setMaxTolerance(Py::Float arg) +{ + getShapeFix_RootPtr()->SetMaxTolerance(arg); +} + +PyObject *ShapeFix_RootPy::getCustomAttributes(const char* /*attr*/) const +{ + return nullptr; +} + +int ShapeFix_RootPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) +{ + return 0; +} diff --git a/src/Mod/Part/App/ShapeFix/ShapeFix_ShellPy.xml b/src/Mod/Part/App/ShapeFix/ShapeFix_ShellPy.xml new file mode 100644 index 0000000000..38d6d0cdd4 --- /dev/null +++ b/src/Mod/Part/App/ShapeFix/ShapeFix_ShellPy.xml @@ -0,0 +1,39 @@ + + + + + + Root class for fixing operations + + + + Initializes by shell + + + + + Iterates on subshapes and performs fixes + + + + + Returns fixed shell (or subset of oriented faces) + + + + + In case of multiconnexity returns compound of fixed shells and one shell otherwise + + + + diff --git a/src/Mod/Part/App/ShapeFix/ShapeFix_ShellPyImp.cpp b/src/Mod/Part/App/ShapeFix/ShapeFix_ShellPyImp.cpp new file mode 100644 index 0000000000..1b29e06674 --- /dev/null +++ b/src/Mod/Part/App/ShapeFix/ShapeFix_ShellPyImp.cpp @@ -0,0 +1,108 @@ +/*************************************************************************** + * Copyright (c) 2022 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 "ShapeFix/ShapeFix_ShellPy.h" +#include "ShapeFix/ShapeFix_ShellPy.cpp" +#include + +using namespace Part; + +// returns a string which represents the object e.g. when printed in python +std::string ShapeFix_ShellPy::representation() const +{ + return ""; +} + +PyObject *ShapeFix_ShellPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper +{ + // create a new instance of ShapeFix_RootPy + return new ShapeFix_ShellPy(new ShapeFix_Shell); +} + +// constructor method +int ShapeFix_ShellPy::PyInit(PyObject* args, PyObject* /*kwds*/) +{ + PyObject* shell = nullptr; + if (!PyArg_ParseTuple(args, "|O!", &TopoShapeShellPy::Type, &shell)) + return -1; + + if (shell) { + getShapeFix_ShellPtr()->Init(TopoDS::Shell(static_cast(shell)->getTopoShapePtr()->getShape())); + } + + return 0; +} + +PyObject* ShapeFix_ShellPy::init(PyObject *args) +{ + PyObject* shell; + if (!PyArg_ParseTuple(args, "O!", &TopoShapeShellPy::Type, &shell)) + return nullptr; + + getShapeFix_ShellPtr()->Init(TopoDS::Shell(static_cast(shell)->getTopoShapePtr()->getShape())); + Py_Return; +} + +PyObject* ShapeFix_ShellPy::perform(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + Standard_Boolean ok = getShapeFix_ShellPtr()->Perform(); + return Py::new_reference_to(Py::Boolean(ok ? true : false)); +} + +PyObject* ShapeFix_ShellPy::shell(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + TopoShape shape = getShapeFix_ShellPtr()->Shell(); + return shape.getPyObject(); +} + +PyObject* ShapeFix_ShellPy::shape(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + TopoShape shape = getShapeFix_ShellPtr()->Shape(); + return shape.getPyObject(); +} + +PyObject *ShapeFix_ShellPy::getCustomAttributes(const char* /*attr*/) const +{ + return nullptr; +} + +int ShapeFix_ShellPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) +{ + return 0; +}