| 1 |
/**************************************************************************** |
| 2 |
* Copyright (c) 2017 Zheng Lei (realthunder) <[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/BoundBoxPy.h> |
| 25 |
#include <Base/MatrixPy.h> |
| 26 |
#include <App/DocumentObjectPy.h> |
| 27 |
#include <App/MaterialPy.h> |
| 28 |
|
| 29 |
#include "LinkViewPy.h" |
| 30 |
#include "LinkViewPy.cpp" |
| 31 |
#include "ViewProviderDocumentObjectPy.h" |
| 32 |
|
| 33 |
|
| 34 |
using namespace Gui; |
| 35 |
|
| 36 |
PyObject* LinkViewPy::PyMake(struct _typeobject*, PyObject*, PyObject*) // Python wrapper |
| 37 |
{ |
| 38 |
return new LinkViewPy(new LinkView); |
| 39 |
} |
| 40 |
|
| 41 |
int LinkViewPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/) |
| 42 |
{ |
| 43 |
return 0; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
// returns a string which represent the object e.g. when printed in python |
| 48 |
std::string LinkViewPy::representation() const |
| 49 |
{ |
| 50 |
return "<Link view>"; |
| 51 |
} |
| 52 |
|
| 53 |
PyObject* LinkViewPy::reset(PyObject* args) |
| 54 |
{ |
| 55 |
if (!PyArg_ParseTuple(args, "")) { |
| 56 |
return nullptr; |
| 57 |
} |
| 58 |
|
| 59 |
PY_TRY |
| 60 |
{ |
| 61 |
auto lv = getLinkViewPtr(); |
| 62 |
lv->setSize(0); |
| 63 |
lv->setLink(nullptr); |
| 64 |
Py_Return; |
| 65 |
} |
| 66 |
PY_CATCH; |
| 67 |
} |
| 68 |
|
| 69 |
PyObject* LinkViewPy::setMaterial(PyObject* args) |
| 70 |
{ |
| 71 |
PyObject* pyObj; |
| 72 |
if (!PyArg_ParseTuple(args, "O", &pyObj)) { |
| 73 |
return nullptr; |
| 74 |
} |
| 75 |
|
| 76 |
PY_TRY |
| 77 |
{ |
| 78 |
auto lv = getLinkViewPtr(); |
| 79 |
if (pyObj == Py_None) { |
| 80 |
lv->setMaterial(-1, nullptr); |
| 81 |
Py_Return; |
| 82 |
} |
| 83 |
if (PyObject_TypeCheck(pyObj, &App::MaterialPy::Type)) { |
| 84 |
lv->setMaterial(-1, static_cast<App::MaterialPy*>(pyObj)->getMaterialPtr()); |
| 85 |
Py_Return; |
| 86 |
} |
| 87 |
if (PyDict_Check(pyObj)) { |
| 88 |
PyObject *key, *value; |
| 89 |
Py_ssize_t pos = 0; |
| 90 |
std::map<int, App::Material*> materials; |
| 91 |
while (PyDict_Next(pyObj, &pos, &key, &value)) { |
| 92 |
Py::Long idx(key); |
| 93 |
if (value == Py_None) { |
| 94 |
materials[(int)idx] = nullptr; |
| 95 |
} |
| 96 |
else if (!PyObject_TypeCheck(value, &App::MaterialPy::Type)) { |
| 97 |
PyErr_SetString(PyExc_TypeError, "exepcting a type of material"); |
| 98 |
return nullptr; |
| 99 |
} |
| 100 |
else { |
| 101 |
materials[(int)idx] = static_cast<App::MaterialPy*>(value)->getMaterialPtr(); |
| 102 |
} |
| 103 |
} |
| 104 |
for (auto& v : materials) { |
| 105 |
lv->setMaterial(v.first, v.second); |
| 106 |
} |
| 107 |
Py_Return; |
| 108 |
} |
| 109 |
if (PySequence_Check(pyObj)) { |
| 110 |
Py::Sequence seq(pyObj); |
| 111 |
std::vector<App::Material*> materials; |
| 112 |
materials.resize(seq.size(), nullptr); |
| 113 |
for (Py_ssize_t i = 0; i < seq.size(); ++i) { |
| 114 |
PyObject* item = seq[i].ptr(); |
| 115 |
if (item == Py_None) { |
| 116 |
continue; |
| 117 |
} |
| 118 |
if (!PyObject_TypeCheck(item, &App::MaterialPy::Type)) { |
| 119 |
PyErr_SetString(PyExc_TypeError, "exepcting a type of material"); |
| 120 |
return nullptr; |
| 121 |
} |
| 122 |
materials[i] = static_cast<App::MaterialPy*>(item)->getMaterialPtr(); |
| 123 |
} |
| 124 |
for (size_t i = 0; i < materials.size(); ++i) { |
| 125 |
lv->setMaterial(i, materials[i]); |
| 126 |
} |
| 127 |
Py_Return; |
| 128 |
} |
| 129 |
|
| 130 |
PyErr_SetString( |
| 131 |
PyExc_TypeError, |
| 132 |
"exepcting a type of Material, [Material,...] or {Int:Material,}" |
| 133 |
); |
| 134 |
return nullptr; |
| 135 |
} |
| 136 |
PY_CATCH; |
| 137 |
} |
| 138 |
|
| 139 |
PyObject* LinkViewPy::setTransform(PyObject* args) |
| 140 |
{ |
| 141 |
PyObject* pyObj; |
| 142 |
if (!PyArg_ParseTuple(args, "O", &pyObj)) { |
| 143 |
return nullptr; |
| 144 |
} |
| 145 |
|
| 146 |
PY_TRY |
| 147 |
{ |
| 148 |
auto lv = getLinkViewPtr(); |
| 149 |
if (PyObject_TypeCheck(pyObj, &Base::MatrixPy::Type)) { |
| 150 |
lv->setTransform(-1, *static_cast<Base::MatrixPy*>(pyObj)->getMatrixPtr()); |
| 151 |
Py_Return; |
| 152 |
} |
| 153 |
if (PyDict_Check(pyObj)) { |
| 154 |
PyObject *key, *value; |
| 155 |
Py_ssize_t pos = 0; |
| 156 |
std::map<int, Base::Matrix4D*> mat; |
| 157 |
while (PyDict_Next(pyObj, &pos, &key, &value)) { |
| 158 |
Py::Long idx(key); |
| 159 |
if (!PyObject_TypeCheck(value, &Base::MatrixPy::Type)) { |
| 160 |
PyErr_SetString(PyExc_TypeError, "exepcting a type of Matrix"); |
| 161 |
return nullptr; |
| 162 |
} |
| 163 |
else { |
| 164 |
mat[(int)idx] = static_cast<Base::MatrixPy*>(value)->getMatrixPtr(); |
| 165 |
} |
| 166 |
} |
| 167 |
for (auto& v : mat) { |
| 168 |
lv->setTransform(v.first, *v.second); |
| 169 |
} |
| 170 |
Py_Return; |
| 171 |
} |
| 172 |
if (PySequence_Check(pyObj)) { |
| 173 |
Py::Sequence seq(pyObj); |
| 174 |
std::vector<Base::Matrix4D*> mat; |
| 175 |
mat.resize(seq.size(), nullptr); |
| 176 |
for (Py_ssize_t i = 0; i < seq.size(); ++i) { |
| 177 |
PyObject* item = seq[i].ptr(); |
| 178 |
if (!PyObject_TypeCheck(item, &Base::MatrixPy::Type)) { |
| 179 |
PyErr_SetString(PyExc_TypeError, "exepcting a type of Matrix"); |
| 180 |
return nullptr; |
| 181 |
} |
| 182 |
mat[i] = static_cast<Base::MatrixPy*>(item)->getMatrixPtr(); |
| 183 |
} |
| 184 |
for (size_t i = 0; i < mat.size(); ++i) { |
| 185 |
lv->setTransform(i, *mat[i]); |
| 186 |
} |
| 187 |
Py_Return; |
| 188 |
} |
| 189 |
|
| 190 |
PyErr_SetString(PyExc_TypeError, "exepcting a type of Matrix, [Matrix,...] or {Int:Matrix,...}"); |
| 191 |
return nullptr; |
| 192 |
} |
| 193 |
PY_CATCH; |
| 194 |
} |
| 195 |
|
| 196 |
PyObject* LinkViewPy::setType(PyObject* args) |
| 197 |
{ |
| 198 |
short type; |
| 199 |
PyObject* sublink = Py_True; |
| 200 |
if (!PyArg_ParseTuple(args, "h|O!", &type, &PyBool_Type, &sublink)) { |
| 201 |
return nullptr; |
| 202 |
} |
| 203 |
|
| 204 |
PY_TRY |
| 205 |
{ |
| 206 |
getLinkViewPtr()->setNodeType((LinkView::SnapshotType)type, Base::asBoolean(sublink)); |
| 207 |
Py_Return; |
| 208 |
} |
| 209 |
PY_CATCH; |
| 210 |
} |
| 211 |
|
| 212 |
PyObject* LinkViewPy::setChildren(PyObject* args) |
| 213 |
{ |
| 214 |
PyObject* pyObj; |
| 215 |
PyObject* pyVis = Py_None; |
| 216 |
short type = 0; |
| 217 |
if (!PyArg_ParseTuple(args, "O|Os", &pyObj, &pyVis, &type)) { |
| 218 |
return nullptr; |
| 219 |
} |
| 220 |
|
| 221 |
PY_TRY |
| 222 |
{ |
| 223 |
App::PropertyBoolList vis; |
| 224 |
App::PropertyLinkList links; |
| 225 |
if (pyObj != Py_None) { |
| 226 |
links.setPyObject(pyObj); |
| 227 |
} |
| 228 |
if (pyVis != Py_None) { |
| 229 |
vis.setPyObject(pyVis); |
| 230 |
} |
| 231 |
getLinkViewPtr()->setChildren( |
| 232 |
links.getValue(), |
| 233 |
vis.getValue(), |
| 234 |
static_cast<LinkView::SnapshotType>(type) |
| 235 |
); |
| 236 |
Py_Return; |
| 237 |
} |
| 238 |
PY_CATCH; |
| 239 |
} |
| 240 |
|
| 241 |
PyObject* LinkViewPy::setLink(PyObject* args) |
| 242 |
{ |
| 243 |
PyObject* pyObj; |
| 244 |
PyObject* pySubName = Py_None; |
| 245 |
if (!PyArg_ParseTuple(args, "O|O", &pyObj, &pySubName)) { |
| 246 |
return nullptr; |
| 247 |
} |
| 248 |
|
| 249 |
PY_TRY |
| 250 |
{ |
| 251 |
ViewProviderDocumentObject* vpd = nullptr; |
| 252 |
App::DocumentObject* obj = nullptr; |
| 253 |
if (pyObj != Py_None) { |
| 254 |
if (PyObject_TypeCheck(pyObj, &App::DocumentObjectPy::Type)) { |
| 255 |
obj = static_cast<App::DocumentObjectPy*>(pyObj)->getDocumentObjectPtr(); |
| 256 |
} |
| 257 |
else if (PyObject_TypeCheck(pyObj, &ViewProviderDocumentObjectPy::Type)) { |
| 258 |
vpd = static_cast<ViewProviderDocumentObjectPy*>(pyObj) |
| 259 |
->getViewProviderDocumentObjectPtr(); |
| 260 |
} |
| 261 |
else { |
| 262 |
PyErr_SetString( |
| 263 |
PyExc_TypeError, |
| 264 |
"exepcting a type of DocumentObject or ViewProviderDocumentObject" |
| 265 |
); |
| 266 |
return nullptr; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
// Too lazy to parse the argument... |
| 271 |
App::PropertyStringList prop; |
| 272 |
if (pySubName != Py_None) { |
| 273 |
prop.setPyObject(pySubName); |
| 274 |
} |
| 275 |
|
| 276 |
if (obj) { |
| 277 |
getLinkViewPtr()->setLink(obj, prop.getValue()); |
| 278 |
} |
| 279 |
else { |
| 280 |
getLinkViewPtr()->setLinkViewObject(vpd, prop.getValue()); |
| 281 |
} |
| 282 |
Py_Return; |
| 283 |
} |
| 284 |
PY_CATCH; |
| 285 |
} |
| 286 |
|
| 287 |
Py::Object LinkViewPy::getOwner() const |
| 288 |
{ |
| 289 |
auto owner = getLinkViewPtr()->getOwner(); |
| 290 |
if (!owner) { |
| 291 |
return Py::Object(); |
| 292 |
} |
| 293 |
return Py::Object(owner->getPyObject(), true); |
| 294 |
} |
| 295 |
|
| 296 |
void LinkViewPy::setOwner(Py::Object owner) |
| 297 |
{ |
| 298 |
ViewProviderDocumentObject* vp = nullptr; |
| 299 |
if (!owner.isNone()) { |
| 300 |
if (!PyObject_TypeCheck(owner.ptr(), &ViewProviderDocumentObjectPy::Type)) { |
| 301 |
throw Py::TypeError("exepcting the owner to be of ViewProviderDocumentObject"); |
| 302 |
} |
| 303 |
vp = static_cast<ViewProviderDocumentObjectPy*>(owner.ptr())->getViewProviderDocumentObjectPtr(); |
| 304 |
} |
| 305 |
getLinkViewPtr()->setOwner(vp); |
| 306 |
} |
| 307 |
|
| 308 |
Py::Object LinkViewPy::getLinkedView() const |
| 309 |
{ |
| 310 |
auto linked = getLinkViewPtr()->getLinkedView(); |
| 311 |
if (!linked) { |
| 312 |
return Py::Object(); |
| 313 |
} |
| 314 |
return Py::Object(linked->getPyObject(), true); |
| 315 |
} |
| 316 |
|
| 317 |
Py::Object LinkViewPy::getSubNames() const |
| 318 |
{ |
| 319 |
const auto& subs = getLinkViewPtr()->getSubNames(); |
| 320 |
if (subs.empty()) { |
| 321 |
return Py::Object(); |
| 322 |
} |
| 323 |
Py::Tuple ret(subs.size()); |
| 324 |
int i = 0; |
| 325 |
for (auto& s : subs) { |
| 326 |
ret.setItem(i++, Py::String(s.c_str())); |
| 327 |
} |
| 328 |
return ret; |
| 329 |
} |
| 330 |
|
| 331 |
PyObject* LinkViewPy::getElementPicked(PyObject* args) |
| 332 |
{ |
| 333 |
PyObject* obj; |
| 334 |
if (!PyArg_ParseTuple(args, "O", &obj)) { |
| 335 |
return nullptr; |
| 336 |
} |
| 337 |
void* ptr = nullptr; |
| 338 |
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoPickedPoint *", obj, &ptr, 0); |
| 339 |
auto pp = static_cast<SoPickedPoint*>(ptr); |
| 340 |
if (!pp) { |
| 341 |
throw Py::TypeError("type must be of coin.SoPickedPoint"); |
| 342 |
} |
| 343 |
PY_TRY |
| 344 |
{ |
| 345 |
std::string name; |
| 346 |
if (!getLinkViewPtr()->linkGetElementPicked(pp, name)) { |
| 347 |
Py_Return; |
| 348 |
} |
| 349 |
return Py::new_reference_to(Py::String(name)); |
| 350 |
} |
| 351 |
PY_CATCH |
| 352 |
} |
| 353 |
|
| 354 |
PyObject* LinkViewPy::getDetailPath(PyObject* args) |
| 355 |
{ |
| 356 |
const char* sub; |
| 357 |
PyObject* path; |
| 358 |
if (!PyArg_ParseTuple(args, "sO", &sub, &path)) { |
| 359 |
return nullptr; |
| 360 |
} |
| 361 |
void* ptr = nullptr; |
| 362 |
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoPath *", path, &ptr, 0); |
| 363 |
auto pPath = static_cast<SoPath*>(ptr); |
| 364 |
if (!pPath) { |
| 365 |
throw Py::TypeError("type must be of coin.SoPath"); |
| 366 |
} |
| 367 |
PY_TRY |
| 368 |
{ |
| 369 |
SoDetail* det = nullptr; |
| 370 |
getLinkViewPtr()->linkGetDetailPath(sub, static_cast<SoFullPath*>(pPath), det); |
| 371 |
if (!det) { |
| 372 |
Py_Return; |
| 373 |
} |
| 374 |
return Base::Interpreter() |
| 375 |
.createSWIGPointerObj("pivy.coin", "SoDetail *", static_cast<void*>(det), 0); |
| 376 |
} |
| 377 |
PY_CATCH |
| 378 |
} |
| 379 |
|
| 380 |
PyObject* LinkViewPy::getBoundBox(PyObject* args) |
| 381 |
{ |
| 382 |
PyObject* vobj = Py_None; |
| 383 |
if (!PyArg_ParseTuple(args, "O", &vobj)) { |
| 384 |
return nullptr; |
| 385 |
} |
| 386 |
|
| 387 |
PY_TRY |
| 388 |
{ |
| 389 |
Base::PyTypeCheck(&vobj, &ViewProviderDocumentObjectPy::Type); |
| 390 |
ViewProviderDocumentObject* vpd = nullptr; |
| 391 |
if (vobj) { |
| 392 |
vpd = static_cast<ViewProviderDocumentObjectPy*>(vobj)->getViewProviderDocumentObjectPtr(); |
| 393 |
} |
| 394 |
|
| 395 |
auto bbox = getLinkViewPtr()->getBoundBox(vpd); |
| 396 |
Py::Object ret(new Base::BoundBoxPy(new Base::BoundBox3d(bbox))); |
| 397 |
return Py::new_reference_to(ret); |
| 398 |
} |
| 399 |
PY_CATCH |
| 400 |
} |
| 401 |
|
| 402 |
PyObject* LinkViewPy::getCustomAttributes(const char*) const |
| 403 |
{ |
| 404 |
return nullptr; |
| 405 |
} |
| 406 |
|
| 407 |
int LinkViewPy::setCustomAttributes(const char*, PyObject*) |
| 408 |
{ |
| 409 |
return 0; |
| 410 |
} |
| 411 |
|
| 412 |
Py::Object LinkViewPy::getRootNode() const |
| 413 |
{ |
| 414 |
try { |
| 415 |
SoNode* node = getLinkViewPtr()->getLinkRoot(); |
| 416 |
PyObject* Ptr = Base::Interpreter().createSWIGPointerObj("pivy.coin", "SoSeparator *", node, 1); |
| 417 |
node->ref(); |
| 418 |
return Py::Object(Ptr, true); |
| 419 |
} |
| 420 |
catch (const Base::Exception& e) { |
| 421 |
throw Py::RuntimeError(e.what()); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
Py::Object LinkViewPy::getVisibilities() const |
| 426 |
{ |
| 427 |
auto linked = getLinkViewPtr(); |
| 428 |
if (!linked->getSize()) { |
| 429 |
return Py::Object(); |
| 430 |
} |
| 431 |
Py::Tuple ret(linked->getSize()); |
| 432 |
for (int i = 0; i < linked->getSize(); ++i) { |
| 433 |
ret.setItem(i, Py::Boolean(linked->isElementVisible(i))); |
| 434 |
} |
| 435 |
return ret; |
| 436 |
} |
| 437 |
|
| 438 |
void LinkViewPy::setVisibilities(Py::Object value) |
| 439 |
{ |
| 440 |
App::PropertyBoolList v; |
| 441 |
if (!value.isNone()) { |
| 442 |
v.setPyObject(value.ptr()); |
| 443 |
} |
| 444 |
|
| 445 |
auto linked = getLinkViewPtr(); |
| 446 |
const auto& vis = v.getValue(); |
| 447 |
for (int i = 0; i < linked->getSize(); ++i) { |
| 448 |
linked->setElementVisible(i, i >= (int)vis.size() || vis[i]); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
PyObject* LinkViewPy::getChildren(PyObject* args) const |
| 453 |
{ |
| 454 |
if (!PyArg_ParseTuple(args, "")) { |
| 455 |
return nullptr; |
| 456 |
} |
| 457 |
auto children = getLinkViewPtr()->getChildren(); |
| 458 |
if (children.empty()) { |
| 459 |
Py_Return; |
| 460 |
} |
| 461 |
Py::Tuple ret(children.size()); |
| 462 |
int i = 0; |
| 463 |
for (auto vp : children) { |
| 464 |
ret.setItem(i++, Py::Object(vp->getPyObject(), true)); |
| 465 |
} |
| 466 |
return Py::new_reference_to(ret); |
| 467 |
} |
| 468 |
|
| 469 |
Py::Long LinkViewPy::getCount() const |
| 470 |
{ |
| 471 |
return Py::Long(getLinkViewPtr()->getSize()); |
| 472 |
} |
| 473 |
|
| 474 |
void LinkViewPy::setCount(Py::Long count) |
| 475 |
{ |
| 476 |
try { |
| 477 |
getLinkViewPtr()->setSize((int)count); |
| 478 |
} |
| 479 |
catch (const Base::Exception& e) { |
| 480 |
throw Py::RuntimeError(e.what()); |
| 481 |
} |
| 482 |
} |