diff --git a/src/App/Document.cpp b/src/App/Document.cpp index 519f5d7e4e..f130bb35a5 100644 --- a/src/App/Document.cpp +++ b/src/App/Document.cpp @@ -63,6 +63,7 @@ recompute path. Also enables more complicated dependencies beyond trees. #include #include #include +#include #include "Document.h" @@ -1689,6 +1690,20 @@ std::vector Document::getObjectsOfType(const Base::Type& typeId return Objects; } +std::vector Document::findObjects(const Base::Type& typeId, const char* objname) const +{ + boost::regex rx(objname); + boost::cmatch what; + std::vector Objects; + for (std::vector::const_iterator it = d->objectArray.begin(); it != d->objectArray.end(); ++it) { + if ((*it)->getTypeId().isDerivedFrom(typeId)) { + if (boost::regex_match((*it)->getNameInDocument(), what, rx)) + Objects.push_back(*it); + } + } + return Objects; +} + int Document::countObjectsOfType(const Base::Type& typeId) const { int ct=0; diff --git a/src/App/Document.h b/src/App/Document.h index dcf56d670d..d1675001e6 100644 --- a/src/App/Document.h +++ b/src/App/Document.h @@ -161,6 +161,7 @@ public: /// Returns a list of all Objects std::vector getObjects() const; std::vector getObjectsOfType(const Base::Type& typeId) const; + std::vector findObjects(const Base::Type& typeId, const char* objname) const; /// Returns an array with the correct types already. template inline std::vector getObjectsOfType() const; int countObjectsOfType(const Base::Type& typeId) const; diff --git a/src/App/DocumentPyImp.cpp b/src/App/DocumentPyImp.cpp index 3b7fdd289d..6bd02f51ca 100644 --- a/src/App/DocumentPyImp.cpp +++ b/src/App/DocumentPyImp.cpp @@ -290,42 +290,36 @@ PyObject* DocumentPy::findObjects(PyObject *args) char *sType="App::DocumentObject", *sName=0; if (!PyArg_ParseTuple(args, "|ss",&sType, &sName)) // convert args: Python->C return NULL; // NULL triggers exception - - Base::Type type = Base::Type::fromName(sType); - if (type == Base::Type::badType()) { - PyErr_Format(PyExc_Exception, "'%s' is not a valid type", sType); - return NULL; - } - - if (!type.isDerivedFrom(App::DocumentObject::getClassTypeId())) { - PyErr_Format(PyExc_Exception, "Type '%s' does not inherit from 'App::DocumentObject'", sType); - return NULL; - } - - std::vector res; - std::vector objs = getDocumentPtr()->getObjectsOfType(type); - - if (sName) { - try { - boost::regex rx(sName); - boost::cmatch what; - for (std::vector::const_iterator It = objs.begin();It != objs.end();++It) { - if (boost::regex_match((*It)->getNameInDocument(), what, rx)) - res.push_back(*It); - } - } - catch (const boost::regex_error& e) { - PyErr_SetString(PyExc_RuntimeError, e.what()); - return 0; - } - } - else { - res = objs; - } - - Py_ssize_t index=0; - PyObject* list = PyList_New((Py_ssize_t)res.size()); - for (std::vector::const_iterator It = res.begin();It != res.end();++It, index++) + + Base::Type type = Base::Type::fromName(sType); + if (type == Base::Type::badType()) { + PyErr_Format(PyExc_Exception, "'%s' is not a valid type", sType); + return NULL; + } + + if (!type.isDerivedFrom(App::DocumentObject::getClassTypeId())) { + PyErr_Format(PyExc_Exception, "Type '%s' does not inherit from 'App::DocumentObject'", sType); + return NULL; + } + + std::vector res; + + if (sName) { + try { + res = getDocumentPtr()->findObjects(type, sName); + } + catch (const boost::regex_error& e) { + PyErr_SetString(PyExc_RuntimeError, e.what()); + return 0; + } + } + else { + res = getDocumentPtr()->getObjectsOfType(type); + } + + Py_ssize_t index=0; + PyObject* list = PyList_New((Py_ssize_t)res.size()); + for (std::vector::const_iterator It = res.begin();It != res.end();++It, index++) PyList_SetItem(list, index, (*It)->getPyObject()); return list; }