ViewProviderPythonFeaturePyImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
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 // inclusion of the generated files (generated out of ViewProviderPythonFeaturePy.xml)
00032 #include "ViewProviderPythonFeaturePy.h"
00033 #include "ViewProviderPythonFeaturePy.cpp"
00034 
00035 using namespace Gui;
00036 
00037 // returns a string which represents the object e.g. when printed in python
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))     // convert args: Python->C
00073         return NULL;                             // NULL triggers exception 
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, ""))     // convert args: Python->C 
00090         return NULL;                    // NULL triggers exception
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         // search for dynamic property
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     // search for dynamic property
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 }

Generated on Wed Nov 23 19:01:00 2011 for FreeCAD by  doxygen 1.6.1