From bcde3cd5a35f3b3e6d4aa79e5319526085c0b3d3 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 23 Jan 2016 19:04:03 +0100 Subject: [PATCH] py3: ported Skechter to python3 --- src/Mod/Sketcher/App/AppSketcher.cpp | 6 ++-- src/Mod/Sketcher/App/ConstraintPy.xml | 4 +-- src/Mod/Sketcher/App/ConstraintPyImp.cpp | 38 ++++++++++++++++---- src/Mod/Sketcher/App/SketchObjectPy.xml | 6 ++-- src/Mod/Sketcher/App/SketchObjectPyImp.cpp | 41 +++++++++++++++------- src/Mod/Sketcher/App/SketchPy.xml | 2 +- src/Mod/Sketcher/App/SketchPyImp.cpp | 14 ++++---- src/Mod/Sketcher/Gui/AppSketcherGui.cpp | 16 ++++++--- src/Mod/Sketcher/InitGui.py | 4 +-- 9 files changed, 91 insertions(+), 40 deletions(-) diff --git a/src/Mod/Sketcher/App/AppSketcher.cpp b/src/Mod/Sketcher/App/AppSketcher.cpp index 29b28c793..9bfa4daaa 100644 --- a/src/Mod/Sketcher/App/AppSketcher.cpp +++ b/src/Mod/Sketcher/App/AppSketcher.cpp @@ -43,7 +43,7 @@ extern PyObject* initModule(); } /* Python entry */ -PyMODINIT_FUNC initSketcher() +PyMOD_INIT_FUNC(Sketcher) { // load dependent module try { @@ -51,7 +51,7 @@ PyMODINIT_FUNC initSketcher() } catch(const Base::Exception& e) { PyErr_SetString(PyExc_ImportError, e.what()); - return; + PyMOD_Return(0); } PyObject* sketcherModule = Sketcher::initModule(); @@ -73,4 +73,6 @@ PyMODINIT_FUNC initSketcher() Sketcher::PropertyConstraintList::init(); Base::Console().Log("Loading Sketcher module... done\n"); + + PyMOD_Return(sketcherModule); } diff --git a/src/Mod/Sketcher/App/ConstraintPy.xml b/src/Mod/Sketcher/App/ConstraintPy.xml index 06750f994..32579e9be 100644 --- a/src/Mod/Sketcher/App/ConstraintPy.xml +++ b/src/Mod/Sketcher/App/ConstraintPy.xml @@ -20,13 +20,13 @@ First geometry index the Constraint refers to - + Second geometry index the Constraint refers to - + diff --git a/src/Mod/Sketcher/App/ConstraintPyImp.cpp b/src/Mod/Sketcher/App/ConstraintPyImp.cpp index fb944a153..9f50e3369 100644 --- a/src/Mod/Sketcher/App/ConstraintPyImp.cpp +++ b/src/Mod/Sketcher/App/ConstraintPyImp.cpp @@ -77,8 +77,13 @@ int ConstraintPy::PyInit(PyObject* args, PyObject* /*kwd*/) if (PyArg_ParseTuple(args, "siO", &ConstraintType, &FirstIndex, &index_or_value)) { // ConstraintType, GeoIndex1, GeoIndex2 +#if PY_MAJOR_VERSION >= 3 + if (PyLong_Check(index_or_value)) { + SecondIndex = PyLong_AsLong(index_or_value); +#else if (PyInt_Check(index_or_value)) { SecondIndex = PyInt_AsLong(index_or_value); +#endif bool valid = false; if (strcmp("Tangent",ConstraintType) == 0) { this->getConstraintPtr()->Type = Tangent; @@ -159,9 +164,15 @@ int ConstraintPy::PyInit(PyObject* args, PyObject* /*kwd*/) if (PyArg_ParseTuple(args, "siiO", &ConstraintType, &FirstIndex, &any_index, &index_or_value)) { // ConstraintType, GeoIndex1, PosIndex1, GeoIndex2 +#if PY_MAJOR_VERSION >= 3 + if (PyLong_Check(index_or_value)) { + FirstPos = any_index; + SecondIndex = PyLong_AsLong(index_or_value); +#else if (PyInt_Check(index_or_value)) { FirstPos = any_index; SecondIndex = PyInt_AsLong(index_or_value); +#endif bool valid = false; if (strcmp("Perpendicular", ConstraintType) == 0) { this->getConstraintPtr()->Type = Perpendicular; @@ -245,8 +256,13 @@ int ConstraintPy::PyInit(PyObject* args, PyObject* /*kwd*/) if (PyArg_ParseTuple(args, "siiiO", &ConstraintType, &intArg1, &intArg2, &intArg3, &oNumArg4)) { // Value, ConstraintType, GeoIndex1, PosIndex1, GeoIndex2, PosIndex2 +#if PY_MAJOR_VERSION >= 3 + if (PyLong_Check(oNumArg4)) { + intArg4 = PyLong_AsLong(oNumArg4); +#else if (PyInt_Check(oNumArg4)) { intArg4 = PyInt_AsLong(oNumArg4); +#endif bool valid = false; if (strcmp("Coincident", ConstraintType) == 0) { this->getConstraintPtr()->Type = Coincident; @@ -337,8 +353,13 @@ int ConstraintPy::PyInit(PyObject* args, PyObject* /*kwd*/) if (PyArg_ParseTuple(args, "siiiiO", &ConstraintType, &intArg1, &intArg2, &intArg3, &intArg4, &oNumArg5)) { // ConstraintType, GeoIndex1, PosIndex1, GeoIndex2, PosIndex2, GeoIndex3 +#if PY_MAJOR_VERSION >= 3 + if (PyLong_Check(oNumArg5)) { + intArg5 = PyLong_AsLong(oNumArg5); +#else if (PyInt_Check(oNumArg5)) { intArg5 = PyInt_AsLong(oNumArg5); +#endif if (strcmp("Symmetric",ConstraintType) == 0 ) { this->getConstraintPtr()->Type = Symmetric; this->getConstraintPtr()->First = intArg1; @@ -404,8 +425,13 @@ int ConstraintPy::PyInit(PyObject* args, PyObject* /*kwd*/) PyErr_Clear(); if (PyArg_ParseTuple(args, "siiiiiO", &ConstraintType, &FirstIndex, &FirstPos, &SecondIndex, &SecondPos, &ThirdIndex, &index_or_value)) { +#if PY_MAJOR_VERSION >= 3 + if (PyLong_Check(index_or_value)) { + ThirdPos = PyLong_AsLong(index_or_value); +#else if (PyInt_Check(index_or_value)) { ThirdPos = PyInt_AsLong(index_or_value); +#endif // ConstraintType, GeoIndex1, PosIndex1, GeoIndex2, PosIndex2, GeoIndex3, PosIndex3 if (strcmp("Symmetric",ConstraintType) == 0 ) { this->getConstraintPtr()->Type = Symmetric; @@ -495,22 +521,22 @@ std::string ConstraintPy::representation(void) const return result.str(); } -Py::Int ConstraintPy::getFirst(void) const +Py::Long ConstraintPy::getFirst(void) const { - return Py::Int(this->getConstraintPtr()->First); + return Py::Long(this->getConstraintPtr()->First); } -void ConstraintPy::setFirst(Py::Int arg) +void ConstraintPy::setFirst(Py::Long arg) { this->getConstraintPtr()->First = arg; } -Py::Int ConstraintPy::getSecond(void) const +Py::Long ConstraintPy::getSecond(void) const { - return Py::Int(this->getConstraintPtr()->Second); + return Py::Long(this->getConstraintPtr()->Second); } -void ConstraintPy::setSecond(Py::Int arg) +void ConstraintPy::setSecond(Py::Long arg) { this->getConstraintPtr()->Second = arg; } diff --git a/src/Mod/Sketcher/App/SketchObjectPy.xml b/src/Mod/Sketcher/App/SketchObjectPy.xml index d09797363..57846b43f 100644 --- a/src/Mod/Sketcher/App/SketchObjectPy.xml +++ b/src/Mod/Sketcher/App/SketchObjectPy.xml @@ -228,13 +228,13 @@ Number of Constraints in this sketch - + Number of geometric objects in this sketch - + @@ -242,7 +242,7 @@ return the the number of construction lines in the sketch which can be used as axes - + diff --git a/src/Mod/Sketcher/App/SketchObjectPyImp.cpp b/src/Mod/Sketcher/App/SketchObjectPyImp.cpp index 7ff121fba..2e0e22eae 100644 --- a/src/Mod/Sketcher/App/SketchObjectPyImp.cpp +++ b/src/Mod/Sketcher/App/SketchObjectPyImp.cpp @@ -124,7 +124,7 @@ PyObject* SketchObjectPy::addGeometry(PyObject *args) PyErr_SetString(PyExc_TypeError, str.str().c_str()); return 0; } - return Py::new_reference_to(Py::Int(ret)); + return Py::new_reference_to(Py::Long(ret)); } else if (PyObject_TypeCheck(pcObj, &(PyList_Type)) || PyObject_TypeCheck(pcObj, &(PyTuple_Type))) { @@ -186,7 +186,7 @@ PyObject* SketchObjectPy::addGeometry(PyObject *args) Py::Tuple tuple(numGeo); for (std::size_t i=0; igetSketchObjectPtr()->noRecomputes) this->getSketchObjectPtr()->setUpSketch(); - return Py::new_reference_to(Py::Int(ret)); + return Py::new_reference_to(Py::Long(ret)); } else if (PyObject_TypeCheck(pcObj, &(PyList_Type)) || PyObject_TypeCheck(pcObj, &(PyTuple_Type))) { @@ -302,7 +302,7 @@ PyObject* SketchObjectPy::addConstraint(PyObject *args) Py::Tuple tuple(numCon); for (std::size_t i=0; i geoIdList; Py::Sequence list(pcObj); for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { +#if PY_MAJOR_VERSION >= 3 + if (PyLong_Check((*it).ptr())) + geoIdList.push_back(PyLong_AsLong((*it).ptr())); +#else if (PyInt_Check((*it).ptr())) geoIdList.push_back(PyInt_AsLong((*it).ptr())); +#endif } int ret = this->getSketchObjectPtr()->addSymmetric(geoIdList,refGeoId,(Sketcher::PointPos) refPosId) + 1; @@ -815,7 +820,7 @@ PyObject* SketchObjectPy::addSymmetric(PyObject *args) Py::Tuple tuple(numGeo); for (std::size_t i=0; i geoIdList; Py::Sequence list(pcObj); for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { +#if PY_MAJOR_VERSION >= 3 + if (PyLong_Check((*it).ptr())) + geoIdList.push_back(PyLong_AsLong((*it).ptr())); +#else if (PyInt_Check((*it).ptr())) geoIdList.push_back(PyInt_AsLong((*it).ptr())); +#endif } int ret = this->getSketchObjectPtr()->addCopy(geoIdList, vect, PyObject_IsTrue(clone) ? true : false) + 1; @@ -854,7 +864,7 @@ PyObject* SketchObjectPy::addCopy(PyObject *args) Py::Tuple tuple(numGeo); for (std::size_t i=0; i geoIdList; Py::Sequence list(pcObj); for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { +#if PY_MAJOR_VERSION >= 3 + if (PyLong_Check((*it).ptr())) + geoIdList.push_back(PyLong_AsLong((*it).ptr())); +#else if (PyInt_Check((*it).ptr())) geoIdList.push_back(PyInt_AsLong((*it).ptr())); +#endif } int ret = this->getSketchObjectPtr()->addCopy(geoIdList,vect, PyObject_IsTrue(clone) ? true : false, @@ -961,7 +976,7 @@ PyObject* SketchObjectPy::changeConstraintsLocking(PyObject *args) int naff = obj->changeConstraintsLocking((bool)bLock); - return Py::new_reference_to(Py::Int(naff)); + return Py::new_reference_to(Py::Long(naff)); } //Deprecated @@ -1069,19 +1084,19 @@ PyObject* SketchObjectPy::increaseBSplineDegree(PyObject *args) Py_Return; } -Py::Int SketchObjectPy::getConstraintCount(void) const +Py::Long SketchObjectPy::getConstraintCount(void) const { - return Py::Int(this->getSketchObjectPtr()->Constraints.getSize()); + return Py::Long(this->getSketchObjectPtr()->Constraints.getSize()); } -Py::Int SketchObjectPy::getGeometryCount(void) const +Py::Long SketchObjectPy::getGeometryCount(void) const { - return Py::Int(this->getSketchObjectPtr()->Geometry.getSize()); + return Py::Long(this->getSketchObjectPtr()->Geometry.getSize()); } -Py::Int SketchObjectPy::getAxisCount(void) const +Py::Long SketchObjectPy::getAxisCount(void) const { - return Py::Int(this->getSketchObjectPtr()->getAxisCount()); + return Py::Long(this->getSketchObjectPtr()->getAxisCount()); } PyObject *SketchObjectPy::getCustomAttributes(const char* /*attr*/) const diff --git a/src/Mod/Sketcher/App/SketchPy.xml b/src/Mod/Sketcher/App/SketchPy.xml index 71c5b3f44..bc4599e11 100644 --- a/src/Mod/Sketcher/App/SketchPy.xml +++ b/src/Mod/Sketcher/App/SketchPy.xml @@ -54,7 +54,7 @@ 0: exactly constraint, -1 under-constraint, 1 over-constraint - + diff --git a/src/Mod/Sketcher/App/SketchPyImp.cpp b/src/Mod/Sketcher/App/SketchPyImp.cpp index 07b4b1bf7..eb6311d8a 100644 --- a/src/Mod/Sketcher/App/SketchPyImp.cpp +++ b/src/Mod/Sketcher/App/SketchPyImp.cpp @@ -65,7 +65,7 @@ PyObject* SketchPy::solve(PyObject *args) if (!PyArg_ParseTuple(args, "")) return 0; getSketchPtr()->resetSolver(); - return Py::new_reference_to(Py::Int(getSketchPtr()->solve())); + return Py::new_reference_to(Py::Long(getSketchPtr()->solve())); } PyObject* SketchPy::addGeometry(PyObject *args) @@ -76,7 +76,7 @@ PyObject* SketchPy::addGeometry(PyObject *args) if (PyObject_TypeCheck(pcObj, &(Part::GeometryPy::Type))) { Part::Geometry *geo = static_cast(pcObj)->getGeometryPtr(); - return Py::new_reference_to(Py::Int(this->getSketchPtr()->addGeometry(geo))); + return Py::new_reference_to(Py::Long(this->getSketchPtr()->addGeometry(geo))); } else if (PyObject_TypeCheck(pcObj, &(PyList_Type)) || PyObject_TypeCheck(pcObj, &(PyTuple_Type))) { @@ -94,7 +94,7 @@ PyObject* SketchPy::addGeometry(PyObject *args) Py::Tuple tuple(numGeo); for (std::size_t i=0; i(pcObj); int ret = getSketchPtr()->addConstraint(pcObject->getConstraintPtr()); - return Py::new_reference_to(Py::Int(ret)); + return Py::new_reference_to(Py::Long(ret)); } else { std::string error = std::string("type must be 'Constraint' or list of 'Constraint', not "); @@ -160,12 +160,12 @@ PyObject* SketchPy::movePoint(PyObject *args) return 0; Base::Vector3d* toPoint = static_cast(pcObj)->getVectorPtr(); - return Py::new_reference_to(Py::Int(getSketchPtr()->movePoint(index1,(Sketcher::PointPos)index2,*toPoint,(relative>0)))); + return Py::new_reference_to(Py::Long(getSketchPtr()->movePoint(index1,(Sketcher::PointPos)index2,*toPoint,(relative>0)))); } // +++ attributes implementer ++++++++++++++++++++++++++++++++++++++++++++++++ -Py::Int SketchPy::getConstraint(void) const +Py::Long SketchPy::getConstraint(void) const { //return Py::Int(); throw Py::AttributeError("Not yet implemented"); diff --git a/src/Mod/Sketcher/Gui/AppSketcherGui.cpp b/src/Mod/Sketcher/Gui/AppSketcherGui.cpp index 8a2116844..488f62e9f 100644 --- a/src/Mod/Sketcher/Gui/AppSketcherGui.cpp +++ b/src/Mod/Sketcher/Gui/AppSketcherGui.cpp @@ -72,14 +72,20 @@ public: private: }; + +PyObject* initModule() +{ + return (new Module)->module().ptr(); +} + } // namespace SketcherGui /* Python entry */ -PyMODINIT_FUNC initSketcherGui() +PyMOD_INIT_FUNC(SketcherGui) { if (!Gui::Application::Instance) { PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application."); - return; + PyMOD_Return(0); } try { Base::Interpreter().runString("import PartGui"); @@ -87,10 +93,10 @@ PyMODINIT_FUNC initSketcherGui() } catch(const Base::Exception& e) { PyErr_SetString(PyExc_ImportError, e.what()); - return; + PyMOD_Return(0); } - (void)new SketcherGui::Module(); + PyObject* mod = SketcherGui::initModule(); Base::Console().Log("Loading GUI of Sketcher module... done\n"); // instantiating the commands @@ -116,4 +122,6 @@ PyMODINIT_FUNC initSketcherGui() // add resources and reloads the translators loadSketcherResource(); + + PyMOD_Return(mod); } diff --git a/src/Mod/Sketcher/InitGui.py b/src/Mod/Sketcher/InitGui.py index 03ff6fd87..b8c7571b0 100644 --- a/src/Mod/Sketcher/InitGui.py +++ b/src/Mod/Sketcher/InitGui.py @@ -32,11 +32,11 @@ class SketcherWorkbench ( Workbench ): - "Sketcher workbench object" + "Sketcher workbench object" def __init__(self): self.__class__.Icon = FreeCAD.getResourceDir() + "Mod/Sketcher/Resources/icons/SketcherWorkbench.svg" self.__class__.MenuText = "Sketcher" - self.__class__.ToolTip = "Sketcher workbench" + self.__class__.ToolTip = "Sketcher workbench" def Initialize(self): # load the module