diff --git a/src/App/Document.cpp b/src/App/Document.cpp index 4bf32a9b98..8830be4367 100644 --- a/src/App/Document.cpp +++ b/src/App/Document.cpp @@ -3279,6 +3279,14 @@ bool Document::containsObject(const DocumentObject* pcObject) const return found != d->objectIdMap.end() && found->second == pcObject; } +/// Remove an object out of the document +void Document::removeObject(const DocumentObject* object) +{ + if (object->getDocument() == this) { + removeObject(object->getNameInDocument()); + } +} + /// Remove an object out of the document void Document::removeObject(const char* sName) { diff --git a/src/App/Document.h b/src/App/Document.h index c64ee69c1b..5e64b72db4 100644 --- a/src/App/Document.h +++ b/src/App/Document.h @@ -499,6 +499,13 @@ public: std::vector addObjects(const char* sType, const std::vector& objectNames, bool isNew = true); + /** + * @brief Remove an object from the document. + * + * @param[in] object The object to remove. + */ + void removeObject(const DocumentObject* object); + /** * @brief Remove an object from the document. * diff --git a/src/App/DocumentPyImp.cpp b/src/App/DocumentPyImp.cpp index 670e421abd..55d767b1d8 100644 --- a/src/App/DocumentPyImp.cpp +++ b/src/App/DocumentPyImp.cpp @@ -411,22 +411,40 @@ PyObject* DocumentPy::addObject(PyObject* args, PyObject* kwd) PyObject* DocumentPy::removeObject(PyObject* args) { - char* sName; - if (!PyArg_ParseTuple(args, "s", &sName)) { - return nullptr; - } + char* sName {}; + if (PyArg_ParseTuple(args, "s", &sName)) { + DocumentObject* object = getDocumentPtr()->getObject(sName); + if (object) { + getDocumentPtr()->removeObject(sName); + Py_Return; + } - - DocumentObject* pcFtr = getDocumentPtr()->getObject(sName); - if (pcFtr) { - getDocumentPtr()->removeObject(sName); - Py_Return; - } - else { std::stringstream str; str << "No document object found with name '" << sName << "'" << std::ends; throw Py::ValueError(str.str()); } + + PyErr_Clear(); + PyObject* objpy {}; + if (PyArg_ParseTuple(args, "O!", &App::DocumentObjectPy::Type, &objpy)) { + DocumentObject* object = static_cast(objpy)->getDocumentObjectPtr(); + if (!object) { + PyErr_Format(PyExc_RuntimeError, "Invalid document object"); + return nullptr; + } + + if (object->getDocument() == getDocumentPtr()) { + getDocumentPtr()->removeObject(object); + Py_Return; + } + + std::stringstream str; + str << "Document object is not part of this document"; + throw Py::ValueError(str.str()); + } + + PyErr_SetString(PyExc_TypeError, "Expect str or DocumentObject"); + return nullptr; } PyObject* DocumentPy::copyObject(PyObject* args, PyObject* kwd)