23
#include "PreCompiled.h"
25
#include <Base/BoundBoxPy.h>
26
#include <Base/MatrixPy.h>
27
#include <App/DocumentObjectPy.h>
28
#include <App/MaterialPy.h>
30
#include "LinkViewPy.h"
31
#include "LinkViewPy.cpp"
32
#include "ViewProviderDocumentObjectPy.h"
37
PyObject *LinkViewPy::PyMake(struct _typeobject *, PyObject *, PyObject *)
39
return new LinkViewPy(new LinkView);
42
int LinkViewPy::PyInit(PyObject* , PyObject* )
49
std::string LinkViewPy::representation() const
54
PyObject* LinkViewPy::reset(PyObject *args) {
55
if (!PyArg_ParseTuple(args, ""))
59
auto lv = getLinkViewPtr();
66
PyObject* LinkViewPy::setMaterial(PyObject *args) {
68
if (!PyArg_ParseTuple(args, "O", &pyObj))
72
auto lv = getLinkViewPtr();
73
if(pyObj == Py_None) {
74
lv->setMaterial(-1,nullptr);
77
if(PyObject_TypeCheck(pyObj,&App::MaterialPy::Type)) {
78
lv->setMaterial(-1,static_cast<App::MaterialPy*>(pyObj)->getMaterialPtr());
81
if(PyDict_Check(pyObj)) {
82
PyObject *key, *value;
84
std::map<int,App::Material*> materials;
85
while(PyDict_Next(pyObj, &pos, &key, &value)) {
88
materials[(int)idx] = nullptr;
89
else if(!PyObject_TypeCheck(value,&App::MaterialPy::Type)) {
90
PyErr_SetString(PyExc_TypeError, "exepcting a type of material");
93
materials[(int)idx] = static_cast<App::MaterialPy*>(value)->getMaterialPtr();
95
for(auto &v : materials)
96
lv->setMaterial(v.first,v.second);
99
if(PySequence_Check(pyObj)) {
100
Py::Sequence seq(pyObj);
101
std::vector<App::Material*> materials;
102
materials.resize(seq.size(),nullptr);
103
for(Py_ssize_t i=0;i<seq.size();++i) {
104
PyObject* item = seq[i].ptr();
105
if(item == Py_None) continue;
106
if(!PyObject_TypeCheck(item,&App::MaterialPy::Type)) {
107
PyErr_SetString(PyExc_TypeError, "exepcting a type of material");
110
materials[i] = static_cast<App::MaterialPy*>(item)->getMaterialPtr();
112
for(size_t i=0;i<materials.size();++i)
113
lv->setMaterial(i,materials[i]);
117
PyErr_SetString(PyExc_TypeError, "exepcting a type of Material, [Material,...] or {Int:Material,}");
122
PyObject* LinkViewPy::setTransform(PyObject *args) {
124
if (!PyArg_ParseTuple(args, "O", &pyObj))
128
auto lv = getLinkViewPtr();
129
if(PyObject_TypeCheck(pyObj,&Base::MatrixPy::Type)) {
130
lv->setTransform(-1,*static_cast<Base::MatrixPy*>(pyObj)->getMatrixPtr());
133
if(PyDict_Check(pyObj)) {
134
PyObject *key, *value;
136
std::map<int,Base::Matrix4D*> mat;
137
while(PyDict_Next(pyObj, &pos, &key, &value)) {
139
if(!PyObject_TypeCheck(value,&Base::MatrixPy::Type)) {
140
PyErr_SetString(PyExc_TypeError, "exepcting a type of Matrix");
143
mat[(int)idx] = static_cast<Base::MatrixPy*>(value)->getMatrixPtr();
146
lv->setTransform(v.first,*v.second);
149
if(PySequence_Check(pyObj)) {
150
Py::Sequence seq(pyObj);
151
std::vector<Base::Matrix4D*> mat;
152
mat.resize(seq.size(),nullptr);
153
for(Py_ssize_t i=0;i<seq.size();++i) {
154
PyObject* item = seq[i].ptr();
155
if(!PyObject_TypeCheck(item,&Base::MatrixPy::Type)) {
156
PyErr_SetString(PyExc_TypeError, "exepcting a type of Matrix");
159
mat[i] = static_cast<Base::MatrixPy*>(item)->getMatrixPtr();
161
for(size_t i=0;i<mat.size();++i)
162
lv->setTransform(i,*mat[i]);
166
PyErr_SetString(PyExc_TypeError, "exepcting a type of Matrix, [Matrix,...] or {Int:Matrix,...}");
171
PyObject* LinkViewPy::setType(PyObject *args) {
173
PyObject *sublink = Py_True;
174
if (!PyArg_ParseTuple(args, "h|O!", &type, &PyBool_Type, &sublink))
178
getLinkViewPtr()->setNodeType((LinkView::SnapshotType)type, Base::asBoolean(sublink));
183
PyObject* LinkViewPy::setChildren(PyObject *args) {
185
PyObject *pyVis = Py_None;
187
if (!PyArg_ParseTuple(args, "O|Os",&pyObj,&pyVis,&type))
191
App::PropertyBoolList vis;
192
App::PropertyLinkList links;
194
links.setPyObject(pyObj);
196
vis.setPyObject(pyVis);
197
getLinkViewPtr()->setChildren(links.getValue(),vis.getValue(),static_cast<LinkView::SnapshotType>(type));
202
PyObject* LinkViewPy::setLink(PyObject *args)
205
PyObject *pySubName = Py_None;
206
if (!PyArg_ParseTuple(args, "O|O",&pyObj,&pySubName))
210
ViewProviderDocumentObject *vpd = nullptr;
211
App::DocumentObject *obj = nullptr;
213
if(PyObject_TypeCheck(pyObj,&App::DocumentObjectPy::Type))
214
obj = static_cast<App::DocumentObjectPy*>(pyObj)->getDocumentObjectPtr();
215
else if(PyObject_TypeCheck(pyObj,&ViewProviderDocumentObjectPy::Type))
216
vpd = static_cast<ViewProviderDocumentObjectPy*>(pyObj)->getViewProviderDocumentObjectPtr();
218
PyErr_SetString(PyExc_TypeError,
219
"exepcting a type of DocumentObject or ViewProviderDocumentObject");
225
App::PropertyStringList prop;
226
if(pySubName!=Py_None)
227
prop.setPyObject(pySubName);
230
getLinkViewPtr()->setLink(obj,prop.getValue());
232
getLinkViewPtr()->setLinkViewObject(vpd,prop.getValue());
237
Py::Object LinkViewPy::getOwner() const {
238
auto owner = getLinkViewPtr()->getOwner();
241
return Py::Object(owner->getPyObject(),true);
244
void LinkViewPy::setOwner(Py::Object owner) {
245
ViewProviderDocumentObject *vp = nullptr;
246
if(!owner.isNone()) {
247
if(!PyObject_TypeCheck(owner.ptr(),&ViewProviderDocumentObjectPy::Type))
248
throw Py::TypeError("exepcting the owner to be of ViewProviderDocumentObject");
249
vp = static_cast<ViewProviderDocumentObjectPy*>(
250
owner.ptr())->getViewProviderDocumentObjectPtr();
252
getLinkViewPtr()->setOwner(vp);
255
Py::Object LinkViewPy::getLinkedView() const {
256
auto linked = getLinkViewPtr()->getLinkedView();
259
return Py::Object(linked->getPyObject(),true);
262
Py::Object LinkViewPy::getSubNames() const {
263
const auto &subs = getLinkViewPtr()->getSubNames();
266
Py::Tuple ret(subs.size());
269
ret.setItem(i++,Py::String(s.c_str()));
273
PyObject* LinkViewPy::getElementPicked(PyObject* args)
276
if (!PyArg_ParseTuple(args, "O",&obj))
279
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoPickedPoint *", obj, &ptr, 0);
280
auto pp = static_cast<SoPickedPoint*>(ptr);
282
throw Py::TypeError("type must be of coin.SoPickedPoint");
285
if(!getLinkViewPtr()->linkGetElementPicked(pp,name))
287
return Py::new_reference_to(Py::String(name));
291
PyObject* LinkViewPy::getDetailPath(PyObject* args)
295
if (!PyArg_ParseTuple(args, "sO",&sub,&path))
298
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoPath *", path, &ptr, 0);
299
auto pPath = static_cast<SoPath*>(ptr);
301
throw Py::TypeError("type must be of coin.SoPath");
303
SoDetail *det = nullptr;
304
getLinkViewPtr()->linkGetDetailPath(sub,static_cast<SoFullPath*>(pPath),det);
307
return Base::Interpreter().createSWIGPointerObj("pivy.coin", "SoDetail *", static_cast<void*>(det), 0);
311
PyObject* LinkViewPy::getBoundBox(PyObject* args)
313
PyObject *vobj = Py_None;
314
if (!PyArg_ParseTuple(args, "O",&vobj))
318
Base::PyTypeCheck(&vobj, &ViewProviderDocumentObjectPy::Type);
319
ViewProviderDocumentObject *vpd = nullptr;
321
vpd = static_cast<ViewProviderDocumentObjectPy*>(vobj)->getViewProviderDocumentObjectPtr();
323
auto bbox = getLinkViewPtr()->getBoundBox(vpd);
324
Py::Object ret(new Base::BoundBoxPy(new Base::BoundBox3d(bbox)));
325
return Py::new_reference_to(ret);
330
PyObject *LinkViewPy::getCustomAttributes(const char*) const
335
int LinkViewPy::setCustomAttributes(const char*, PyObject*)
340
Py::Object LinkViewPy::getRootNode() const
343
SoNode* node = getLinkViewPtr()->getLinkRoot();
344
PyObject* Ptr = Base::Interpreter().createSWIGPointerObj("pivy.coin","SoSeparator *", node, 1);
346
return Py::Object(Ptr, true);
348
catch (const Base::Exception& e) {
349
throw Py::RuntimeError(e.what());
353
Py::Object LinkViewPy::getVisibilities() const {
354
auto linked = getLinkViewPtr();
355
if(!linked->getSize())
357
Py::Tuple ret(linked->getSize());
358
for(int i=0;i<linked->getSize();++i)
359
ret.setItem(i,Py::Boolean(linked->isElementVisible(i)));
363
void LinkViewPy::setVisibilities(Py::Object value) {
364
App::PropertyBoolList v;
366
v.setPyObject(value.ptr());
368
auto linked = getLinkViewPtr();
369
const auto &vis = v.getValue();
370
for(int i=0;i<linked->getSize();++i)
371
linked->setElementVisible(i,i>=(int)vis.size()||vis[i]);
374
PyObject* LinkViewPy::getChildren(PyObject *args) {
375
if (!PyArg_ParseTuple(args, ""))
377
auto children = getLinkViewPtr()->getChildren();
380
Py::Tuple ret(children.size());
382
for(auto vp : children)
383
ret.setItem(i++,Py::Object(vp->getPyObject(),true));
384
return Py::new_reference_to(ret);
387
Py::Int LinkViewPy::getCount() const {
388
return Py::Int(getLinkViewPtr()->getSize());
391
void LinkViewPy::setCount(Py::Int count) {
393
getLinkViewPtr()->setSize((int)count);
394
} catch (const Base::Exception& e) {
395
throw Py::RuntimeError(e.what());