1
/***************************************************************************
2
* Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
# include <Inventor/SbRotation.h>
26
# include <Inventor/SoFullPath.h>
27
# include <Inventor/details/SoDetail.h>
28
# include <Inventor/nodes/SoSeparator.h>
29
# include <Inventor/nodes/SoSwitch.h>
31
# include <QDataStream>
34
#include <Base/BoundBoxPy.h>
35
#include <Base/PyWrapParseTupleAndKeywords.h>
37
#include "PythonWrapper.h"
40
// inclusion of the generated files (generated out of ViewProviderPy.xml)
41
#include <Gui/ViewProviderPy.h>
42
#include <Gui/ViewProviderPy.cpp>
43
#include <Gui/View3DPy.h>
44
#include <Gui/View3DInventor.h>
45
#include <Base/Interpreter.h>
46
#include <Base/Matrix.h>
47
#include <Base/MatrixPy.h>
48
#include <Base/Placement.h>
49
#include <Base/PlacementPy.h>
50
#include <App/Document.h>
51
#include <App/DocumentObject.h>
52
#include <App/DocumentObjectPy.h>
57
// returns a string which represent the object e.g. when printed in python
58
std::string ViewProviderPy::representation() const
60
return "<View provider object>";
63
PyObject* ViewProviderPy::addProperty(PyObject *args)
65
char *sType,*sName=nullptr,*sGroup=nullptr,*sDoc=nullptr;
68
PyObject *ro = Py_False, *hd = Py_False;
69
if (!PyArg_ParseTuple(args, "s|ssethO!O!", &sType,&sName,&sGroup,"utf-8",&sDoc,&attr,
70
&PyBool_Type, &ro, &PyBool_Type, &hd))
78
App::Property* prop=nullptr;
80
prop = getViewProviderPtr()->addDynamicProperty(sType,sName,sGroup,sDocStr.c_str(),attr,
81
Base::asBoolean(ro), Base::asBoolean(hd));
83
catch (const Base::Exception& e) {
84
throw Py::RuntimeError(e.what());
87
std::stringstream str;
88
str << "No property found of type '" << sType << "'" << std::ends;
89
throw Py::TypeError(str.str());
92
return Py::new_reference_to(this);
95
PyObject* ViewProviderPy::removeProperty(PyObject *args)
98
if (!PyArg_ParseTuple(args, "s", &sName))
102
bool ok = getViewProviderPtr()->removeDynamicProperty(sName);
103
return Py_BuildValue("O", (ok ? Py_True : Py_False));
105
catch (const Base::Exception& e) {
106
throw Py::RuntimeError(e.what());
110
PyObject* ViewProviderPy::supportedProperties(PyObject *args)
112
if (!PyArg_ParseTuple(args, ""))
115
std::vector<Base::Type> ary;
116
Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary);
118
for (auto & it : ary) {
119
auto data = static_cast<Base::BaseClass*>(it.createInstance());
122
res.append(Py::String(it.getName()));
125
return Py::new_reference_to(res);
128
PyObject* ViewProviderPy::show(PyObject *args)
130
if (!PyArg_ParseTuple(args, ""))
134
getViewProviderPtr()->show();
140
PyObject* ViewProviderPy::hide(PyObject *args)
142
if (!PyArg_ParseTuple(args, ""))
146
getViewProviderPtr()->hide();
152
PyObject* ViewProviderPy::isVisible(PyObject *args)
154
if (!PyArg_ParseTuple(args, ""))
158
return Py::new_reference_to(Py::Boolean(getViewProviderPtr()->isShow()));
163
PyObject* ViewProviderPy::canDragObject(PyObject *args)
165
PyObject *obj = Py_None;
166
if (!PyArg_ParseTuple(args, "|O", &obj))
170
Base::PyTypeCheck(&obj, &App::DocumentObjectPy::Type);
173
ret = getViewProviderPtr()->canDragObjects();
175
ret = getViewProviderPtr()->canDragObject(
176
static_cast<App::DocumentObjectPy*>(obj)->getDocumentObjectPtr());
178
return Py::new_reference_to(Py::Boolean(ret));
183
PyObject* ViewProviderPy::canDropObject(PyObject *args, PyObject *kw)
185
PyObject *obj = Py_None;
186
PyObject *owner = Py_None;
187
PyObject *pyElements = Py_None;
188
const char *subname = nullptr;
189
static const std::array<const char *, 5> kwlist{"obj", "owner", "subname", "elem", nullptr};
190
if (!Base::Wrapped_ParseTupleAndKeywords(args, kw, "|OOsO", kwlist,
191
&obj, &owner, &subname, &pyElements)) {
196
Base::PyTypeCheck(&obj, &App::DocumentObjectPy::Type, "expecting 'obj' to be of type App.DocumentObject or None");
197
Base::PyTypeCheck(&owner, &App::DocumentObjectPy::Type, "expecting 'owner' to be of type App.DocumentObject or None");
198
Base::PyTypeCheck(&pyElements, PySequence_Check, "expecting 'elem' to be sequence or None");
201
App::DocumentObject* pcObject;
202
App::DocumentObject* pcOwner = nullptr;
203
App::PropertyStringList elements;
204
if (!obj && (owner || pyElements || subname)) {
205
PyErr_SetString(PyExc_ValueError, "'obj' must be specified if 'owner', 'subname' or 'elem' is given");
209
ret = getViewProviderPtr()->canDropObjects();
210
return Py::new_reference_to(Py::Boolean(ret));
212
pcObject = static_cast<App::DocumentObjectPy*>(obj)->getDocumentObjectPtr();
214
pcOwner = static_cast<App::DocumentObjectPy*>(owner)->getDocumentObjectPtr();
217
elements.setPyObject(pyElements);
220
PyErr_SetString(PyExc_TypeError, "'elem' must be a sequence of strings");
224
ret = getViewProviderPtr()->canDropObjectEx(pcObject,pcOwner,subname,elements.getValues());
225
return Py::new_reference_to(Py::Boolean(ret));
230
PyObject* ViewProviderPy::canDragAndDropObject(PyObject *args)
233
if (!PyArg_ParseTuple(args, "O!", &App::DocumentObjectPy::Type,&obj))
237
bool ret = getViewProviderPtr()->canDragAndDropObject(
238
static_cast<App::DocumentObjectPy*>(obj)->getDocumentObjectPtr());
239
return Py::new_reference_to(Py::Boolean(ret));
244
PyObject* ViewProviderPy::dropObject(PyObject *args, PyObject *kw)
247
PyObject *owner = Py_None;
248
PyObject *pyElements = Py_None;
249
const char *subname = nullptr;
250
static const std::array<const char *, 5> kwlist{"obj", "owner", "subname", "elem", nullptr};
251
if (!Base::Wrapped_ParseTupleAndKeywords(args, kw, "O!|OsO", kwlist,
252
&App::DocumentObjectPy::Type, &obj, &owner, &subname, &pyElements)) {
257
Base::PyTypeCheck(&owner, &App::DocumentObjectPy::Type, "expecting 'owner' to be of type App.DocumentObject or None");
258
Base::PyTypeCheck(&pyElements, PySequence_Check, "expecting 'elem' to be sequence or None");
260
auto pcObject = static_cast<App::DocumentObjectPy*>(obj)->getDocumentObjectPtr();
261
App::DocumentObject *pcOwner = nullptr;
262
App::PropertyStringList elements;
264
pcOwner = static_cast<App::DocumentObjectPy*>(owner)->getDocumentObjectPtr();
267
elements.setPyObject(pyElements);
270
PyErr_SetString(PyExc_TypeError, "'elem' must be a sequence of strings");
274
auto ret = getViewProviderPtr()->dropObjectEx(pcObject,pcOwner, subname,elements.getValues());
275
return Py::new_reference_to(Py::String(ret));
280
PyObject* ViewProviderPy::dragObject(PyObject *args)
283
if (!PyArg_ParseTuple(args, "O!", &App::DocumentObjectPy::Type,&obj))
287
getViewProviderPtr()->dragObject(
288
static_cast<App::DocumentObjectPy*>(obj)->getDocumentObjectPtr());
294
PyObject* ViewProviderPy::replaceObject(PyObject *args)
298
if (!PyArg_ParseTuple(args, "O!O!",
299
&App::DocumentObjectPy::Type,&oldObj,
300
&App::DocumentObjectPy::Type,&newObj))
304
int ret = getViewProviderPtr()->replaceObject(
305
static_cast<App::DocumentObjectPy*>(oldObj)->getDocumentObjectPtr(),
306
static_cast<App::DocumentObjectPy*>(newObj)->getDocumentObjectPtr());
307
return Py::new_reference_to(Py::Int(ret));
312
PyObject* ViewProviderPy::addDisplayMode(PyObject * args)
316
if (!PyArg_ParseTuple(args, "Os", &obj, &mode))
321
Base::Interpreter().convertSWIGPointerObj("pivy.coin","_p_SoNode", obj, &ptr, 0);
323
catch (const Base::Exception& e) {
324
PyErr_SetString(PyExc_RuntimeError, e.what());
329
auto node = static_cast<SoNode*>(ptr);
330
getViewProviderPtr()->addDisplayMaskMode(node,mode);
336
PyObject* ViewProviderPy::listDisplayModes(PyObject *args)
338
if (!PyArg_ParseTuple(args, ""))
342
std::vector<std::string> modes = getViewProviderPtr()->getDisplayModes();
343
PyObject* pyList = PyList_New(modes.size());
346
for (const auto & mode : modes) {
347
PyObject* str = PyUnicode_FromString(mode.c_str());
348
PyList_SetItem(pyList, i++, str);
356
PyObject* ViewProviderPy::toString(PyObject *args)
358
if (!PyArg_ParseTuple(args, ""))
362
std::string buffer = getViewProviderPtr()->toString();
363
return Py::new_reference_to(Py::String(buffer));
368
PyObject* ViewProviderPy::setTransformation(PyObject *args)
372
if (PyArg_ParseTuple(args, "O!",&(Base::MatrixPy::Type),&p)) {
373
mat = *static_cast<Base::MatrixPy*>(p)->getMatrixPtr();
374
getViewProviderPtr()->setTransformation(mat);
379
if (PyArg_ParseTuple(args, "O!",&(Base::PlacementPy::Type),&p)) {
380
auto plc = static_cast<Base::PlacementPy*>(p);
381
getViewProviderPtr()->setTransformation(plc->getPlacementPtr()->toMatrix());
385
PyErr_SetString(PyExc_TypeError, "The transformation must be a Base.Matrix or a Base.Placement");
389
PyObject* ViewProviderPy::claimChildren(PyObject* args)
391
if (!PyArg_ParseTuple(args, ""))
394
std::vector<App::DocumentObject*> children = this->getViewProviderPtr()->claimChildren();
396
for(auto* child : children){
398
ret.append(Py::asObject(child->getPyObject()));
400
ret.append(Py::None());
402
return Py::new_reference_to(ret);
405
PyObject* ViewProviderPy::claimChildrenRecursive(PyObject* args)
407
if (!PyArg_ParseTuple(args, ""))
410
std::vector<App::DocumentObject*> children = this->getViewProviderPtr()->claimChildrenRecursive();
412
for(auto* child : children){
414
ret.append(Py::asObject(child->getPyObject()));
416
ret.append(Py::None());
418
return Py::new_reference_to(ret);
421
PyObject* ViewProviderPy::partialRender(PyObject* args)
423
PyObject *value = Py_None;
424
PyObject *clear = Py_False;
425
if (!PyArg_ParseTuple(args, "|OO!",&value,&PyBool_Type,&clear))
428
std::vector<std::string> values;
429
if (value != Py_None) {
430
std::vector<Py::Object> pylist;
431
if (PyList_Check(value) || PyTuple_Check(value)) {
432
Py::Sequence seq(value);
433
for (const auto& it : seq) {
434
pylist.emplace_back(it);
438
pylist.emplace_back(value);
441
values.reserve(pylist.size());
442
for (const auto& it : pylist) {
444
values.push_back(Py::String(it));
447
std::string error = std::string("type must be str");
449
error += it.ptr()->ob_type->tp_name;
450
throw Py::TypeError(error);
455
Py::Int ret(getViewProviderPtr()->partialRender(values, Base::asBoolean(clear)));
456
return Py::new_reference_to(ret);
459
PyObject* ViewProviderPy::getElementColors(PyObject* args)
461
const char *element = nullptr;
462
if (!PyArg_ParseTuple(args, "|s", &element))
466
for(auto &v : getViewProviderPtr()->getElementColors(element)) {
468
dict.setItem(Py::String(v.first),
469
Py::TupleN(Py::Float(c.r),Py::Float(c.g),Py::Float(c.b),Py::Float(c.a)));
471
return Py::new_reference_to(dict);
474
PyObject* ViewProviderPy::setElementColors(PyObject* args)
477
if (!PyArg_ParseTuple(args, "O", &pyObj))
480
if(!PyDict_Check(pyObj))
481
throw Py::TypeError("Expect a dict");
483
std::map<std::string,App::Color> colors;
484
Py::Dict dict(pyObj);
485
for(auto it=dict.begin();it!=dict.end();++it) {
486
const auto &value = *it;
487
if(!value.first.isString() || !value.second.isSequence())
488
throw Py::TypeError("Expect the dictionary to contain items of type elementName:(r,g,b,a)");
490
App::PropertyColor prop;
491
prop.setPyObject(value.second.ptr());
492
colors[value.first.as_string()] = prop.getValue();
494
getViewProviderPtr()->setElementColors(colors);
498
PyObject* ViewProviderPy::getElementPicked(PyObject* args)
501
if (!PyArg_ParseTuple(args, "O",&obj))
505
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "_p_SoPickedPoint", obj, &ptr, 0);
506
auto pp = static_cast<SoPickedPoint*>(ptr);
508
throw Base::TypeError("type must be coin.SoPickedPoint");
511
if(!getViewProviderPtr()->getElementPicked(pp,name))
514
return Py::new_reference_to(Py::String(name));
517
PyObject* ViewProviderPy::getDetailPath(PyObject* args)
521
PyObject *append = Py_True;
522
if (!PyArg_ParseTuple(args, "sO|O!",&sub,&path,&PyBool_Type,&append))
526
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "_p_SoPath", path, &ptr, 0);
527
auto pPath = static_cast<SoPath*>(ptr);
529
throw Base::TypeError("'path' must be a coin.SoPath");
530
SoDetail *det = nullptr;
531
if(!getViewProviderPtr()->getDetailPath(sub,static_cast<SoFullPath*>(pPath),append,det)) {
537
return Base::Interpreter().createSWIGPointerObj("pivy.coin", "_p_SoDetail", static_cast<void*>(det), 0);
540
PyObject *ViewProviderPy::signalChangeIcon(PyObject *args)
542
if (!PyArg_ParseTuple(args, ""))
545
getViewProviderPtr()->signalChangeIcon();
549
PyObject *ViewProviderPy::getBoundingBox(PyObject *args) {
550
PyObject *transform=Py_True;
551
PyObject *pyView = nullptr;
552
const char *subname = nullptr;
553
if (!PyArg_ParseTuple(args, "|sO!O!", &subname,&PyBool_Type,&transform,View3DInventorPy::type_object(),&pyView))
557
View3DInventor *view = nullptr;
559
view = static_cast<View3DInventorPy*>(pyView)->getView3DIventorPtr();
560
auto bbox = getViewProviderPtr()->getBoundingBox(subname, Base::asBoolean(transform), view);
561
return new Base::BoundBoxPy(new Base::BoundBox3d(bbox));
566
PyObject *ViewProviderPy::doubleClicked(PyObject *args) {
567
if(!PyArg_ParseTuple(args, ""))
571
return Py::new_reference_to(Py::Boolean(getViewProviderPtr()->doubleClicked()));
576
PyObject *ViewProviderPy::getCustomAttributes(const char* attr) const
578
// search for dynamic property
579
App::Property* prop = getViewProviderPtr()->getDynamicPropertyByName(attr);
581
return prop->getPyObject();
586
int ViewProviderPy::setCustomAttributes(const char* attr, PyObject* value)
588
// search for dynamic property
590
App::Property* prop = getViewProviderPtr()->getDynamicPropertyByName(attr);
592
prop->setPyObject(value);
597
catch (Base::Exception &exc) {
598
PyErr_Format(PyExc_AttributeError, "Attribute (Name: %s) error: '%s' ", attr, exc.what());
602
PyErr_Format(PyExc_AttributeError, "Unknown error in attribute %s", attr);
607
Py::Object ViewProviderPy::getAnnotation() const
610
auto node = getViewProviderPtr()->getAnnotation();
611
PyObject* Ptr = Base::Interpreter().createSWIGPointerObj("pivy.coin", "_p_SoSeparator", node, 1);
613
return Py::Object(Ptr, true);
615
catch (const Base::Exception& e) {
616
throw Py::RuntimeError(e.what());
620
void ViewProviderPy::setAnnotation(Py::Object)
625
Py::Object ViewProviderPy::getRootNode() const
628
SoSeparator* node = getViewProviderPtr()->getRoot();
629
PyObject* Ptr = Base::Interpreter().createSWIGPointerObj("pivy.coin","_p_SoSeparator", node, 1);
631
return Py::Object(Ptr, true);
633
catch (const Base::Exception& e) {
634
throw Py::RuntimeError(e.what());
638
void ViewProviderPy::setRootNode(Py::Object)
643
Py::Object ViewProviderPy::getSwitchNode() const
646
SoSwitch* node = getViewProviderPtr()->getModeSwitch();
647
PyObject* Ptr = Base::Interpreter().createSWIGPointerObj("pivy.coin","_p_SoSwitch", node, 1);
649
return Py::Object(Ptr, true);
651
catch (const Base::Exception& e) {
652
throw Py::RuntimeError(e.what());
656
void ViewProviderPy::setSwitchNode(Py::Object)
661
Py::String ViewProviderPy::getIV() const
663
std::string buf = Gui::SoFCDB::writeNodesToString(getViewProviderPtr()->getRoot());
667
Py::Object ViewProviderPy::getIcon() const
670
wrap.loadGuiModule();
671
wrap.loadWidgetsModule();
672
QIcon icon = getViewProviderPtr()->getIcon();
673
return wrap.fromQIcon(new QIcon(icon));
676
Py::Int ViewProviderPy::getDefaultMode() const
678
return Py::Int((long)getViewProviderPtr()->getDefaultMode());
681
void ViewProviderPy::setDefaultMode(Py::Int arg)
683
return getViewProviderPtr()->setDefaultMode(arg);
686
Py::Boolean ViewProviderPy::getCanRemoveChildrenFromRoot() const
688
return {getViewProviderPtr()->canRemoveChildrenFromRoot()};
691
Py::Boolean ViewProviderPy::getLinkVisibility() const
693
return {getViewProviderPtr()->isLinkVisible()};
696
void ViewProviderPy::setLinkVisibility(Py::Boolean arg)
698
getViewProviderPtr()->setLinkVisible(arg);
701
Py::String ViewProviderPy::getDropPrefix() const
703
return {getViewProviderPtr()->getDropPrefix()};