| 261 | } |
| 262 | |
| 263 | Py::Object DocumentObjectPy::getViewObject() const |
| 264 | { |
| 265 | try { |
| 266 | PyObject* dict = PySys_GetObject("modules"); |
| 267 | if (!dict) { |
| 268 | return Py::None(); |
| 269 | } |
| 270 | |
| 271 | // check if the FreeCADGui module is already loaded, if not then don't try to load it |
| 272 | Py::Dict sysmod(dict); |
| 273 | if (!sysmod.hasKey("FreeCADGui")) { |
| 274 | return Py::None(); |
| 275 | } |
| 276 | |
| 277 | // double-check that the module doesn't have a null pointer |
| 278 | Py::Module module(PyImport_ImportModule("FreeCADGui"), true); |
| 279 | if (module.isNull() || !module.hasAttr("getDocument")) { |
| 280 | // in v0.14+, the GUI module can be loaded in console mode (but doesn't have all its |
| 281 | // document methods) |
| 282 | return Py::None(); |
| 283 | } |
| 284 | if (!App::MainThreadSignalConfig::isMainThread()) { |
| 285 | Base::Console().error( |
| 286 | "GUI API 'App::DocumentObjectPy::getViewObject' may only be used from the main " |
| 287 | "thread.\n" |
| 288 | ); |
| 289 | throw Py::RuntimeError( |
| 290 | "GUI API 'App::DocumentObjectPy::getViewObject' may only be used from the main " |
| 291 | "thread" |
| 292 | ); |
| 293 | } |
| 294 | if (!getDocumentObjectPtr()->getDocument()) { |
| 295 | throw Py::RuntimeError("Object has no document"); |
| 296 | } |
| 297 | const char* internalName = getDocumentObjectPtr()->getNameInDocument(); |
| 298 | if (!internalName) { |
| 299 | throw Py::RuntimeError("Object has been removed from document"); |
| 300 | } |
| 301 | |
| 302 | Py::Callable method(module.getAttr("getDocument")); |
| 303 | Py::Tuple arg(1); |
| 304 | arg.setItem(0, Py::String(getDocumentObjectPtr()->getDocument()->getName())); |
| 305 | Py::Object doc = method.apply(arg); |
| 306 | method = doc.getAttr("getObject"); |
| 307 | arg.setItem(0, Py::String(internalName)); |
| 308 | Py::Object obj = method.apply(arg); |
| 309 | return obj; |
| 310 | } |
| 311 | catch (Py::Exception& e) { |
| 312 | if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 313 | // the GUI is not up, hence None is returned |
| 314 | e.clear(); |
| 315 | return Py::None(); |
| 316 | } |
| 317 | // FreeCADGui is loaded, so there must be wrong something else |
| 318 | throw; // re-throw |
| 319 | } |
| 320 | } |
nothing calls this directly
no test coverage detected