| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2007 Jürgen Riegel <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <Base/GeometryPyCXX.h> |
| 25 |
#include <Base/MatrixPy.h> |
| 26 |
#include <Base/PlacementPy.h> |
| 27 |
#include <Base/PyWrapParseTupleAndKeywords.h> |
| 28 |
|
| 29 |
#include "DocumentObject.h" |
| 30 |
#include "Document.h" |
| 31 |
#include "ExpressionParser.h" |
| 32 |
#include "GeoFeature.h" |
| 33 |
#include "GeoFeatureGroupExtension.h" |
| 34 |
#include "GroupExtension.h" |
| 35 |
|
| 36 |
|
| 37 |
// inclusion of the generated files (generated out of DocumentObjectPy.xml) |
| 38 |
#include <App/DocumentObjectPy.h> |
| 39 |
#include <App/DocumentObjectPy.cpp> |
| 40 |
|
| 41 |
using namespace App; |
| 42 |
|
| 43 |
// returns a string which represent the object e.g. when printed in python |
| 44 |
std::string DocumentObjectPy::representation() const |
| 45 |
{ |
| 46 |
DocumentObject* object = this->getDocumentObjectPtr(); |
| 47 |
std::stringstream str; |
| 48 |
str << "<" << object->getTypeId().getName() << " object>"; |
| 49 |
return str.str(); |
| 50 |
} |
| 51 |
|
| 52 |
Py::Object DocumentObjectPy::getName() const |
| 53 |
{ |
| 54 |
DocumentObject* object = this->getDocumentObjectPtr(); |
| 55 |
const char* internal = object->getNameInDocument(); |
| 56 |
if (!internal) { |
| 57 |
return Py::None(); |
| 58 |
} |
| 59 |
return Py::String(internal); |
| 60 |
} |
| 61 |
|
| 62 |
Py::String DocumentObjectPy::getFullName() const |
| 63 |
{ |
| 64 |
return {getDocumentObjectPtr()->getFullName()}; |
| 65 |
} |
| 66 |
|
| 67 |
Py::Object DocumentObjectPy::getDocument() const |
| 68 |
{ |
| 69 |
DocumentObject* object = this->getDocumentObjectPtr(); |
| 70 |
Document* doc = object->getDocument(); |
| 71 |
if (!doc) { |
| 72 |
return Py::None(); |
| 73 |
} |
| 74 |
else { |
| 75 |
return Py::Object(doc->getPyObject(), true); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
PyObject* DocumentObjectPy::isAttachedToDocument(PyObject* args) const |
| 80 |
{ |
| 81 |
if (!PyArg_ParseTuple(args, "")) { |
| 82 |
return nullptr; |
| 83 |
} |
| 84 |
|
| 85 |
DocumentObject* object = this->getDocumentObjectPtr(); |
| 86 |
bool ok = object->isAttachedToDocument(); |
| 87 |
return Py::new_reference_to(Py::Boolean(ok)); |
| 88 |
} |
| 89 |
|
| 90 |
PyObject* DocumentObjectPy::addProperty(PyObject* args, PyObject* kwd) |
| 91 |
{ |
| 92 |
char *sType, *sName = nullptr, *sGroup = nullptr, *sDoc = nullptr; |
| 93 |
short attr = 0; |
| 94 |
PyObject *ro = Py_False, *hd = Py_False, *lk = Py_False; |
| 95 |
PyObject* enumVals = nullptr; |
| 96 |
const std::array<const char*, 10> kwlist {"type", |
| 97 |
"name", |
| 98 |
"group", |
| 99 |
"doc", |
| 100 |
"attr", |
| 101 |
"read_only", |
| 102 |
"hidden", |
| 103 |
"locked", |
| 104 |
"enum_vals", |
| 105 |
nullptr}; |
| 106 |
if (!Base::Wrapped_ParseTupleAndKeywords(args, |
| 107 |
kwd, |
| 108 |
"ss|sethO!O!O!O", |
| 109 |
kwlist, |
| 110 |
&sType, |
| 111 |
&sName, |
| 112 |
&sGroup, |
| 113 |
"utf-8", |
| 114 |
&sDoc, |
| 115 |
&attr, |
| 116 |
&PyBool_Type, |
| 117 |
&ro, |
| 118 |
&PyBool_Type, |
| 119 |
&hd, |
| 120 |
&PyBool_Type, |
| 121 |
&lk, |
| 122 |
&enumVals)) { |
| 123 |
return nullptr; |
| 124 |
} |
| 125 |
|
| 126 |
Property* prop = getDocumentObjectPtr()->addDynamicProperty(sType, |
| 127 |
sName, |
| 128 |
sGroup, |
| 129 |
sDoc, |
| 130 |
attr, |
| 131 |
Base::asBoolean(ro), |
| 132 |
Base::asBoolean(hd)); |
| 133 |
|
| 134 |
prop->setStatus(Property::LockDynamic, Base::asBoolean(lk)); |
| 135 |
|
| 136 |
// enum support |
| 137 |
auto* propEnum = dynamic_cast<App::PropertyEnumeration*>(prop); |
| 138 |
if (propEnum && enumVals) { |
| 139 |
propEnum->setPyObject(enumVals); |
| 140 |
} |
| 141 |
|
| 142 |
return Py::new_reference_to(this); |
| 143 |
} |
| 144 |
|
| 145 |
PyObject* DocumentObjectPy::removeProperty(PyObject* args) |
| 146 |
{ |
| 147 |
char* sName; |
| 148 |
if (!PyArg_ParseTuple(args, "s", &sName)) { |
| 149 |
return nullptr; |
| 150 |
} |
| 151 |
|
| 152 |
bool ok = getDocumentObjectPtr()->removeDynamicProperty(sName); |
| 153 |
return Py_BuildValue("O", (ok ? Py_True : Py_False)); |
| 154 |
} |
| 155 |
|
| 156 |
PyObject* DocumentObjectPy::supportedProperties(PyObject* args) |
| 157 |
{ |
| 158 |
if (!PyArg_ParseTuple(args, "")) { |
| 159 |
return nullptr; |
| 160 |
} |
| 161 |
|
| 162 |
std::vector<Base::Type> ary; |
| 163 |
Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary); |
| 164 |
Py::List res; |
| 165 |
for (auto& it : ary) { |
| 166 |
Base::BaseClass* data = static_cast<Base::BaseClass*>(it.createInstance()); |
| 167 |
if (data) { |
| 168 |
delete data; |
| 169 |
res.append(Py::String(it.getName())); |
| 170 |
} |
| 171 |
} |
| 172 |
return Py::new_reference_to(res); |
| 173 |
} |
| 174 |
|
| 175 |
PyObject* DocumentObjectPy::touch(PyObject* args) |
| 176 |
{ |
| 177 |
char* propName = nullptr; |
| 178 |
if (!PyArg_ParseTuple(args, "|s", &propName)) { |
| 179 |
return nullptr; |
| 180 |
} |
| 181 |
if (propName) { |
| 182 |
if (!propName[0]) { |
| 183 |
getDocumentObjectPtr()->touch(true); |
| 184 |
Py_Return; |
| 185 |
} |
| 186 |
auto prop = getDocumentObjectPtr()->getPropertyByName(propName); |
| 187 |
if (!prop) { |
| 188 |
throw Py::RuntimeError("Property not found"); |
| 189 |
} |
| 190 |
prop->touch(); |
| 191 |
Py_Return; |
| 192 |
} |
| 193 |
|
| 194 |
getDocumentObjectPtr()->touch(); |
| 195 |
Py_Return; |
| 196 |
} |
| 197 |
|
| 198 |
PyObject* DocumentObjectPy::purgeTouched(PyObject* args) |
| 199 |
{ |
| 200 |
if (!PyArg_ParseTuple(args, "")) { |
| 201 |
return nullptr; |
| 202 |
} |
| 203 |
getDocumentObjectPtr()->purgeTouched(); |
| 204 |
Py_Return; |
| 205 |
} |
| 206 |
|
| 207 |
PyObject* DocumentObjectPy::enforceRecompute(PyObject* args) |
| 208 |
{ |
| 209 |
if (!PyArg_ParseTuple(args, "")) { |
| 210 |
return nullptr; |
| 211 |
} |
| 212 |
getDocumentObjectPtr()->enforceRecompute(); |
| 213 |
Py_Return; |
| 214 |
} |
| 215 |
|
| 216 |
Py::List DocumentObjectPy::getState() const |
| 217 |
{ |
| 218 |
DocumentObject* object = this->getDocumentObjectPtr(); |
| 219 |
Py::List list; |
| 220 |
bool uptodate = true; |
| 221 |
if (object->isTouched()) { |
| 222 |
uptodate = false; |
| 223 |
list.append(Py::String("Touched")); |
| 224 |
} |
| 225 |
if (object->isError()) { |
| 226 |
uptodate = false; |
| 227 |
list.append(Py::String("Invalid")); |
| 228 |
} |
| 229 |
if (object->isRecomputing()) { |
| 230 |
uptodate = false; |
| 231 |
list.append(Py::String("Recompute")); |
| 232 |
} |
| 233 |
if (object->testStatus(App::Recompute2)) { |
| 234 |
list.append(Py::String("Recompute2")); |
| 235 |
} |
| 236 |
if (object->isRestoring()) { |
| 237 |
uptodate = false; |
| 238 |
list.append(Py::String("Restore")); |
| 239 |
} |
| 240 |
if (object->testStatus(App::Expand)) { |
| 241 |
list.append(Py::String("Expanded")); |
| 242 |
} |
| 243 |
if (object->testStatus(App::PartialObject)) { |
| 244 |
list.append(Py::String("Partial")); |
| 245 |
} |
| 246 |
if (object->testStatus(App::ObjImporting)) { |
| 247 |
list.append(Py::String("Importing")); |
| 248 |
} |
| 249 |
if (uptodate) { |
| 250 |
list.append(Py::String("Up-to-date")); |
| 251 |
} |
| 252 |
return list; |
| 253 |
} |
| 254 |
|
| 255 |
Py::Object DocumentObjectPy::getViewObject() const |
| 256 |
{ |
| 257 |
try { |
| 258 |
PyObject* dict = PySys_GetObject("modules"); |
| 259 |
if (!dict) { |
| 260 |
return Py::None(); |
| 261 |
} |
| 262 |
|
| 263 |
// check if the FreeCADGui module is already loaded, if not then don't try to load it |
| 264 |
Py::Dict sysmod(dict); |
| 265 |
if (!sysmod.hasKey("FreeCADGui")) { |
| 266 |
return Py::None(); |
| 267 |
} |
| 268 |
|
| 269 |
// double-check that the module doesn't have a null pointer |
| 270 |
Py::Module module(PyImport_ImportModule("FreeCADGui"), true); |
| 271 |
if (module.isNull() || !module.hasAttr("getDocument")) { |
| 272 |
// in v0.14+, the GUI module can be loaded in console mode (but doesn't have all its |
| 273 |
// document methods) |
| 274 |
return Py::None(); |
| 275 |
} |
| 276 |
if (!getDocumentObjectPtr()->getDocument()) { |
| 277 |
throw Py::RuntimeError("Object has no document"); |
| 278 |
} |
| 279 |
const char* internalName = getDocumentObjectPtr()->getNameInDocument(); |
| 280 |
if (!internalName) { |
| 281 |
throw Py::RuntimeError("Object has been removed from document"); |
| 282 |
} |
| 283 |
|
| 284 |
Py::Callable method(module.getAttr("getDocument")); |
| 285 |
Py::Tuple arg(1); |
| 286 |
arg.setItem(0, Py::String(getDocumentObjectPtr()->getDocument()->getName())); |
| 287 |
Py::Object doc = method.apply(arg); |
| 288 |
method = doc.getAttr("getObject"); |
| 289 |
arg.setItem(0, Py::String(internalName)); |
| 290 |
Py::Object obj = method.apply(arg); |
| 291 |
return obj; |
| 292 |
} |
| 293 |
catch (Py::Exception& e) { |
| 294 |
if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 295 |
// the GUI is not up, hence None is returned |
| 296 |
e.clear(); |
| 297 |
return Py::None(); |
| 298 |
} |
| 299 |
// FreeCADGui is loaded, so there must be wrong something else |
| 300 |
throw; // re-throw |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
Py::List DocumentObjectPy::getInList() const |
| 305 |
{ |
| 306 |
Py::List ret; |
| 307 |
std::vector<DocumentObject*> list = getDocumentObjectPtr()->getInList(); |
| 308 |
|
| 309 |
for (auto It : list) { |
| 310 |
ret.append(Py::Object(It->getPyObject(), true)); |
| 311 |
} |
| 312 |
|
| 313 |
return ret; |
| 314 |
} |
| 315 |
|
| 316 |
Py::List DocumentObjectPy::getInListRecursive() const |
| 317 |
{ |
| 318 |
Py::List ret; |
| 319 |
try { |
| 320 |
std::vector<DocumentObject*> list = getDocumentObjectPtr()->getInListRecursive(); |
| 321 |
|
| 322 |
for (auto It : list) { |
| 323 |
ret.append(Py::Object(It->getPyObject(), true)); |
| 324 |
} |
| 325 |
} |
| 326 |
catch (const Base::Exception& e) { |
| 327 |
throw Py::IndexError(e.what()); |
| 328 |
} |
| 329 |
return ret; |
| 330 |
} |
| 331 |
|
| 332 |
Py::List DocumentObjectPy::getOutList() const |
| 333 |
{ |
| 334 |
Py::List ret; |
| 335 |
std::vector<DocumentObject*> list = getDocumentObjectPtr()->getOutList(); |
| 336 |
|
| 337 |
for (auto It : list) { |
| 338 |
ret.append(Py::Object(It->getPyObject(), true)); |
| 339 |
} |
| 340 |
|
| 341 |
return ret; |
| 342 |
} |
| 343 |
|
| 344 |
Py::List DocumentObjectPy::getOutListRecursive() const |
| 345 |
{ |
| 346 |
Py::List ret; |
| 347 |
try { |
| 348 |
std::vector<DocumentObject*> list = getDocumentObjectPtr()->getOutListRecursive(); |
| 349 |
|
| 350 |
// create the python list for the output |
| 351 |
for (auto It : list) { |
| 352 |
ret.append(Py::Object(It->getPyObject(), true)); |
| 353 |
} |
| 354 |
} |
| 355 |
catch (const Base::Exception& e) { |
| 356 |
throw Py::IndexError(e.what()); |
| 357 |
} |
| 358 |
|
| 359 |
return ret; |
| 360 |
} |
| 361 |
|
| 362 |
PyObject* DocumentObjectPy::setExpression(PyObject* args) |
| 363 |
{ |
| 364 |
char* path = nullptr; |
| 365 |
PyObject* expr; |
| 366 |
char* comment = nullptr; |
| 367 |
|
| 368 |
if (!PyArg_ParseTuple(args, "sO|s", &path, &expr, &comment)) { |
| 369 |
return nullptr; |
| 370 |
} |
| 371 |
|
| 372 |
App::ObjectIdentifier p(ObjectIdentifier::parse(getDocumentObjectPtr(), path)); |
| 373 |
|
| 374 |
if (Py::Object(expr).isNone()) { |
| 375 |
getDocumentObjectPtr()->clearExpression(p); |
| 376 |
Py_Return; |
| 377 |
} |
| 378 |
else if (PyUnicode_Check(expr)) { |
| 379 |
const char* exprStr = PyUnicode_AsUTF8(expr); |
| 380 |
std::shared_ptr<Expression> shared_expr(Expression::parse(getDocumentObjectPtr(), exprStr)); |
| 381 |
if (shared_expr && comment) { |
| 382 |
shared_expr->comment = comment; |
| 383 |
} |
| 384 |
|
| 385 |
getDocumentObjectPtr()->setExpression(p, std::move(shared_expr)); |
| 386 |
Py_Return; |
| 387 |
} |
| 388 |
|
| 389 |
throw Py::TypeError("String or None expected."); |
| 390 |
} |
| 391 |
|
| 392 |
PyObject* DocumentObjectPy::clearExpression(PyObject* args) |
| 393 |
{ |
| 394 |
char* path = nullptr; |
| 395 |
if (!PyArg_ParseTuple(args, "s", &path)) { |
| 396 |
return nullptr; |
| 397 |
} |
| 398 |
|
| 399 |
App::ObjectIdentifier p(ObjectIdentifier::parse(getDocumentObjectPtr(), path)); |
| 400 |
getDocumentObjectPtr()->clearExpression(p); |
| 401 |
Py_Return; |
| 402 |
} |
| 403 |
|
| 404 |
PyObject* DocumentObjectPy::evalExpression(PyObject* self, PyObject* args) |
| 405 |
{ |
| 406 |
const char* expr; |
| 407 |
if (!PyArg_ParseTuple(args, "s", &expr)) { |
| 408 |
return nullptr; |
| 409 |
} |
| 410 |
|
| 411 |
// HINT: |
| 412 |
// The standard behaviour of Python for class methods is to always pass the class |
| 413 |
// object as first argument. |
| 414 |
// For FreeCAD-specific types the behaviour is a bit different: |
| 415 |
// When calling this method for an instance then this is passed as first argument |
| 416 |
// and otherwise the class object is passed. |
| 417 |
// This behaviour is achieved by the function _getattr() that passed 'this' to |
| 418 |
// PyCFunction_New(). |
| 419 |
// |
| 420 |
// evalExpression() is a class method and thus 'self' can either be an instance of |
| 421 |
// DocumentObjectPy or a type object. |
| 422 |
App::DocumentObject* obj = nullptr; |
| 423 |
if (self && PyObject_TypeCheck(self, &DocumentObjectPy::Type)) { |
| 424 |
obj = static_cast<DocumentObjectPy*>(self)->getDocumentObjectPtr(); |
| 425 |
} |
| 426 |
|
| 427 |
PY_TRY |
| 428 |
{ |
| 429 |
std::shared_ptr<Expression> shared_expr(Expression::parse(obj, expr)); |
| 430 |
if (shared_expr) { |
| 431 |
return Py::new_reference_to(shared_expr->getPyValue()); |
| 432 |
} |
| 433 |
Py_Return; |
| 434 |
} |
| 435 |
PY_CATCH |
| 436 |
} |
| 437 |
|
| 438 |
PyObject* DocumentObjectPy::recompute(PyObject* args) |
| 439 |
{ |
| 440 |
PyObject* recursive = Py_False; |
| 441 |
if (!PyArg_ParseTuple(args, "|O!", &PyBool_Type, &recursive)) { |
| 442 |
return nullptr; |
| 443 |
} |
| 444 |
|
| 445 |
try { |
| 446 |
bool ok = getDocumentObjectPtr()->recomputeFeature(Base::asBoolean(recursive)); |
| 447 |
return Py_BuildValue("O", (ok ? Py_True : Py_False)); |
| 448 |
} |
| 449 |
catch (const Base::Exception& e) { |
| 450 |
throw Py::RuntimeError(e.what()); |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
PyObject* DocumentObjectPy::isValid(PyObject* args) const |
| 455 |
{ |
| 456 |
if (!PyArg_ParseTuple(args, "")) { |
| 457 |
return nullptr; |
| 458 |
} |
| 459 |
|
| 460 |
try { |
| 461 |
bool ok = getDocumentObjectPtr()->isValid(); |
| 462 |
return Py_BuildValue("O", (ok ? Py_True : Py_False)); |
| 463 |
} |
| 464 |
catch (const Base::Exception& e) { |
| 465 |
throw Py::RuntimeError(e.what()); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
PyObject* DocumentObjectPy::getStatusString(PyObject* args) const |
| 470 |
{ |
| 471 |
if (!PyArg_ParseTuple(args, "")) { |
| 472 |
return nullptr; |
| 473 |
} |
| 474 |
|
| 475 |
try { |
| 476 |
Py::String text(getDocumentObjectPtr()->getStatusString()); |
| 477 |
return Py::new_reference_to(text); |
| 478 |
} |
| 479 |
catch (const Base::Exception& e) { |
| 480 |
throw Py::RuntimeError(e.what()); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
PyObject* DocumentObjectPy::getSubObject(PyObject* args, PyObject* keywds) |
| 485 |
{ |
| 486 |
enum class ReturnType |
| 487 |
{ |
| 488 |
PyObject = 0, |
| 489 |
DocObject = 1, |
| 490 |
DocAndPyObject = 2, |
| 491 |
Placement = 3, |
| 492 |
Matrix = 4, |
| 493 |
LinkAndPlacement = 5, |
| 494 |
LinkAndMatrix = 6 |
| 495 |
}; |
| 496 |
|
| 497 |
PyObject* obj; |
| 498 |
short retType = 0; |
| 499 |
PyObject* pyMat = nullptr; |
| 500 |
PyObject* doTransform = Py_True; |
| 501 |
short depth = 0; |
| 502 |
|
| 503 |
static const std::array<const char*, 6> kwlist {"subname", |
| 504 |
"retType", |
| 505 |
"matrix", |
| 506 |
"transform", |
| 507 |
"depth", |
| 508 |
nullptr}; |
| 509 |
if (!Base::Wrapped_ParseTupleAndKeywords(args, |
| 510 |
keywds, |
| 511 |
"O|hO!O!h", |
| 512 |
kwlist, |
| 513 |
&obj, |
| 514 |
&retType, |
| 515 |
&Base::MatrixPy::Type, |
| 516 |
&pyMat, |
| 517 |
&PyBool_Type, |
| 518 |
&doTransform, |
| 519 |
&depth)) { |
| 520 |
return nullptr; |
| 521 |
} |
| 522 |
|
| 523 |
if (retType < 0 || static_cast<size_t>(retType) > kwlist.size()) { |
| 524 |
PyErr_SetString(PyExc_ValueError, "invalid retType, can only be integer 0~6"); |
| 525 |
return nullptr; |
| 526 |
} |
| 527 |
|
| 528 |
ReturnType retEnum = ReturnType(retType); |
| 529 |
std::vector<std::string> subs; |
| 530 |
bool single = true; |
| 531 |
|
| 532 |
if (PyUnicode_Check(obj)) { |
| 533 |
subs.emplace_back(PyUnicode_AsUTF8(obj)); |
| 534 |
} |
| 535 |
else if (PySequence_Check(obj)) { |
| 536 |
single = false; |
| 537 |
Py::Sequence shapeSeq(obj); |
| 538 |
for (Py::Sequence::iterator it = shapeSeq.begin(); it != shapeSeq.end(); ++it) { |
| 539 |
PyObject* item = (*it).ptr(); |
| 540 |
if (PyUnicode_Check(item)) { |
| 541 |
subs.emplace_back(PyUnicode_AsUTF8(item)); |
| 542 |
} |
| 543 |
else { |
| 544 |
PyErr_SetString(PyExc_TypeError, "non-string object in sequence"); |
| 545 |
return nullptr; |
| 546 |
} |
| 547 |
} |
| 548 |
} |
| 549 |
else { |
| 550 |
PyErr_SetString(PyExc_TypeError, "subname must be either a string or sequence of string"); |
| 551 |
return nullptr; |
| 552 |
} |
| 553 |
|
| 554 |
bool transform = Base::asBoolean(doTransform); |
| 555 |
|
| 556 |
struct SubInfo |
| 557 |
{ |
| 558 |
App::DocumentObject* sobj {nullptr}; |
| 559 |
Py::Object obj; |
| 560 |
Py::Object pyObj; |
| 561 |
Base::Matrix4D mat; |
| 562 |
explicit SubInfo(const Base::Matrix4D& mat) |
| 563 |
: mat(mat) |
| 564 |
{} |
| 565 |
}; |
| 566 |
|
| 567 |
Base::Matrix4D mat; |
| 568 |
if (pyMat) { |
| 569 |
mat = *static_cast<Base::MatrixPy*>(pyMat)->getMatrixPtr(); |
| 570 |
} |
| 571 |
|
| 572 |
PY_TRY |
| 573 |
{ |
| 574 |
std::vector<SubInfo> ret; |
| 575 |
for (const auto& sub : subs) { |
| 576 |
ret.emplace_back(mat); |
| 577 |
auto& info = ret.back(); |
| 578 |
PyObject* pyObj = nullptr; |
| 579 |
|
| 580 |
info.sobj = getDocumentObjectPtr()->getSubObject( |
| 581 |
sub.c_str(), |
| 582 |
retEnum != ReturnType::PyObject && retEnum != ReturnType::DocAndPyObject ? nullptr |
| 583 |
: &pyObj, |
| 584 |
&info.mat, |
| 585 |
transform, |
| 586 |
depth); |
| 587 |
if (pyObj) { |
| 588 |
info.pyObj = Py::asObject(pyObj); |
| 589 |
} |
| 590 |
if (info.sobj) { |
| 591 |
info.obj = Py::asObject(info.sobj->getPyObject()); |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
if (ret.empty()) { |
| 596 |
Py_Return; |
| 597 |
} |
| 598 |
|
| 599 |
auto getReturnValue = [retEnum, pyMat](SubInfo& ret) -> Py::Object { |
| 600 |
if (retEnum == ReturnType::PyObject) { |
| 601 |
return ret.pyObj; |
| 602 |
} |
| 603 |
else if (retEnum == ReturnType::DocObject && !pyMat) { |
| 604 |
return ret.obj; |
| 605 |
} |
| 606 |
else if (!ret.sobj) { |
| 607 |
return Py::None(); |
| 608 |
} |
| 609 |
else if (retEnum == ReturnType::Placement) { |
| 610 |
return Py::Placement(Base::Placement(ret.mat)); |
| 611 |
} |
| 612 |
else if (retEnum == ReturnType::Matrix) { |
| 613 |
return Py::Matrix(ret.mat); |
| 614 |
} |
| 615 |
else if (retEnum == ReturnType::LinkAndPlacement |
| 616 |
|| retEnum == ReturnType::LinkAndMatrix) { |
| 617 |
ret.sobj->getLinkedObject(true, &ret.mat, false); |
| 618 |
if (retEnum == ReturnType::LinkAndPlacement) { |
| 619 |
return Py::Placement(Base::Placement(ret.mat)); |
| 620 |
} |
| 621 |
else { |
| 622 |
return Py::Matrix(ret.mat); |
| 623 |
} |
| 624 |
} |
| 625 |
else { |
| 626 |
Py::Tuple rret(retEnum == ReturnType::DocObject ? 2 : 3); |
| 627 |
rret.setItem(0, ret.obj); |
| 628 |
rret.setItem(1, Py::asObject(new Base::MatrixPy(ret.mat))); |
| 629 |
if (retEnum != ReturnType::DocObject) { |
| 630 |
rret.setItem(2, ret.pyObj); |
| 631 |
} |
| 632 |
return rret; |
| 633 |
} |
| 634 |
}; |
| 635 |
|
| 636 |
if (single) { |
| 637 |
return Py::new_reference_to(getReturnValue(ret[0])); |
| 638 |
} |
| 639 |
|
| 640 |
Py::Tuple tuple(ret.size()); |
| 641 |
for (size_t i = 0; i < ret.size(); ++i) { |
| 642 |
tuple.setItem(i, getReturnValue(ret[i])); |
| 643 |
} |
| 644 |
return Py::new_reference_to(tuple); |
| 645 |
} |
| 646 |
PY_CATCH |
| 647 |
} |
| 648 |
|
| 649 |
PyObject* DocumentObjectPy::getSubObjectList(PyObject* args) |
| 650 |
{ |
| 651 |
const char* subname; |
| 652 |
if (!PyArg_ParseTuple(args, "s", &subname)) { |
| 653 |
return nullptr; |
| 654 |
} |
| 655 |
Py::List res; |
| 656 |
PY_TRY |
| 657 |
{ |
| 658 |
for (auto o : getDocumentObjectPtr()->getSubObjectList(subname)) { |
| 659 |
res.append(Py::asObject(o->getPyObject())); |
| 660 |
} |
| 661 |
return Py::new_reference_to(res); |
| 662 |
} |
| 663 |
PY_CATCH |
| 664 |
} |
| 665 |
|
| 666 |
PyObject* DocumentObjectPy::getSubObjects(PyObject* args) |
| 667 |
{ |
| 668 |
int reason = 0; |
| 669 |
if (!PyArg_ParseTuple(args, "|i", &reason)) { |
| 670 |
return nullptr; |
| 671 |
} |
| 672 |
|
| 673 |
PY_TRY |
| 674 |
{ |
| 675 |
auto names = getDocumentObjectPtr()->getSubObjects(reason); |
| 676 |
Py::Tuple pyObjs(names.size()); |
| 677 |
for (size_t i = 0; i < names.size(); ++i) { |
| 678 |
pyObjs.setItem(i, Py::String(names[i])); |
| 679 |
} |
| 680 |
return Py::new_reference_to(pyObjs); |
| 681 |
} |
| 682 |
PY_CATCH; |
| 683 |
} |
| 684 |
|
| 685 |
PyObject* DocumentObjectPy::getLinkedObject(PyObject* args, PyObject* keywds) |
| 686 |
{ |
| 687 |
PyObject* recursive = Py_True; |
| 688 |
PyObject* pyMat = Py_None; |
| 689 |
PyObject* transform = Py_True; |
| 690 |
short depth = 0; |
| 691 |
static const std::array<const char*, 5> kwlist {"recursive", |
| 692 |
"matrix", |
| 693 |
"transform", |
| 694 |
"depth", |
| 695 |
nullptr}; |
| 696 |
if (!Base::Wrapped_ParseTupleAndKeywords(args, |
| 697 |
keywds, |
| 698 |
"|O!OO!h", |
| 699 |
kwlist, |
| 700 |
&PyBool_Type, |
| 701 |
&recursive, |
| 702 |
&pyMat, |
| 703 |
&PyBool_Type, |
| 704 |
&transform, |
| 705 |
&depth)) { |
| 706 |
return nullptr; |
| 707 |
} |
| 708 |
|
| 709 |
PY_TRY |
| 710 |
{ |
| 711 |
Base::PyTypeCheck(&pyMat, |
| 712 |
&Base::MatrixPy::Type, |
| 713 |
"expect argument 'matrix' to be of type Base.Matrix"); |
| 714 |
Base::Matrix4D _mat; |
| 715 |
Base::Matrix4D* mat = nullptr; |
| 716 |
if (pyMat) { |
| 717 |
_mat = *static_cast<Base::MatrixPy*>(pyMat)->getMatrixPtr(); |
| 718 |
mat = &_mat; |
| 719 |
} |
| 720 |
|
| 721 |
auto linked = getDocumentObjectPtr()->getLinkedObject(Base::asBoolean(recursive), |
| 722 |
mat, |
| 723 |
Base::asBoolean(transform), |
| 724 |
depth); |
| 725 |
if (!linked) { |
| 726 |
linked = getDocumentObjectPtr(); |
| 727 |
} |
| 728 |
auto pyObj = Py::Object(linked->getPyObject(), true); |
| 729 |
if (mat) { |
| 730 |
Py::Tuple ret(2); |
| 731 |
ret.setItem(0, pyObj); |
| 732 |
ret.setItem(1, Py::asObject(new Base::MatrixPy(*mat))); |
| 733 |
return Py::new_reference_to(ret); |
| 734 |
} |
| 735 |
|
| 736 |
return Py::new_reference_to(pyObj); |
| 737 |
} |
| 738 |
PY_CATCH; |
| 739 |
} |
| 740 |
|
| 741 |
PyObject* DocumentObjectPy::isElementVisible(PyObject* args) |
| 742 |
{ |
| 743 |
char* element = nullptr; |
| 744 |
if (!PyArg_ParseTuple(args, "s", &element)) { |
| 745 |
return nullptr; |
| 746 |
} |
| 747 |
PY_TRY |
| 748 |
{ |
| 749 |
return Py_BuildValue("h", getDocumentObjectPtr()->isElementVisible(element)); |
| 750 |
} |
| 751 |
PY_CATCH; |
| 752 |
} |
| 753 |
|
| 754 |
PyObject* DocumentObjectPy::setElementVisible(PyObject* args) |
| 755 |
{ |
| 756 |
char* element = nullptr; |
| 757 |
PyObject* visible = Py_True; |
| 758 |
if (!PyArg_ParseTuple(args, "s|O!", &element, &PyBool_Type, &visible)) { |
| 759 |
return nullptr; |
| 760 |
} |
| 761 |
PY_TRY |
| 762 |
{ |
| 763 |
return Py_BuildValue( |
| 764 |
"h", |
| 765 |
getDocumentObjectPtr()->setElementVisible(element, Base::asBoolean(visible))); |
| 766 |
} |
| 767 |
PY_CATCH; |
| 768 |
} |
| 769 |
|
| 770 |
PyObject* DocumentObjectPy::hasChildElement(PyObject* args) |
| 771 |
{ |
| 772 |
if (!PyArg_ParseTuple(args, "")) { |
| 773 |
return nullptr; |
| 774 |
} |
| 775 |
PY_TRY |
| 776 |
{ |
| 777 |
return Py_BuildValue("O", getDocumentObjectPtr()->hasChildElement() ? Py_True : Py_False); |
| 778 |
} |
| 779 |
PY_CATCH; |
| 780 |
} |
| 781 |
|
| 782 |
PyObject* DocumentObjectPy::getParentGroup(PyObject* args) |
| 783 |
{ |
| 784 |
if (!PyArg_ParseTuple(args, "")) { |
| 785 |
return nullptr; |
| 786 |
} |
| 787 |
|
| 788 |
try { |
| 789 |
auto grp = GroupExtension::getGroupOfObject(getDocumentObjectPtr()); |
| 790 |
if (!grp) { |
| 791 |
Py_INCREF(Py_None); |
| 792 |
return Py_None; |
| 793 |
} |
| 794 |
return grp->getPyObject(); |
| 795 |
} |
| 796 |
catch (const Base::Exception& e) { |
| 797 |
throw Py::RuntimeError(e.what()); |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
PyObject* DocumentObjectPy::getParentGeoFeatureGroup(PyObject* args) |
| 802 |
{ |
| 803 |
if (!PyArg_ParseTuple(args, "")) { |
| 804 |
return nullptr; |
| 805 |
} |
| 806 |
|
| 807 |
try { |
| 808 |
auto grp = GeoFeatureGroupExtension::getGroupOfObject(getDocumentObjectPtr()); |
| 809 |
if (!grp) { |
| 810 |
Py_INCREF(Py_None); |
| 811 |
return Py_None; |
| 812 |
} |
| 813 |
return grp->getPyObject(); |
| 814 |
} |
| 815 |
catch (const Base::Exception& e) { |
| 816 |
throw Py::RuntimeError(e.what()); |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
PyObject* DocumentObjectPy::getParent(PyObject* args) |
| 821 |
{ |
| 822 |
if (!PyArg_ParseTuple(args, "")) { |
| 823 |
return nullptr; |
| 824 |
} |
| 825 |
|
| 826 |
try { |
| 827 |
auto grp = getDocumentObjectPtr()->getFirstParent(); |
| 828 |
if (!grp) { |
| 829 |
Py_INCREF(Py_None); |
| 830 |
return Py_None; |
| 831 |
} |
| 832 |
return grp->getPyObject(); |
| 833 |
} |
| 834 |
catch (const Base::Exception& e) { |
| 835 |
throw Py::RuntimeError(e.what()); |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
Py::Boolean DocumentObjectPy::getMustExecute() const |
| 840 |
{ |
| 841 |
try { |
| 842 |
return {getDocumentObjectPtr()->mustExecute() ? true : false}; |
| 843 |
} |
| 844 |
catch (const Base::Exception& e) { |
| 845 |
throw Py::RuntimeError(e.what()); |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
PyObject* DocumentObjectPy::getPathsByOutList(PyObject* args) |
| 850 |
{ |
| 851 |
PyObject* o; |
| 852 |
if (!PyArg_ParseTuple(args, "O!", &DocumentObjectPy::Type, &o)) { |
| 853 |
return nullptr; |
| 854 |
} |
| 855 |
|
| 856 |
try { |
| 857 |
DocumentObject* target = static_cast<DocumentObjectPy*>(o)->getDocumentObjectPtr(); |
| 858 |
auto array = getDocumentObjectPtr()->getPathsByOutList(target); |
| 859 |
Py::List list; |
| 860 |
for (const auto& it : array) { |
| 861 |
Py::List path; |
| 862 |
for (auto jt : it) { |
| 863 |
path.append(Py::asObject(jt->getPyObject())); |
| 864 |
} |
| 865 |
list.append(path); |
| 866 |
} |
| 867 |
return Py::new_reference_to(list); |
| 868 |
} |
| 869 |
catch (const Base::Exception& e) { |
| 870 |
throw Py::RuntimeError(e.what()); |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
PyObject* DocumentObjectPy::getElementMapVersion(PyObject* args) const |
| 875 |
{ |
| 876 |
const char* name; |
| 877 |
PyObject* restored = Py_False; |
| 878 |
if (!PyArg_ParseTuple(args, "s|O", &name, &restored)) { |
| 879 |
return NULL; |
| 880 |
} |
| 881 |
|
| 882 |
Property* prop = getDocumentObjectPtr()->getPropertyByName(name); |
| 883 |
if (!prop) { |
| 884 |
throw Py::ValueError("property not found"); |
| 885 |
} |
| 886 |
return Py::new_reference_to( |
| 887 |
Py::String(getDocumentObjectPtr()->getElementMapVersion(prop, Base::asBoolean(restored)))); |
| 888 |
} |
| 889 |
|
| 890 |
PyObject* DocumentObjectPy::getCustomAttributes(const char*) const |
| 891 |
{ |
| 892 |
return nullptr; |
| 893 |
} |
| 894 |
// remove |
| 895 |
int DocumentObjectPy::setCustomAttributes(const char*, PyObject*) |
| 896 |
{ |
| 897 |
return 0; |
| 898 |
} |
| 899 |
|
| 900 |
Py::Long DocumentObjectPy::getID() const |
| 901 |
{ |
| 902 |
return Py::Long(getDocumentObjectPtr()->getID()); |
| 903 |
} |
| 904 |
|
| 905 |
Py::Boolean DocumentObjectPy::getRemoving() const |
| 906 |
{ |
| 907 |
return {getDocumentObjectPtr()->testStatus(ObjectStatus::Remove)}; |
| 908 |
} |
| 909 |
|
| 910 |
PyObject* DocumentObjectPy::resolve(PyObject* args) const |
| 911 |
{ |
| 912 |
const char* subname; |
| 913 |
if (!PyArg_ParseTuple(args, "s", &subname)) { |
| 914 |
return nullptr; |
| 915 |
} |
| 916 |
|
| 917 |
PY_TRY |
| 918 |
{ |
| 919 |
std::string elementName; |
| 920 |
const char* subElement = nullptr; |
| 921 |
App::DocumentObject* parent = nullptr; |
| 922 |
auto obj = getDocumentObjectPtr()->resolve(subname, &parent, &elementName, &subElement); |
| 923 |
|
| 924 |
Py::Tuple ret(4); |
| 925 |
ret.setItem(0, obj ? Py::Object(obj->getPyObject(), true) : Py::None()); |
| 926 |
ret.setItem(1, parent ? Py::Object(parent->getPyObject(), true) : Py::None()); |
| 927 |
ret.setItem(2, Py::String(elementName.c_str())); |
| 928 |
ret.setItem(3, Py::String(subElement ? subElement : "")); |
| 929 |
return Py::new_reference_to(ret); |
| 930 |
} |
| 931 |
PY_CATCH; |
| 932 |
|
| 933 |
Py_Return; |
| 934 |
} |
| 935 |
|
| 936 |
PyObject* DocumentObjectPy::resolveSubElement(PyObject* args) const |
| 937 |
{ |
| 938 |
const char* subname; |
| 939 |
PyObject* append = Py_False; |
| 940 |
int type = 0; |
| 941 |
if (!PyArg_ParseTuple(args, "s|O!i", &subname, &PyBool_Type, &append, &type)) { |
| 942 |
return nullptr; |
| 943 |
} |
| 944 |
|
| 945 |
PY_TRY |
| 946 |
{ |
| 947 |
ElementNamePair elementName; |
| 948 |
auto obj = GeoFeature::resolveElement(getDocumentObjectPtr(), |
| 949 |
subname, |
| 950 |
elementName, |
| 951 |
Base::asBoolean(append), |
| 952 |
static_cast<GeoFeature::ElementNameType>(type)); |
| 953 |
Py::Tuple ret(3); |
| 954 |
ret.setItem(0, obj ? Py::Object(obj->getPyObject(), true) : Py::None()); |
| 955 |
ret.setItem(1, Py::String(elementName.newName)); |
| 956 |
ret.setItem(2, Py::String(elementName.oldName)); |
| 957 |
return Py::new_reference_to(ret); |
| 958 |
} |
| 959 |
PY_CATCH; |
| 960 |
|
| 961 |
Py_Return; |
| 962 |
} |
| 963 |
|
| 964 |
Py::List DocumentObjectPy::getParents() const |
| 965 |
{ |
| 966 |
Py::List ret; |
| 967 |
for (auto& v : getDocumentObjectPtr()->getParents()) { |
| 968 |
ret.append(Py::TupleN(Py::Object(v.first->getPyObject(), true), Py::String(v.second))); |
| 969 |
} |
| 970 |
return ret; |
| 971 |
} |
| 972 |
|
| 973 |
PyObject* DocumentObjectPy::adjustRelativeLinks(PyObject* args) |
| 974 |
{ |
| 975 |
PyObject* pyobj; |
| 976 |
PyObject* recursive = Py_True; |
| 977 |
if (!PyArg_ParseTuple(args, "O!|O", &DocumentObjectPy::Type, &pyobj, &recursive)) { |
| 978 |
return nullptr; |
| 979 |
} |
| 980 |
PY_TRY |
| 981 |
{ |
| 982 |
auto obj = static_cast<DocumentObjectPy*>(pyobj)->getDocumentObjectPtr(); |
| 983 |
auto inList = obj->getInListEx(true); |
| 984 |
inList.insert(obj); |
| 985 |
std::set<App::DocumentObject*> visited; |
| 986 |
return Py::new_reference_to(Py::Boolean(getDocumentObjectPtr()->adjustRelativeLinks( |
| 987 |
inList, |
| 988 |
Base::asBoolean(recursive) ? &visited : nullptr))); |
| 989 |
} |
| 990 |
PY_CATCH |
| 991 |
} |
| 992 |
|
| 993 |
Py::String DocumentObjectPy::getOldLabel() const |
| 994 |
{ |
| 995 |
return {getDocumentObjectPtr()->getOldLabel()}; |
| 996 |
} |
| 997 |
|
| 998 |
Py::Boolean DocumentObjectPy::getNoTouch() const |
| 999 |
{ |
| 1000 |
return {getDocumentObjectPtr()->testStatus(ObjectStatus::NoTouch)}; |
| 1001 |
} |
| 1002 |
|
| 1003 |
void DocumentObjectPy::setNoTouch(Py::Boolean value) |
| 1004 |
{ |
| 1005 |
getDocumentObjectPtr()->setStatus(ObjectStatus::NoTouch, value.isTrue()); |
| 1006 |
} |
| 1007 |
|
| 1008 |
PyObject* DocumentObjectPy::getPlacementOf(PyObject* args) |
| 1009 |
{ |
| 1010 |
char* subname; |
| 1011 |
PyObject* target = Py_None; // Initialize to None |
| 1012 |
|
| 1013 |
if (!PyArg_ParseTuple(args, "s|O", &subname, &target)) { |
| 1014 |
return nullptr; |
| 1015 |
} |
| 1016 |
|
| 1017 |
App::DocumentObject* targetObj = nullptr; |
| 1018 |
|
| 1019 |
// Check if a target was provided and is not None |
| 1020 |
if (target && target != Py_None) { |
| 1021 |
// Now perform the type check manually |
| 1022 |
if (!PyObject_TypeCheck(target, &DocumentObjectPy::Type)) { |
| 1023 |
PyErr_SetString(PyExc_TypeError, "Target argument must be a DocumentObject or None"); |
| 1024 |
return nullptr; |
| 1025 |
} |
| 1026 |
targetObj = static_cast<DocumentObjectPy*>(target)->getDocumentObjectPtr(); |
| 1027 |
} |
| 1028 |
|
| 1029 |
PY_TRY |
| 1030 |
{ |
| 1031 |
Base::Placement p = getDocumentObjectPtr()->getPlacementOf(subname, targetObj); |
| 1032 |
return new Base::PlacementPy(new Base::Placement(p)); |
| 1033 |
} |
| 1034 |
PY_CATCH |
| 1035 |
} |