ViewProviderPythonFeaturePyImp.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "PreCompiled.h"
00024 #ifndef _PreComp_
00025 # include <Inventor/nodes/SoNode.h>
00026 #endif
00027
00028 #include "Gui/ViewProviderPythonFeature.h"
00029 #include <Base/Interpreter.h>
00030
00031
00032 #include "ViewProviderPythonFeaturePy.h"
00033 #include "ViewProviderPythonFeaturePy.cpp"
00034
00035 using namespace Gui;
00036
00037
00038 std::string ViewProviderPythonFeaturePy::representation(void) const
00039 {
00040 return "<ViewProviderPythonFeature object>";
00041 }
00042
00043 PyObject* ViewProviderPythonFeaturePy::addDisplayMode(PyObject * args)
00044 {
00045 char* mode;
00046 PyObject* obj;
00047 if (!PyArg_ParseTuple(args, "Os", &obj, &mode))
00048 return NULL;
00049
00050 void* ptr = 0;
00051 try {
00052 Base::Interpreter().convertSWIGPointerObj("pivy.coin","SoNode *", obj, &ptr, 0);
00053 }
00054 catch (const Base::Exception& e) {
00055 PyErr_SetString(PyExc_RuntimeError, e.what());
00056 return 0;
00057 }
00058
00059 PY_TRY {
00060 SoNode* node = reinterpret_cast<SoNode*>(ptr);
00061 getViewProviderPythonFeaturePtr()->addDisplayMaskMode(node,mode);
00062 Py_Return;
00063 } PY_CATCH;
00064 }
00065
00066 PyObject* ViewProviderPythonFeaturePy::addProperty(PyObject *args)
00067 {
00068 char *sType,*sName=0,*sGroup=0,*sDoc=0;
00069 short attr=0;
00070 PyObject *ro = Py_False, *hd = Py_False;
00071 if (!PyArg_ParseTuple(args, "s|ssshO!O!", &sType,&sName,&sGroup,&sDoc,&attr,
00072 &PyBool_Type, &ro, &PyBool_Type, &hd))
00073 return NULL;
00074
00075 App::Property* prop=0;
00076 prop = getViewProviderPythonFeaturePtr()->addDynamicProperty(sType,sName,sGroup,sDoc,attr,ro==Py_True,hd==Py_True);
00077
00078 if (!prop) {
00079 std::stringstream str;
00080 str << "No property found of type '" << sType << "'" << std::ends;
00081 throw Py::Exception(PyExc_Exception,str.str());
00082 }
00083
00084 return Py::new_reference_to(this);
00085 }
00086
00087 PyObject* ViewProviderPythonFeaturePy::supportedProperties(PyObject *args)
00088 {
00089 if (!PyArg_ParseTuple(args, ""))
00090 return NULL;
00091
00092 std::vector<Base::Type> ary;
00093 Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary);
00094 Py::List res;
00095 for (std::vector<Base::Type>::iterator it = ary.begin(); it != ary.end(); ++it) {
00096 Base::BaseClass *data = static_cast<Base::BaseClass*>(it->createInstance());
00097 if (data) {
00098 delete data;
00099 res.append(Py::String(it->getName()));
00100 }
00101 }
00102 return Py::new_reference_to(res);
00103 }
00104
00105 PyObject *ViewProviderPythonFeaturePy::getCustomAttributes(const char* attr) const
00106 {
00107 PY_TRY{
00108 if (Base::streq(attr, "__dict__")){
00109 PyObject* dict = ViewProviderDocumentObjectPy::getCustomAttributes(attr);
00110 if (dict){
00111 std::vector<std::string> Props = getViewProviderPythonFeaturePtr()->getDynamicPropertyNames();
00112 for (std::vector<std::string>::const_iterator it = Props.begin(); it != Props.end(); ++it)
00113 PyDict_SetItem(dict, PyString_FromString(it->c_str()), PyString_FromString(""));
00114 }
00115 return dict;
00116 }
00117
00118
00119 App::Property* prop = getViewProviderPythonFeaturePtr()->getDynamicPropertyByName(attr);
00120 if (prop) return prop->getPyObject();
00121 } PY_CATCH;
00122
00123 return 0;
00124 }
00125
00126 int ViewProviderPythonFeaturePy::setCustomAttributes(const char* attr, PyObject *value)
00127 {
00128
00129 App::Property* prop = getViewProviderPythonFeaturePtr()->getDynamicPropertyByName(attr);
00130
00131 if (!prop)
00132 return ViewProviderDocumentObjectPy::setCustomAttributes(attr, value);
00133 else {
00134 try {
00135 prop->setPyObject(value);
00136 } catch (Base::Exception &exc) {
00137 PyErr_Format(PyExc_AttributeError, "Attribute (Name: %s) error: '%s' ", attr, exc.what());
00138 return -1;
00139 } catch (...) {
00140 PyErr_Format(PyExc_AttributeError, "Unknown error in attribute %s", attr);
00141 return -1;
00142 }
00143
00144 return 1;
00145 }
00146 }