00001 /*************************************************************************** 00002 * Copyright (c) Jürgen Riegel (juergen.riegel@web.de) 2007 * 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 #include "DocumentObject.h" 00026 #include "Document.h" 00027 00028 // inclusion of the generated files (generated out of DocumentObjectPy.xml) 00029 #include "DocumentObjectPy.h" 00030 #include "DocumentObjectPy.cpp" 00031 00032 using namespace App; 00033 00034 // returns a string which represent the object e.g. when printed in python 00035 std::string DocumentObjectPy::representation(void) const 00036 { 00037 return std::string("<Document object>"); 00038 } 00039 00040 Py::String DocumentObjectPy::getName(void) const 00041 { 00042 DocumentObject* object = this->getDocumentObjectPtr(); 00043 const char* internal = object->getNameInDocument(); 00044 if (!internal) { 00045 throw Py::RuntimeError(std::string("This object is currently not part of a document")); 00046 } 00047 return Py::String(std::string(internal)); 00048 } 00049 00050 Py::Object DocumentObjectPy::getDocument(void) const 00051 { 00052 DocumentObject* object = this->getDocumentObjectPtr(); 00053 Document* doc = object->getDocument(); 00054 if (!doc) { 00055 return Py::None(); 00056 } 00057 else { 00058 return Py::Object(doc->getPyObject(), true); 00059 } 00060 } 00061 00062 PyObject* DocumentObjectPy::touch(PyObject * args) 00063 { 00064 if (!PyArg_ParseTuple(args, "")) // convert args: Python->C 00065 return NULL; // NULL triggers exception 00066 getDocumentObjectPtr()->touch(); 00067 Py_Return; 00068 } 00069 00070 PyObject* DocumentObjectPy::purgeTouched(PyObject * args) 00071 { 00072 if (!PyArg_ParseTuple(args, "")) // convert args: Python->C 00073 return NULL; // NULL triggers exception 00074 getDocumentObjectPtr()->purgeTouched(); 00075 Py_Return; 00076 } 00077 00078 Py::List DocumentObjectPy::getState(void) const 00079 { 00080 DocumentObject* object = this->getDocumentObjectPtr(); 00081 Py::List list; 00082 bool uptodate = true; 00083 if (object->isTouched()) { 00084 uptodate = false; 00085 list.append(Py::String("Touched")); 00086 } 00087 if (object->isError()) { 00088 uptodate = false; 00089 list.append(Py::String("Invalid")); 00090 } 00091 if (uptodate) { 00092 list.append(Py::String("Up-to-date")); 00093 } 00094 return list; 00095 } 00096 00097 Py::Object DocumentObjectPy::getViewObject(void) const 00098 { 00099 try { 00100 Py::Module module(PyImport_ImportModule("FreeCADGui"),true); 00101 Py::Callable method(module.getAttr("getDocument")); 00102 Py::Tuple arg(1); 00103 arg.setItem(0, Py::String(getDocumentObjectPtr()->getDocument()->getName())); 00104 Py::Object doc = method.apply(arg); 00105 method = doc.getAttr("getObject"); 00106 arg.setItem(0, Py::String(getDocumentObjectPtr()->getNameInDocument())); 00107 Py::Object obj = method.apply(arg); 00108 return obj; 00109 } 00110 catch (Py::Exception& e) { 00111 if (PyErr_ExceptionMatches(PyExc_ImportError)) { 00112 // the GUI is not up, hence None is returned 00113 e.clear(); 00114 return Py::None(); 00115 } 00116 // FreeCADGui is loaded, so there must be wrong something else 00117 throw; // re-throw 00118 } 00119 } 00120 00121 Py::List DocumentObjectPy::getInList(void) const 00122 { 00123 Py::List ret; 00124 std::vector<DocumentObject*> list = getDocumentObjectPtr()->getInList(); 00125 00126 for (std::vector<DocumentObject*>::iterator It=list.begin();It!=list.end();++It) 00127 ret.append(Py::Object((*It)->getPyObject(), true)); 00128 00129 return ret; 00130 } 00131 00132 Py::List DocumentObjectPy::getOutList(void) const 00133 { 00134 Py::List ret; 00135 std::vector<DocumentObject*> list = getDocumentObjectPtr()->getOutList(); 00136 00137 for (std::vector<DocumentObject*>::iterator It=list.begin();It!=list.end();++It) 00138 ret.append(Py::Object((*It)->getPyObject(), true)); 00139 00140 return ret; 00141 } 00142 00143 PyObject *DocumentObjectPy::getCustomAttributes(const char* /*attr*/) const 00144 { 00145 return 0; 00146 } 00147 00148 int DocumentObjectPy::setCustomAttributes(const char* attr, PyObject *obj) 00149 { 00150 // search in PropertyList 00151 Property *prop = getDocumentObjectPtr()->getPropertyByName(attr); 00152 if (prop) { 00153 // Read-only attributes must not be set over its Python interface 00154 short Type = getDocumentObjectPtr()->getPropertyType(prop); 00155 if (Type & Prop_ReadOnly) { 00156 std::stringstream s; 00157 s << "'DocumentObject' attribute '" << attr << "' is read-only"; 00158 throw Py::AttributeError(s.str()); 00159 } 00160 00161 prop->setPyObject(obj); 00162 return 1; 00163 } 00164 00165 return 0; 00166 }