| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 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 <Inventor/SbRotation.h> |
| 25 |
#include <Inventor/SoFullPath.h> |
| 26 |
#include <Inventor/details/SoDetail.h> |
| 27 |
#include <Inventor/nodes/SoSeparator.h> |
| 28 |
#include <Inventor/nodes/SoSwitch.h> |
| 29 |
#include <QByteArray> |
| 30 |
#include <QDataStream> |
| 31 |
|
| 32 |
|
| 33 |
#include <Base/BoundBoxPy.h> |
| 34 |
#include <Base/PyWrapParseTupleAndKeywords.h> |
| 35 |
|
| 36 |
#include "PythonWrapper.h" |
| 37 |
#include "SoFCDB.h" |
| 38 |
|
| 39 |
// inclusion of the generated files (generated out of ViewProviderPy.pyi) |
| 40 |
#include <Gui/ViewProviderPy.h> |
| 41 |
#include <Gui/ViewProviderPy.cpp> |
| 42 |
#include <Gui/View3DPy.h> |
| 43 |
#include <Gui/View3DInventor.h> |
| 44 |
#include <Base/Interpreter.h> |
| 45 |
#include <Base/Matrix.h> |
| 46 |
#include <Base/MatrixPy.h> |
| 47 |
#include <Base/Placement.h> |
| 48 |
#include <Base/PlacementPy.h> |
| 49 |
#include <App/Document.h> |
| 50 |
#include <App/DocumentObject.h> |
| 51 |
#include <App/DocumentObjectPy.h> |
| 52 |
|
| 53 |
|
| 54 |
using namespace Gui; |
| 55 |
|
| 56 |
// returns a string which represent the object e.g. when printed in python |
| 57 |
std::string ViewProviderPy::representation() const |
| 58 |
{ |
| 59 |
return "<View provider object>"; |
| 60 |
} |
| 61 |
|
| 62 |
PyObject* ViewProviderPy::addProperty(PyObject* args, PyObject* kwd) |
| 63 |
{ |
| 64 |
char *sType, *sName = nullptr, *sGroup = nullptr, *sDoc = nullptr; |
| 65 |
short attr = 0; |
| 66 |
PyObject *ro = Py_False, *hd = Py_False, *lk = Py_False; |
| 67 |
PyObject* enumVals = nullptr; |
| 68 |
const std::array<const char*, 10> kwlist { |
| 69 |
"type", |
| 70 |
"name", |
| 71 |
"group", |
| 72 |
"doc", |
| 73 |
"attr", |
| 74 |
"read_only", |
| 75 |
"hidden", |
| 76 |
"locked", |
| 77 |
"enum_vals", |
| 78 |
nullptr |
| 79 |
}; |
| 80 |
if (!Base::Wrapped_ParseTupleAndKeywords( |
| 81 |
args, |
| 82 |
kwd, |
| 83 |
"ss|sethO!O!O!O", |
| 84 |
kwlist, |
| 85 |
&sType, |
| 86 |
&sName, |
| 87 |
&sGroup, |
| 88 |
"utf-8", |
| 89 |
&sDoc, |
| 90 |
&attr, |
| 91 |
&PyBool_Type, |
| 92 |
&ro, |
| 93 |
&PyBool_Type, |
| 94 |
&hd, |
| 95 |
&PyBool_Type, |
| 96 |
&lk, |
| 97 |
&enumVals |
| 98 |
)) { |
| 99 |
return nullptr; |
| 100 |
} |
| 101 |
|
| 102 |
App::Property* prop = getViewProviderPtr()->addDynamicProperty( |
| 103 |
sType, |
| 104 |
sName, |
| 105 |
sGroup, |
| 106 |
sDoc, |
| 107 |
attr, |
| 108 |
Base::asBoolean(ro), |
| 109 |
Base::asBoolean(hd) |
| 110 |
); |
| 111 |
|
| 112 |
prop->setStatus(App::Property::LockDynamic, Base::asBoolean(lk)); |
| 113 |
|
| 114 |
// enum support |
| 115 |
auto* propEnum = freecad_cast<App::PropertyEnumeration*>(prop); |
| 116 |
if (propEnum && enumVals) { |
| 117 |
propEnum->setPyObject(enumVals); |
| 118 |
} |
| 119 |
|
| 120 |
return Py::new_reference_to(this); |
| 121 |
} |
| 122 |
|
| 123 |
PyObject* ViewProviderPy::removeProperty(PyObject* args) |
| 124 |
{ |
| 125 |
char* sName; |
| 126 |
if (!PyArg_ParseTuple(args, "s", &sName)) { |
| 127 |
return nullptr; |
| 128 |
} |
| 129 |
|
| 130 |
try { |
| 131 |
bool ok = getViewProviderPtr()->removeDynamicProperty(sName); |
| 132 |
return Py_BuildValue("O", (ok ? Py_True : Py_False)); |
| 133 |
} |
| 134 |
catch (const Base::Exception& e) { |
| 135 |
throw Py::RuntimeError(e.what()); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
PyObject* ViewProviderPy::supportedProperties(PyObject* args) |
| 140 |
{ |
| 141 |
if (!PyArg_ParseTuple(args, "")) { |
| 142 |
return nullptr; |
| 143 |
} |
| 144 |
|
| 145 |
std::vector<Base::Type> ary; |
| 146 |
Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary); |
| 147 |
Py::List res; |
| 148 |
for (auto& it : ary) { |
| 149 |
auto data = static_cast<Base::BaseClass*>(it.createInstance()); |
| 150 |
if (data) { |
| 151 |
delete data; |
| 152 |
res.append(Py::String(it.getName())); |
| 153 |
} |
| 154 |
} |
| 155 |
return Py::new_reference_to(res); |
| 156 |
} |
| 157 |
|
| 158 |
PyObject* ViewProviderPy::show(PyObject* args) |
| 159 |
{ |
| 160 |
if (!PyArg_ParseTuple(args, "")) { |
| 161 |
return nullptr; |
| 162 |
} |
| 163 |
|
| 164 |
PY_TRY |
| 165 |
{ |
| 166 |
getViewProviderPtr()->show(); |
| 167 |
Py_Return; |
| 168 |
} |
| 169 |
PY_CATCH; |
| 170 |
} |
| 171 |
|
| 172 |
PyObject* ViewProviderPy::hide(PyObject* args) |
| 173 |
{ |
| 174 |
if (!PyArg_ParseTuple(args, "")) { |
| 175 |
return nullptr; |
| 176 |
} |
| 177 |
|
| 178 |
PY_TRY |
| 179 |
{ |
| 180 |
getViewProviderPtr()->hide(); |
| 181 |
Py_Return; |
| 182 |
} |
| 183 |
PY_CATCH; |
| 184 |
} |
| 185 |
|
| 186 |
PyObject* ViewProviderPy::isVisible(PyObject* args) |
| 187 |
{ |
| 188 |
if (!PyArg_ParseTuple(args, "")) { |
| 189 |
return nullptr; |
| 190 |
} |
| 191 |
|
| 192 |
PY_TRY |
| 193 |
{ |
| 194 |
return Py::new_reference_to(Py::Boolean(getViewProviderPtr()->isShow())); |
| 195 |
} |
| 196 |
PY_CATCH; |
| 197 |
} |
| 198 |
|
| 199 |
PyObject* ViewProviderPy::canDragObject(PyObject* args) |
| 200 |
{ |
| 201 |
PyObject* obj = Py_None; |
| 202 |
if (!PyArg_ParseTuple(args, "|O", &obj)) { |
| 203 |
return nullptr; |
| 204 |
} |
| 205 |
|
| 206 |
PY_TRY |
| 207 |
{ |
| 208 |
Base::PyTypeCheck(&obj, &App::DocumentObjectPy::Type); |
| 209 |
bool ret; |
| 210 |
if (!obj) { |
| 211 |
ret = getViewProviderPtr()->canDragObjects(); |
| 212 |
} |
| 213 |
else { |
| 214 |
ret = getViewProviderPtr()->canDragObject( |
| 215 |
static_cast<App::DocumentObjectPy*>(obj)->getDocumentObjectPtr() |
| 216 |
); |
| 217 |
} |
| 218 |
|
| 219 |
return Py::new_reference_to(Py::Boolean(ret)); |
| 220 |
} |
| 221 |
PY_CATCH; |
| 222 |
} |
| 223 |
|
| 224 |
PyObject* ViewProviderPy::canDropObject(PyObject* args, PyObject* kw) |
| 225 |
{ |
| 226 |
PyObject* obj = Py_None; |
| 227 |
PyObject* owner = Py_None; |
| 228 |
PyObject* pyElements = Py_None; |
| 229 |
const char* subname = nullptr; |
| 230 |
static const std::array<const char*, 5> kwlist {"obj", "owner", "subname", "elem", nullptr}; |
| 231 |
if (!Base::Wrapped_ParseTupleAndKeywords(args, kw, "|OOsO", kwlist, &obj, &owner, &subname, &pyElements)) { |
| 232 |
return nullptr; |
| 233 |
} |
| 234 |
|
| 235 |
PY_TRY |
| 236 |
{ |
| 237 |
Base::PyTypeCheck( |
| 238 |
&obj, |
| 239 |
&App::DocumentObjectPy::Type, |
| 240 |
"expecting 'obj' to be of type App.DocumentObject or None" |
| 241 |
); |
| 242 |
Base::PyTypeCheck( |
| 243 |
&owner, |
| 244 |
&App::DocumentObjectPy::Type, |
| 245 |
"expecting 'owner' to be of type App.DocumentObject or None" |
| 246 |
); |
| 247 |
Base::PyTypeCheck(&pyElements, PySequence_Check, "expecting 'elem' to be sequence or None"); |
| 248 |
|
| 249 |
bool ret; |
| 250 |
App::DocumentObject* pcObject; |
| 251 |
App::DocumentObject* pcOwner = nullptr; |
| 252 |
App::PropertyStringList elements; |
| 253 |
if (!obj && (owner || pyElements || subname)) { |
| 254 |
PyErr_SetString( |
| 255 |
PyExc_ValueError, |
| 256 |
"'obj' must be specified if 'owner', 'subname' or 'elem' is given" |
| 257 |
); |
| 258 |
return nullptr; |
| 259 |
} |
| 260 |
if (!obj) { |
| 261 |
ret = getViewProviderPtr()->canDropObjects(); |
| 262 |
return Py::new_reference_to(Py::Boolean(ret)); |
| 263 |
} |
| 264 |
pcObject = static_cast<App::DocumentObjectPy*>(obj)->getDocumentObjectPtr(); |
| 265 |
if (owner) { |
| 266 |
pcOwner = static_cast<App::DocumentObjectPy*>(owner)->getDocumentObjectPtr(); |
| 267 |
} |
| 268 |
if (pyElements) { |
| 269 |
try { |
| 270 |
elements.setPyObject(pyElements); |
| 271 |
} |
| 272 |
catch (...) { |
| 273 |
PyErr_SetString(PyExc_TypeError, "'elem' must be a sequence of strings"); |
| 274 |
return nullptr; |
| 275 |
} |
| 276 |
} |
| 277 |
ret = getViewProviderPtr()->canDropObjectEx(pcObject, pcOwner, subname, elements.getValues()); |
| 278 |
return Py::new_reference_to(Py::Boolean(ret)); |
| 279 |
} |
| 280 |
PY_CATCH; |
| 281 |
} |
| 282 |
|
| 283 |
PyObject* ViewProviderPy::canDragAndDropObject(PyObject* args) |
| 284 |
{ |
| 285 |
PyObject* obj; |
| 286 |
if (!PyArg_ParseTuple(args, "O!", &App::DocumentObjectPy::Type, &obj)) { |
| 287 |
return nullptr; |
| 288 |
} |
| 289 |
|
| 290 |
PY_TRY |
| 291 |
{ |
| 292 |
bool ret = getViewProviderPtr()->canDragAndDropObject( |
| 293 |
static_cast<App::DocumentObjectPy*>(obj)->getDocumentObjectPtr() |
| 294 |
); |
| 295 |
return Py::new_reference_to(Py::Boolean(ret)); |
| 296 |
} |
| 297 |
PY_CATCH; |
| 298 |
} |
| 299 |
|
| 300 |
PyObject* ViewProviderPy::dropObject(PyObject* args, PyObject* kw) |
| 301 |
{ |
| 302 |
PyObject* obj; |
| 303 |
PyObject* owner = Py_None; |
| 304 |
PyObject* pyElements = Py_None; |
| 305 |
const char* subname = nullptr; |
| 306 |
static const std::array<const char*, 5> kwlist {"obj", "owner", "subname", "elem", nullptr}; |
| 307 |
if (!Base::Wrapped_ParseTupleAndKeywords( |
| 308 |
args, |
| 309 |
kw, |
| 310 |
"O!|OsO", |
| 311 |
kwlist, |
| 312 |
&App::DocumentObjectPy::Type, |
| 313 |
&obj, |
| 314 |
&owner, |
| 315 |
&subname, |
| 316 |
&pyElements |
| 317 |
)) { |
| 318 |
return nullptr; |
| 319 |
} |
| 320 |
|
| 321 |
PY_TRY |
| 322 |
{ |
| 323 |
Base::PyTypeCheck( |
| 324 |
&owner, |
| 325 |
&App::DocumentObjectPy::Type, |
| 326 |
"expecting 'owner' to be of type App.DocumentObject or None" |
| 327 |
); |
| 328 |
Base::PyTypeCheck(&pyElements, PySequence_Check, "expecting 'elem' to be sequence or None"); |
| 329 |
|
| 330 |
auto pcObject = static_cast<App::DocumentObjectPy*>(obj)->getDocumentObjectPtr(); |
| 331 |
App::DocumentObject* pcOwner = nullptr; |
| 332 |
App::PropertyStringList elements; |
| 333 |
if (owner) { |
| 334 |
pcOwner = static_cast<App::DocumentObjectPy*>(owner)->getDocumentObjectPtr(); |
| 335 |
} |
| 336 |
if (pyElements) { |
| 337 |
try { |
| 338 |
elements.setPyObject(pyElements); |
| 339 |
} |
| 340 |
catch (...) { |
| 341 |
PyErr_SetString(PyExc_TypeError, "'elem' must be a sequence of strings"); |
| 342 |
return nullptr; |
| 343 |
} |
| 344 |
} |
| 345 |
auto ret = getViewProviderPtr()->dropObjectEx(pcObject, pcOwner, subname, elements.getValues()); |
| 346 |
return Py::new_reference_to(Py::String(ret)); |
| 347 |
} |
| 348 |
PY_CATCH; |
| 349 |
} |
| 350 |
|
| 351 |
PyObject* ViewProviderPy::dragObject(PyObject* args) |
| 352 |
{ |
| 353 |
PyObject* obj; |
| 354 |
if (!PyArg_ParseTuple(args, "O!", &App::DocumentObjectPy::Type, &obj)) { |
| 355 |
return nullptr; |
| 356 |
} |
| 357 |
|
| 358 |
PY_TRY |
| 359 |
{ |
| 360 |
getViewProviderPtr()->dragObject( |
| 361 |
static_cast<App::DocumentObjectPy*>(obj)->getDocumentObjectPtr() |
| 362 |
); |
| 363 |
Py_Return; |
| 364 |
} |
| 365 |
PY_CATCH; |
| 366 |
} |
| 367 |
|
| 368 |
PyObject* ViewProviderPy::replaceObject(PyObject* args) |
| 369 |
{ |
| 370 |
PyObject* oldObj; |
| 371 |
PyObject* newObj; |
| 372 |
if (!PyArg_ParseTuple( |
| 373 |
args, |
| 374 |
"O!O!", |
| 375 |
&App::DocumentObjectPy::Type, |
| 376 |
&oldObj, |
| 377 |
&App::DocumentObjectPy::Type, |
| 378 |
&newObj |
| 379 |
)) { |
| 380 |
return nullptr; |
| 381 |
} |
| 382 |
|
| 383 |
PY_TRY |
| 384 |
{ |
| 385 |
int ret = getViewProviderPtr()->replaceObject( |
| 386 |
static_cast<App::DocumentObjectPy*>(oldObj)->getDocumentObjectPtr(), |
| 387 |
static_cast<App::DocumentObjectPy*>(newObj)->getDocumentObjectPtr() |
| 388 |
); |
| 389 |
return Py::new_reference_to(Py::Long(ret)); |
| 390 |
} |
| 391 |
PY_CATCH; |
| 392 |
} |
| 393 |
|
| 394 |
PyObject* ViewProviderPy::addDisplayMode(PyObject* args) |
| 395 |
{ |
| 396 |
char* mode; |
| 397 |
PyObject* obj; |
| 398 |
if (!PyArg_ParseTuple(args, "Os", &obj, &mode)) { |
| 399 |
return nullptr; |
| 400 |
} |
| 401 |
|
| 402 |
void* ptr = nullptr; |
| 403 |
try { |
| 404 |
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "_p_SoNode", obj, &ptr, 0); |
| 405 |
if (!ptr) { |
| 406 |
PyErr_SetString(PyExc_RuntimeError, "Conversion of coin.SoNode failed"); |
| 407 |
return nullptr; |
| 408 |
} |
| 409 |
} |
| 410 |
catch (const Base::Exception& e) { |
| 411 |
PyErr_SetString(PyExc_RuntimeError, e.what()); |
| 412 |
return nullptr; |
| 413 |
} |
| 414 |
|
| 415 |
PY_TRY |
| 416 |
{ |
| 417 |
auto node = static_cast<SoNode*>(ptr); |
| 418 |
getViewProviderPtr()->addDisplayMaskMode(node, mode); |
| 419 |
Py_Return; |
| 420 |
} |
| 421 |
PY_CATCH; |
| 422 |
} |
| 423 |
|
| 424 |
PyObject* ViewProviderPy::listDisplayModes(PyObject* args) |
| 425 |
{ |
| 426 |
if (!PyArg_ParseTuple(args, "")) { |
| 427 |
return nullptr; |
| 428 |
} |
| 429 |
|
| 430 |
PY_TRY |
| 431 |
{ |
| 432 |
std::vector<std::string> modes = getViewProviderPtr()->getDisplayModes(); |
| 433 |
PyObject* pyList = PyList_New(modes.size()); |
| 434 |
int i = 0; |
| 435 |
|
| 436 |
for (const auto& mode : modes) { |
| 437 |
PyObject* str = PyUnicode_FromString(mode.c_str()); |
| 438 |
PyList_SetItem(pyList, i++, str); |
| 439 |
} |
| 440 |
|
| 441 |
return pyList; |
| 442 |
} |
| 443 |
PY_CATCH; |
| 444 |
} |
| 445 |
|
| 446 |
PyObject* ViewProviderPy::toString(PyObject* args) |
| 447 |
{ |
| 448 |
if (!PyArg_ParseTuple(args, "")) { |
| 449 |
return nullptr; |
| 450 |
} |
| 451 |
|
| 452 |
PY_TRY |
| 453 |
{ |
| 454 |
std::string buffer = getViewProviderPtr()->toString(); |
| 455 |
return Py::new_reference_to(Py::String(buffer)); |
| 456 |
} |
| 457 |
PY_CATCH; |
| 458 |
} |
| 459 |
|
| 460 |
PyObject* ViewProviderPy::setTransformation(PyObject* args) |
| 461 |
{ |
| 462 |
PyObject* p; |
| 463 |
Base::Matrix4D mat; |
| 464 |
if (PyArg_ParseTuple(args, "O!", &(Base::MatrixPy::Type), &p)) { |
| 465 |
mat = *static_cast<Base::MatrixPy*>(p)->getMatrixPtr(); |
| 466 |
getViewProviderPtr()->setTransformation(mat); |
| 467 |
Py_Return; |
| 468 |
} |
| 469 |
PyErr_Clear(); |
| 470 |
|
| 471 |
if (PyArg_ParseTuple(args, "O!", &(Base::PlacementPy::Type), &p)) { |
| 472 |
auto plc = static_cast<Base::PlacementPy*>(p); |
| 473 |
getViewProviderPtr()->setTransformation(plc->getPlacementPtr()->toMatrix()); |
| 474 |
Py_Return; |
| 475 |
} |
| 476 |
|
| 477 |
PyErr_SetString(PyExc_TypeError, "The transformation must be a Base.Matrix or a Base.Placement"); |
| 478 |
return nullptr; |
| 479 |
} |
| 480 |
|
| 481 |
PyObject* ViewProviderPy::claimChildren(PyObject* args) const |
| 482 |
{ |
| 483 |
if (!PyArg_ParseTuple(args, "")) { |
| 484 |
return nullptr; |
| 485 |
} |
| 486 |
|
| 487 |
std::vector<App::DocumentObject*> children = this->getViewProviderPtr()->claimChildren(); |
| 488 |
Py::List ret; |
| 489 |
for (auto* child : children) { |
| 490 |
if (child) { |
| 491 |
ret.append(Py::asObject(child->getPyObject())); |
| 492 |
} |
| 493 |
else { |
| 494 |
ret.append(Py::None()); |
| 495 |
} |
| 496 |
} |
| 497 |
return Py::new_reference_to(ret); |
| 498 |
} |
| 499 |
|
| 500 |
PyObject* ViewProviderPy::claimChildrenRecursive(PyObject* args) const |
| 501 |
{ |
| 502 |
if (!PyArg_ParseTuple(args, "")) { |
| 503 |
return nullptr; |
| 504 |
} |
| 505 |
|
| 506 |
std::vector<App::DocumentObject*> children = this->getViewProviderPtr()->claimChildrenRecursive(); |
| 507 |
Py::List ret; |
| 508 |
for (auto* child : children) { |
| 509 |
if (child) { |
| 510 |
ret.append(Py::asObject(child->getPyObject())); |
| 511 |
} |
| 512 |
else { |
| 513 |
ret.append(Py::None()); |
| 514 |
} |
| 515 |
} |
| 516 |
return Py::new_reference_to(ret); |
| 517 |
} |
| 518 |
|
| 519 |
PyObject* ViewProviderPy::partialRender(PyObject* args) |
| 520 |
{ |
| 521 |
PyObject* value = Py_None; |
| 522 |
PyObject* clear = Py_False; |
| 523 |
if (!PyArg_ParseTuple(args, "|OO!", &value, &PyBool_Type, &clear)) { |
| 524 |
return nullptr; |
| 525 |
} |
| 526 |
|
| 527 |
std::vector<std::string> values; |
| 528 |
if (value != Py_None) { |
| 529 |
std::vector<Py::Object> pylist; |
| 530 |
if (PyList_Check(value) || PyTuple_Check(value)) { |
| 531 |
Py::Sequence seq(value); |
| 532 |
for (const auto& it : seq) { |
| 533 |
pylist.emplace_back(it); |
| 534 |
} |
| 535 |
} |
| 536 |
else { |
| 537 |
pylist.emplace_back(value); |
| 538 |
} |
| 539 |
|
| 540 |
values.reserve(pylist.size()); |
| 541 |
for (const auto& it : pylist) { |
| 542 |
if (it.isString()) { |
| 543 |
values.push_back(Py::String(it)); |
| 544 |
} |
| 545 |
else { |
| 546 |
std::string error = std::string("type must be str"); |
| 547 |
error += " not, "; |
| 548 |
error += it.ptr()->ob_type->tp_name; |
| 549 |
throw Py::TypeError(error); |
| 550 |
} |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
Py::Long ret(getViewProviderPtr()->partialRender(values, Base::asBoolean(clear))); |
| 555 |
return Py::new_reference_to(ret); |
| 556 |
} |
| 557 |
|
| 558 |
PyObject* ViewProviderPy::getElementColors(PyObject* args) |
| 559 |
{ |
| 560 |
const char* element = nullptr; |
| 561 |
if (!PyArg_ParseTuple(args, "|s", &element)) { |
| 562 |
return nullptr; |
| 563 |
} |
| 564 |
|
| 565 |
Py::Dict dict; |
| 566 |
for (auto& v : getViewProviderPtr()->getElementColors(element)) { |
| 567 |
auto& c = v.second; |
| 568 |
dict.setItem( |
| 569 |
Py::String(v.first), |
| 570 |
Py::TupleN(Py::Float(c.r), Py::Float(c.g), Py::Float(c.b), Py::Float(c.a)) |
| 571 |
); |
| 572 |
} |
| 573 |
return Py::new_reference_to(dict); |
| 574 |
} |
| 575 |
|
| 576 |
PyObject* ViewProviderPy::setElementColors(PyObject* args) |
| 577 |
{ |
| 578 |
PyObject* pyObj; |
| 579 |
if (!PyArg_ParseTuple(args, "O", &pyObj)) { |
| 580 |
return nullptr; |
| 581 |
} |
| 582 |
|
| 583 |
if (!PyDict_Check(pyObj)) { |
| 584 |
throw Py::TypeError("Expect a dict"); |
| 585 |
} |
| 586 |
|
| 587 |
std::map<std::string, Base::Color> colors; |
| 588 |
Py::Dict dict(pyObj); |
| 589 |
for (auto it = dict.begin(); it != dict.end(); ++it) { |
| 590 |
const auto& value = *it; |
| 591 |
if (!value.first.isString() || !value.second.isSequence()) { |
| 592 |
throw Py::TypeError("Expect the dictionary to contain items of type elementName:(r,g,b,a)"); |
| 593 |
} |
| 594 |
|
| 595 |
App::PropertyColor prop; |
| 596 |
prop.setPyObject(value.second.ptr()); |
| 597 |
colors[value.first.as_string()] = prop.getValue(); |
| 598 |
} |
| 599 |
getViewProviderPtr()->setElementColors(colors); |
| 600 |
Py_Return; |
| 601 |
} |
| 602 |
|
| 603 |
PyObject* ViewProviderPy::getElementPicked(PyObject* args) const |
| 604 |
{ |
| 605 |
PyObject* obj; |
| 606 |
if (!PyArg_ParseTuple(args, "O", &obj)) { |
| 607 |
return nullptr; |
| 608 |
} |
| 609 |
|
| 610 |
void* ptr = nullptr; |
| 611 |
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "_p_SoPickedPoint", obj, &ptr, 0); |
| 612 |
auto pp = static_cast<SoPickedPoint*>(ptr); |
| 613 |
if (!pp) { |
| 614 |
throw Base::TypeError("type must be coin.SoPickedPoint"); |
| 615 |
} |
| 616 |
|
| 617 |
std::string name; |
| 618 |
if (!getViewProviderPtr()->getElementPicked(pp, name)) { |
| 619 |
Py_Return; |
| 620 |
} |
| 621 |
|
| 622 |
return Py::new_reference_to(Py::String(name)); |
| 623 |
} |
| 624 |
|
| 625 |
PyObject* ViewProviderPy::getDetailPath(PyObject* args) const |
| 626 |
{ |
| 627 |
const char* sub; |
| 628 |
PyObject* path; |
| 629 |
PyObject* append = Py_True; |
| 630 |
if (!PyArg_ParseTuple(args, "sO|O!", &sub, &path, &PyBool_Type, &append)) { |
| 631 |
return nullptr; |
| 632 |
} |
| 633 |
|
| 634 |
void* ptr = nullptr; |
| 635 |
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "_p_SoPath", path, &ptr, 0); |
| 636 |
auto pPath = static_cast<SoPath*>(ptr); |
| 637 |
if (!pPath) { |
| 638 |
throw Base::TypeError("'path' must be a coin.SoPath"); |
| 639 |
} |
| 640 |
SoDetail* det = nullptr; |
| 641 |
if (!getViewProviderPtr()->getDetailPath(sub, static_cast<SoFullPath*>(pPath), append, det)) { |
| 642 |
delete det; |
| 643 |
Py_Return; |
| 644 |
} |
| 645 |
if (!det) { |
| 646 |
Py_Return; |
| 647 |
} |
| 648 |
return Base::Interpreter() |
| 649 |
.createSWIGPointerObj("pivy.coin", "_p_SoDetail", static_cast<void*>(det), 0); |
| 650 |
} |
| 651 |
|
| 652 |
PyObject* ViewProviderPy::signalChangeIcon(PyObject* args) const |
| 653 |
{ |
| 654 |
if (!PyArg_ParseTuple(args, "")) { |
| 655 |
return nullptr; |
| 656 |
} |
| 657 |
|
| 658 |
getViewProviderPtr()->signalChangeIcon(); |
| 659 |
Py_Return; |
| 660 |
} |
| 661 |
|
| 662 |
PyObject* ViewProviderPy::getBoundingBox(PyObject* args) |
| 663 |
{ |
| 664 |
PyObject* transform = Py_True; |
| 665 |
PyObject* pyView = nullptr; |
| 666 |
const char* subname = nullptr; |
| 667 |
if (!PyArg_ParseTuple( |
| 668 |
args, |
| 669 |
"|sO!O!", |
| 670 |
&subname, |
| 671 |
&PyBool_Type, |
| 672 |
&transform, |
| 673 |
View3DInventorPy::type_object(), |
| 674 |
&pyView |
| 675 |
)) { |
| 676 |
return nullptr; |
| 677 |
} |
| 678 |
|
| 679 |
PY_TRY |
| 680 |
{ |
| 681 |
View3DInventor* view = nullptr; |
| 682 |
if (pyView) { |
| 683 |
view = static_cast<View3DInventorPy*>(pyView)->getView3DInventorPtr(); |
| 684 |
} |
| 685 |
auto bbox = getViewProviderPtr()->getBoundingBox(subname, Base::asBoolean(transform), view); |
| 686 |
return new Base::BoundBoxPy(new Base::BoundBox3d(bbox)); |
| 687 |
} |
| 688 |
PY_CATCH; |
| 689 |
} |
| 690 |
|
| 691 |
PyObject* ViewProviderPy::doubleClicked(PyObject* args) |
| 692 |
{ |
| 693 |
if (!PyArg_ParseTuple(args, "")) { |
| 694 |
return nullptr; |
| 695 |
} |
| 696 |
|
| 697 |
PY_TRY |
| 698 |
{ |
| 699 |
return Py::new_reference_to(Py::Boolean(getViewProviderPtr()->doubleClicked())); |
| 700 |
} |
| 701 |
PY_CATCH; |
| 702 |
} |
| 703 |
|
| 704 |
PyObject* ViewProviderPy::getCustomAttributes(const char* attr) const |
| 705 |
{ |
| 706 |
// search for dynamic property |
| 707 |
App::Property* prop = getViewProviderPtr()->getDynamicPropertyByName(attr); |
| 708 |
if (prop) { |
| 709 |
return prop->getPyObject(); |
| 710 |
} |
| 711 |
else { |
| 712 |
return nullptr; |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
int ViewProviderPy::setCustomAttributes(const char* attr, PyObject* value) |
| 717 |
{ |
| 718 |
// search for dynamic property |
| 719 |
try { |
| 720 |
App::Property* prop = getViewProviderPtr()->getDynamicPropertyByName(attr); |
| 721 |
if (prop) { |
| 722 |
prop->setPyObject(value); |
| 723 |
return 1; |
| 724 |
} |
| 725 |
return 0; |
| 726 |
} |
| 727 |
catch (Base::Exception& exc) { |
| 728 |
PyErr_Format(PyExc_AttributeError, "Attribute (Name: %s) error: '%s' ", attr, exc.what()); |
| 729 |
return -1; |
| 730 |
} |
| 731 |
catch (...) { |
| 732 |
PyErr_Format(PyExc_AttributeError, "Unknown error in attribute %s", attr); |
| 733 |
return -1; |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
Py::Object ViewProviderPy::getAnnotation() const |
| 738 |
{ |
| 739 |
try { |
| 740 |
auto node = getViewProviderPtr()->getAnnotation(); |
| 741 |
PyObject* Ptr |
| 742 |
= Base::Interpreter().createSWIGPointerObj("pivy.coin", "_p_SoSeparator", node, 1); |
| 743 |
node->ref(); |
| 744 |
return Py::Object(Ptr, true); |
| 745 |
} |
| 746 |
catch (const Base::Exception& e) { |
| 747 |
throw Py::RuntimeError(e.what()); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
void ViewProviderPy::setAnnotation(Py::Object) |
| 752 |
{} |
| 753 |
|
| 754 |
Py::Object ViewProviderPy::getRootNode() const |
| 755 |
{ |
| 756 |
try { |
| 757 |
SoSeparator* node = getViewProviderPtr()->getRoot(); |
| 758 |
PyObject* Ptr |
| 759 |
= Base::Interpreter().createSWIGPointerObj("pivy.coin", "_p_SoSeparator", node, 1); |
| 760 |
node->ref(); |
| 761 |
return Py::Object(Ptr, true); |
| 762 |
} |
| 763 |
catch (const Base::Exception& e) { |
| 764 |
throw Py::RuntimeError(e.what()); |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
void ViewProviderPy::setRootNode(Py::Object) |
| 769 |
{} |
| 770 |
|
| 771 |
Py::Object ViewProviderPy::getSwitchNode() const |
| 772 |
{ |
| 773 |
try { |
| 774 |
SoSwitch* node = getViewProviderPtr()->getModeSwitch(); |
| 775 |
PyObject* Ptr = Base::Interpreter().createSWIGPointerObj("pivy.coin", "_p_SoSwitch", node, 1); |
| 776 |
node->ref(); |
| 777 |
return Py::Object(Ptr, true); |
| 778 |
} |
| 779 |
catch (const Base::Exception& e) { |
| 780 |
throw Py::RuntimeError(e.what()); |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
void ViewProviderPy::setSwitchNode(Py::Object) |
| 785 |
{} |
| 786 |
|
| 787 |
Py::String ViewProviderPy::getIV() const |
| 788 |
{ |
| 789 |
std::string buf = Gui::SoFCDB::writeNodesToString(getViewProviderPtr()->getRoot()); |
| 790 |
return {buf}; |
| 791 |
} |
| 792 |
|
| 793 |
Py::Object ViewProviderPy::getIcon() const |
| 794 |
{ |
| 795 |
PythonWrapper wrap; |
| 796 |
wrap.loadGuiModule(); |
| 797 |
wrap.loadWidgetsModule(); |
| 798 |
QIcon icon = getViewProviderPtr()->getIcon(); |
| 799 |
return wrap.fromQIcon(new QIcon(icon)); |
| 800 |
} |
| 801 |
|
| 802 |
Py::Long ViewProviderPy::getDefaultMode() const |
| 803 |
{ |
| 804 |
return Py::Long((long)getViewProviderPtr()->getDefaultMode()); |
| 805 |
} |
| 806 |
|
| 807 |
void ViewProviderPy::setDefaultMode(Py::Long arg) |
| 808 |
{ |
| 809 |
return getViewProviderPtr()->setDefaultMode(arg); |
| 810 |
} |
| 811 |
|
| 812 |
Py::Boolean ViewProviderPy::getCanRemoveChildrenFromRoot() const |
| 813 |
{ |
| 814 |
return {getViewProviderPtr()->canRemoveChildrenFromRoot()}; |
| 815 |
} |
| 816 |
|
| 817 |
Py::Boolean ViewProviderPy::getLinkVisibility() const |
| 818 |
{ |
| 819 |
return {getViewProviderPtr()->isLinkVisible()}; |
| 820 |
} |
| 821 |
|
| 822 |
void ViewProviderPy::setLinkVisibility(Py::Boolean arg) |
| 823 |
{ |
| 824 |
getViewProviderPtr()->setLinkVisible(arg); |
| 825 |
} |
| 826 |
|
| 827 |
Py::String ViewProviderPy::getDropPrefix() const |
| 828 |
{ |
| 829 |
return {getViewProviderPtr()->getDropPrefix()}; |
| 830 |
} |
| 831 |
|
| 832 |
void ViewProviderPy::setToggleVisibility(Py::Object arg) |
| 833 |
{ |
| 834 |
std::string val; |
| 835 |
|
| 836 |
if (PyObject_HasAttrString(arg.ptr(), "value")) { |
| 837 |
// we are dealing with the enum |
| 838 |
val = Py::String(arg.getAttr("value")); |
| 839 |
} |
| 840 |
else { |
| 841 |
// we are dealing with a string |
| 842 |
val = Py::String(arg); |
| 843 |
} |
| 844 |
|
| 845 |
if (val == "CanToggleVisibility") { |
| 846 |
getViewProviderPtr()->setToggleVisibility( |
| 847 |
ViewProvider::ToggleVisibilityMode::CanToggleVisibility |
| 848 |
); |
| 849 |
} |
| 850 |
else if (val == "NoToggleVisibility") { |
| 851 |
getViewProviderPtr()->setToggleVisibility( |
| 852 |
ViewProvider::ToggleVisibilityMode::NoToggleVisibility |
| 853 |
); |
| 854 |
} |
| 855 |
else { |
| 856 |
throw Py::ValueError( |
| 857 |
"Invalid ToggleVisibility mode. Use 'CanToggleVisibility' or 'NoToggleVisibility'." |
| 858 |
); |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
Py::Object ViewProviderPy::getToggleVisibility() const |
| 863 |
{ |
| 864 |
bool canToggleVisibility = getViewProviderPtr()->canToggleVisibility(); |
| 865 |
|
| 866 |
return Py::String(canToggleVisibility ? "CanToggleVisibility" : "NoToggleVisibility"); |
| 867 |
} |