Add method Document::findObject()

This commit is contained in:
wmayer 2012-05-23 16:57:36 +02:00
parent e499e33444
commit c1f8018a12
3 changed files with 46 additions and 36 deletions

View File

@ -63,6 +63,7 @@ recompute path. Also enables more complicated dependencies beyond trees.
#include <boost/graph/visitors.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/bind.hpp>
#include <boost/regex.hpp>
#include "Document.h"
@ -1689,6 +1690,20 @@ std::vector<DocumentObject*> Document::getObjectsOfType(const Base::Type& typeId
return Objects;
}
std::vector<DocumentObject*> Document::findObjects(const Base::Type& typeId, const char* objname) const
{
boost::regex rx(objname);
boost::cmatch what;
std::vector<DocumentObject*> Objects;
for (std::vector<DocumentObject*>::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;

View File

@ -161,6 +161,7 @@ public:
/// Returns a list of all Objects
std::vector<DocumentObject*> getObjects() const;
std::vector<DocumentObject*> getObjectsOfType(const Base::Type& typeId) const;
std::vector<DocumentObject*> findObjects(const Base::Type& typeId, const char* objname) const;
/// Returns an array with the correct types already.
template<typename T> inline std::vector<T*> getObjectsOfType() const;
int countObjectsOfType(const Base::Type& typeId) const;

View File

@ -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<DocumentObject*> res;
std::vector<DocumentObject*> objs = getDocumentPtr()->getObjectsOfType(type);
if (sName) {
try {
boost::regex rx(sName);
boost::cmatch what;
for (std::vector<DocumentObject*>::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<DocumentObject*>::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<DocumentObject*> 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<DocumentObject*>::const_iterator It = res.begin();It != res.end();++It, index++)
PyList_SetItem(list, index, (*It)->getPyObject());
return list;
}