| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2002 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 |
#include <sstream> |
| 24 |
|
| 25 |
#include <App/DocumentObjectPy.h> |
| 26 |
#include <Base/Interpreter.h> |
| 27 |
#include <Base/MatrixPy.h> |
| 28 |
#include <Base/PlacementPy.h> |
| 29 |
#include <Base/Tools.h> |
| 30 |
|
| 31 |
#include "FeaturePython.h" |
| 32 |
#include "FeaturePythonPyImp.h" |
| 33 |
|
| 34 |
|
| 35 |
using namespace App; |
| 36 |
|
| 37 |
FeaturePythonImp::FeaturePythonImp(App::DocumentObject* o) |
| 38 |
: object(o) |
| 39 |
{} |
| 40 |
|
| 41 |
FeaturePythonImp::~FeaturePythonImp() |
| 42 |
{ |
| 43 |
Base::PyGILStateLocker lock; |
| 44 |
#undef FC_PY_ELEMENT |
| 45 |
#define FC_PY_ELEMENT(_name) py_##_name = Py::None(); |
| 46 |
|
| 47 |
try { |
| 48 |
FC_PY_FEATURE_PYTHON |
| 49 |
} |
| 50 |
catch (Py::Exception& e) { |
| 51 |
e.clear(); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
void FeaturePythonImp::init(PyObject* pyobj) |
| 56 |
{ |
| 57 |
Base::PyGILStateLocker lock; |
| 58 |
has__object__ = !!PyObject_HasAttrString(pyobj, "__object__"); |
| 59 |
|
| 60 |
#undef FC_PY_ELEMENT |
| 61 |
#define FC_PY_ELEMENT(_name) FC_PY_ELEMENT_INIT(_name) |
| 62 |
|
| 63 |
FC_PY_FEATURE_PYTHON |
| 64 |
} |
| 65 |
|
| 66 |
#define FC_PY_CALL_CHECK(_name) _FC_PY_CALL_CHECK(_name, return (false)) |
| 67 |
|
| 68 |
/*! |
| 69 |
Calls the execute() method of the Python feature class. If the Python feature class doesn't have an |
| 70 |
execute() method or if it returns False this method also return false and true otherwise. |
| 71 |
*/ |
| 72 |
bool FeaturePythonImp::execute() |
| 73 |
{ |
| 74 |
FC_PY_CALL_CHECK(execute) |
| 75 |
Base::PyGILStateLocker lock; |
| 76 |
try { |
| 77 |
if (has__object__) { |
| 78 |
Py::Object res = Base::pyCall(py_execute.ptr()); |
| 79 |
if (res.isBoolean() && !res.isTrue()) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
return true; |
| 83 |
} |
| 84 |
else { |
| 85 |
Py::Tuple args(1); |
| 86 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 87 |
Py::Object res = Base::pyCall(py_execute.ptr(), args.ptr()); |
| 88 |
if (res.isBoolean() && !res.isTrue()) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
return true; |
| 92 |
} |
| 93 |
} |
| 94 |
catch (Py::Exception&) { |
| 95 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 96 |
PyErr_Clear(); |
| 97 |
return false; |
| 98 |
} |
| 99 |
Base::PyException::throwException(); // extract the Python error text |
| 100 |
} |
| 101 |
|
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
bool FeaturePythonImp::mustExecute() const |
| 106 |
{ |
| 107 |
FC_PY_CALL_CHECK(mustExecute) |
| 108 |
Base::PyGILStateLocker lock; |
| 109 |
try { |
| 110 |
if (has__object__) { |
| 111 |
Py::Object res(Base::pyCall(py_mustExecute.ptr())); |
| 112 |
return res.isTrue(); |
| 113 |
} |
| 114 |
else { |
| 115 |
Py::Tuple args(1); |
| 116 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 117 |
Py::Object res(Base::pyCall(py_mustExecute.ptr(), args.ptr())); |
| 118 |
return res.isTrue(); |
| 119 |
} |
| 120 |
} |
| 121 |
catch (Py::Exception&) { |
| 122 |
Base::PyException e; // extract the Python error text |
| 123 |
e.reportException(); |
| 124 |
} |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
void FeaturePythonImp::onBeforeChange(const Property* prop) |
| 130 |
{ |
| 131 |
if (py_onBeforeChange.isNone()) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
// Run the execute method of the proxy object. |
| 136 |
Base::PyGILStateLocker lock; |
| 137 |
try { |
| 138 |
const char* prop_name = object->getPropertyName(prop); |
| 139 |
if (!prop_name) { |
| 140 |
return; |
| 141 |
} |
| 142 |
if (has__object__) { |
| 143 |
Py::Tuple args(1); |
| 144 |
args.setItem(0, Py::String(prop_name)); |
| 145 |
Base::pyCall(py_onBeforeChange.ptr(), args.ptr()); |
| 146 |
} |
| 147 |
else { |
| 148 |
Py::Tuple args(2); |
| 149 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 150 |
args.setItem(1, Py::String(prop_name)); |
| 151 |
Base::pyCall(py_onBeforeChange.ptr(), args.ptr()); |
| 152 |
} |
| 153 |
} |
| 154 |
catch (Py::Exception&) { |
| 155 |
Base::PyException e; // extract the Python error text |
| 156 |
e.reportException(); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
bool FeaturePythonImp::onBeforeChangeLabel(std::string& newLabel) |
| 161 |
{ |
| 162 |
if (py_onBeforeChangeLabel.isNone()) { |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
// Run the execute method of the proxy object. |
| 167 |
Base::PyGILStateLocker lock; |
| 168 |
try { |
| 169 |
Py::Tuple args(2); |
| 170 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 171 |
args.setItem(1, Py::String(newLabel)); |
| 172 |
Py::Object ret(Base::pyCall(py_onBeforeChangeLabel.ptr(), args.ptr())); |
| 173 |
if (!ret.isNone()) { |
| 174 |
if (!ret.isString()) { |
| 175 |
throw Py::TypeError("onBeforeChangeLabel expects to return a string"); |
| 176 |
} |
| 177 |
newLabel = ret.as_string(); |
| 178 |
return true; |
| 179 |
} |
| 180 |
} |
| 181 |
catch (Py::Exception&) { |
| 182 |
Base::PyException e; // extract the Python error text |
| 183 |
e.reportException(); |
| 184 |
} |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
void FeaturePythonImp::onChanged(const Property* prop) |
| 189 |
{ |
| 190 |
if (py_onChanged.isNone()) { |
| 191 |
return; |
| 192 |
} |
| 193 |
// Run the execute method of the proxy object. |
| 194 |
Base::PyGILStateLocker lock; |
| 195 |
try { |
| 196 |
const char* prop_name = object->getPropertyName(prop); |
| 197 |
if (!prop_name) { |
| 198 |
return; |
| 199 |
} |
| 200 |
if (has__object__) { |
| 201 |
Py::Tuple args(1); |
| 202 |
args.setItem(0, Py::String(prop_name)); |
| 203 |
Base::pyCall(py_onChanged.ptr(), args.ptr()); |
| 204 |
} |
| 205 |
else { |
| 206 |
Py::Tuple args(2); |
| 207 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 208 |
args.setItem(1, Py::String(prop_name)); |
| 209 |
Base::pyCall(py_onChanged.ptr(), args.ptr()); |
| 210 |
} |
| 211 |
} |
| 212 |
catch (Py::Exception&) { |
| 213 |
Base::PyException e; // extract the Python error text |
| 214 |
e.reportException(); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
void FeaturePythonImp::onDocumentRestored() |
| 219 |
{ |
| 220 |
_FC_PY_CALL_CHECK(onDocumentRestored, return); |
| 221 |
|
| 222 |
// Run the execute method of the proxy object. |
| 223 |
Base::PyGILStateLocker lock; |
| 224 |
try { |
| 225 |
if (has__object__) { |
| 226 |
Base::pyCall(py_onDocumentRestored.ptr()); |
| 227 |
} |
| 228 |
else { |
| 229 |
Py::Tuple args(1); |
| 230 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 231 |
Base::pyCall(py_onDocumentRestored.ptr(), args.ptr()); |
| 232 |
} |
| 233 |
} |
| 234 |
catch (Py::Exception&) { |
| 235 |
Base::PyException e; // extract the Python error text |
| 236 |
e.reportException(); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
void FeaturePythonImp::unsetupObject() |
| 241 |
{ |
| 242 |
_FC_PY_CALL_CHECK(unsetupObject, return); |
| 243 |
|
| 244 |
// Run the execute method of the proxy object. |
| 245 |
Base::PyGILStateLocker lock; |
| 246 |
try { |
| 247 |
if (has__object__) { |
| 248 |
Base::pyCall(py_unsetupObject.ptr()); |
| 249 |
} |
| 250 |
else { |
| 251 |
Py::Tuple args(1); |
| 252 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 253 |
Base::pyCall(py_unsetupObject.ptr(), args.ptr()); |
| 254 |
} |
| 255 |
} |
| 256 |
catch (Py::Exception&) { |
| 257 |
Base::PyException e; // extract the Python error text |
| 258 |
e.reportException(); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
bool FeaturePythonImp::getSubObject(DocumentObject*& ret, |
| 263 |
const char* subname, |
| 264 |
PyObject** pyObj, |
| 265 |
Base::Matrix4D* _mat, |
| 266 |
bool transform, |
| 267 |
int depth) const |
| 268 |
{ |
| 269 |
FC_PY_CALL_CHECK(getSubObject); |
| 270 |
Base::PyGILStateLocker lock; |
| 271 |
try { |
| 272 |
Py::Tuple args(6); |
| 273 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 274 |
if (!subname) { |
| 275 |
subname = ""; |
| 276 |
} |
| 277 |
args.setItem(1, Py::String(subname)); |
| 278 |
args.setItem(2, Py::Long(pyObj ? 2 : 1)); |
| 279 |
Base::MatrixPy* pyMat = new Base::MatrixPy(new Base::Matrix4D); |
| 280 |
if (_mat) { |
| 281 |
*pyMat->getMatrixPtr() = *_mat; |
| 282 |
} |
| 283 |
args.setItem(3, Py::asObject(pyMat)); |
| 284 |
args.setItem(4, Py::Boolean(transform)); |
| 285 |
args.setItem(5, Py::Long(depth)); |
| 286 |
|
| 287 |
Py::Object res(Base::pyCall(py_getSubObject.ptr(), args.ptr())); |
| 288 |
if (res.isNone()) { |
| 289 |
ret = nullptr; |
| 290 |
return true; |
| 291 |
} |
| 292 |
if (!res.isTrue()) { |
| 293 |
return false; |
| 294 |
} |
| 295 |
if (!res.isSequence()) { |
| 296 |
throw Py::TypeError("getSubObject expects return type of tuple"); |
| 297 |
} |
| 298 |
Py::Sequence seq(res); |
| 299 |
if (seq.length() < 2 |
| 300 |
|| (!seq.getItem(0).isNone() |
| 301 |
&& !PyObject_TypeCheck(seq.getItem(0).ptr(), &DocumentObjectPy::Type)) |
| 302 |
|| !PyObject_TypeCheck(seq.getItem(1).ptr(), &Base::MatrixPy::Type)) { |
| 303 |
throw Py::TypeError("getSubObject expects return type of (obj,matrix,pyobj)"); |
| 304 |
} |
| 305 |
if (_mat) { |
| 306 |
*_mat = *static_cast<Base::MatrixPy*>(seq.getItem(1).ptr())->getMatrixPtr(); |
| 307 |
} |
| 308 |
if (pyObj) { |
| 309 |
if (seq.length() > 2) { |
| 310 |
*pyObj = Py::new_reference_to(seq.getItem(2)); |
| 311 |
} |
| 312 |
else { |
| 313 |
*pyObj = Py::new_reference_to(Py::None()); |
| 314 |
} |
| 315 |
} |
| 316 |
if (seq.getItem(0).isNone()) { |
| 317 |
ret = nullptr; |
| 318 |
} |
| 319 |
else { |
| 320 |
ret = static_cast<DocumentObjectPy*>(seq.getItem(0).ptr())->getDocumentObjectPtr(); |
| 321 |
} |
| 322 |
return true; |
| 323 |
} |
| 324 |
catch (Py::Exception&) { |
| 325 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 326 |
PyErr_Clear(); |
| 327 |
return false; |
| 328 |
} |
| 329 |
Base::PyException e; // extract the Python error text |
| 330 |
e.reportException(); |
| 331 |
ret = nullptr; |
| 332 |
return true; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
bool FeaturePythonImp::getSubObjects(std::vector<std::string>& ret, int reason) const |
| 337 |
{ |
| 338 |
FC_PY_CALL_CHECK(getSubObjects); |
| 339 |
Base::PyGILStateLocker lock; |
| 340 |
try { |
| 341 |
Py::Tuple args(2); |
| 342 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 343 |
args.setItem(1, Py::Long(reason)); |
| 344 |
Py::Object res(Base::pyCall(py_getSubObjects.ptr(), args.ptr())); |
| 345 |
if (!res.isTrue()) { |
| 346 |
return true; |
| 347 |
} |
| 348 |
if (!res.isSequence()) { |
| 349 |
throw Py::TypeError("getSubObjects expects return type of tuple"); |
| 350 |
} |
| 351 |
Py::Sequence seq(res); |
| 352 |
for (Py_ssize_t i = 0; i < seq.length(); ++i) { |
| 353 |
Py::Object name(seq[i].ptr()); |
| 354 |
if (!name.isString()) { |
| 355 |
throw Py::TypeError("getSubObjects expects string in returned sequence"); |
| 356 |
} |
| 357 |
ret.push_back(name.as_string()); |
| 358 |
} |
| 359 |
return true; |
| 360 |
} |
| 361 |
catch (Py::Exception&) { |
| 362 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 363 |
PyErr_Clear(); |
| 364 |
return false; |
| 365 |
} |
| 366 |
Base::PyException e; // extract the Python error text |
| 367 |
e.reportException(); |
| 368 |
return true; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
bool FeaturePythonImp::getLinkedObject(DocumentObject*& ret, |
| 373 |
bool recurse, |
| 374 |
Base::Matrix4D* _mat, |
| 375 |
bool transform, |
| 376 |
int depth) const |
| 377 |
{ |
| 378 |
FC_PY_CALL_CHECK(getLinkedObject); |
| 379 |
Base::PyGILStateLocker lock; |
| 380 |
try { |
| 381 |
Py::Tuple args(5); |
| 382 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 383 |
args.setItem(1, Py::Boolean(recurse)); |
| 384 |
Base::MatrixPy* pyMat = new Base::MatrixPy(new Base::Matrix4D); |
| 385 |
if (_mat) { |
| 386 |
*pyMat->getMatrixPtr() = *_mat; |
| 387 |
} |
| 388 |
args.setItem(2, Py::asObject(pyMat)); |
| 389 |
args.setItem(3, Py::Boolean(transform)); |
| 390 |
args.setItem(4, Py::Long(depth)); |
| 391 |
|
| 392 |
Py::Object res(Base::pyCall(py_getLinkedObject.ptr(), args.ptr())); |
| 393 |
if (!res.isTrue()) { |
| 394 |
ret = object; |
| 395 |
return true; |
| 396 |
} |
| 397 |
if (!res.isSequence()) { |
| 398 |
throw Py::TypeError("getLinkedObject expects return type of (object,matrix)"); |
| 399 |
} |
| 400 |
Py::Sequence seq(res); |
| 401 |
if (seq.length() != 2 |
| 402 |
|| (!seq.getItem(0).isNone() |
| 403 |
&& !PyObject_TypeCheck(seq.getItem(0).ptr(), &DocumentObjectPy::Type)) |
| 404 |
|| !PyObject_TypeCheck(seq.getItem(1).ptr(), &Base::MatrixPy::Type)) { |
| 405 |
throw Py::TypeError("getLinkedObject expects return type of (object,matrix)"); |
| 406 |
} |
| 407 |
if (_mat) { |
| 408 |
*_mat = *static_cast<Base::MatrixPy*>(seq.getItem(1).ptr())->getMatrixPtr(); |
| 409 |
} |
| 410 |
if (seq.getItem(0).isNone()) { |
| 411 |
ret = object; |
| 412 |
} |
| 413 |
else { |
| 414 |
ret = static_cast<DocumentObjectPy*>(seq.getItem(0).ptr())->getDocumentObjectPtr(); |
| 415 |
} |
| 416 |
return true; |
| 417 |
} |
| 418 |
catch (Py::Exception&) { |
| 419 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 420 |
PyErr_Clear(); |
| 421 |
return false; |
| 422 |
} |
| 423 |
Base::PyException e; // extract the Python error text |
| 424 |
e.reportException(); |
| 425 |
ret = nullptr; |
| 426 |
return true; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
PyObject* FeaturePythonImp::getPyObject() |
| 431 |
{ |
| 432 |
// ref counter is set to 1 |
| 433 |
return new FeaturePythonPyT<DocumentObjectPy>(object); |
| 434 |
} |
| 435 |
|
| 436 |
FeaturePythonImp::ValueT FeaturePythonImp::hasChildElement() const |
| 437 |
{ |
| 438 |
_FC_PY_CALL_CHECK(hasChildElement, return (NotImplemented)); |
| 439 |
Base::PyGILStateLocker lock; |
| 440 |
try { |
| 441 |
Py::Tuple args(1); |
| 442 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 443 |
Py::Boolean ok(Base::pyCall(py_hasChildElement.ptr(), args.ptr())); |
| 444 |
return static_cast<bool>(ok) ? Accepted : Rejected; |
| 445 |
} |
| 446 |
catch (Py::Exception&) { |
| 447 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 448 |
PyErr_Clear(); |
| 449 |
return NotImplemented; |
| 450 |
} |
| 451 |
|
| 452 |
Base::PyException e; // extract the Python error text |
| 453 |
e.reportException(); |
| 454 |
return Rejected; |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
int FeaturePythonImp::isElementVisible(const char* element) const |
| 459 |
{ |
| 460 |
_FC_PY_CALL_CHECK(isElementVisible, return (-2)); |
| 461 |
Base::PyGILStateLocker lock; |
| 462 |
try { |
| 463 |
Py::Tuple args(2); |
| 464 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 465 |
args.setItem(1, Py::String(element ? element : "")); |
| 466 |
return Py::Long(Base::pyCall(py_isElementVisible.ptr(), args.ptr())); |
| 467 |
} |
| 468 |
catch (Py::Exception&) { |
| 469 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 470 |
PyErr_Clear(); |
| 471 |
return -2; |
| 472 |
} |
| 473 |
Base::PyException e; // extract the Python error text |
| 474 |
e.reportException(); |
| 475 |
return -1; |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
int FeaturePythonImp::setElementVisible(const char* element, bool visible) |
| 480 |
{ |
| 481 |
_FC_PY_CALL_CHECK(setElementVisible, return (-2)); |
| 482 |
Base::PyGILStateLocker lock; |
| 483 |
try { |
| 484 |
Py::Tuple args(3); |
| 485 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 486 |
args.setItem(1, Py::String(element ? element : "")); |
| 487 |
args.setItem(2, Py::Boolean(visible)); |
| 488 |
return Py::Long(Base::pyCall(py_setElementVisible.ptr(), args.ptr())); |
| 489 |
} |
| 490 |
catch (Py::Exception&) { |
| 491 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 492 |
PyErr_Clear(); |
| 493 |
return -2; |
| 494 |
} |
| 495 |
Base::PyException e; // extract the Python error text |
| 496 |
e.reportException(); |
| 497 |
return -1; |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
std::string FeaturePythonImp::getViewProviderName() |
| 502 |
{ |
| 503 |
_FC_PY_CALL_CHECK(getViewProviderName, return (std::string())); |
| 504 |
Base::PyGILStateLocker lock; |
| 505 |
try { |
| 506 |
Py::TupleN args(Py::Object(object->getPyObject(), true)); |
| 507 |
Py::String ret(Base::pyCall(py_getViewProviderName.ptr(), args.ptr())); |
| 508 |
return ret.as_string(); |
| 509 |
} |
| 510 |
catch (Py::Exception&) { |
| 511 |
Base::PyException e; // extract the Python error text |
| 512 |
e.reportException(); |
| 513 |
} |
| 514 |
|
| 515 |
return {}; |
| 516 |
} |
| 517 |
|
| 518 |
FeaturePythonImp::ValueT FeaturePythonImp::canLinkProperties() const |
| 519 |
{ |
| 520 |
_FC_PY_CALL_CHECK(canLinkProperties, return (NotImplemented)); |
| 521 |
Base::PyGILStateLocker lock; |
| 522 |
try { |
| 523 |
Py::Tuple args(1); |
| 524 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 525 |
Py::Boolean ok(Base::pyCall(py_canLinkProperties.ptr(), args.ptr())); |
| 526 |
return ok ? Accepted : Rejected; |
| 527 |
} |
| 528 |
catch (Py::Exception&) { |
| 529 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 530 |
PyErr_Clear(); |
| 531 |
return NotImplemented; |
| 532 |
} |
| 533 |
Base::PyException e; // extract the Python error text |
| 534 |
e.reportException(); |
| 535 |
return Rejected; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
FeaturePythonImp::ValueT FeaturePythonImp::allowDuplicateLabel() const |
| 540 |
{ |
| 541 |
_FC_PY_CALL_CHECK(allowDuplicateLabel, return (NotImplemented)); |
| 542 |
Base::PyGILStateLocker lock; |
| 543 |
try { |
| 544 |
Py::Tuple args(1); |
| 545 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 546 |
Py::Boolean ok(Base::pyCall(py_allowDuplicateLabel.ptr(), args.ptr())); |
| 547 |
return ok ? Accepted : Rejected; |
| 548 |
} |
| 549 |
catch (Py::Exception&) { |
| 550 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 551 |
PyErr_Clear(); |
| 552 |
return NotImplemented; |
| 553 |
} |
| 554 |
|
| 555 |
Base::PyException e; // extract the Python error text |
| 556 |
e.reportException(); |
| 557 |
return Rejected; |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
int FeaturePythonImp::canLoadPartial() const |
| 562 |
{ |
| 563 |
_FC_PY_CALL_CHECK(canLoadPartial, return (-1)); |
| 564 |
Base::PyGILStateLocker lock; |
| 565 |
try { |
| 566 |
Py::Tuple args(1); |
| 567 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 568 |
Py::Long ret(Base::pyCall(py_canLoadPartial.ptr(), args.ptr())); |
| 569 |
return ret; |
| 570 |
} |
| 571 |
catch (Py::Exception&) { |
| 572 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 573 |
PyErr_Clear(); |
| 574 |
return -1; |
| 575 |
} |
| 576 |
Base::PyException e; // extract the Python error text |
| 577 |
e.reportException(); |
| 578 |
return 0; |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
FeaturePythonImp::ValueT FeaturePythonImp::redirectSubName(std::ostringstream& ss, |
| 583 |
App::DocumentObject* topParent, |
| 584 |
App::DocumentObject* child) const |
| 585 |
{ |
| 586 |
_FC_PY_CALL_CHECK(redirectSubName, return (NotImplemented)); |
| 587 |
Base::PyGILStateLocker lock; |
| 588 |
try { |
| 589 |
Py::Tuple args(4); |
| 590 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 591 |
args.setItem(1, Py::String(ss.str())); |
| 592 |
args.setItem(2, topParent ? Py::Object(topParent->getPyObject(), true) : Py::Object()); |
| 593 |
args.setItem(3, child ? Py::Object(child->getPyObject(), true) : Py::Object()); |
| 594 |
Py::Object ret(Base::pyCall(py_redirectSubName.ptr(), args.ptr())); |
| 595 |
if (ret.isNone()) { |
| 596 |
return Rejected; |
| 597 |
} |
| 598 |
ss.str(""); |
| 599 |
ss << ret.as_string(); |
| 600 |
return Accepted; |
| 601 |
} |
| 602 |
catch (Py::Exception&) { |
| 603 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 604 |
PyErr_Clear(); |
| 605 |
return NotImplemented; |
| 606 |
} |
| 607 |
|
| 608 |
Base::PyException e; // extract the Python error text |
| 609 |
e.reportException(); |
| 610 |
return Rejected; |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
bool FeaturePythonImp::editProperty(const char* name) |
| 615 |
{ |
| 616 |
_FC_PY_CALL_CHECK(editProperty, return false); |
| 617 |
Base::PyGILStateLocker lock; |
| 618 |
try { |
| 619 |
Py::Tuple args(1); |
| 620 |
args.setItem(0, Py::String(name)); |
| 621 |
Py::Object ret(Base::pyCall(py_editProperty.ptr(), args.ptr())); |
| 622 |
return ret.isTrue(); |
| 623 |
} |
| 624 |
catch (Py::Exception&) { |
| 625 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 626 |
PyErr_Clear(); |
| 627 |
return false; |
| 628 |
} |
| 629 |
|
| 630 |
Base::PyException e; // extract the Python error text |
| 631 |
e.reportException(); |
| 632 |
} |
| 633 |
return false; |
| 634 |
} |
| 635 |
|
| 636 |
FeaturePythonImp::ValueT FeaturePythonImp::isLink() const |
| 637 |
{ |
| 638 |
_FC_PY_CALL_CHECK(isLink, return (NotImplemented)); |
| 639 |
Base::PyGILStateLocker lock; |
| 640 |
try { |
| 641 |
Py::Tuple args(1); |
| 642 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 643 |
Py::Boolean ok(Base::pyCall(py_isLink.ptr(), args.ptr())); |
| 644 |
return ok ? Accepted : Rejected; |
| 645 |
} |
| 646 |
catch (Py::Exception&) { |
| 647 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 648 |
PyErr_Clear(); |
| 649 |
return NotImplemented; |
| 650 |
} |
| 651 |
|
| 652 |
Base::PyException e; // extract the Python error text |
| 653 |
e.reportException(); |
| 654 |
return Rejected; |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
FeaturePythonImp::ValueT FeaturePythonImp::isLinkGroup() const |
| 659 |
{ |
| 660 |
_FC_PY_CALL_CHECK(isLinkGroup, return (NotImplemented)); |
| 661 |
Base::PyGILStateLocker lock; |
| 662 |
try { |
| 663 |
Py::Tuple args(1); |
| 664 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 665 |
Py::Boolean ok(Base::pyCall(py_isLinkGroup.ptr(), args.ptr())); |
| 666 |
return ok ? Accepted : Rejected; |
| 667 |
} |
| 668 |
catch (Py::Exception&) { |
| 669 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 670 |
PyErr_Clear(); |
| 671 |
return NotImplemented; |
| 672 |
} |
| 673 |
|
| 674 |
Base::PyException e; // extract the Python error text |
| 675 |
e.reportException(); |
| 676 |
return Rejected; |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
bool FeaturePythonImp::getPlacementOf( |
| 681 |
Base::Placement& ret, |
| 682 |
const char* subname, |
| 683 |
App::DocumentObject* target |
| 684 |
) const |
| 685 |
{ |
| 686 |
FC_PY_CALL_CHECK(getPlacementOf); // Standard macro check |
| 687 |
Base::PyGILStateLocker lock; |
| 688 |
try { |
| 689 |
Py::Tuple args(3); |
| 690 |
|
| 691 |
// Arg 0: The object itself |
| 692 |
args.setItem(0, Py::Object(object->getPyObject(), true)); |
| 693 |
|
| 694 |
// Arg 1: The subname string |
| 695 |
args.setItem(1, Py::String(subname ? subname : "")); |
| 696 |
|
| 697 |
// Arg 2: The target object (or None) |
| 698 |
if (target) { |
| 699 |
args.setItem(2, Py::Object(target->getPyObject(), true)); |
| 700 |
} |
| 701 |
else { |
| 702 |
args.setItem(2, Py::None()); |
| 703 |
} |
| 704 |
|
| 705 |
// Call the Python method |
| 706 |
Py::Object res(Base::pyCall(py_getPlacementOf.ptr(), args.ptr())); |
| 707 |
|
| 708 |
// Check if Python returned None (implies "use base implementation") |
| 709 |
if (res.isNone()) { |
| 710 |
return false; |
| 711 |
} |
| 712 |
|
| 713 |
// Check return type |
| 714 |
if (!PyObject_TypeCheck(res.ptr(), &Base::PlacementPy::Type)) { |
| 715 |
throw Py::TypeError("getPlacementOf expects a Base.Placement object return"); |
| 716 |
} |
| 717 |
|
| 718 |
// Convert Python object back to C++ Placement |
| 719 |
ret = *static_cast<Base::PlacementPy*>(res.ptr())->getPlacementPtr(); |
| 720 |
return true; |
| 721 |
} |
| 722 |
catch (Py::Exception&) { |
| 723 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 724 |
PyErr_Clear(); |
| 725 |
return false; |
| 726 |
} |
| 727 |
Base::PyException e; |
| 728 |
e.reportException(); |
| 729 |
return false; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
// --------------------------------------------------------- |
| 734 |
|
| 735 |
namespace App |
| 736 |
{ |
| 737 |
PROPERTY_SOURCE_TEMPLATE(App::FeaturePython, App::DocumentObject) |
| 738 |
template<> |
| 739 |
const char* App::FeaturePython::getViewProviderName() const |
| 740 |
{ |
| 741 |
return "Gui::ViewProviderFeaturePython"; |
| 742 |
} |
| 743 |
template<> |
| 744 |
PyObject* App::FeaturePython::getPyObject() |
| 745 |
{ |
| 746 |
if (PythonObject.is(Py::_None())) { |
| 747 |
// ref counter is set to 1 |
| 748 |
PythonObject = Py::Object(new FeaturePythonPyT<DocumentObjectPy>(this), true); |
| 749 |
} |
| 750 |
return Py::new_reference_to(PythonObject); |
| 751 |
} |
| 752 |
// explicit template instantiation |
| 753 |
template class AppExport FeaturePythonT<DocumentObject>; |
| 754 |
} // namespace App |
| 755 |
|
| 756 |
// --------------------------------------------------------- |
| 757 |
|
| 758 |
namespace App |
| 759 |
{ |
| 760 |
PROPERTY_SOURCE_TEMPLATE(App::GeometryPython, App::GeoFeature) |
| 761 |
template<> |
| 762 |
const char* App::GeometryPython::getViewProviderName() const |
| 763 |
{ |
| 764 |
return "Gui::ViewProviderGeometryPython"; |
| 765 |
} |
| 766 |
// explicit template instantiation |
| 767 |
template class AppExport FeaturePythonT<GeoFeature>; |
| 768 |
} // namespace App |