From 284c4d93f75a73532f1ac3552a6d988e2d127d9e Mon Sep 17 00:00:00 2001 From: Abdullah Tahiri Date: Wed, 28 Oct 2020 11:48:07 +0100 Subject: [PATCH] Sketcher: Extend SketchObject Python Interface to get/set SketchGeometryExtension GeometryId (convenience interface) --- src/Mod/Sketcher/App/SketchObject.cpp | 55 ++++++++++++++++++++++ src/Mod/Sketcher/App/SketchObject.h | 4 ++ src/Mod/Sketcher/App/SketchObjectPy.xml | 10 ++++ src/Mod/Sketcher/App/SketchObjectPyImp.cpp | 38 +++++++++++++++ 4 files changed, 107 insertions(+) diff --git a/src/Mod/Sketcher/App/SketchObject.cpp b/src/Mod/Sketcher/App/SketchObject.cpp index 0ecd466bae..7ee636ead5 100644 --- a/src/Mod/Sketcher/App/SketchObject.cpp +++ b/src/Mod/Sketcher/App/SketchObject.cpp @@ -78,6 +78,8 @@ #include #include +#include "GeometryFacade.h" + #include "SketchObject.h" #include "Sketch.h" #include @@ -7657,6 +7659,59 @@ std::vector SketchObject::getOpenVertices(void) const return points; } +// SketchGeometryExtension interface + +int SketchObject::setGeometryId(int GeoId, long id) +{ + Base::StateLocker lock(managedoperation, true); // no need to check input data validity as this is an sketchobject managed operation. + + if (GeoId < 0 || GeoId >= int(Geometry.getValues().size())) + return -1; + + const std::vector< Part::Geometry * > &vals = getInternalGeometry(); + + + + std::vector< Part::Geometry * > newVals(vals); + + // deep copy + for(size_t i=0; iclone(); + + if((int)i == GeoId) { + auto gf = GeometryFacade::getFacade(newVals[i]); + + gf->setId(id); + } + } + + // There is not actual internal transaction going on here, however neither the geometry indices nor the vertices need to be updated + // so this is a convenient way of preventing it. + { + Base::StateLocker lock(internaltransaction, true); + this->Geometry.setValues(std::move(newVals)); + } + + return 0; +} + +int SketchObject::getGeometryId(int GeoId, long &id) const +{ + if (GeoId < 0 || GeoId >= int(Geometry.getValues().size())) + return -1; + + const std::vector< Part::Geometry * > &vals = getInternalGeometry(); + + auto gf = GeometryFacade::getFacade(vals[GeoId]); + + id = gf->getId(); + + return 0; +} + + + + // Python Sketcher feature --------------------------------------------------------- namespace App { diff --git a/src/Mod/Sketcher/App/SketchObject.h b/src/Mod/Sketcher/App/SketchObject.h index 6c5535a59c..b42804f5bf 100644 --- a/src/Mod/Sketcher/App/SketchObject.h +++ b/src/Mod/Sketcher/App/SketchObject.h @@ -418,6 +418,10 @@ public: // Validation routines std::vector getOpenVertices(void) const; +public: // geometry extension functionalities for single element sketch object user convenience + int setGeometryId(int GeoId, long id); + int getGeometryId(int GeoId, long &id) const; + protected: /// get called by the container when a property has changed virtual void onChanged(const App::Property* /*prop*/) override; diff --git a/src/Mod/Sketcher/App/SketchObjectPy.xml b/src/Mod/Sketcher/App/SketchObjectPy.xml index 44a50b8fcd..abca43ff6d 100644 --- a/src/Mod/Sketcher/App/SketchObjectPy.xml +++ b/src/Mod/Sketcher/App/SketchObjectPy.xml @@ -458,5 +458,15 @@ If there is no such constraint an exception is raised. + + + switch a geometry to a construction line + + + + + switch a geometry to a construction line + + diff --git a/src/Mod/Sketcher/App/SketchObjectPyImp.cpp b/src/Mod/Sketcher/App/SketchObjectPyImp.cpp index 162f608f14..ab045293f6 100644 --- a/src/Mod/Sketcher/App/SketchObjectPyImp.cpp +++ b/src/Mod/Sketcher/App/SketchObjectPyImp.cpp @@ -1792,6 +1792,44 @@ void SketchObjectPy::setGeometryFacadeList(Py::List value) getSketchObjectPtr()->Geometry.setValues(std::move(list)); } +PyObject* SketchObjectPy::getGeometryId(PyObject *args) +{ + int Index; + if (!PyArg_ParseTuple(args, "i", &Index)) + return 0; + + long Id; + + if (this->getSketchObjectPtr()->getGeometryId(Index, Id)) { + std::stringstream str; + str << "Not able to set geometry Id of a geometry with the given index: " << Index; + PyErr_SetString(PyExc_ValueError, str.str().c_str()); + Py_Return; + } + + return Py::new_reference_to(Py::Long(Id)); +} + +PyObject* SketchObjectPy::setGeometryId(PyObject *args) +{ + int Index; + long Id; + if (!PyArg_ParseTuple(args, "il", &Index, &Id)) + return 0; + + if (this->getSketchObjectPtr()->setGeometryId(Index, Id)) { + std::stringstream str; + str << "Not able to set construction mode of a geometry with the given index: " << Index; + PyErr_SetString(PyExc_ValueError, str.str().c_str()); + return 0; + } + + Py_Return; +} + + + + PyObject *SketchObjectPy::getCustomAttributes(const char* /*attr*/) const { return 0;