From 20f9aa42e7a85e837463bb0b9e34d95a4938471d Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 21 Dec 2021 19:45:01 +0100 Subject: [PATCH] Part: expose Poly HLR algorithm to Python --- src/Mod/Part/App/AppPart.cpp | 4 + src/Mod/Part/App/CMakeLists.txt | 7 + .../Part/App/HLRBRep/HLRBRep_PolyAlgoPy.xml | 127 ++++++++ .../App/HLRBRep/HLRBRep_PolyAlgoPyImp.cpp | 281 ++++++++++++++++++ src/Mod/Part/App/HLRBRep/PolyHLRToShapePy.xml | 84 ++++++ .../Part/App/HLRBRep/PolyHLRToShapePyImp.cpp | 236 +++++++++++++++ 6 files changed, 739 insertions(+) create mode 100644 src/Mod/Part/App/HLRBRep/HLRBRep_PolyAlgoPy.xml create mode 100644 src/Mod/Part/App/HLRBRep/HLRBRep_PolyAlgoPyImp.cpp create mode 100644 src/Mod/Part/App/HLRBRep/PolyHLRToShapePy.xml create mode 100644 src/Mod/Part/App/HLRBRep/PolyHLRToShapePyImp.cpp diff --git a/src/Mod/Part/App/AppPart.cpp b/src/Mod/Part/App/AppPart.cpp index 8c44453731..991bbe5601 100644 --- a/src/Mod/Part/App/AppPart.cpp +++ b/src/Mod/Part/App/AppPart.cpp @@ -144,6 +144,8 @@ #include #include #include +#include +#include #include #include "PropertyGeometryList.h" #include "DatumFeature.h" @@ -301,6 +303,8 @@ PyMOD_INIT_FUNC(Part) PyObject* hlrfeatModule(module.getAttr("HLRBRep").ptr()); Base::Interpreter().addType(&Part::HLRBRep_AlgoPy::Type,hlrfeatModule,"Algo"); Base::Interpreter().addType(&Part::HLRToShapePy::Type,hlrfeatModule,"HLRToShape"); + Base::Interpreter().addType(&Part::HLRBRep_PolyAlgoPy::Type,hlrfeatModule,"PolyAlgo"); + Base::Interpreter().addType(&Part::PolyHLRToShapePy::Type,hlrfeatModule,"PolyHLRToShape"); // Geom2d package PyObject* geom2dModule(module.getAttr("Geom2d").ptr()); diff --git a/src/Mod/Part/App/CMakeLists.txt b/src/Mod/Part/App/CMakeLists.txt index 90623fb850..dc88ba51c2 100644 --- a/src/Mod/Part/App/CMakeLists.txt +++ b/src/Mod/Part/App/CMakeLists.txt @@ -126,6 +126,8 @@ generate_from_xml(GeomPlate/PointConstraintPy) generate_from_xml(HLRBRep/HLRBRep_AlgoPy) generate_from_xml(HLRBRep/HLRToShapePy) +generate_from_xml(HLRBRep/HLRBRep_PolyAlgoPy) +generate_from_xml(HLRBRep/PolyHLRToShapePy) generate_from_xml(ShapeUpgrade/UnifySameDomainPy) @@ -386,6 +388,11 @@ SET(HLRBRepPy_SRCS HLRBRep/HLRBRep_AlgoPyImp.cpp HLRBRep/HLRToShapePy.xml HLRBRep/HLRToShapePyImp.cpp + + HLRBRep/HLRBRep_PolyAlgoPy.xml + HLRBRep/HLRBRep_PolyAlgoPyImp.cpp + HLRBRep/PolyHLRToShapePy.xml + HLRBRep/PolyHLRToShapePyImp.cpp ) SOURCE_GROUP("HLRBRep" FILES ${HLRBRepPy_SRCS}) diff --git a/src/Mod/Part/App/HLRBRep/HLRBRep_PolyAlgoPy.xml b/src/Mod/Part/App/HLRBRep/HLRBRep_PolyAlgoPy.xml new file mode 100644 index 0000000000..b2f219652c --- /dev/null +++ b/src/Mod/Part/App/HLRBRep/HLRBRep_PolyAlgoPy.xml @@ -0,0 +1,127 @@ + + + + + + Describes functions to use HLR algorithm. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +private: + Handle(HLRBRep_PolyAlgo) hAlgo; + +public: + Handle(HLRBRep_PolyAlgo) handle() { + return hAlgo; + } + + + diff --git a/src/Mod/Part/App/HLRBRep/HLRBRep_PolyAlgoPyImp.cpp b/src/Mod/Part/App/HLRBRep/HLRBRep_PolyAlgoPyImp.cpp new file mode 100644 index 0000000000..fb2bad2ba7 --- /dev/null +++ b/src/Mod/Part/App/HLRBRep/HLRBRep_PolyAlgoPyImp.cpp @@ -0,0 +1,281 @@ +/*************************************************************************** + * Copyright (c) 2021 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 "HLRBRep/HLRBRep_PolyAlgoPy.h" +#include "HLRBRep/HLRBRep_PolyAlgoPy.cpp" +#include +#include +#include +#include + +#include +#include + +using namespace Part; + + +PyObject *HLRBRep_PolyAlgoPy::PyMake(struct _typeobject *, PyObject *, PyObject *) +{ + // create a new instance of HLRBRep_AlgoPy + return new HLRBRep_PolyAlgoPy(nullptr); +} + +// constructor method +int HLRBRep_PolyAlgoPy::PyInit(PyObject* args, PyObject* /*kwds*/) +{ + PyObject* shape = nullptr; + if (!PyArg_ParseTuple(args, "|O!", &Part::TopoShapePy::Type, &shape)) + return -1; + + // The lifetime of the HLRBRep_PolyAlgo is controlled by the hAlgo handle + if (shape) { + TopoDS_Shape input = static_cast(shape)->getTopoShapePtr()->getShape(); + HLRBRep_PolyAlgo* algo = new HLRBRep_PolyAlgo(input); + hAlgo = algo; + setTwinPointer(algo); + } + else { + HLRBRep_PolyAlgo* algo = new HLRBRep_PolyAlgo(); + hAlgo = algo; + setTwinPointer(algo); + } + + return 0; +} + +// returns a string which represents the object e.g. when printed in python +std::string HLRBRep_PolyAlgoPy::representation(void) const +{ + return std::string(""); +} + +PyObject* HLRBRep_PolyAlgoPy::setProjector(PyObject *args, PyObject *kwds) +{ + PyObject* ps = nullptr; + PyObject* zd = nullptr; + PyObject* xd = nullptr; + double focus = std::numeric_limits::quiet_NaN(); + + static char *kwlist[] = {"Origin", "ZDir", "XDir", nullptr}; + if (PyArg_ParseTupleAndKeywords(args, kwds, "|O!O!O!d", kwlist, + &Base::VectorPy::Type, &ps, + &Base::VectorPy::Type, &zd, + &Base::VectorPy::Type, &xd, + &focus)) { + gp_Ax2 ax2; + if (ps && zd && xd) { + Base::Vector3d p = Py::Vector(ps,false).toVector(); + Base::Vector3d z = Py::Vector(zd,false).toVector(); + Base::Vector3d x = Py::Vector(xd,false).toVector(); + ax2.SetLocation(Base::convertTo(p)); + ax2.SetDirection(Base::convertTo(z)); + ax2.SetXDirection(Base::convertTo(x)); + } + else if (ps && zd) { + Base::Vector3d p = Py::Vector(ps,false).toVector(); + Base::Vector3d z = Py::Vector(zd,false).toVector(); + ax2.SetLocation(Base::convertTo(p)); + ax2.SetDirection(Base::convertTo(z)); + } + + if (boost::math::isnan(focus)) + getHLRBRep_PolyAlgoPtr()->Projector(HLRAlgo_Projector(ax2)); + else + getHLRBRep_PolyAlgoPtr()->Projector(HLRAlgo_Projector(ax2, focus)); + Py_Return; + } + + return nullptr; +} + +PyObject* HLRBRep_PolyAlgoPy::update(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + getHLRBRep_PolyAlgoPtr()->Update(); + Py_Return; +} + +PyObject* HLRBRep_PolyAlgoPy::load(PyObject *args) +{ + PyObject* shape; + if (!PyArg_ParseTuple(args, "O!", &Part::TopoShapePy::Type, &shape)) + return nullptr; + + TopoDS_Shape input = static_cast(shape)->getTopoShapePtr()->getShape(); + getHLRBRep_PolyAlgoPtr()->Load(input); + Py_Return; +} + +PyObject* HLRBRep_PolyAlgoPy::remove(PyObject *args) +{ + int index; + if (!PyArg_ParseTuple(args, "i", &index)) + return nullptr; + + getHLRBRep_PolyAlgoPtr()->Remove(index); + Py_Return; +} + +PyObject* HLRBRep_PolyAlgoPy::nbShapes(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + int num = getHLRBRep_PolyAlgoPtr()->NbShapes(); + return Py_BuildValue("i", num); +} + +PyObject* HLRBRep_PolyAlgoPy::shape(PyObject *args) +{ + int index; + if (!PyArg_ParseTuple(args, "i", &index)) + return nullptr; + + TopoDS_Shape result = getHLRBRep_PolyAlgoPtr()->Shape(index); + return new TopoShapePy(new TopoShape(result)); +} + +PyObject* HLRBRep_PolyAlgoPy::index(PyObject *args) +{ + PyObject* shape; + if (!PyArg_ParseTuple(args, "O!", &Part::TopoShapePy::Type, &shape)) + return nullptr; + + TopoDS_Shape input = static_cast(shape)->getTopoShapePtr()->getShape(); + int value = getHLRBRep_PolyAlgoPtr()->Index(input); + return Py_BuildValue("i", value); +} + +PyObject* HLRBRep_PolyAlgoPy::initHide(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + getHLRBRep_PolyAlgoPtr()->InitHide(); + Py_Return; +} + +PyObject* HLRBRep_PolyAlgoPy::moreHide(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + getHLRBRep_PolyAlgoPtr()->MoreHide(); + Py_Return; +} + +PyObject* HLRBRep_PolyAlgoPy::nextHide(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + getHLRBRep_PolyAlgoPtr()->NextHide(); + Py_Return; +} + +PyObject* HLRBRep_PolyAlgoPy::initShow(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + getHLRBRep_PolyAlgoPtr()->InitShow(); + Py_Return; +} + +PyObject* HLRBRep_PolyAlgoPy::moreShow(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + getHLRBRep_PolyAlgoPtr()->MoreShow(); + Py_Return; +} + +PyObject* HLRBRep_PolyAlgoPy::nextShow(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + getHLRBRep_PolyAlgoPtr()->NextShow(); + Py_Return; +} + +PyObject* HLRBRep_PolyAlgoPy::outLinedShape(PyObject *args) +{ + PyObject* shape; + if (!PyArg_ParseTuple(args, "O!", &Part::TopoShapePy::Type, &shape)) + return nullptr; + + TopoDS_Shape input = static_cast(shape)->getTopoShapePtr()->getShape(); + TopoDS_Shape result = getHLRBRep_PolyAlgoPtr()->OutLinedShape(input); + return new TopoShapePy(new TopoShape(result)); +} + +Py::Float HLRBRep_PolyAlgoPy::getAngle() const +{ + return Py::Float(getHLRBRep_PolyAlgoPtr()->Angle()); +} + +void HLRBRep_PolyAlgoPy::setAngle(Py::Float arg) +{ + getHLRBRep_PolyAlgoPtr()->Angle(static_cast(arg)); +} + +Py::Float HLRBRep_PolyAlgoPy::getTolAngular() const +{ + return Py::Float(getHLRBRep_PolyAlgoPtr()->TolAngular()); +} + +void HLRBRep_PolyAlgoPy::setTolAngular(Py::Float arg) +{ + getHLRBRep_PolyAlgoPtr()->TolAngular(static_cast(arg)); +} + +Py::Float HLRBRep_PolyAlgoPy::getTolCoef() const +{ + return Py::Float(getHLRBRep_PolyAlgoPtr()->TolCoef()); +} + +void HLRBRep_PolyAlgoPy::setTolCoef(Py::Float arg) +{ + getHLRBRep_PolyAlgoPtr()->TolCoef(static_cast(arg)); +} + +PyObject *HLRBRep_PolyAlgoPy::getCustomAttributes(const char* /*attr*/) const +{ + return nullptr; +} + +int HLRBRep_PolyAlgoPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) +{ + return 0; +} diff --git a/src/Mod/Part/App/HLRBRep/PolyHLRToShapePy.xml b/src/Mod/Part/App/HLRBRep/PolyHLRToShapePy.xml new file mode 100644 index 0000000000..b6b61572a4 --- /dev/null +++ b/src/Mod/Part/App/HLRBRep/PolyHLRToShapePy.xml @@ -0,0 +1,84 @@ + + + + + + Describes functions to use HLR algorithm. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Mod/Part/App/HLRBRep/PolyHLRToShapePyImp.cpp b/src/Mod/Part/App/HLRBRep/PolyHLRToShapePyImp.cpp new file mode 100644 index 0000000000..d2012eed23 --- /dev/null +++ b/src/Mod/Part/App/HLRBRep/PolyHLRToShapePyImp.cpp @@ -0,0 +1,236 @@ +/*************************************************************************** + * Copyright (c) 2021 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_ +#endif + +#include "HLRBRep/PolyHLRToShapePy.h" +#include "HLRBRep/PolyHLRToShapePy.cpp" +#include "HLRBRep/HLRBRep_PolyAlgoPy.h" +#include + +using namespace Part; + + +PyObject *PolyHLRToShapePy::PyMake(struct _typeobject *, PyObject *, PyObject *) +{ + // create a new instance of HLRBRep_AlgoPy + return new PolyHLRToShapePy(new HLRBRep_PolyHLRToShape()); +} + +// constructor method +int PolyHLRToShapePy::PyInit(PyObject* args, PyObject* /*kwds*/) +{ + PyObject* algo = nullptr; + if (!PyArg_ParseTuple(args, "|O!", &Part::HLRBRep_PolyAlgoPy::Type, &algo)) + return -1; + + if (algo) { + HLRBRep_PolyAlgoPy* py = static_cast(algo); + getHLRBRep_PolyHLRToShapePtr()->Update(py->handle()); + } + + return 0; +} + +// returns a string which represents the object e.g. when printed in python +std::string PolyHLRToShapePy::representation(void) const +{ + return std::string(""); +} + +PyObject* PolyHLRToShapePy::update(PyObject *args) +{ + PyObject* algo; + if (!PyArg_ParseTuple(args, "O!", &Part::HLRBRep_PolyAlgoPy::Type, &algo)) + return nullptr; + + HLRBRep_PolyAlgoPy* py = static_cast(algo); + getHLRBRep_PolyHLRToShapePtr()->Update(py->handle()); + Py_Return; +} + +PyObject* PolyHLRToShapePy::show(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + getHLRBRep_PolyHLRToShapePtr()->Show(); + Py_Return; +} + +PyObject* PolyHLRToShapePy::hide(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + getHLRBRep_PolyHLRToShapePtr()->Hide(); + Py_Return; +} + +PyObject* PolyHLRToShapePy::vCompound(PyObject *args) +{ + PyObject* shape = nullptr; + if (!PyArg_ParseTuple(args, "|O!", &Part::TopoShapePy::Type, &shape)) + return nullptr; + + if (shape) { + TopoDS_Shape input = static_cast(shape)->getTopoShapePtr()->getShape(); + TopoDS_Shape result = getHLRBRep_PolyHLRToShapePtr()->VCompound(input); + return new TopoShapePy(new TopoShape(result)); + } + else { + TopoDS_Shape result = getHLRBRep_PolyHLRToShapePtr()->VCompound(); + return new TopoShapePy(new TopoShape(result)); + } +} + +PyObject* PolyHLRToShapePy::Rg1LineVCompound(PyObject *args) +{ + PyObject* shape = nullptr; + if (!PyArg_ParseTuple(args, "|O!", &Part::TopoShapePy::Type, &shape)) + return nullptr; + + if (shape) { + TopoDS_Shape input = static_cast(shape)->getTopoShapePtr()->getShape(); + TopoDS_Shape result = getHLRBRep_PolyHLRToShapePtr()->Rg1LineVCompound(input); + return new TopoShapePy(new TopoShape(result)); + } + else { + TopoDS_Shape result = getHLRBRep_PolyHLRToShapePtr()->Rg1LineVCompound(); + return new TopoShapePy(new TopoShape(result)); + } +} + +PyObject* PolyHLRToShapePy::RgNLineVCompound(PyObject *args) +{ + PyObject* shape = nullptr; + if (!PyArg_ParseTuple(args, "|O!", &Part::TopoShapePy::Type, &shape)) + return nullptr; + + if (shape) { + TopoDS_Shape input = static_cast(shape)->getTopoShapePtr()->getShape(); + TopoDS_Shape result = getHLRBRep_PolyHLRToShapePtr()->RgNLineVCompound(input); + return new TopoShapePy(new TopoShape(result)); + } + else { + TopoDS_Shape result = getHLRBRep_PolyHLRToShapePtr()->RgNLineVCompound(); + return new TopoShapePy(new TopoShape(result)); + } +} + +PyObject* PolyHLRToShapePy::outLineVCompound(PyObject *args) +{ + PyObject* shape = nullptr; + if (!PyArg_ParseTuple(args, "|O!", &Part::TopoShapePy::Type, &shape)) + return nullptr; + + if (shape) { + TopoDS_Shape input = static_cast(shape)->getTopoShapePtr()->getShape(); + TopoDS_Shape result = getHLRBRep_PolyHLRToShapePtr()->OutLineVCompound(input); + return new TopoShapePy(new TopoShape(result)); + } + else { + TopoDS_Shape result = getHLRBRep_PolyHLRToShapePtr()->OutLineVCompound(); + return new TopoShapePy(new TopoShape(result)); + } +} + +PyObject* PolyHLRToShapePy::hCompound(PyObject *args) +{ + PyObject* shape = nullptr; + if (!PyArg_ParseTuple(args, "|O!", &Part::TopoShapePy::Type, &shape)) + return nullptr; + + if (shape) { + TopoDS_Shape input = static_cast(shape)->getTopoShapePtr()->getShape(); + TopoDS_Shape result = getHLRBRep_PolyHLRToShapePtr()->HCompound(input); + return new TopoShapePy(new TopoShape(result)); + } + else { + TopoDS_Shape result = getHLRBRep_PolyHLRToShapePtr()->HCompound(); + return new TopoShapePy(new TopoShape(result)); + } +} + +PyObject* PolyHLRToShapePy::Rg1LineHCompound(PyObject *args) +{ + PyObject* shape = nullptr; + if (!PyArg_ParseTuple(args, "|O!", &Part::TopoShapePy::Type, &shape)) + return nullptr; + + if (shape) { + TopoDS_Shape input = static_cast(shape)->getTopoShapePtr()->getShape(); + TopoDS_Shape result = getHLRBRep_PolyHLRToShapePtr()->Rg1LineHCompound(input); + return new TopoShapePy(new TopoShape(result)); + } + else { + TopoDS_Shape result = getHLRBRep_PolyHLRToShapePtr()->Rg1LineHCompound(); + return new TopoShapePy(new TopoShape(result)); + } +} + +PyObject* PolyHLRToShapePy::RgNLineHCompound(PyObject *args) +{ + PyObject* shape = nullptr; + if (!PyArg_ParseTuple(args, "|O!", &Part::TopoShapePy::Type, &shape)) + return nullptr; + + if (shape) { + TopoDS_Shape input = static_cast(shape)->getTopoShapePtr()->getShape(); + TopoDS_Shape result = getHLRBRep_PolyHLRToShapePtr()->RgNLineHCompound(input); + return new TopoShapePy(new TopoShape(result)); + } + else { + TopoDS_Shape result = getHLRBRep_PolyHLRToShapePtr()->RgNLineHCompound(); + return new TopoShapePy(new TopoShape(result)); + } +} + +PyObject* PolyHLRToShapePy::outLineHCompound(PyObject *args) +{ + PyObject* shape = nullptr; + if (!PyArg_ParseTuple(args, "|O!", &Part::TopoShapePy::Type, &shape)) + return nullptr; + + if (shape) { + TopoDS_Shape input = static_cast(shape)->getTopoShapePtr()->getShape(); + TopoDS_Shape result = getHLRBRep_PolyHLRToShapePtr()->OutLineHCompound(input); + return new TopoShapePy(new TopoShape(result)); + } + else { + TopoDS_Shape result = getHLRBRep_PolyHLRToShapePtr()->OutLineHCompound(); + return new TopoShapePy(new TopoShape(result)); + } +} + +PyObject *PolyHLRToShapePy::getCustomAttributes(const char* /*attr*/) const +{ + return nullptr; +} + +int PolyHLRToShapePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) +{ + return 0; +}