Gui/DocumentPyImp.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 #include "PreCompiled.h"
00024 
00025 #ifndef _PreComp_
00026 # include <sstream>
00027 #endif
00028 
00029 #include <Base/Matrix.h>
00030 #include <Base/MatrixPy.h>
00031 
00032 #include <App/Document.h>
00033 
00034 #include "Document.h"
00035 #include "ViewProviderExtern.h"
00036 
00037 // inclusion of the generated files (generated out of DocumentPy.xml)
00038 #include "DocumentPy.h"
00039 #include "DocumentPy.cpp"
00040 
00041 using namespace Gui;
00042 
00043 // returns a string which represent the object e.g. when printed in python
00044 std::string DocumentPy::representation(void) const
00045 {
00046     std::stringstream str;
00047     str << "<GUI Document object at " << getDocumentPtr() << ">";
00048 
00049     return str.str();
00050 }
00051 
00052 
00053 PyObject*  DocumentPy::show(PyObject *args)
00054 {
00055     char *psFeatStr;
00056     if (!PyArg_ParseTuple(args, "s;Name of the Feature to show have to be given!",&psFeatStr))     // convert args: Python->C 
00057         return NULL;  // NULL triggers exception 
00058 
00059     PY_TRY {
00060         getDocumentPtr()->setShow(psFeatStr);  
00061         Py_Return;
00062     } PY_CATCH;
00063 }
00064 
00065 PyObject*  DocumentPy::hide(PyObject *args)
00066 {
00067     char *psFeatStr;
00068     if (!PyArg_ParseTuple(args, "s;Name of the Feature to hide have to be given!",&psFeatStr))     // convert args: Python->C 
00069         return NULL;  // NULL triggers exception 
00070 
00071     PY_TRY {
00072         getDocumentPtr()->setHide(psFeatStr);  
00073         Py_Return;
00074     } PY_CATCH;
00075 }
00076 
00077 PyObject*  DocumentPy::setPos(PyObject *args)
00078 {
00079     char *psFeatStr;
00080     Base::Matrix4D mat;
00081     PyObject *pcMatObj;
00082     if (!PyArg_ParseTuple(args, "sO!;Name of the Feature and the transformation matrix have to be given!",
00083                           &psFeatStr,
00084                           &(Base::MatrixPy::Type), &pcMatObj))     // convert args: Python->C 
00085         return NULL;  // NULL triggers exception 
00086 
00087     mat = static_cast<Base::MatrixPy*>(pcMatObj)->value();
00088 
00089     PY_TRY {
00090         getDocumentPtr()->setPos(psFeatStr,mat);  
00091         Py_Return;
00092     } PY_CATCH;
00093 }
00094 
00095 PyObject* DocumentPy::setEdit(PyObject *args)
00096 {
00097     char *psFeatStr;
00098     int mod = 0;
00099     if (!PyArg_ParseTuple(args, "s|i;Name of the object to edit has to be given!",
00100                           &psFeatStr,&mod))     // convert args: Python->C 
00101         return NULL;  // NULL triggers exception 
00102     App::DocumentObject * obj = getDocumentPtr()->getDocument()->getObject(psFeatStr);
00103     if (!obj) {
00104         PyErr_Format(PyExc_Exception, "No such object found in document: '%s'", psFeatStr);
00105         return 0;
00106     }
00107     
00108     bool ok = getDocumentPtr()->setEdit(getDocumentPtr()->getViewProvider(obj),mod);
00109     if (!ok) {
00110         PyErr_Format(PyExc_Exception, "Failed to set object '%s' in edit mode", psFeatStr);
00111         return 0;
00112     }
00113 
00114     Py_Return;
00115 }
00116 
00117 PyObject* DocumentPy::resetEdit(PyObject *args)
00118 {
00119     if (!PyArg_ParseTuple(args, ";No arguments allowed"))     // convert args: Python->C 
00120         return NULL;  // NULL triggers exception 
00121     getDocumentPtr()->resetEdit();
00122 
00123     Py_Return;
00124 }
00125 
00126 PyObject*  DocumentPy::addAnnotation(PyObject *args)
00127 {
00128     char *psAnnoName,*psFileName,*psModName=0;
00129     if (!PyArg_ParseTuple(args, "ss|s;Name of the Annotation and a file name have to be given!",&psAnnoName,&psFileName,&psModName))     // convert args: Python->C 
00130         return NULL;  // NULL triggers exception 
00131 
00132     PY_TRY {
00133         ViewProviderExtern *pcExt = new ViewProviderExtern();
00134 
00135         pcExt->setModeByFile(psModName?psModName:"Main",psFileName);
00136         pcExt->adjustDocumentName(getDocumentPtr()->getDocument()->getName());
00137 
00138         getDocumentPtr()->setAnnotationViewProvider(psAnnoName,pcExt);
00139 
00140         Py_Return;
00141 
00142     } PY_CATCH;
00143 }
00144 
00145 PyObject*  DocumentPy::update(PyObject *args)
00146 {
00147     if (!PyArg_ParseTuple(args, ""))     // convert args: Python->C 
00148         return NULL;                       // NULL triggers exception 
00149 
00150     PY_TRY {
00151         getDocumentPtr()->onUpdate();
00152         Py_Return;
00153     } PY_CATCH;
00154 }
00155 
00156 PyObject*  DocumentPy::getObject(PyObject *args)
00157 {
00158     char *sName;
00159     if (!PyArg_ParseTuple(args, "s",&sName))     // convert args: Python->C 
00160         return NULL;                             // NULL triggers exception 
00161 
00162     PY_TRY {
00163         ViewProvider *pcView = getDocumentPtr()->getViewProviderByName(sName);
00164         if (pcView)
00165             return pcView->getPyObject();
00166         else {
00167             Py_Return;
00168         }
00169     } PY_CATCH;
00170 }
00171 
00172 PyObject*  DocumentPy::activeObject(PyObject *args)
00173 {
00174     if (!PyArg_ParseTuple(args, ""))     // convert args: Python->C 
00175         return NULL;                       // NULL triggers exception 
00176 
00177     PY_TRY {
00178         App::DocumentObject *pcFtr = getDocumentPtr()->getDocument()->getActiveObject();
00179         if (pcFtr) {
00180             ViewProvider *pcView = getDocumentPtr()->getViewProvider(pcFtr);
00181             return pcView->getPyObject();
00182         } else {
00183             Py_Return;
00184         }
00185     } PY_CATCH;
00186 }
00187 
00188 PyObject*  DocumentPy::activeView(PyObject *args)
00189 {
00190     if (!PyArg_ParseTuple(args, ""))     // convert args: Python->C 
00191         return NULL;                             // NULL triggers exception 
00192 
00193     PY_TRY {
00194         Gui::MDIView  *pcView = getDocumentPtr()->getActiveView();
00195         if (pcView){
00196             // already incremented in getPyObject().
00197             return pcView->getPyObject();
00198         } else {
00199             Py_Return;
00200         }
00201     } PY_CATCH;
00202 }
00203 
00204 Py::Object DocumentPy::getActiveObject(void) const
00205 {
00206     App::DocumentObject *object = getDocumentPtr()->getDocument()->getActiveObject();
00207     if (object) {
00208         ViewProvider *viewObj = getDocumentPtr()->getViewProvider(object);
00209         return Py::Object(viewObj->getPyObject(), true);
00210     } else {
00211         return Py::None();
00212     }
00213 }
00214 
00215 void  DocumentPy::setActiveObject(Py::Object arg)
00216 {
00217     throw Py::AttributeError("'Document' object attribute 'ActiveObject' is read-only");
00218 }
00219 
00220 Py::Object DocumentPy::getActiveView(void) const
00221 {
00222     Gui::MDIView *view = getDocumentPtr()->getActiveView();
00223     if (view) {
00224         // already incremented in getPyObject().
00225         return Py::Object(view->getPyObject(), true);
00226     } else {
00227         return Py::None();
00228     }
00229 }
00230 
00231 void  DocumentPy::setActiveView(Py::Object arg)
00232 {
00233     throw Py::AttributeError("'Document' object attribute 'ActiveView' is read-only");
00234 }
00235 
00236 Py::Object DocumentPy::getDocument(void) const
00237 {
00238     App::Document *doc = getDocumentPtr()->getDocument();
00239     if (doc) {
00240         // already incremented in getPyObject().
00241         return Py::Object(doc->getPyObject(), true);
00242     } else {
00243         return Py::None();
00244     }
00245 }
00246 
00247 PyObject *DocumentPy::getCustomAttributes(const char* attr) const
00248 {
00249     // Note: Here we want to return only a document object if its
00250     // name matches 'attr'. However, it is possible to have an object
00251     // with the same name as an attribute. If so, we return 0 as other-
00252     // wise it wouldn't be possible to address this attribute any more.
00253     // The object must then be addressed by the getObject() method directly.
00254     if (this->ob_type->tp_dict == NULL) {
00255         if (PyType_Ready(this->ob_type) < 0)
00256             return 0;
00257     }
00258     PyObject* item = PyDict_GetItemString(this->ob_type->tp_dict, attr);
00259     if (item) return 0;
00260     // search for an object with this name
00261     ViewProvider* obj = getDocumentPtr()->getViewProviderByName(attr);
00262     return (obj ? obj->getPyObject() : 0);
00263 }
00264 
00265 int DocumentPy::setCustomAttributes(const char* attr, PyObject *)
00266 {
00267     // Note: Here we want to return only a document object if its
00268     // name matches 'attr'. However, it is possible to have an object
00269     // with the same name as an attribute. If so, we return 0 as other-
00270     // wise it wouldn't be possible to address this attribute any more.
00271     // The object must then be addressed by the getObject() method directly.
00272     if (this->ob_type->tp_dict == NULL) {
00273         if (PyType_Ready(this->ob_type) < 0)
00274             return 0;
00275     }
00276     PyObject* item = PyDict_GetItemString(this->ob_type->tp_dict, attr);
00277     if (item) return 0;
00278     ViewProvider* obj = getDocumentPtr()->getViewProviderByName(attr);
00279     if (obj) {
00280         std::stringstream str;
00281         str << "'Document' object attribute '" << attr 
00282             << "' must not be set this way" << std::ends;
00283         throw Py::AttributeError(str.str());
00284     }
00285     
00286     return 0;
00287 }
00288 
00289 

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