diff --git a/src/Mod/Part/App/Geometry.cpp b/src/Mod/Part/App/Geometry.cpp index 317546722d..0cc5f6b42a 100644 --- a/src/Mod/Part/App/Geometry.cpp +++ b/src/Mod/Part/App/Geometry.cpp @@ -296,6 +296,16 @@ const std::weak_ptr Geometry::getExtension(Base::Type type) c throw Base::ValueError("No geometry extension of the requested type."); } +const std::weak_ptr Geometry::getExtension(std::string name) const +{ + for( auto ext : extensions) { + if(ext->getName() == name) + return ext; + } + + throw Base::ValueError("No geometry extension with the requested name."); +} + void Geometry::setExtension(std::unique_ptr && geo) { bool hasext=false; diff --git a/src/Mod/Part/App/Geometry.h b/src/Mod/Part/App/Geometry.h index 986c2ed5cc..769cc06c3c 100644 --- a/src/Mod/Part/App/Geometry.h +++ b/src/Mod/Part/App/Geometry.h @@ -102,6 +102,7 @@ public: bool hasExtension(Base::Type type) const; bool hasExtension(std::string name) const; const std::weak_ptr getExtension(Base::Type type) const; + const std::weak_ptr getExtension(std::string name) const; void setExtension(std::unique_ptr &&geo); void deleteExtension(Base::Type type); void deleteExtension(std::string name); diff --git a/src/Mod/Part/App/GeometryPy.xml b/src/Mod/Part/App/GeometryPy.xml index b794705abd..f6ec0be9a9 100644 --- a/src/Mod/Part/App/GeometryPy.xml +++ b/src/Mod/Part/App/GeometryPy.xml @@ -53,19 +53,24 @@ It describes the common behavior of these objects when: Create a clone of this geometry with the same Tag - + Returns a boolean indicating whether a geometry extension of the type indicated as a string exists. - + Returns a boolean indicating whether a geometry extension with the name indicated as a string exists. - + - Gets a geometry extension of the indicated type. + Gets the first geometry extension of the type indicated by the string. + + + + + Gets the first geometry extension of the name indicated by the string. @@ -73,12 +78,12 @@ It describes the common behavior of these objects when: Sets a geometry extension of the indicated type. - + Deletes all extensions of the indicated type. - + Deletes all extensions of the indicated name. diff --git a/src/Mod/Part/App/GeometryPyImp.cpp b/src/Mod/Part/App/GeometryPyImp.cpp index 458a47c9b8..0a658e9704 100644 --- a/src/Mod/Part/App/GeometryPyImp.cpp +++ b/src/Mod/Part/App/GeometryPyImp.cpp @@ -257,7 +257,7 @@ PyObject* GeometryPy::setExtension(PyObject *args) return 0; } -PyObject* GeometryPy::getExtension(PyObject *args) +PyObject* GeometryPy::getExtensionOfType(PyObject *args) { char* o; if (PyArg_ParseTuple(args, "s", &o)) { @@ -266,18 +266,21 @@ PyObject* GeometryPy::getExtension(PyObject *args) if(type != Base::Type::badType()) { try { - const std::weak_ptr ext = this->getGeometryPtr()->getExtension(type); + std::shared_ptr ext(this->getGeometryPtr()->getExtension(type)); - std::unique_ptr cext = ext.lock()->copy(); + // we create a copy and tranfer this copy's memory management responsibility to Python + GeometryExtension * rext = ext->copy().release(); - GeometryExtension * pcext = cext.release(); - - return pcext->getPyObject(); + return rext->getPyObject(); } catch(Base::ValueError e) { PyErr_SetString(PartExceptionOCCError, e.what()); return 0; } + catch(std::bad_weak_ptr e) { + PyErr_SetString(PartExceptionOCCError, "Geometry extension does not exist anymore."); + return 0; + } } else { @@ -291,7 +294,35 @@ PyObject* GeometryPy::getExtension(PyObject *args) return 0; } -PyObject* GeometryPy::hasExtensionType(PyObject *args) +PyObject* GeometryPy::getExtensionOfName(PyObject *args) +{ + char* o; + if (PyArg_ParseTuple(args, "s", &o)) { + + try { + std::shared_ptr ext(this->getGeometryPtr()->getExtension(std::string(o))); + + // we create a copy and tranfer this copy's memory management responsibility to Python + GeometryExtension * rext = ext->copy().release(); + + return rext->getPyObject(); + } + catch(Base::ValueError e) { + PyErr_SetString(PartExceptionOCCError, e.what()); + return 0; + } + catch(std::bad_weak_ptr e) { + PyErr_SetString(PartExceptionOCCError, "Geometry extension does not exist anymore."); + return 0; + } + + } + + PyErr_SetString(PartExceptionOCCError, "A string with the name of the geometry extension was expected"); + return 0; +} + +PyObject* GeometryPy::hasExtensionOfType(PyObject *args) { char* o; if (PyArg_ParseTuple(args, "s", &o)) { @@ -319,7 +350,7 @@ PyObject* GeometryPy::hasExtensionType(PyObject *args) return 0; } -PyObject* GeometryPy::hasExtensionName(PyObject *args) +PyObject* GeometryPy::hasExtensionOfName(PyObject *args) { char* o; if (PyArg_ParseTuple(args, "s", &o)) { @@ -338,7 +369,7 @@ PyObject* GeometryPy::hasExtensionName(PyObject *args) return 0; } -PyObject* GeometryPy::deleteExtensionType(PyObject *args) +PyObject* GeometryPy::deleteExtensionOfType(PyObject *args) { char* o; if (PyArg_ParseTuple(args, "s", &o)) { @@ -367,7 +398,7 @@ PyObject* GeometryPy::deleteExtensionType(PyObject *args) return 0; } -PyObject* GeometryPy::deleteExtensionName(PyObject *args) +PyObject* GeometryPy::deleteExtensionOfName(PyObject *args) { char* o; if (PyArg_ParseTuple(args, "s", &o)) {