Sketcher/Part: Python handling of GeometryExtensions without a Python counterpart

=================================================================================

Some geometry extensions do not provide a PyObject as they do not have a Python counterpart
as it would serve no purpose to have it.

This commit handles this situation making sure to provide the right error to Python
or to ignore the extension where appropriate.
This commit is contained in:
Abdullah Tahiri
2020-12-09 19:28:19 +01:00
committed by abdullahtahiriyo
parent 40c0f0aa01
commit 9add3ba199
4 changed files with 59 additions and 12 deletions

View File

@@ -261,6 +261,10 @@ PyObject* GeometryPy::getExtensionOfType(PyObject *args)
PyErr_SetString(PartExceptionOCCError, "Geometry extension does not exist anymore.");
return 0;
}
catch(Base::NotImplementedError) {
PyErr_SetString(Part::PartExceptionOCCError, "Geometry extension does not implement a Python counterpart.");
return 0;
}
}
else
{
@@ -296,6 +300,10 @@ PyObject* GeometryPy::getExtensionOfName(PyObject *args)
PyErr_SetString(PartExceptionOCCError, "Geometry extension does not exist anymore.");
return 0;
}
catch(Base::NotImplementedError) {
PyErr_SetString(Part::PartExceptionOCCError, "Geometry extension does not implement a Python counterpart.");
return 0;
}
}
@@ -408,7 +416,7 @@ PyObject* GeometryPy::getExtensions(PyObject *args)
try {
const std::vector<std::weak_ptr<const GeometryExtension>> ext = this->getGeometryPtr()->getExtensions();
PyObject* list = PyList_New(ext.size());
PyObject* list = PyList_New(0);
for (std::size_t i=0; i<ext.size(); ++i) {
@@ -417,10 +425,17 @@ PyObject* GeometryPy::getExtensions(PyObject *args)
if(p) {
// we create a python copy and add it to the list
Py::Tuple tuple;
PyObject* cpy = static_cast<GeometryExtensionPy *>(p->getPyObject())->copy(tuple.ptr());
PyList_SetItem( list, i, cpy);
try {
Py::Tuple tuple;
PyObject* cpy = static_cast<GeometryExtensionPy *>(p->getPyObject())->copy(tuple.ptr());
PyList_Append( list, cpy);
Py_DECREF(cpy);
}
catch(Base::NotImplementedError) {
// silently ignoring extensions not having a Python object
}
}
}

View File

@@ -41,6 +41,10 @@ class ExternalGeometryFacadePy;
//
// Exactly the same considerations as for GeometryFacade apply (see documentation of GeometryFacade).
//
// It was not made publicly deriving from GeometryFacade because it is not possible to differentiate functions by return type, which is the
// case of getFacade() returning a unique_ptr to GeometryFacade in GeometryFacade, and one to ExternalGeometryFacade. I have not managed to
// find a good solution to this problem, thus the code duplication.
//
// Summary Remarks:
// It is intended to have a separate type (not being a Geometry type).
// it is intended to have the relevant interface in full for the sketcher extension only

View File

@@ -318,6 +318,10 @@ PyObject* ExternalGeometryFacadePy::getExtensionOfType(PyObject *args)
PyErr_SetString(Part::PartExceptionOCCError, "Geometry extension does not exist anymore.");
return 0;
}
catch(Base::NotImplementedError) {
PyErr_SetString(Part::PartExceptionOCCError, "Geometry extension does not implement a Python counterpart.");
return 0;
}
}
else
{
@@ -353,6 +357,10 @@ PyObject* ExternalGeometryFacadePy::getExtensionOfName(PyObject *args)
PyErr_SetString(Part::PartExceptionOCCError, "Geometry extension does not exist anymore.");
return 0;
}
catch(Base::NotImplementedError) {
PyErr_SetString(Part::PartExceptionOCCError, "Geometry extension does not implement a Python counterpart.");
return 0;
}
}
@@ -465,7 +473,7 @@ PyObject* ExternalGeometryFacadePy::getExtensions(PyObject *args)
try {
const std::vector<std::weak_ptr<const Part::GeometryExtension>> ext = this->getExternalGeometryFacadePtr()->getExtensions();
PyObject* list = PyList_New(ext.size());
PyObject* list = PyList_New(0);
for (std::size_t i=0; i<ext.size(); ++i) {
@@ -473,10 +481,16 @@ PyObject* ExternalGeometryFacadePy::getExtensions(PyObject *args)
if(p) {
// we create a python copy and add it to the list
Py::Tuple tuple;
PyObject* cpy = static_cast<Part::GeometryExtensionPy *>(std::const_pointer_cast<Part::GeometryExtension>(p)->getPyObject())->copy(tuple.ptr());
try {
Py::Tuple tuple;
PyObject* cpy = static_cast<Part::GeometryExtensionPy *>(std::const_pointer_cast<Part::GeometryExtension>(p)->getPyObject())->copy(tuple.ptr());
PyList_SetItem( list, i, cpy);
PyList_Append( list, cpy);
Py_DECREF(cpy);
}
catch(Base::NotImplementedError) {
// silently ignoring extensions not having a Python object
}
}
}

View File

@@ -297,6 +297,10 @@ PyObject* GeometryFacadePy::getExtensionOfType(PyObject *args)
PyErr_SetString(Part::PartExceptionOCCError, "Geometry extension does not exist anymore.");
return 0;
}
catch(Base::NotImplementedError) {
PyErr_SetString(Part::PartExceptionOCCError, "Geometry extension does not implement a Python counterpart.");
return 0;
}
}
else
{
@@ -332,7 +336,10 @@ PyObject* GeometryFacadePy::getExtensionOfName(PyObject *args)
PyErr_SetString(Part::PartExceptionOCCError, "Geometry extension does not exist anymore.");
return 0;
}
catch(Base::NotImplementedError) {
PyErr_SetString(Part::PartExceptionOCCError, "Geometry extension does not implement a Python counterpart.");
return 0;
}
}
PyErr_SetString(Part::PartExceptionOCCError, "A string with the name of the geometry extension was expected");
@@ -444,7 +451,7 @@ PyObject* GeometryFacadePy::getExtensions(PyObject *args)
try {
const std::vector<std::weak_ptr<const Part::GeometryExtension>> ext = this->getGeometryFacadePtr()->getExtensions();
PyObject* list = PyList_New(ext.size());
PyObject* list = PyList_New(0);
for (std::size_t i=0; i<ext.size(); ++i) {
@@ -453,9 +460,16 @@ PyObject* GeometryFacadePy::getExtensions(PyObject *args)
if(p) {
// we create a python copy and add it to the list
Py::Tuple tuple;
PyObject* cpy = static_cast<Part::GeometryExtensionPy *>(std::const_pointer_cast<Part::GeometryExtension>(p)->getPyObject())->copy(tuple.ptr());
PyList_SetItem( list, i, cpy);
try {
PyObject* cpy = static_cast<Part::GeometryExtensionPy *>(std::const_pointer_cast<Part::GeometryExtension>(p)->getPyObject())->copy(tuple.ptr());
PyList_Append( list, cpy);
Py_DECREF(cpy);
}
catch(Base::NotImplementedError) {
// silently ignoring extensions not having a Python object
}
}
}