| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2014 Stefan Tröger <[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 <Inventor/nodes/SoCamera.h> |
| 24 |
#include <string> |
| 25 |
|
| 26 |
#include <Base/GeometryPyCXX.h> |
| 27 |
#include <Base/Interpreter.h> |
| 28 |
#include <Base/MatrixPy.h> |
| 29 |
|
| 30 |
#include "PythonWrapper.h" |
| 31 |
#include "Navigation/NavigationStyle.h" |
| 32 |
#include "View3DViewerPy.h" |
| 33 |
#include "View3DInventorViewer.h" |
| 34 |
|
| 35 |
|
| 36 |
using namespace Gui; |
| 37 |
|
| 38 |
|
| 39 |
void View3DInventorViewerPy::init_type() |
| 40 |
{ |
| 41 |
behaviors().name("View3DInventorViewerPy"); |
| 42 |
behaviors().doc("Python binding class for the 3D viewer class"); |
| 43 |
// you must have overwritten the virtual functions |
| 44 |
behaviors().supportRepr(); |
| 45 |
behaviors().supportGetattr(); |
| 46 |
behaviors().supportSetattr(); |
| 47 |
|
| 48 |
add_varargs_method( |
| 49 |
"getSoRenderManager", |
| 50 |
&View3DInventorViewerPy::getSoRenderManager, |
| 51 |
"getSoRenderManager() -> SoRenderManager\n" |
| 52 |
"Returns the render manager which is used to handle everything related to\n" |
| 53 |
"rendering the scene graph. It can be used to get full control over the\n" |
| 54 |
"render process\n" |
| 55 |
); |
| 56 |
add_varargs_method( |
| 57 |
"getSoEventManager", |
| 58 |
&View3DInventorViewerPy::getSoEventManager, |
| 59 |
"getSoEventManager() -> SoEventManager\n" |
| 60 |
"Returns the event manager which is used to handle everything event related in\n" |
| 61 |
"the viewer. It can be used to change the event processing. This must however be\n" |
| 62 |
"done very carefully to not change the user interaction in an unpredictable manner.\n" |
| 63 |
); |
| 64 |
add_varargs_method( |
| 65 |
"getSceneGraph", |
| 66 |
&View3DInventorViewerPy::getSceneGraph, |
| 67 |
"getSceneGraph() -> SoNode" |
| 68 |
); |
| 69 |
add_varargs_method("setSceneGraph", &View3DInventorViewerPy::setSceneGraph, "setSceneGraph(SoNode)"); |
| 70 |
|
| 71 |
add_varargs_method( |
| 72 |
"seekToPoint", |
| 73 |
&View3DInventorViewerPy::seekToPoint, |
| 74 |
"seekToPoint(tuple) -> None\n" |
| 75 |
"Initiate a seek action towards the 3D intersection of the scene and the\n" |
| 76 |
"ray from the screen coordinate's point and in the same direction as the\n" |
| 77 |
"camera is pointing. If the tuple has two entries it is interpreted as the\n" |
| 78 |
"screen coordinates xy and the intersection point with the scene is\n" |
| 79 |
"calculated. If three entries are given it is interpreted as the intersection\n" |
| 80 |
"point xyz and the seek is done towards this point" |
| 81 |
); |
| 82 |
add_varargs_method( |
| 83 |
"setFocalDistance", |
| 84 |
&View3DInventorViewerPy::setFocalDistance, |
| 85 |
"setFocalDistance(float) -> None\n" |
| 86 |
); |
| 87 |
add_varargs_method( |
| 88 |
"getFocalDistance", |
| 89 |
&View3DInventorViewerPy::getFocalDistance, |
| 90 |
"getFocalDistance() -> float\n" |
| 91 |
); |
| 92 |
add_varargs_method( |
| 93 |
"getPoint", |
| 94 |
&View3DInventorViewerPy::getPointOnFocalPlane, |
| 95 |
"Same as getPointOnFocalPlane" |
| 96 |
); |
| 97 |
add_varargs_method( |
| 98 |
"getPointOnFocalPlane", |
| 99 |
&View3DInventorViewerPy::getPointOnFocalPlane, |
| 100 |
"getPointOnFocalPlane(x, y) -> Base::Vector(x,y,z)" |
| 101 |
); |
| 102 |
add_varargs_method( |
| 103 |
"getPickRadius", |
| 104 |
&View3DInventorViewerPy::getPickRadius, |
| 105 |
"getPickRadius(): returns radius of confusion in pixels for picking objects on screen " |
| 106 |
"(selection)." |
| 107 |
); |
| 108 |
add_varargs_method( |
| 109 |
"setPickRadius", |
| 110 |
&View3DInventorViewerPy::setPickRadius, |
| 111 |
"setPickRadius(new_radius): sets radius of confusion in pixels for picking objects on " |
| 112 |
"screen (selection)." |
| 113 |
); |
| 114 |
add_varargs_method( |
| 115 |
"setupEditingRoot", |
| 116 |
&View3DInventorViewerPy::setupEditingRoot, |
| 117 |
"setupEditingRoot(matrix=None): setup the editing ViewProvider's root node.\n" |
| 118 |
"All child coin nodes of the current editing ViewProvider will be transferred to\n" |
| 119 |
"an internal editing node of this viewer, with a new transformation node specified\n" |
| 120 |
"by 'matrix'. All ViewProviderLink to the editing ViewProvider will be temporary\n" |
| 121 |
"hidden. Call resetEditingRoot() to restore everything back to normal" |
| 122 |
); |
| 123 |
add_varargs_method( |
| 124 |
"resetEditingRoot", |
| 125 |
&View3DInventorViewerPy::resetEditingRoot, |
| 126 |
"resetEditingRoot(updateLinks=True): restore the editing ViewProvider's root node" |
| 127 |
); |
| 128 |
add_varargs_method( |
| 129 |
"setBackgroundColor", |
| 130 |
&View3DInventorViewerPy::setBackgroundColor, |
| 131 |
"setBackgroundColor(r,g,b): sets the background color of the current viewer." |
| 132 |
); |
| 133 |
add_varargs_method( |
| 134 |
"setGradientBackground", |
| 135 |
&View3DInventorViewerPy::setGradientBackground, |
| 136 |
"setGradientBackground(str): sets the background gradient of the current viewer." |
| 137 |
); |
| 138 |
add_varargs_method( |
| 139 |
"setGradientBackgroundColor", |
| 140 |
&View3DInventorViewerPy::setGradientBackgroundColor, |
| 141 |
"setGradientBackgroundColor(tuple,tuple,[tuple]): sets the gradient colors of the current " |
| 142 |
"viewer." |
| 143 |
); |
| 144 |
add_varargs_method( |
| 145 |
"setRedirectToSceneGraph", |
| 146 |
&View3DInventorViewerPy::setRedirectToSceneGraph, |
| 147 |
"setRedirectToSceneGraph(bool): enables or disables to redirect events directly to the " |
| 148 |
"scene graph." |
| 149 |
); |
| 150 |
add_varargs_method( |
| 151 |
"isRedirectedToSceneGraph", |
| 152 |
&View3DInventorViewerPy::isRedirectedToSceneGraph, |
| 153 |
"isRedirectedToSceneGraph() -> bool: check whether event redirection is enabled." |
| 154 |
); |
| 155 |
add_varargs_method( |
| 156 |
"grabFramebuffer", |
| 157 |
&View3DInventorViewerPy::grabFramebuffer, |
| 158 |
"grabFramebuffer() -> QImage: renders and returns a 32-bit RGB image of the framebuffer." |
| 159 |
); |
| 160 |
|
| 161 |
add_varargs_method( |
| 162 |
"setOverrideMode", |
| 163 |
&View3DInventorViewerPy::setOverrideMode, |
| 164 |
"setOverrideMode(mode): sets the display override mode." |
| 165 |
); |
| 166 |
|
| 167 |
add_varargs_method( |
| 168 |
"setEnabledNaviCube", |
| 169 |
&View3DInventorViewerPy::setEnabledNaviCube, |
| 170 |
"setEnabledNaviCube(bool): enables or disables the navi cube of the viewer." |
| 171 |
); |
| 172 |
add_varargs_method( |
| 173 |
"isEnabledNaviCube", |
| 174 |
&View3DInventorViewerPy::isEnabledNaviCube, |
| 175 |
"isEnabledNaviCube() -> bool: check whether the navi cube is enabled." |
| 176 |
); |
| 177 |
add_varargs_method( |
| 178 |
"setNaviCubeCorner", |
| 179 |
&View3DInventorViewerPy::setNaviCubeCorner, |
| 180 |
"setNaviCubeCorner(int): sets the corner where to show the navi cube:\n" |
| 181 |
"0=top left, 1=top right, 2=bottom left, 3=bottom right" |
| 182 |
); |
| 183 |
|
| 184 |
add_varargs_method( |
| 185 |
"getNavigationStyle", |
| 186 |
&View3DInventorViewerPy::getNavigationStyle, |
| 187 |
"getNavigationStyle() -> NavigationStyle\n" |
| 188 |
"Returns the current viewer navigation style class.\n" |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
View3DInventorViewerPy::View3DInventorViewerPy(View3DInventorViewer* vi) |
| 193 |
: _viewer(vi) |
| 194 |
{} |
| 195 |
|
| 196 |
View3DInventorViewerPy::~View3DInventorViewerPy() |
| 197 |
{ |
| 198 |
Base::PyGILStateLocker lock; |
| 199 |
for (auto it : callbacks) { |
| 200 |
Py_DECREF(it); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
Py::Object View3DInventorViewerPy::repr() |
| 206 |
{ |
| 207 |
if (!_viewer) { |
| 208 |
throw Py::RuntimeError("Cannot print representation of deleted object"); |
| 209 |
} |
| 210 |
|
| 211 |
return Py::String("View3DInventorViewer"); |
| 212 |
} |
| 213 |
|
| 214 |
View3DInventorViewerPy::method_varargs_handler View3DInventorViewerPy::pycxx_handler = nullptr; |
| 215 |
|
| 216 |
PyObject* View3DInventorViewerPy::method_varargs_ext_handler( |
| 217 |
PyObject* _self_and_name_tuple, |
| 218 |
PyObject* _args |
| 219 |
) |
| 220 |
{ |
| 221 |
try { |
| 222 |
return pycxx_handler(_self_and_name_tuple, _args); |
| 223 |
} |
| 224 |
catch (const Base::Exception& e) { |
| 225 |
throw Py::RuntimeError(e.what()); |
| 226 |
} |
| 227 |
catch (const std::exception& e) { |
| 228 |
throw Py::RuntimeError(e.what()); |
| 229 |
} |
| 230 |
catch (...) { |
| 231 |
throw Py::RuntimeError("Unknown C++ exception"); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
Py::Object View3DInventorViewerPy::getattr(const char* attr) |
| 236 |
{ |
| 237 |
if (!_viewer) { |
| 238 |
std::string s; |
| 239 |
std::ostringstream s_out; |
| 240 |
s_out << "Cannot access attribute '" << attr << "' of deleted object"; |
| 241 |
throw Py::RuntimeError(s_out.str()); |
| 242 |
} |
| 243 |
else { |
| 244 |
Py::Object obj = Py::PythonExtension<View3DInventorViewerPy>::getattr(attr); |
| 245 |
if (PyCFunction_Check(obj.ptr())) { |
| 246 |
auto op = reinterpret_cast<PyCFunctionObject*>(obj.ptr()); |
| 247 |
if (!pycxx_handler) { |
| 248 |
pycxx_handler = op->m_ml->ml_meth; |
| 249 |
} |
| 250 |
op->m_ml->ml_meth = method_varargs_ext_handler; |
| 251 |
} |
| 252 |
return obj; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
int View3DInventorViewerPy::setattr(const char* attr, const Py::Object& value) |
| 257 |
{ |
| 258 |
if (!_viewer) { |
| 259 |
std::string s; |
| 260 |
std::ostringstream s_out; |
| 261 |
s_out << "Cannot access attribute '" << attr << "' of deleted object"; |
| 262 |
throw Py::RuntimeError(s_out.str()); |
| 263 |
} |
| 264 |
else { |
| 265 |
return Py::PythonExtension<View3DInventorViewerPy>::setattr(attr, value); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
Py::Object View3DInventorViewerPy::getSoRenderManager(const Py::Tuple& args) |
| 270 |
{ |
| 271 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 272 |
throw Py::Exception(); |
| 273 |
} |
| 274 |
|
| 275 |
try { |
| 276 |
SoRenderManager* manager = _viewer->getSoRenderManager(); |
| 277 |
PyObject* proxy = nullptr; |
| 278 |
proxy = Base::Interpreter().createSWIGPointerObj( |
| 279 |
"pivy.coin", |
| 280 |
"SoRenderManager *", |
| 281 |
static_cast<void*>(manager), |
| 282 |
0 |
| 283 |
); |
| 284 |
return Py::Object(proxy, true); |
| 285 |
} |
| 286 |
catch (const Base::Exception& e) { |
| 287 |
throw Py::RuntimeError(e.what()); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
Py::Object View3DInventorViewerPy::getSceneGraph(const Py::Tuple& args) |
| 292 |
{ |
| 293 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 294 |
throw Py::Exception(); |
| 295 |
} |
| 296 |
|
| 297 |
try { |
| 298 |
SoNode* scene = _viewer->getSceneGraph(); |
| 299 |
PyObject* proxy = nullptr; |
| 300 |
proxy = Base::Interpreter() |
| 301 |
.createSWIGPointerObj("pivy.coin", "SoSeparator *", static_cast<void*>(scene), 1); |
| 302 |
scene->ref(); |
| 303 |
return Py::Object(proxy, true); |
| 304 |
} |
| 305 |
catch (const Base::Exception& e) { |
| 306 |
throw Py::RuntimeError(e.what()); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
Py::Object View3DInventorViewerPy::setSceneGraph(const Py::Tuple& args) |
| 311 |
{ |
| 312 |
PyObject* proxy; |
| 313 |
if (!PyArg_ParseTuple(args.ptr(), "O", &proxy)) { |
| 314 |
throw Py::Exception(); |
| 315 |
} |
| 316 |
|
| 317 |
void* ptr = nullptr; |
| 318 |
try { |
| 319 |
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoNode *", proxy, &ptr, 0); |
| 320 |
if (!ptr) { |
| 321 |
throw Py::RuntimeError("Conversion of coin.SoNode failed"); |
| 322 |
} |
| 323 |
auto node = static_cast<SoNode*>(ptr); |
| 324 |
_viewer->setSceneGraph(node); |
| 325 |
return Py::None(); |
| 326 |
} |
| 327 |
catch (const Base::Exception& e) { |
| 328 |
throw Py::RuntimeError(e.what()); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
Py::Object View3DInventorViewerPy::getSoEventManager(const Py::Tuple& args) |
| 333 |
{ |
| 334 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 335 |
throw Py::Exception(); |
| 336 |
} |
| 337 |
|
| 338 |
try { |
| 339 |
SoEventManager* manager = _viewer->getSoEventManager(); |
| 340 |
PyObject* proxy = nullptr; |
| 341 |
proxy = Base::Interpreter().createSWIGPointerObj( |
| 342 |
"pivy.coin", |
| 343 |
"SoEventManager *", |
| 344 |
static_cast<void*>(manager), |
| 345 |
0 |
| 346 |
); |
| 347 |
return Py::Object(proxy, true); |
| 348 |
} |
| 349 |
catch (const Base::Exception& e) { |
| 350 |
throw Py::RuntimeError(e.what()); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
Py::Object View3DInventorViewerPy::seekToPoint(const Py::Tuple& args) |
| 355 |
{ |
| 356 |
PyObject* object; |
| 357 |
if (!PyArg_ParseTuple(args.ptr(), "O", &object)) { |
| 358 |
throw Py::Exception(); |
| 359 |
} |
| 360 |
|
| 361 |
try { |
| 362 |
const Py::Tuple tuple(object); |
| 363 |
|
| 364 |
// If the 3d point is given |
| 365 |
if (tuple.size() == 3) { |
| 366 |
Py::Float x = tuple[0]; |
| 367 |
Py::Float y = tuple[1]; |
| 368 |
Py::Float z = tuple[2]; |
| 369 |
|
| 370 |
SbVec3f hitpoint((float)x, (float)y, (float)z); |
| 371 |
_viewer->seekToPoint(hitpoint); |
| 372 |
} |
| 373 |
else { |
| 374 |
Py::Long x(tuple[0]); |
| 375 |
Py::Long y(tuple[1]); |
| 376 |
|
| 377 |
SbVec2s hitpoint((long)x, (long)y); |
| 378 |
_viewer->seekToPoint(hitpoint); |
| 379 |
} |
| 380 |
|
| 381 |
return Py::None(); |
| 382 |
} |
| 383 |
catch (const Py::Exception&) { |
| 384 |
throw; |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
Py::Object View3DInventorViewerPy::setFocalDistance(const Py::Tuple& args) |
| 389 |
{ |
| 390 |
float distance; |
| 391 |
if (!PyArg_ParseTuple(args.ptr(), "f", &distance)) { |
| 392 |
throw Py::Exception(); |
| 393 |
} |
| 394 |
|
| 395 |
try { |
| 396 |
SoCamera* cam = _viewer->getSoRenderManager()->getCamera(); |
| 397 |
if (cam) { |
| 398 |
cam->focalDistance.setValue(distance); |
| 399 |
} |
| 400 |
} |
| 401 |
catch (const Py::Exception&) { |
| 402 |
throw; // re-throw |
| 403 |
} |
| 404 |
catch (const Base::Exception& e) { |
| 405 |
throw Py::RuntimeError(e.what()); |
| 406 |
} |
| 407 |
catch (const std::exception& e) { |
| 408 |
throw Py::RuntimeError(e.what()); |
| 409 |
} |
| 410 |
catch (...) { |
| 411 |
throw Py::RuntimeError("Unknown C++ exception"); |
| 412 |
} |
| 413 |
|
| 414 |
return Py::None(); |
| 415 |
} |
| 416 |
|
| 417 |
Py::Object View3DInventorViewerPy::getFocalDistance(const Py::Tuple& args) |
| 418 |
{ |
| 419 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 420 |
throw Py::Exception(); |
| 421 |
} |
| 422 |
|
| 423 |
try { |
| 424 |
double d = _viewer->getSoRenderManager()->getCamera()->focalDistance.getValue(); |
| 425 |
return Py::Float(d); |
| 426 |
} |
| 427 |
catch (const Base::Exception& e) { |
| 428 |
throw Py::RuntimeError(e.what()); |
| 429 |
} |
| 430 |
catch (const std::exception& e) { |
| 431 |
throw Py::RuntimeError(e.what()); |
| 432 |
} |
| 433 |
catch (...) { |
| 434 |
throw Py::RuntimeError("Unknown C++ exception"); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
Py::Object View3DInventorViewerPy::getPointOnFocalPlane(const Py::Tuple& args) |
| 439 |
{ |
| 440 |
short x, y; |
| 441 |
if (!PyArg_ParseTuple(args.ptr(), "hh", &x, &y)) { |
| 442 |
PyErr_Clear(); |
| 443 |
Py::Tuple t(args[0]); |
| 444 |
x = (int)Py::Long(t[0]); |
| 445 |
y = (int)Py::Long(t[1]); |
| 446 |
} |
| 447 |
try { |
| 448 |
SbVec3f pt = _viewer->getPointOnFocalPlane(SbVec2s(x, y)); |
| 449 |
return Py::Vector(Base::Vector3f(pt[0], pt[1], pt[2])); |
| 450 |
} |
| 451 |
catch (const Base::Exception& e) { |
| 452 |
throw Py::RuntimeError(e.what()); |
| 453 |
} |
| 454 |
catch (const Py::Exception&) { |
| 455 |
throw; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
Py::Object View3DInventorViewerPy::getPickRadius(const Py::Tuple& args) |
| 460 |
{ |
| 461 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 462 |
throw Py::Exception(); |
| 463 |
} |
| 464 |
|
| 465 |
double d = _viewer->getPickRadius(); |
| 466 |
return Py::Float(d); |
| 467 |
} |
| 468 |
|
| 469 |
Py::Object View3DInventorViewerPy::setPickRadius(const Py::Tuple& args) |
| 470 |
{ |
| 471 |
float r = 0.0; |
| 472 |
if (!PyArg_ParseTuple(args.ptr(), "f", &r)) { |
| 473 |
throw Py::Exception(); |
| 474 |
} |
| 475 |
|
| 476 |
if (r < 0.001) { |
| 477 |
throw Py::ValueError( |
| 478 |
std::string("Pick radius is zero or negative; positive number is required.") |
| 479 |
); |
| 480 |
} |
| 481 |
try { |
| 482 |
_viewer->setPickRadius(r); |
| 483 |
return Py::None(); |
| 484 |
} |
| 485 |
catch (const Base::Exception& e) { |
| 486 |
throw Py::RuntimeError(e.what()); |
| 487 |
} |
| 488 |
catch (const std::exception& e) { |
| 489 |
throw Py::RuntimeError(e.what()); |
| 490 |
} |
| 491 |
catch (...) { |
| 492 |
throw Py::RuntimeError("Unknown C++ exception"); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
Py::Object View3DInventorViewerPy::setupEditingRoot(const Py::Tuple& args) |
| 497 |
{ |
| 498 |
PyObject* pynode = Py_None; |
| 499 |
PyObject* pymat = Py_None; |
| 500 |
if (!PyArg_ParseTuple(args.ptr(), "|OO!", &pynode, &Base::MatrixPy::Type, &pymat)) { |
| 501 |
throw Py::Exception(); |
| 502 |
} |
| 503 |
|
| 504 |
Base::Matrix4D* mat = nullptr; |
| 505 |
if (pymat != Py_None) { |
| 506 |
mat = static_cast<Base::MatrixPy*>(pymat)->getMatrixPtr(); |
| 507 |
} |
| 508 |
|
| 509 |
try { |
| 510 |
SoNode* node = nullptr; |
| 511 |
if (pynode != Py_None) { |
| 512 |
void* ptr = nullptr; |
| 513 |
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoNode *", pynode, &ptr, 0); |
| 514 |
if (!ptr) { |
| 515 |
throw Py::RuntimeError("Conversion of coin.SoNode failed"); |
| 516 |
} |
| 517 |
node = static_cast<SoNode*>(ptr); |
| 518 |
} |
| 519 |
_viewer->setupEditingRoot(node, mat); |
| 520 |
return Py::None(); |
| 521 |
} |
| 522 |
catch (const Base::Exception& e) { |
| 523 |
e.setPyException(); |
| 524 |
throw Py::Exception(); |
| 525 |
} |
| 526 |
catch (const std::exception& e) { |
| 527 |
throw Py::RuntimeError(e.what()); |
| 528 |
} |
| 529 |
catch (...) { |
| 530 |
throw Py::RuntimeError("Unknown C++ exception"); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
Py::Object View3DInventorViewerPy::resetEditingRoot(const Py::Tuple& args) |
| 535 |
{ |
| 536 |
PyObject* updateLinks = Py_True; |
| 537 |
if (!PyArg_ParseTuple(args.ptr(), "|O!", &PyBool_Type, &updateLinks)) { |
| 538 |
throw Py::Exception(); |
| 539 |
} |
| 540 |
try { |
| 541 |
_viewer->resetEditingRoot(Base::asBoolean(updateLinks)); |
| 542 |
return Py::None(); |
| 543 |
} |
| 544 |
catch (const Base::Exception& e) { |
| 545 |
e.setPyException(); |
| 546 |
throw Py::Exception(); |
| 547 |
} |
| 548 |
catch (const std::exception& e) { |
| 549 |
throw Py::RuntimeError(e.what()); |
| 550 |
} |
| 551 |
catch (...) { |
| 552 |
throw Py::RuntimeError("Unknown C++ exception"); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
Py::Object View3DInventorViewerPy::setGradientBackground(const Py::Tuple& args) |
| 557 |
{ |
| 558 |
const char* background; |
| 559 |
if (!PyArg_ParseTuple(args.ptr(), "s", &background)) { |
| 560 |
throw Py::Exception(); |
| 561 |
} |
| 562 |
try { |
| 563 |
View3DInventorViewer::Background gradient = View3DInventorViewer::Background::NoGradient; |
| 564 |
if (strcmp(background, "LINEAR") == 0) { |
| 565 |
gradient = View3DInventorViewer::Background::LinearGradient; |
| 566 |
} |
| 567 |
else if (strcmp(background, "RADIAL") == 0) { |
| 568 |
gradient = View3DInventorViewer::Background::RadialGradient; |
| 569 |
} |
| 570 |
|
| 571 |
_viewer->setGradientBackground(gradient); |
| 572 |
_viewer->redraw(); |
| 573 |
return Py::None(); |
| 574 |
} |
| 575 |
catch (const Base::Exception& e) { |
| 576 |
throw Py::RuntimeError(e.what()); |
| 577 |
} |
| 578 |
catch (const std::exception& e) { |
| 579 |
throw Py::RuntimeError(e.what()); |
| 580 |
} |
| 581 |
catch (...) { |
| 582 |
throw Py::RuntimeError("Unknown C++ exception"); |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
Py::Object View3DInventorViewerPy::setGradientBackgroundColor(const Py::Tuple& args) |
| 587 |
{ |
| 588 |
PyObject* col1; |
| 589 |
PyObject* col2; |
| 590 |
PyObject* col3 = nullptr; |
| 591 |
if (!PyArg_ParseTuple( |
| 592 |
args.ptr(), |
| 593 |
"O!O!|O!", |
| 594 |
&PyTuple_Type, |
| 595 |
&col1, |
| 596 |
&PyTuple_Type, |
| 597 |
&col2, |
| 598 |
&PyTuple_Type, |
| 599 |
&col3 |
| 600 |
)) { |
| 601 |
throw Py::Exception(); |
| 602 |
} |
| 603 |
|
| 604 |
auto tupleToColor = [](PyObject* col) { |
| 605 |
SbColor color; |
| 606 |
Py::Tuple tuple(col); |
| 607 |
for (int i = 0; i < 3; i++) { |
| 608 |
color[i] = static_cast<float>(Py::Float(tuple[i])); |
| 609 |
} |
| 610 |
|
| 611 |
return color; |
| 612 |
}; |
| 613 |
|
| 614 |
try { |
| 615 |
SbColor midColor(-1, -1, -1); |
| 616 |
SbColor fromColor = tupleToColor(col1); |
| 617 |
SbColor toColor = tupleToColor(col2); |
| 618 |
if (col3) { |
| 619 |
midColor = tupleToColor(col3); |
| 620 |
} |
| 621 |
|
| 622 |
_viewer->setGradientBackgroundColor(fromColor, toColor, midColor); |
| 623 |
_viewer->redraw(); |
| 624 |
return Py::None(); |
| 625 |
} |
| 626 |
catch (const Base::Exception& e) { |
| 627 |
throw Py::RuntimeError(e.what()); |
| 628 |
} |
| 629 |
catch (const std::exception& e) { |
| 630 |
throw Py::RuntimeError(e.what()); |
| 631 |
} |
| 632 |
catch (...) { |
| 633 |
throw Py::RuntimeError("Unknown C++ exception"); |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
Py::Object View3DInventorViewerPy::setBackgroundColor(const Py::Tuple& args) |
| 638 |
{ |
| 639 |
float red, green, blue = 0.0; |
| 640 |
if (!PyArg_ParseTuple(args.ptr(), "fff", &red, &green, &blue)) { |
| 641 |
throw Py::Exception(); |
| 642 |
} |
| 643 |
try { |
| 644 |
SbColor col(red, green, blue); |
| 645 |
_viewer->setGradientBackgroundColor(col, col); |
| 646 |
|
| 647 |
QColor qtcolor; |
| 648 |
qtcolor.setRedF(red); |
| 649 |
qtcolor.setGreenF(green); |
| 650 |
qtcolor.setBlueF(blue); |
| 651 |
if (qtcolor.isValid()) { |
| 652 |
_viewer->setBackgroundColor(qtcolor); |
| 653 |
} |
| 654 |
_viewer->redraw(); |
| 655 |
return Py::None(); |
| 656 |
} |
| 657 |
catch (const Base::Exception& e) { |
| 658 |
throw Py::RuntimeError(e.what()); |
| 659 |
} |
| 660 |
catch (const std::exception& e) { |
| 661 |
throw Py::RuntimeError(e.what()); |
| 662 |
} |
| 663 |
catch (...) { |
| 664 |
throw Py::RuntimeError("Unknown C++ exception"); |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
Py::Object View3DInventorViewerPy::setRedirectToSceneGraph(const Py::Tuple& args) |
| 669 |
{ |
| 670 |
PyObject* m = Py_False; |
| 671 |
if (!PyArg_ParseTuple(args.ptr(), "O!", &PyBool_Type, &m)) { |
| 672 |
throw Py::Exception(); |
| 673 |
} |
| 674 |
_viewer->setRedirectToSceneGraph(Base::asBoolean(m)); |
| 675 |
return Py::None(); |
| 676 |
} |
| 677 |
|
| 678 |
Py::Object View3DInventorViewerPy::isRedirectedToSceneGraph(const Py::Tuple& args) |
| 679 |
{ |
| 680 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 681 |
throw Py::Exception(); |
| 682 |
} |
| 683 |
bool ok = _viewer->isRedirectedToSceneGraph(); |
| 684 |
return Py::Boolean(ok); |
| 685 |
} |
| 686 |
|
| 687 |
Py::Object View3DInventorViewerPy::grabFramebuffer(const Py::Tuple& args) |
| 688 |
{ |
| 689 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 690 |
throw Py::Exception(); |
| 691 |
} |
| 692 |
QImage img = _viewer->grabFramebuffer(); |
| 693 |
|
| 694 |
PythonWrapper wrap; |
| 695 |
wrap.loadGuiModule(); |
| 696 |
return wrap.fromQImage(img.mirrored()); |
| 697 |
} |
| 698 |
|
| 699 |
Py::Object View3DInventorViewerPy::setOverrideMode(const Py::Tuple& args) |
| 700 |
{ |
| 701 |
const char* mode; |
| 702 |
if (!PyArg_ParseTuple(args.ptr(), "s", &mode)) { |
| 703 |
throw Py::Exception(); |
| 704 |
} |
| 705 |
|
| 706 |
_viewer->setOverrideMode(std::string(mode)); |
| 707 |
return Py::None(); |
| 708 |
} |
| 709 |
|
| 710 |
Py::Object View3DInventorViewerPy::setEnabledNaviCube(const Py::Tuple& args) |
| 711 |
{ |
| 712 |
PyObject* m = Py_False; |
| 713 |
if (!PyArg_ParseTuple(args.ptr(), "O!", &PyBool_Type, &m)) { |
| 714 |
throw Py::Exception(); |
| 715 |
} |
| 716 |
_viewer->setEnabledNaviCube(Base::asBoolean(m)); |
| 717 |
return Py::None(); |
| 718 |
} |
| 719 |
|
| 720 |
Py::Object View3DInventorViewerPy::isEnabledNaviCube(const Py::Tuple& args) |
| 721 |
{ |
| 722 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 723 |
throw Py::Exception(); |
| 724 |
} |
| 725 |
bool ok = _viewer->isEnabledNaviCube(); |
| 726 |
return Py::Boolean(ok); |
| 727 |
} |
| 728 |
|
| 729 |
Py::Object View3DInventorViewerPy::setNaviCubeCorner(const Py::Tuple& args) |
| 730 |
{ |
| 731 |
int pos; |
| 732 |
if (!PyArg_ParseTuple(args.ptr(), "i", &pos)) { |
| 733 |
throw Py::Exception(); |
| 734 |
} |
| 735 |
if (pos < 0 || pos > 3) { |
| 736 |
throw Py::IndexError("Value out of range"); |
| 737 |
} |
| 738 |
_viewer->setNaviCubeCorner(pos); |
| 739 |
return Py::None(); |
| 740 |
} |
| 741 |
|
| 742 |
Py::Object View3DInventorViewerPy::getNavigationStyle(const Py::Tuple& args) |
| 743 |
{ |
| 744 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 745 |
throw Py::Exception(); |
| 746 |
} |
| 747 |
|
| 748 |
NavigationStyle* navigationStyle = _viewer->navigationStyle(); |
| 749 |
if (navigationStyle) { |
| 750 |
return Py::asObject(navigationStyle->getPyObject()); |
| 751 |
} |
| 752 |
return Py::None(); |
| 753 |
} |