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 FeaturePythonPy::Type = {
00021 PyObject_HEAD_INIT(&PyType_Type)
00022 0,
00023 "App.FeaturePython",
00024 sizeof(FeaturePythonPy),
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 Feature classes",
00046 0,
00047 0,
00048 0,
00049 0,
00050 0,
00051 0,
00052 App::FeaturePythonPy::Methods,
00053 0,
00054 App::FeaturePythonPy::GetterSetter,
00055 &App::DocumentObjectPy::Type,
00056 0,
00057 0,
00058 0,
00059 0,
00060 __PyInit,
00061 0,
00062 App::FeaturePythonPy::PyMake,
00063 0,
00064 0,
00065 0,
00066 0,
00067 0,
00068 0,
00069 0,
00070 0
00071 };
00072
00074 PyMethodDef FeaturePythonPy::Methods[] = {
00075 {"addProperty",
00076 (PyCFunction) staticCallback_addProperty,
00077 METH_VARARGS,
00078 "addProperty(string, string) -- Add a generic property.\nThe first argument specifies the type, the second the\nname of the property.\n "
00079 },
00080 {"supportedProperties",
00081 (PyCFunction) staticCallback_supportedProperties,
00082 METH_VARARGS,
00083 "A list of supported property types"
00084 },
00085 {NULL, NULL, 0, NULL}
00086 };
00087
00088
00089
00091 PyGetSetDef FeaturePythonPy::GetterSetter[] = {
00092 {NULL, NULL, NULL, NULL, NULL}
00093 };
00094
00095
00096
00097
00098 PyObject * FeaturePythonPy::staticCallback_addProperty (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 = ((FeaturePythonPy*)self)->addProperty(args);
00114 if (ret != 0)
00115 ((FeaturePythonPy*)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 * FeaturePythonPy::staticCallback_supportedProperties (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 = ((FeaturePythonPy*)self)->supportedProperties(args);
00190 if (ret != 0)
00191 ((FeaturePythonPy*)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 FeaturePythonPy::Parents[] = { PARENTSAppFeaturePythonPy };
00253
00254
00255
00256
00257 FeaturePythonPy::FeaturePythonPy(DocumentObject *pcObject, PyTypeObject *T)
00258 : DocumentObjectPy(reinterpret_cast<DocumentObjectPy::PointerType>(pcObject), T)
00259 {
00260 }
00261
00262 PyObject *FeaturePythonPy::PyMake(struct _typeobject *, PyObject *, PyObject *)
00263 {
00264
00265 PyErr_SetString(PyExc_RuntimeError, "You cannot create directly an instance of 'FeaturePythonPy'.");
00266
00267 return 0;
00268 }
00269
00270 int FeaturePythonPy::PyInit(PyObject* , PyObject* )
00271 {
00272 return 0;
00273 }
00274
00275
00276
00277
00278 FeaturePythonPy::~FeaturePythonPy()
00279 {
00280 }
00281
00282
00283
00284
00285 PyObject *FeaturePythonPy::_repr(void)
00286 {
00287 return Py_BuildValue("s", representation().c_str());
00288 }
00289
00290
00291
00292
00293 PyObject *FeaturePythonPy::_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 DocumentObjectPy::_getattr(attr);
00354 }
00355 else
00356 {
00357 return rvalue;
00358 }
00359 }
00360
00361 int FeaturePythonPy::_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 DocumentObjectPy::_setattr(attr, value);
00418 }
00419
00420 DocumentObject *FeaturePythonPy::getFeaturePythonPtr(void) const
00421 {
00422 return static_cast<DocumentObject *>(_pcTwinPointer);
00423 }
00424
00425 #if 0
00426
00427
00428
00429
00430
00431
00432 std::string FeaturePythonPy::representation(void) const
00433 {
00434 return std::string("<FeaturePython object>");
00435 }
00436
00437 PyObject* FeaturePythonPy::addProperty(PyObject *args)
00438 {
00439 PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
00440 return 0;
00441 }
00442
00443 PyObject* FeaturePythonPy::supportedProperties(PyObject *args)
00444 {
00445 PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
00446 return 0;
00447 }
00448
00449
00450
00451 PyObject *FeaturePythonPy::getCustomAttributes(const char* attr) const
00452 {
00453 return 0;
00454 }
00455
00456 int FeaturePythonPy::setCustomAttributes(const char* attr, PyObject *obj)
00457 {
00458 return 0;
00459 }
00460 #endif
00461
00462
00463