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 Gui;
00018
00020 PyTypeObject WorkbenchPy::Type = {
00021 PyObject_HEAD_INIT(&PyType_Type)
00022 0,
00023 "Gui.Workbench",
00024 sizeof(WorkbenchPy),
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 base class for workbenches",
00046 0,
00047 0,
00048 0,
00049 0,
00050 0,
00051 0,
00052 Gui::WorkbenchPy::Methods,
00053 0,
00054 Gui::WorkbenchPy::GetterSetter,
00055 &Base::BaseClassPy::Type,
00056 0,
00057 0,
00058 0,
00059 0,
00060 __PyInit,
00061 0,
00062 Gui::WorkbenchPy::PyMake,
00063 0,
00064 0,
00065 0,
00066 0,
00067 0,
00068 0,
00069 0,
00070 0
00071 };
00072
00074 PyMethodDef WorkbenchPy::Methods[] = {
00075 {"name",
00076 (PyCFunction) staticCallback_name,
00077 METH_VARARGS,
00078 "Return the workbench name"
00079 },
00080 {"activate",
00081 (PyCFunction) staticCallback_activate,
00082 METH_VARARGS,
00083 "Activate this workbench"
00084 },
00085 {NULL, NULL, 0, NULL}
00086 };
00087
00088
00089
00091 PyGetSetDef WorkbenchPy::GetterSetter[] = {
00092 {NULL, NULL, NULL, NULL, NULL}
00093 };
00094
00095
00096
00097
00098 PyObject * WorkbenchPy::staticCallback_name (PyObject *self, PyObject *args)
00099 {
00100
00101 if (!((PyObjectBase*) self)->isValid()){
00102 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00103 return NULL;
00104 }
00105
00106
00107 if (((PyObjectBase*) self)->isConst()){
00108 PyErr_SetString(PyExc_ReferenceError, "This object is immutable, you can not set any attribute or call a non const method");
00109 return NULL;
00110 }
00111
00112 try {
00113 PyObject* ret = ((WorkbenchPy*)self)->name(args);
00114 if (ret != 0)
00115 ((WorkbenchPy*)self)->startNotify();
00116 return ret;
00117 }
00118 catch(const Base::Exception& e)
00119 {
00120 std::string str;
00121 str += "FreeCAD exception thrown (";
00122 str += e.what();
00123 str += ")";
00124 e.ReportException();
00125 PyErr_SetString(PyExc_Exception,str.c_str());
00126 return NULL;
00127 }
00128 catch(const boost::filesystem::filesystem_error& e)
00129 {
00130 std::string str;
00131 str += "File system exception thrown (";
00132
00133
00134 str += e.what();
00135 str += ")\n";
00136 Base::Console().Error(str.c_str());
00137 PyErr_SetString(PyExc_Exception,str.c_str());
00138 return NULL;
00139 }
00140 catch(const Py::Exception&)
00141 {
00142
00143 return NULL;
00144 }
00145 catch(const char* e)
00146 {
00147 Base::Console().Error(e);
00148 PyErr_SetString(PyExc_Exception,e);
00149 return NULL;
00150 }
00151
00152 #ifndef DONT_CATCH_CXX_EXCEPTIONS
00153 catch(const std::exception& e)
00154 {
00155 std::string str;
00156 str += "FC++ exception thrown (";
00157 str += e.what();
00158 str += ")";
00159 Base::Console().Error(str.c_str());
00160 PyErr_SetString(PyExc_Exception,str.c_str());
00161 return NULL;
00162 }
00163 catch(...)
00164 {
00165 PyErr_SetString(PyExc_Exception,"Unknown C++ exception");
00166 return NULL;
00167 }
00168 #endif
00169 }
00170
00171
00172
00173
00174 PyObject * WorkbenchPy::staticCallback_activate (PyObject *self, PyObject *args)
00175 {
00176
00177 if (!((PyObjectBase*) self)->isValid()){
00178 PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
00179 return NULL;
00180 }
00181
00182
00183 if (((PyObjectBase*) self)->isConst()){
00184 PyErr_SetString(PyExc_ReferenceError, "This object is immutable, you can not set any attribute or call a non const method");
00185 return NULL;
00186 }
00187
00188 try {
00189 PyObject* ret = ((WorkbenchPy*)self)->activate(args);
00190 if (ret != 0)
00191 ((WorkbenchPy*)self)->startNotify();
00192 return ret;
00193 }
00194 catch(const Base::Exception& e)
00195 {
00196 std::string str;
00197 str += "FreeCAD exception thrown (";
00198 str += e.what();
00199 str += ")";
00200 e.ReportException();
00201 PyErr_SetString(PyExc_Exception,str.c_str());
00202 return NULL;
00203 }
00204 catch(const boost::filesystem::filesystem_error& e)
00205 {
00206 std::string str;
00207 str += "File system exception thrown (";
00208
00209
00210 str += e.what();
00211 str += ")\n";
00212 Base::Console().Error(str.c_str());
00213 PyErr_SetString(PyExc_Exception,str.c_str());
00214 return NULL;
00215 }
00216 catch(const Py::Exception&)
00217 {
00218
00219 return NULL;
00220 }
00221 catch(const char* e)
00222 {
00223 Base::Console().Error(e);
00224 PyErr_SetString(PyExc_Exception,e);
00225 return NULL;
00226 }
00227
00228 #ifndef DONT_CATCH_CXX_EXCEPTIONS
00229 catch(const std::exception& e)
00230 {
00231 std::string str;
00232 str += "FC++ exception thrown (";
00233 str += e.what();
00234 str += ")";
00235 Base::Console().Error(str.c_str());
00236 PyErr_SetString(PyExc_Exception,str.c_str());
00237 return NULL;
00238 }
00239 catch(...)
00240 {
00241 PyErr_SetString(PyExc_Exception,"Unknown C++ exception");
00242 return NULL;
00243 }
00244 #endif
00245 }
00246
00247
00248
00249
00250
00251
00252 PyParentObject WorkbenchPy::Parents[] = { PARENTSGuiWorkbenchPy };
00253
00254
00255
00256
00257 WorkbenchPy::WorkbenchPy(Workbench *pcObject, PyTypeObject *T)
00258 : BaseClassPy(reinterpret_cast<BaseClassPy::PointerType>(pcObject), T)
00259 {
00260 }
00261
00262 PyObject *WorkbenchPy::PyMake(struct _typeobject *, PyObject *, PyObject *)
00263 {
00264
00265 PyErr_SetString(PyExc_RuntimeError, "You cannot create directly an instance of 'WorkbenchPy'.");
00266
00267 return 0;
00268 }
00269
00270 int WorkbenchPy::PyInit(PyObject* , PyObject* )
00271 {
00272 return 0;
00273 }
00274
00275
00276
00277
00278 WorkbenchPy::~WorkbenchPy()
00279 {
00280 }
00281
00282
00283
00284
00285 PyObject *WorkbenchPy::_repr(void)
00286 {
00287 return Py_BuildValue("s", representation().c_str());
00288 }
00289
00290
00291
00292
00293 PyObject *WorkbenchPy::_getattr(char *attr)
00294 {
00295 try {
00296
00297 PyObject *r = getCustomAttributes(attr);
00298 if(r) return r;
00299 }
00300 #ifndef DONT_CATCH_CXX_EXCEPTIONS
00301 catch(const Base::Exception& e)
00302 {
00303 std::string str;
00304 str += "FreeCAD exception thrown (";
00305 str += e.what();
00306 str += ")";
00307 e.ReportException();
00308 PyErr_SetString(PyExc_Exception,str.c_str());
00309 return NULL;
00310 }
00311 catch(const std::exception& e)
00312 {
00313 std::string str;
00314 str += "FC++ exception thrown (";
00315 str += e.what();
00316 str += ")";
00317 Base::Console().Error(str.c_str());
00318 PyErr_SetString(PyExc_Exception,str.c_str());
00319 return NULL;
00320 }
00321 catch(const Py::Exception&)
00322 {
00323
00324 return NULL;
00325 }
00326 catch(...)
00327 {
00328 PyErr_SetString(PyExc_Exception,"Unknown C++ exception");
00329 return NULL;
00330 }
00331 #else // DONT_CATCH_CXX_EXCEPTIONS
00332 catch(const Base::Exception& e)
00333 {
00334 std::string str;
00335 str += "FreeCAD exception thrown (";
00336 str += e.what();
00337 str += ")";
00338 e.ReportException();
00339 PyErr_SetString(PyExc_Exception,str.c_str());
00340 return NULL;
00341 }
00342 catch(const Py::Exception&)
00343 {
00344
00345 return NULL;
00346 }
00347 #endif // DONT_CATCH_CXX_EXCEPTIONS
00348
00349 PyObject *rvalue = Py_FindMethod(Methods, this, attr);
00350 if (rvalue == NULL)
00351 {
00352 PyErr_Clear();
00353 return BaseClassPy::_getattr(attr);
00354 }
00355 else
00356 {
00357 return rvalue;
00358 }
00359 }
00360
00361 int WorkbenchPy::_setattr(char *attr, PyObject *value)
00362 {
00363 try {
00364
00365 int r = setCustomAttributes(attr, value);
00366 if(r==1) return 0;
00367 }
00368 #ifndef DONT_CATCH_CXX_EXCEPTIONS
00369 catch(const Base::Exception& e)
00370 {
00371 std::string str;
00372 str += "FreeCAD exception thrown (";
00373 str += e.what();
00374 str += ")";
00375 e.ReportException();
00376 PyErr_SetString(PyExc_Exception,str.c_str());
00377 return -1;
00378 }
00379 catch(const std::exception& e)
00380 {
00381 std::string str;
00382 str += "FC++ exception thrown (";
00383 str += e.what();
00384 str += ")";
00385 Base::Console().Error(str.c_str());
00386 PyErr_SetString(PyExc_Exception,str.c_str());
00387 return -1;
00388 }
00389 catch(const Py::Exception&)
00390 {
00391
00392 return -1;
00393 }
00394 catch(...)
00395 {
00396 PyErr_SetString(PyExc_Exception,"Unknown C++ exception");
00397 return -1;
00398 }
00399 #else // DONT_CATCH_CXX_EXCEPTIONS
00400 catch(const Base::Exception& e)
00401 {
00402 std::string str;
00403 str += "FreeCAD exception thrown (";
00404 str += e.what();
00405 str += ")";
00406 e.ReportException();
00407 PyErr_SetString(PyExc_Exception,str.c_str());
00408 return -1;
00409 }
00410 catch(const Py::Exception&)
00411 {
00412
00413 return -1;
00414 }
00415 #endif // DONT_CATCH_CXX_EXCEPTIONS
00416
00417 return BaseClassPy::_setattr(attr, value);
00418 }
00419
00420 Workbench *WorkbenchPy::getWorkbenchPtr(void) const
00421 {
00422 return static_cast<Workbench *>(_pcTwinPointer);
00423 }
00424
00425 #if 0
00426
00427
00428
00429
00430
00431
00432 std::string WorkbenchPy::representation(void) const
00433 {
00434 return std::string("<Workbench object>");
00435 }
00436
00437 PyObject* WorkbenchPy::name(PyObject *args)
00438 {
00439 PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
00440 return 0;
00441 }
00442
00443 PyObject* WorkbenchPy::activate(PyObject *args)
00444 {
00445 PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
00446 return 0;
00447 }
00448
00449
00450
00451 PyObject *WorkbenchPy::getCustomAttributes(const char* attr) const
00452 {
00453 return 0;
00454 }
00455
00456 int WorkbenchPy::setCustomAttributes(const char* attr, PyObject *obj)
00457 {
00458 return 0;
00459 }
00460 #endif
00461
00462
00463