00001
00002
00003
00004
00005
00006 #include <boost/filesystem/path.hpp>
00007 #include <boost/filesystem/operations.hpp>
00008 #include <boost/filesystem/exception.hpp>
00009 #include <Base/PyObjectBase.h>
00010 #include <Base/Console.h>
00011 #include <Base/Exception.h>
00012 #include <CXX/Objects.hxx>
00013
00014 #define new DEBUG_CLIENTBLOCK
00015
00016 using Base::streq;
00017 using namespace App;
00018
00020 PyTypeObject DocumentObjectPy::Type = {
00021 PyObject_HEAD_INIT(&PyType_Type)
00022 0,
00023 "App.DocumentObject",
00024 sizeof(DocumentObjectPy),
00025 0,
00026
00027 PyDestructor,
00028 0,
00029 __getattr,
00030 __setattr,
00031 0,
00032 __repr,
00033 0,
00034 0,
00035 0,
00036 0,
00037 0,
00038 0,
00039 0,
00040 0,
00041
00042 0,
00043
00044 Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_CLASS,
00045 "This is the father of all classes handled by the document",
00046 0,
00047 0,
00048 0,
00049 0,
00050 0,
00051 0,
00052 App::DocumentObjectPy::Methods,
00053 0,
00054 App::DocumentObjectPy::GetterSetter,
00055 &App::PropertyContainerPy::Type,
00056 0,
00057 0,
00058 0,
00059 0,
00060 __PyInit,
00061 0,
00062 App::DocumentObjectPy::PyMake,
00063 0,
00064 0,
00065 0,
00066 0,
00067 0,
00068 0,
00069 0,
00070 0
00071 };
00072
00074 PyMethodDef DocumentObjectPy::Methods[] = {
00075 {"touch",
00076 (PyCFunction) staticCallback_touch,
00077 METH_VARARGS,
00078 "Mark the object as changed (touched)"
00079 },
00080 {"purgeTouched",
00081 (PyCFunction) staticCallback_purgeTouched,
00082 METH_VARARGS,
00083 "Mark the object as unchanged"
00084 },
00085 {NULL, NULL, 0, NULL}
00086 };
00087
00088
00089
00091 PyGetSetDef DocumentObjectPy::GetterSetter[] = {
00092 {"OutList",
00093 (getter) staticCallback_getOutList,
00094 (setter) staticCallback_setOutList,
00095 "A list of all objects this object links to.",
00096 NULL
00097 },
00098 {"InList",
00099 (getter) staticCallback_getInList,
00100 (setter) staticCallback_setInList,
00101 "A list of all objects which link tobthis object.",
00102 NULL
00103 },
00104 {"Name",
00105 (getter) staticCallback_getName,
00106 (setter) staticCallback_setName,
00107 "Return the internal name of this object",
00108 NULL
00109 },
00110 {"Document",
00111 (getter) staticCallback_getDocument,
00112 (setter) staticCallback_setDocument,
00113 "Return the document this object is part of",
00114 NULL
00115 },
00116 {"State",
00117 (getter) staticCallback_getState,
00118 (setter) staticCallback_setState,
00119 "State of the object in the document",
00120 NULL
00121 },
00122 {"ViewObject",
00123 (getter) staticCallback_getViewObject,
00124 (setter) staticCallback_setViewObject,
00125 "If the GUI is loaded the associated view provider is returned\nor None if the GUI is not up",
00126 NULL
00127 },
00128 {NULL, NULL, NULL, NULL, NULL}
00129 };
00130
00131
00132
00133
00134 PyObject * DocumentObjectPy::staticCallback_touch (PyObject *self, PyObject *args)
00135 {
00136
00137 if (!((PyObjectBase*) self)->isValid()){
00138 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00139 return NULL;
00140 }
00141
00142
00143 if (((PyObjectBase*) self)->isConst()){
00144 PyErr_SetString(PyExc_ReferenceError, "This object is immutable, you can not set any attribute or call a non const method");
00145 return NULL;
00146 }
00147
00148 try {
00149 PyObject* ret = ((DocumentObjectPy*)self)->touch(args);
00150 if (ret != 0)
00151 ((DocumentObjectPy*)self)->startNotify();
00152 return ret;
00153 }
00154 catch(const Base::Exception& e)
00155 {
00156 std::string str;
00157 str += "FreeCAD exception thrown (";
00158 str += e.what();
00159 str += ")";
00160 e.ReportException();
00161 PyErr_SetString(PyExc_Exception,str.c_str());
00162 return NULL;
00163 }
00164 catch(const boost::filesystem::filesystem_error& e)
00165 {
00166 std::string str;
00167 str += "File system exception thrown (";
00168
00169
00170 str += e.what();
00171 str += ")\n";
00172 Base::Console().Error(str.c_str());
00173 PyErr_SetString(PyExc_Exception,str.c_str());
00174 return NULL;
00175 }
00176 catch(const Py::Exception&)
00177 {
00178
00179 return NULL;
00180 }
00181 catch(const char* e)
00182 {
00183 Base::Console().Error(e);
00184 PyErr_SetString(PyExc_Exception,e);
00185 return NULL;
00186 }
00187
00188 #ifndef DONT_CATCH_CXX_EXCEPTIONS
00189 catch(const std::exception& e)
00190 {
00191 std::string str;
00192 str += "FC++ exception thrown (";
00193 str += e.what();
00194 str += ")";
00195 Base::Console().Error(str.c_str());
00196 PyErr_SetString(PyExc_Exception,str.c_str());
00197 return NULL;
00198 }
00199 catch(...)
00200 {
00201 PyErr_SetString(PyExc_Exception,"Unknown C++ exception");
00202 return NULL;
00203 }
00204 #endif
00205 }
00206
00207
00208
00209
00210 PyObject * DocumentObjectPy::staticCallback_purgeTouched (PyObject *self, PyObject *args)
00211 {
00212
00213 if (!((PyObjectBase*) self)->isValid()){
00214 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00215 return NULL;
00216 }
00217
00218
00219 if (((PyObjectBase*) self)->isConst()){
00220 PyErr_SetString(PyExc_ReferenceError, "This object is immutable, you can not set any attribute or call a non const method");
00221 return NULL;
00222 }
00223
00224 try {
00225 PyObject* ret = ((DocumentObjectPy*)self)->purgeTouched(args);
00226 if (ret != 0)
00227 ((DocumentObjectPy*)self)->startNotify();
00228 return ret;
00229 }
00230 catch(const Base::Exception& e)
00231 {
00232 std::string str;
00233 str += "FreeCAD exception thrown (";
00234 str += e.what();
00235 str += ")";
00236 e.ReportException();
00237 PyErr_SetString(PyExc_Exception,str.c_str());
00238 return NULL;
00239 }
00240 catch(const boost::filesystem::filesystem_error& e)
00241 {
00242 std::string str;
00243 str += "File system exception thrown (";
00244
00245
00246 str += e.what();
00247 str += ")\n";
00248 Base::Console().Error(str.c_str());
00249 PyErr_SetString(PyExc_Exception,str.c_str());
00250 return NULL;
00251 }
00252 catch(const Py::Exception&)
00253 {
00254
00255 return NULL;
00256 }
00257 catch(const char* e)
00258 {
00259 Base::Console().Error(e);
00260 PyErr_SetString(PyExc_Exception,e);
00261 return NULL;
00262 }
00263
00264 #ifndef DONT_CATCH_CXX_EXCEPTIONS
00265 catch(const std::exception& e)
00266 {
00267 std::string str;
00268 str += "FC++ exception thrown (";
00269 str += e.what();
00270 str += ")";
00271 Base::Console().Error(str.c_str());
00272 PyErr_SetString(PyExc_Exception,str.c_str());
00273 return NULL;
00274 }
00275 catch(...)
00276 {
00277 PyErr_SetString(PyExc_Exception,"Unknown C++ exception");
00278 return NULL;
00279 }
00280 #endif
00281 }
00282
00283
00284
00285
00286 PyObject * DocumentObjectPy::staticCallback_getOutList (PyObject *self, void * )
00287 {
00288 if (!((PyObjectBase*) self)->isValid()){
00289 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00290 return NULL;
00291 }
00292
00293 try {
00294 return Py::new_reference_to(((DocumentObjectPy*)self)->getOutList());
00295 } catch (const Py::Exception&) {
00296
00297 return NULL;
00298 } catch (...) {
00299 PyErr_SetString(PyExc_Exception, "Unknown exception while reading attribute 'OutList' of object 'DocumentObject'");
00300 return NULL;
00301 }
00302 }
00303
00304 int DocumentObjectPy::staticCallback_setOutList (PyObject *self, PyObject * , void * )
00305 {
00306 if (!((PyObjectBase*) self)->isValid()){
00307 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00308 return -1;
00309 }
00310
00311 PyErr_SetString(PyExc_AttributeError, "Attribute 'OutList' of object 'DocumentObject' is read-only");
00312 return -1;
00313 }
00314
00315
00316
00317
00318 PyObject * DocumentObjectPy::staticCallback_getInList (PyObject *self, void * )
00319 {
00320 if (!((PyObjectBase*) self)->isValid()){
00321 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00322 return NULL;
00323 }
00324
00325 try {
00326 return Py::new_reference_to(((DocumentObjectPy*)self)->getInList());
00327 } catch (const Py::Exception&) {
00328
00329 return NULL;
00330 } catch (...) {
00331 PyErr_SetString(PyExc_Exception, "Unknown exception while reading attribute 'InList' of object 'DocumentObject'");
00332 return NULL;
00333 }
00334 }
00335
00336 int DocumentObjectPy::staticCallback_setInList (PyObject *self, PyObject * , void * )
00337 {
00338 if (!((PyObjectBase*) self)->isValid()){
00339 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00340 return -1;
00341 }
00342
00343 PyErr_SetString(PyExc_AttributeError, "Attribute 'InList' of object 'DocumentObject' is read-only");
00344 return -1;
00345 }
00346
00347
00348
00349
00350 PyObject * DocumentObjectPy::staticCallback_getName (PyObject *self, void * )
00351 {
00352 if (!((PyObjectBase*) self)->isValid()){
00353 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00354 return NULL;
00355 }
00356
00357 try {
00358 return Py::new_reference_to(((DocumentObjectPy*)self)->getName());
00359 } catch (const Py::Exception&) {
00360
00361 return NULL;
00362 } catch (...) {
00363 PyErr_SetString(PyExc_Exception, "Unknown exception while reading attribute 'Name' of object 'DocumentObject'");
00364 return NULL;
00365 }
00366 }
00367
00368 int DocumentObjectPy::staticCallback_setName (PyObject *self, PyObject * , void * )
00369 {
00370 if (!((PyObjectBase*) self)->isValid()){
00371 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00372 return -1;
00373 }
00374
00375 PyErr_SetString(PyExc_AttributeError, "Attribute 'Name' of object 'DocumentObject' is read-only");
00376 return -1;
00377 }
00378
00379
00380
00381
00382 PyObject * DocumentObjectPy::staticCallback_getDocument (PyObject *self, void * )
00383 {
00384 if (!((PyObjectBase*) self)->isValid()){
00385 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00386 return NULL;
00387 }
00388
00389 try {
00390 return Py::new_reference_to(((DocumentObjectPy*)self)->getDocument());
00391 } catch (const Py::Exception&) {
00392
00393 return NULL;
00394 } catch (...) {
00395 PyErr_SetString(PyExc_Exception, "Unknown exception while reading attribute 'Document' of object 'DocumentObject'");
00396 return NULL;
00397 }
00398 }
00399
00400 int DocumentObjectPy::staticCallback_setDocument (PyObject *self, PyObject * , void * )
00401 {
00402 if (!((PyObjectBase*) self)->isValid()){
00403 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00404 return -1;
00405 }
00406
00407 PyErr_SetString(PyExc_AttributeError, "Attribute 'Document' of object 'DocumentObject' is read-only");
00408 return -1;
00409 }
00410
00411
00412
00413
00414 PyObject * DocumentObjectPy::staticCallback_getState (PyObject *self, void * )
00415 {
00416 if (!((PyObjectBase*) self)->isValid()){
00417 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00418 return NULL;
00419 }
00420
00421 try {
00422 return Py::new_reference_to(((DocumentObjectPy*)self)->getState());
00423 } catch (const Py::Exception&) {
00424
00425 return NULL;
00426 } catch (...) {
00427 PyErr_SetString(PyExc_Exception, "Unknown exception while reading attribute 'State' of object 'DocumentObject'");
00428 return NULL;
00429 }
00430 }
00431
00432 int DocumentObjectPy::staticCallback_setState (PyObject *self, PyObject * , void * )
00433 {
00434 if (!((PyObjectBase*) self)->isValid()){
00435 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00436 return -1;
00437 }
00438
00439 PyErr_SetString(PyExc_AttributeError, "Attribute 'State' of object 'DocumentObject' is read-only");
00440 return -1;
00441 }
00442
00443
00444
00445
00446 PyObject * DocumentObjectPy::staticCallback_getViewObject (PyObject *self, void * )
00447 {
00448 if (!((PyObjectBase*) self)->isValid()){
00449 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00450 return NULL;
00451 }
00452
00453 try {
00454 return Py::new_reference_to(((DocumentObjectPy*)self)->getViewObject());
00455 } catch (const Py::Exception&) {
00456
00457 return NULL;
00458 } catch (...) {
00459 PyErr_SetString(PyExc_Exception, "Unknown exception while reading attribute 'ViewObject' of object 'DocumentObject'");
00460 return NULL;
00461 }
00462 }
00463
00464 int DocumentObjectPy::staticCallback_setViewObject (PyObject *self, PyObject * , void * )
00465 {
00466 if (!((PyObjectBase*) self)->isValid()){
00467 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00468 return -1;
00469 }
00470
00471 PyErr_SetString(PyExc_AttributeError, "Attribute 'ViewObject' of object 'DocumentObject' is read-only");
00472 return -1;
00473 }
00474
00475
00476
00477
00478
00479
00480 PyParentObject DocumentObjectPy::Parents[] = { PARENTSAppDocumentObjectPy };
00481
00482
00483
00484
00485 DocumentObjectPy::DocumentObjectPy(DocumentObject *pcObject, PyTypeObject *T)
00486 : PropertyContainerPy(reinterpret_cast<PropertyContainerPy::PointerType>(pcObject), T)
00487 {
00488 }
00489
00490 PyObject *DocumentObjectPy::PyMake(struct _typeobject *, PyObject *, PyObject *)
00491 {
00492
00493 PyErr_SetString(PyExc_RuntimeError, "You cannot create directly an instance of 'DocumentObjectPy'.");
00494
00495 return 0;
00496 }
00497
00498 int DocumentObjectPy::PyInit(PyObject* , PyObject* )
00499 {
00500 return 0;
00501 }
00502
00503
00504
00505
00506 DocumentObjectPy::~DocumentObjectPy()
00507 {
00508 }
00509
00510
00511
00512
00513 PyObject *DocumentObjectPy::_repr(void)
00514 {
00515 return Py_BuildValue("s", representation().c_str());
00516 }
00517
00518
00519
00520
00521 PyObject *DocumentObjectPy::_getattr(char *attr)
00522 {
00523 try {
00524
00525 PyObject *r = getCustomAttributes(attr);
00526 if(r) return r;
00527 }
00528 #ifndef DONT_CATCH_CXX_EXCEPTIONS
00529 catch(const Base::Exception& e)
00530 {
00531 std::string str;
00532 str += "FreeCAD exception thrown (";
00533 str += e.what();
00534 str += ")";
00535 e.ReportException();
00536 PyErr_SetString(PyExc_Exception,str.c_str());
00537 return NULL;
00538 }
00539 catch(const std::exception& e)
00540 {
00541 std::string str;
00542 str += "FC++ exception thrown (";
00543 str += e.what();
00544 str += ")";
00545 Base::Console().Error(str.c_str());
00546 PyErr_SetString(PyExc_Exception,str.c_str());
00547 return NULL;
00548 }
00549 catch(const Py::Exception&)
00550 {
00551
00552 return NULL;
00553 }
00554 catch(...)
00555 {
00556 PyErr_SetString(PyExc_Exception,"Unknown C++ exception");
00557 return NULL;
00558 }
00559 #else // DONT_CATCH_CXX_EXCEPTIONS
00560 catch(const Base::Exception& e)
00561 {
00562 std::string str;
00563 str += "FreeCAD exception thrown (";
00564 str += e.what();
00565 str += ")";
00566 e.ReportException();
00567 PyErr_SetString(PyExc_Exception,str.c_str());
00568 return NULL;
00569 }
00570 catch(const Py::Exception&)
00571 {
00572
00573 return NULL;
00574 }
00575 #endif // DONT_CATCH_CXX_EXCEPTIONS
00576
00577 PyObject *rvalue = Py_FindMethod(Methods, this, attr);
00578 if (rvalue == NULL)
00579 {
00580 PyErr_Clear();
00581 return PropertyContainerPy::_getattr(attr);
00582 }
00583 else
00584 {
00585 return rvalue;
00586 }
00587 }
00588
00589 int DocumentObjectPy::_setattr(char *attr, PyObject *value)
00590 {
00591 try {
00592
00593 int r = setCustomAttributes(attr, value);
00594 if(r==1) return 0;
00595 }
00596 #ifndef DONT_CATCH_CXX_EXCEPTIONS
00597 catch(const Base::Exception& e)
00598 {
00599 std::string str;
00600 str += "FreeCAD exception thrown (";
00601 str += e.what();
00602 str += ")";
00603 e.ReportException();
00604 PyErr_SetString(PyExc_Exception,str.c_str());
00605 return -1;
00606 }
00607 catch(const std::exception& e)
00608 {
00609 std::string str;
00610 str += "FC++ exception thrown (";
00611 str += e.what();
00612 str += ")";
00613 Base::Console().Error(str.c_str());
00614 PyErr_SetString(PyExc_Exception,str.c_str());
00615 return -1;
00616 }
00617 catch(const Py::Exception&)
00618 {
00619
00620 return -1;
00621 }
00622 catch(...)
00623 {
00624 PyErr_SetString(PyExc_Exception,"Unknown C++ exception");
00625 return -1;
00626 }
00627 #else // DONT_CATCH_CXX_EXCEPTIONS
00628 catch(const Base::Exception& e)
00629 {
00630 std::string str;
00631 str += "FreeCAD exception thrown (";
00632 str += e.what();
00633 str += ")";
00634 e.ReportException();
00635 PyErr_SetString(PyExc_Exception,str.c_str());
00636 return -1;
00637 }
00638 catch(const Py::Exception&)
00639 {
00640
00641 return -1;
00642 }
00643 #endif // DONT_CATCH_CXX_EXCEPTIONS
00644
00645 return PropertyContainerPy::_setattr(attr, value);
00646 }
00647
00648 DocumentObject *DocumentObjectPy::getDocumentObjectPtr(void) const
00649 {
00650 return static_cast<DocumentObject *>(_pcTwinPointer);
00651 }
00652
00653 #if 0
00654
00655
00656
00657
00658
00659
00660 std::string DocumentObjectPy::representation(void) const
00661 {
00662 return std::string("<DocumentObject object>");
00663 }
00664
00665 PyObject* DocumentObjectPy::touch(PyObject *args)
00666 {
00667 PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
00668 return 0;
00669 }
00670
00671 PyObject* DocumentObjectPy::purgeTouched(PyObject *args)
00672 {
00673 PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
00674 return 0;
00675 }
00676
00677
00678
00679 Py::List DocumentObjectPy::getOutList(void) const
00680 {
00681
00682 throw Py::AttributeError("Not yet implemented");
00683 }
00684
00685 Py::List DocumentObjectPy::getInList(void) const
00686 {
00687
00688 throw Py::AttributeError("Not yet implemented");
00689 }
00690
00691 Py::String DocumentObjectPy::getName(void) const
00692 {
00693
00694 throw Py::AttributeError("Not yet implemented");
00695 }
00696
00697 Py::Object DocumentObjectPy::getDocument(void) const
00698 {
00699
00700 throw Py::AttributeError("Not yet implemented");
00701 }
00702
00703 Py::List DocumentObjectPy::getState(void) const
00704 {
00705
00706 throw Py::AttributeError("Not yet implemented");
00707 }
00708
00709 Py::Object DocumentObjectPy::getViewObject(void) const
00710 {
00711
00712 throw Py::AttributeError("Not yet implemented");
00713 }
00714
00715 PyObject *DocumentObjectPy::getCustomAttributes(const char* attr) const
00716 {
00717 return 0;
00718 }
00719
00720 int DocumentObjectPy::setCustomAttributes(const char* attr, PyObject *obj)
00721 {
00722 return 0;
00723 }
00724 #endif
00725
00726
00727