Add method Document::findObject()
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user