From 4187bef89366d313463bf73746f8cb2ced7749e9 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 24 Oct 2015 18:04:53 +0200 Subject: [PATCH] + move module Python stuff into C++ class --- .../App/AppReverseEngineering.cpp | 153 ++++++++++++++++- .../App/AppReverseEngineeringPy.cpp | 160 ------------------ src/Mod/ReverseEngineering/App/CMakeLists.txt | 1 - .../Gui/AppReverseEngineeringGui.cpp | 22 ++- .../Gui/AppReverseEngineeringGuiPy.cpp | 33 ---- src/Mod/ReverseEngineering/Gui/CMakeLists.txt | 1 - 6 files changed, 167 insertions(+), 203 deletions(-) delete mode 100644 src/Mod/ReverseEngineering/App/AppReverseEngineeringPy.cpp delete mode 100644 src/Mod/ReverseEngineering/Gui/AppReverseEngineeringGuiPy.cpp diff --git a/src/Mod/ReverseEngineering/App/AppReverseEngineering.cpp b/src/Mod/ReverseEngineering/App/AppReverseEngineering.cpp index 3fe48e7ad2..1fc70e6ce9 100644 --- a/src/Mod/ReverseEngineering/App/AppReverseEngineering.cpp +++ b/src/Mod/ReverseEngineering/App/AppReverseEngineering.cpp @@ -24,16 +24,158 @@ #include "PreCompiled.h" #ifndef _PreComp_ # include +# include +# include #endif #include #include - +#include -extern struct PyMethodDef ReverseEngineering_methods[]; +#include +#include -PyDoc_STRVAR(module_ReverseEngineering_doc, -"This module is the ReverseEngineering module."); +#include +#include +#include +#include + +#include "ApproxSurface.h" +#include "SurfaceTriangulation.h" + +using namespace Reen; + +namespace Reen { +class Module : public Py::ExtensionModule +{ +public: + Module() : Py::ExtensionModule("ReverseEngineering") + { + add_keyword_method("approxSurface",&Module::approxSurface, + "approxSurface(Points=,UDegree=3,VDegree=3,NbUPoles=6,NbVPoles=6,Smooth=True)\n" + "Weight=0.1,Grad=1.0,Bend=0.0,\n" + "Iterations=5,Correction=True,PatchFactor=1.0" + ); +#if defined(HAVE_PCL_SURFACE) + add_varargs_method("triangulate",&Module::triangulate, + "triangulate(PointKernel,searchRadius[,mu=2.5])." + ); +#endif + initialize("This module is the ReverseEngineering module."); // register with Python + } + + virtual ~Module() {} + +private: + Py::Object approxSurface(const Py::Tuple& args, const Py::Dict& kwds) + { + PyObject *o; + // spline parameters + int uDegree = 3; + int vDegree = 3; + int uPoles = 6; + int vPoles = 6; + // smoothing + PyObject* smooth = Py_True; + double weight = 0.1; + double grad = 1.0; //0.5 + double bend = 0.0; //0.2 + // other parameters + int iteration = 5; + PyObject* correction = Py_True; + double factor = 1.0; + + static char* kwds_approx[] = {"Points", "UDegree", "VDegree", "NbUPoles", "NbVPoles", + "Smooth", "Weight", "Grad", "Bend", + "Iterations", "Correction", "PatchFactor", NULL}; + if (!PyArg_ParseTupleAndKeywords(args.ptr(), kwds.ptr(), "O|iiiiO!dddiO!d",kwds_approx, + &o,&uDegree,&vDegree,&uPoles,&vPoles, + &PyBool_Type,&smooth,&weight,&grad,&bend, + &iteration,&PyBool_Type,&correction,&factor)) + throw Py::Exception(); + + double curvdiv = 1.0 - (grad + bend); + int uOrder = uDegree + 1; + int vOrder = vDegree + 1; + + // error checking + if (grad < 0.0 || grad > 1.0) { + throw Py::ValueError("Value of Grad out of range [0,1]"); + } + if (bend < 0.0 || bend > 1.0) { + throw Py::ValueError("Value of Bend out of range [0,1]"); + } + if (curvdiv < 0.0 || curvdiv > 1.0) { + throw Py::ValueError("Sum of Grad and Bend out of range [0,1]"); + } + if (uDegree < 1 || uOrder > uPoles) { + throw Py::ValueError("Value of uDegree out of range [1,NbUPoles-1]"); + } + if (vDegree < 1 || vOrder > vPoles) { + throw Py::ValueError("Value of vDegree out of range [1,NbVPoles-1]"); + } + + try { + Py::Sequence l(o); + TColgp_Array1OfPnt clPoints(0, l.size()-1); + + int index=0; + for (Py::Sequence::iterator it = l.begin(); it != l.end(); ++it) { + Py::Tuple t(*it); + clPoints(index++) = gp_Pnt( + (double)Py::Float(t.getItem(0)), + (double)Py::Float(t.getItem(1)), + (double)Py::Float(t.getItem(2))); + } + + Reen::BSplineParameterCorrection pc(uOrder,vOrder,uPoles,vPoles); + Handle_Geom_BSplineSurface hSurf; + + pc.EnableSmoothing(PyObject_IsTrue(smooth) ? true : false, weight, grad, bend, curvdiv); + hSurf = pc.CreateSurface(clPoints, iteration, PyObject_IsTrue(correction) ? true : false, factor); + if (!hSurf.IsNull()) { + return Py::asObject(new Part::BSplineSurfacePy(new Part::GeomBSplineSurface(hSurf))); + } + + throw Py::RuntimeError("Computation of B-Spline surface failed"); + } + catch (Standard_Failure &e) { + std::string str; + Standard_CString msg = e.GetMessageString(); + str += typeid(e).name(); + str += " "; + if (msg) {str += msg;} + else {str += "No OCCT Exception Message";} + throw Py::RuntimeError(str); + } + catch (const Base::Exception &e) { + throw Py::RuntimeError(e.what()); + } + catch (...) { + throw Py::RuntimeError("Unknown C++ exception"); + } + } +#if defined(HAVE_PCL_SURFACE) + Py::Object triangulate(const Py::Tuple& args) + { + PyObject *pcObj; + double searchRadius; + double mu=2.5; + if (!PyArg_ParseTuple(args.ptr(), "O!d|d", &(Points::PointsPy::Type), &pcObj, &searchRadius, &mu)) + throw Py::Exception(); + + Points::PointsPy* pPoints = static_cast(pcObj); + Points::PointKernel* points = pPoints->getPointKernelPtr(); + + Mesh::MeshObject* mesh = new Mesh::MeshObject(); + SurfaceTriangulation tria(*points, *mesh); + tria.perform(searchRadius, mu); + + return Py::asObject(new Mesh::MeshPy(mesh)); + } +#endif +}; +} // namespace Reen /* Python entry */ @@ -49,7 +191,8 @@ void ReenExport initReverseEngineering() PyErr_SetString(PyExc_ImportError, e.what()); return; } - Py_InitModule3("ReverseEngineering", ReverseEngineering_methods, module_ReverseEngineering_doc); /* mod name, table ptr */ + + new Reen::Module(); Base::Console().Log("Loading ReverseEngineering module... done\n"); } diff --git a/src/Mod/ReverseEngineering/App/AppReverseEngineeringPy.cpp b/src/Mod/ReverseEngineering/App/AppReverseEngineeringPy.cpp deleted file mode 100644 index 164645ef0c..0000000000 --- a/src/Mod/ReverseEngineering/App/AppReverseEngineeringPy.cpp +++ /dev/null @@ -1,160 +0,0 @@ -/*************************************************************************** - * Copyright (c) 2008 Jürgen Riegel (juergen.riegel@web.de) * - * * - * 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 -#include -#include - -#include -#include -#include -#include -#include - -#include "ApproxSurface.h" -#include "SurfaceTriangulation.h" - -using namespace Reen; - - -/* module functions */ -static PyObject * approxSurface(PyObject *self, PyObject *args, PyObject *kwds) -{ - PyObject *o; - // spline parameters - int uDegree = 3; - int vDegree = 3; - int uPoles = 6; - int vPoles = 6; - // smoothing - PyObject* smooth = Py_True; - double weight = 0.1; - double grad = 1.0; //0.5 - double bend = 0.0; //0.2 - // other parameters - int iteration = 5; - PyObject* correction = Py_True; - double factor = 1.0; - - static char* kwds_approx[] = {"Points", "UDegree", "VDegree", "NbUPoles", "NbVPoles", - "Smooth", "Weight", "Grad", "Bend", - "Iterations", "Correction", "PatchFactor", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|iiiiO!dddiO!d",kwds_approx, - &o,&uDegree,&vDegree,&uPoles,&vPoles, - &PyBool_Type,&smooth,&weight,&grad,&bend, - &iteration,&PyBool_Type,&correction,&factor)) - return 0; - - double curvdiv = 1.0 - (grad + bend); - int uOrder = uDegree + 1; - int vOrder = vDegree + 1; - - // error checking - if (grad < 0.0 || grad > 1.0) { - PyErr_SetString(PyExc_ValueError, "Value of Grad out of range [0,1]"); - return 0; - } - if (bend < 0.0 || bend > 1.0) { - PyErr_SetString(PyExc_ValueError, "Value of Bend out of range [0,1]"); - return 0; - } - if (curvdiv < 0.0 || curvdiv > 1.0) { - PyErr_SetString(PyExc_ValueError, "Sum of Grad and Bend out of range [0,1]"); - return 0; - } - if (uDegree < 1 || uOrder > uPoles) { - PyErr_SetString(PyExc_ValueError, "Value of uDegree out of range [1,NbUPoles-1]"); - return 0; - } - if (vDegree < 1 || vOrder > vPoles) { - PyErr_SetString(PyExc_ValueError, "Value of vDegree out of range [1,NbVPoles-1]"); - return 0; - } - - PY_TRY { - Py::Sequence l(o); - TColgp_Array1OfPnt clPoints(0, l.size()-1); - - int index=0; - for (Py::Sequence::iterator it = l.begin(); it != l.end(); ++it) { - Py::Tuple t(*it); - clPoints(index++) = gp_Pnt( - (double)Py::Float(t.getItem(0)), - (double)Py::Float(t.getItem(1)), - (double)Py::Float(t.getItem(2))); - } - - Reen::BSplineParameterCorrection pc(uOrder,vOrder,uPoles,vPoles); - Handle_Geom_BSplineSurface hSurf; - - pc.EnableSmoothing(PyObject_IsTrue(smooth) ? true : false, weight, grad, bend, curvdiv); - hSurf = pc.CreateSurface(clPoints, iteration, PyObject_IsTrue(correction) ? true : false, factor); - if (!hSurf.IsNull()) { - return new Part::BSplineSurfacePy(new Part::GeomBSplineSurface(hSurf)); - } - - PyErr_SetString(Base::BaseExceptionFreeCADError, "Computation of B-Spline surface failed"); - return 0; - } PY_CATCH_OCC; -} - -#if defined(HAVE_PCL_SURFACE) -static PyObject * -triangulate(PyObject *self, PyObject *args) -{ - PyObject *pcObj; - double searchRadius; - double mu=2.5; - if (!PyArg_ParseTuple(args, "O!d|d", &(Points::PointsPy::Type), &pcObj, &searchRadius, &mu)) - return NULL; - - Points::PointsPy* pPoints = static_cast(pcObj); - Points::PointKernel* points = pPoints->getPointKernelPtr(); - - Mesh::MeshObject* mesh = new Mesh::MeshObject(); - SurfaceTriangulation tria(*points, *mesh); - tria.perform(searchRadius, mu); - - return new Mesh::MeshPy(mesh); -} -#endif - -/* registration table */ -struct PyMethodDef ReverseEngineering_methods[] = { - {"approxSurface", (PyCFunction)approxSurface, METH_VARARGS|METH_KEYWORDS, - "approxSurface(Points=,UDegree=3,VDegree=3,NbUPoles=6,NbVPoles=6,Smooth=True)\n" - "Weight=0.1,Grad=1.0,Bend=0.0,\n" - "Iterations=5,Correction=True,PatchFactor=1.0" - }, -#if defined(HAVE_PCL_SURFACE) - {"triangulate" , triangulate, METH_VARARGS}, -#endif - {NULL, NULL} /* end of table marker */ -}; - diff --git a/src/Mod/ReverseEngineering/App/CMakeLists.txt b/src/Mod/ReverseEngineering/App/CMakeLists.txt index 08a73f3804..049ce841ca 100644 --- a/src/Mod/ReverseEngineering/App/CMakeLists.txt +++ b/src/Mod/ReverseEngineering/App/CMakeLists.txt @@ -36,7 +36,6 @@ set(Reen_LIBS SET(Reen_SRCS AppReverseEngineering.cpp - AppReverseEngineeringPy.cpp ApproxSurface.cpp ApproxSurface.h SurfaceTriangulation.cpp diff --git a/src/Mod/ReverseEngineering/Gui/AppReverseEngineeringGui.cpp b/src/Mod/ReverseEngineering/Gui/AppReverseEngineeringGui.cpp index 4fcba3cd5a..4401158b0c 100644 --- a/src/Mod/ReverseEngineering/Gui/AppReverseEngineeringGui.cpp +++ b/src/Mod/ReverseEngineering/Gui/AppReverseEngineeringGui.cpp @@ -30,6 +30,10 @@ #include #include #include "Workbench.h" + +#include +#include + //#include "resources/qrc_ReverseEngineering.cpp" // use a different name to CreateCommand() @@ -42,8 +46,20 @@ void loadReverseEngineeringResource() Gui::Translator::instance()->refresh(); } -/* registration table */ -extern struct PyMethodDef ReverseEngineeringGui_Import_methods[]; +namespace ReverseEngineeringGui { +class Module : public Py::ExtensionModule +{ +public: + Module() : Py::ExtensionModule("ReverseEngineeringGui") + { + initialize("This module is the ReverseEngineeringGui module."); // register with Python + } + + virtual ~Module() {} + +private: +}; +} // namespace ReverseEngineeringGui /* Python entry */ @@ -55,7 +71,7 @@ void ReenGuiExport initReverseEngineeringGui() return; } - (void) Py_InitModule("ReverseEngineeringGui", ReverseEngineeringGui_Import_methods); /* mod name, table ptr */ + new ReverseEngineeringGui::Module(); Base::Console().Log("Loading GUI of ReverseEngineering module... done\n"); // instantiating the commands diff --git a/src/Mod/ReverseEngineering/Gui/AppReverseEngineeringGuiPy.cpp b/src/Mod/ReverseEngineering/Gui/AppReverseEngineeringGuiPy.cpp deleted file mode 100644 index fcd60222ea..0000000000 --- a/src/Mod/ReverseEngineering/Gui/AppReverseEngineeringGuiPy.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/*************************************************************************** - * Copyright (c) 2008 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 -#endif - - -/* registration table */ -struct PyMethodDef ReverseEngineeringGui_Import_methods[] = { - {NULL, NULL} /* end of table marker */ -}; diff --git a/src/Mod/ReverseEngineering/Gui/CMakeLists.txt b/src/Mod/ReverseEngineering/Gui/CMakeLists.txt index a06ad687d8..b906600c14 100644 --- a/src/Mod/ReverseEngineering/Gui/CMakeLists.txt +++ b/src/Mod/ReverseEngineering/Gui/CMakeLists.txt @@ -28,7 +28,6 @@ qt4_add_resources(ReenGui_QRC_SRCS Resources/ReverseEngineering.qrc) SET(ReenGui_SRCS ${ReenGui_QRC_SRCS} AppReverseEngineeringGui.cpp - AppReverseEngineeringGuiPy.cpp Command.cpp Resources/ReverseEngineering.qrc PreCompiled.cpp