ViewProviderPyImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2007 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 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <Inventor/nodes/SoSeparator.h>
00027 #endif
00028 
00029 #include <Inventor/SoDB.h>
00030 #include <Inventor/actions/SoWriteAction.h>
00031 #include <Inventor/nodes/SoCone.h>
00032 #include <Inventor/nodes/SoSeparator.h>
00033 
00034 #include "ViewProvider.h"
00035 
00036 // inclusion of the generated files (generated out of ViewProviderPy2.xml)
00037 #include "ViewProviderPy.h"
00038 #include "ViewProviderPy.cpp"
00039 #include <Base/Interpreter.h>
00040 #include <Base/Matrix.h>
00041 #include <Base/MatrixPy.h>
00042 #include <Base/Placement.h>
00043 #include <Base/PlacementPy.h>
00044 
00045 using namespace Gui;
00046 
00047 // returns a string which represent the object e.g. when printed in python
00048 std::string ViewProviderPy::representation(void) const
00049 {
00050     return "<View provider object>";
00051 }
00052 
00053 PyObject*  ViewProviderPy::show(PyObject *args)
00054 {
00055     if (!PyArg_ParseTuple(args, ""))     // convert args: Python->C 
00056         return NULL;                       // NULL triggers exception 
00057     PY_TRY {
00058         getViewProviderPtr()->show();  
00059         Py_Return;
00060     } PY_CATCH;
00061 }
00062 
00063 PyObject*  ViewProviderPy::hide(PyObject *args)
00064 {
00065     if (!PyArg_ParseTuple(args, ""))     // convert args: Python->C 
00066         return NULL;                       // NULL triggers exception 
00067     PY_TRY {
00068         getViewProviderPtr()->hide();  
00069         Py_Return;
00070     } PY_CATCH;
00071 }
00072 
00073 PyObject*  ViewProviderPy::isVisible(PyObject *args)
00074 {
00075     if (!PyArg_ParseTuple(args, ""))     // convert args: Python->C 
00076         return NULL;                       // NULL triggers exception 
00077     PY_TRY {
00078         return Py_BuildValue("O", (getViewProviderPtr()->isShow() ? Py_True : Py_False));
00079     } PY_CATCH;
00080 }
00081 
00082 PyObject*  ViewProviderPy::listDisplayModes(PyObject *args)
00083 {
00084     if (!PyArg_ParseTuple(args, ""))     // convert args: Python->C 
00085         return NULL;                       // NULL triggers exception 
00086     PY_TRY {
00087         std::vector<std::string> modes = getViewProviderPtr()->getDisplayModes();  
00088         PyObject* pyList = PyList_New(modes.size()); 
00089         int i=0;
00090 
00091         for ( std::vector<std::string>::iterator it = modes.begin(); it != modes.end(); ++it ) {
00092             PyObject* str = PyString_FromString(it->c_str());
00093             PyList_SetItem(pyList, i++, str);
00094         }
00095 
00096         return pyList;
00097     } PY_CATCH;
00098 }
00099 
00100 PyObject*  ViewProviderPy::toString(PyObject *args)
00101 {
00102     if (!PyArg_ParseTuple(args, ""))     // convert args: Python->C 
00103         return NULL;                     // NULL triggers exception 
00104     PY_TRY {
00105         std::string buffer = getViewProviderPtr()->toString();
00106         return Py::new_reference_to(Py::String(buffer));
00107     } PY_CATCH;
00108 }
00109 
00110 PyObject* ViewProviderPy::startEditing(PyObject *args)
00111 {
00112     int mode=0;
00113     if (!PyArg_ParseTuple(args, "|i", &mode))     // convert args: Python->C 
00114         return NULL;                     // NULL triggers exception 
00115     bool edit = getViewProviderPtr()->startEditing(mode);
00116     Py::Boolean ok(edit);
00117     return Py::new_reference_to(ok);
00118 }
00119 
00120 PyObject* ViewProviderPy::finishEditing(PyObject *args)
00121 {
00122     if (!PyArg_ParseTuple(args, ""))     // convert args: Python->C 
00123         return NULL;                     // NULL triggers exception 
00124     getViewProviderPtr()->finishEditing();
00125     Py_Return;
00126 }
00127 
00128 PyObject* ViewProviderPy::isEditing(PyObject *args)
00129 {
00130     if (!PyArg_ParseTuple(args, ""))     // convert args: Python->C 
00131         return NULL;                     // NULL triggers exception 
00132     bool edit = getViewProviderPtr()->isEditing();
00133     Py::Boolean ok(edit);
00134     return Py::new_reference_to(ok);
00135 }
00136 
00137 PyObject*  ViewProviderPy::setTransformation(PyObject *args)
00138 {
00139     PyObject* p;
00140     Base::Matrix4D mat;
00141     if (PyArg_ParseTuple(args, "O!",&(Base::MatrixPy::Type),&p)) {
00142         mat = *static_cast<Base::MatrixPy*>(p)->getMatrixPtr();
00143         getViewProviderPtr()->setTransformation(mat);
00144         Py_Return;
00145     }
00146     PyErr_Clear();
00147     if (PyArg_ParseTuple(args, "O!",&(Base::PlacementPy::Type),&p)) {
00148         Base::PlacementPy* plc = static_cast<Base::PlacementPy*>(p);
00149         getViewProviderPtr()->setTransformation(plc->getPlacementPtr()->toMatrix());
00150         Py_Return;
00151     }
00152 
00153     PyErr_SetString(PyExc_Exception, "Either set matrix or placement to set transformation");
00154     return 0;
00155 }
00156 
00157 PyObject *ViewProviderPy::getCustomAttributes(const char* attr) const
00158 {
00159     return 0;
00160 }
00161 
00162 int ViewProviderPy::setCustomAttributes(const char* attr, PyObject *obj)
00163 {
00164     return 0; 
00165 }
00166 
00167 Py::Object ViewProviderPy::getAnnotation(void) const
00168 {
00169     try {
00170         SoNode* node = getViewProviderPtr()->getAnnotation();
00171         PyObject* Ptr = Base::Interpreter().createSWIGPointerObj("pivy.coin", "SoSeparator *", node, 1);
00172         node->ref();
00173         return Py::Object(Ptr, true);
00174     }
00175     catch (const Base::Exception& e) {
00176         throw Py::Exception(e.what());
00177     }
00178 }
00179 
00180 void  ViewProviderPy::setAnnotation(Py::Object arg)
00181 {
00182 
00183 }
00184 
00185 Py::Object ViewProviderPy::getRootNode(void) const
00186 {
00187     try {
00188         SoNode* node = getViewProviderPtr()->getRoot();
00189         PyObject* Ptr = Base::Interpreter().createSWIGPointerObj("pivy.coin","SoSeparator *", node, 1);
00190         node->ref();
00191         return Py::Object(Ptr, true);
00192     }
00193     catch (const Base::Exception& e) {
00194         throw Py::Exception(e.what());
00195     }
00196 }
00197 
00198 void  ViewProviderPy::setRootNode(Py::Object arg)
00199 {
00200 
00201 }
00202 
00203 static char * buffer;
00204 static size_t buffer_size = 0;
00205 
00206 static void *
00207 buffer_realloc(void * bufptr, size_t size)
00208 {
00209     buffer = (char *)realloc(bufptr, size);
00210     buffer_size = size;
00211     return buffer;
00212 }
00213 
00214 static SbString
00215 buffer_writeaction(SoNode * root)
00216 {
00217     SoOutput out;
00218     buffer = (char *)malloc(1024);
00219     buffer_size = 1024;
00220     out.setBuffer(buffer, buffer_size, buffer_realloc);
00221 
00222     SoWriteAction wa(&out);
00223     wa.apply(root);
00224 
00225     SbString s(buffer);
00226     free(buffer);
00227     return s;
00228 }
00229 
00230 Py::String ViewProviderPy::getIV(void) const
00231 {
00232     SbString buf = buffer_writeaction(getViewProviderPtr()->getRoot());
00233     return Py::String(buf.getString());
00234 }

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