DocumentObjectGroupPyImp.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 
00026 #include "DocumentObjectGroup.h"
00027 #include "Document.h"
00028 
00029 // inclusion of the generated files (generated out of DocumentObjectGroupPy.xml)
00030 #include "DocumentObjectGroupPy.h"
00031 #include "DocumentObjectGroupPy.cpp"
00032 
00033 using namespace App;
00034 
00035 // returns a string which represent the object e.g. when printed in python
00036 std::string DocumentObjectGroupPy::representation(void) const
00037 {
00038     return std::string("<group object>");
00039 }
00040 
00041 PyObject*  DocumentObjectGroupPy::newObject(PyObject *args)
00042 {
00043     char *sType,*sName=0;
00044     if (!PyArg_ParseTuple(args, "s|s", &sType,&sName))     // convert args: Python->C
00045         return NULL;
00046 
00047     DocumentObject *object = getDocumentObjectGroupPtr()->addObject(sType, sName);
00048     if ( object ) {
00049         return object->getPyObject();
00050     } 
00051     else {
00052         PyErr_Format(PyExc_Exception, "Cannot create object of type '%s'", sType);
00053         return NULL;
00054     }
00055 }
00056 
00057 PyObject*  DocumentObjectGroupPy::addObject(PyObject *args)
00058 {
00059     PyObject *object;
00060     if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object))     // convert args: Python->C 
00061         return NULL;                             // NULL triggers exception 
00062 
00063     DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
00064     if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) {
00065         PyErr_SetString(PyExc_Exception, "Cannot add an invalid object");
00066         return NULL;
00067     }
00068     if (docObj->getDocumentObjectPtr()->getDocument() != getDocumentObjectGroupPtr()->getDocument()) {
00069         PyErr_SetString(PyExc_Exception, "Cannot add an object from another document to this group");
00070         return NULL;
00071     }
00072     if (docObj->getDocumentObjectPtr() == this->getDocumentObjectGroupPtr()) {
00073         PyErr_SetString(PyExc_Exception, "Cannot add a group object to itself");
00074         return NULL;
00075     }
00076     if (docObj->getDocumentObjectPtr()->getTypeId().isDerivedFrom(DocumentObjectGroup::getClassTypeId())) {
00077         App::DocumentObjectGroup* docGrp = static_cast<DocumentObjectGroup*>(docObj->getDocumentObjectPtr());
00078         if (this->getDocumentObjectGroupPtr()->isChildOf(docGrp)) {
00079             PyErr_SetString(PyExc_Exception, "Cannot add a group object to a child group");
00080             return NULL;
00081         }
00082     }
00083 
00084     getDocumentObjectGroupPtr()->addObject(docObj->getDocumentObjectPtr());
00085     Py_Return;
00086 }
00087 
00088 PyObject*  DocumentObjectGroupPy::removeObject(PyObject *args)
00089 {
00090     PyObject *object;
00091     if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object))     // convert args: Python->C 
00092         return NULL;                             // NULL triggers exception 
00093 
00094     DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
00095     if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) {
00096         PyErr_SetString(PyExc_Exception, "Cannot remove an invalid object");
00097         return NULL;
00098     }
00099     if (docObj->getDocumentObjectPtr()->getDocument() != getDocumentObjectGroupPtr()->getDocument()) {
00100         PyErr_SetString(PyExc_Exception, "Cannot remove an object from another document from this group");
00101         return NULL;
00102     }
00103 
00104     getDocumentObjectGroupPtr()->removeObject(docObj->getDocumentObjectPtr());
00105     Py_Return;
00106 }
00107 
00108 PyObject*  DocumentObjectGroupPy::removeObjectsFromDocument(PyObject *args)
00109 {
00110     if (!PyArg_ParseTuple(args, ""))     // convert args: Python->C 
00111         return NULL;                    // NULL triggers exception 
00112 
00113     getDocumentObjectGroupPtr()->removeObjectsFromDocument();
00114     Py_Return;
00115 }
00116 
00117 PyObject*  DocumentObjectGroupPy::getObject(PyObject *args)
00118 {
00119     char* pcName;
00120     if (!PyArg_ParseTuple(args, "s", &pcName))     // convert args: Python->C 
00121         return NULL;                    // NULL triggers exception 
00122 
00123     DocumentObject* obj = getDocumentObjectGroupPtr()->getObject(pcName);
00124     if ( obj ) {
00125         return obj->getPyObject();
00126     } else {
00127         Py_Return;
00128     }
00129 }
00130 
00131 PyObject*  DocumentObjectGroupPy::hasObject(PyObject *args)
00132 {
00133     PyObject *object;
00134     if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object))     // convert args: Python->C 
00135         return NULL;                             // NULL triggers exception 
00136 
00137     DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
00138     if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) {
00139         PyErr_SetString(PyExc_Exception, "Cannot check an invalid object");
00140         return NULL;
00141     }
00142     if (docObj->getDocumentObjectPtr()->getDocument() != getDocumentObjectGroupPtr()->getDocument()) {
00143         PyErr_SetString(PyExc_Exception, "Cannot check an object from another document with this group");
00144         return NULL;
00145     }
00146 
00147     if (getDocumentObjectGroupPtr()->hasObject(docObj->getDocumentObjectPtr())) {
00148         Py_INCREF(Py_True);
00149         return Py_True;
00150     } 
00151     else {
00152         Py_INCREF(Py_False);
00153         return Py_False;
00154     }
00155 }
00156 
00157 PyObject *DocumentObjectGroupPy::getCustomAttributes(const char* /*attr*/) const
00158 {
00159     return 0;
00160 }
00161 
00162 int DocumentObjectGroupPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00163 {
00164     return 0; 
00165 }
00166 

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