~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/App/DocumentObjectGroupPyImp.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
ImportĀ upstreamĀ versionĀ 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (c) 2007 Werner Mayer <wmayer@users.sourceforge.net>        *
 
3
 *                                                                         *
 
4
 *   This file is part of the FreeCAD CAx development system.              *
 
5
 *                                                                         *
 
6
 *   This library is free software; you can redistribute it and/or         *
 
7
 *   modify it under the terms of the GNU Library General Public           *
 
8
 *   License as published by the Free Software Foundation; either          *
 
9
 *   version 2 of the License, or (at your option) any later version.      *
 
10
 *                                                                         *
 
11
 *   This library  is distributed in the hope that it will be useful,      *
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
14
 *   GNU Library General Public License for more details.                  *
 
15
 *                                                                         *
 
16
 *   You should have received a copy of the GNU Library General Public     *
 
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
 
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 
20
 *                                                                         *
 
21
 ***************************************************************************/
 
22
 
 
23
 
 
24
#include "PreCompiled.h"
 
25
 
 
26
#include "DocumentObjectGroup.h"
 
27
#include "Document.h"
 
28
 
 
29
// inclusion of the generated files (generated out of DocumentObjectGroupPy.xml)
 
30
#include "DocumentObjectGroupPy.h"
 
31
#include "DocumentObjectGroupPy.cpp"
 
32
 
 
33
using namespace App;
 
34
 
 
35
// returns a string which represent the object e.g. when printed in python
 
36
const char *DocumentObjectGroupPy::representation(void) const
 
37
{
 
38
    return "<group object>";
 
39
}
 
40
 
 
41
PyObject*  DocumentObjectGroupPy::newObject(PyObject *args)
 
42
{
 
43
    char *sType,*sName=0;
 
44
    if (!PyArg_ParseTuple(args, "s|s", &sType,&sName))     // convert args: Python->C
 
45
        return NULL;
 
46
 
 
47
    DocumentObject *object = getDocumentObjectGroupPtr()->addObject(sType, sName);
 
48
    if ( object ) {
 
49
        return object->getPyObject();
 
50
    } 
 
51
    else {
 
52
        PyErr_Format(PyExc_Exception, "Cannot create object of type '%s'", sType);
 
53
        return NULL;
 
54
    }
 
55
}
 
56
 
 
57
PyObject*  DocumentObjectGroupPy::addObject(PyObject *args)
 
58
{
 
59
    PyObject *object;
 
60
    if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object))     // convert args: Python->C 
 
61
        return NULL;                             // NULL triggers exception 
 
62
 
 
63
    DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
 
64
    if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) {
 
65
        PyErr_SetString(PyExc_Exception, "Cannot add an invalid object");
 
66
        return NULL;
 
67
    }
 
68
    if (docObj->getDocumentObjectPtr()->getDocument() != getDocumentObjectGroupPtr()->getDocument()) {
 
69
        PyErr_SetString(PyExc_Exception, "Cannot add an object from another document to this group");
 
70
        return NULL;
 
71
    }
 
72
    if (docObj->getDocumentObjectPtr() == this->getDocumentObjectGroupPtr()) {
 
73
        PyErr_SetString(PyExc_Exception, "Cannot add a group object to itself");
 
74
        return NULL;
 
75
    }
 
76
    if (docObj->getDocumentObjectPtr()->getTypeId().isDerivedFrom(DocumentObjectGroup::getClassTypeId())) {
 
77
        App::DocumentObjectGroup* docGrp = static_cast<DocumentObjectGroup*>(docObj->getDocumentObjectPtr());
 
78
        if (this->getDocumentObjectGroupPtr()->isChildOf(docGrp)) {
 
79
            PyErr_SetString(PyExc_Exception, "Cannot add a group object to a child group");
 
80
            return NULL;
 
81
        }
 
82
    }
 
83
 
 
84
    getDocumentObjectGroupPtr()->addObject(docObj->getDocumentObjectPtr());
 
85
    Py_Return;
 
86
}
 
87
 
 
88
PyObject*  DocumentObjectGroupPy::removeObject(PyObject *args)
 
89
{
 
90
    PyObject *object;
 
91
    if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object))     // convert args: Python->C 
 
92
        return NULL;                             // NULL triggers exception 
 
93
 
 
94
    DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
 
95
    if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) {
 
96
        PyErr_SetString(PyExc_Exception, "Cannot remove an invalid object");
 
97
        return NULL;
 
98
    }
 
99
    if (docObj->getDocumentObjectPtr()->getDocument() != getDocumentObjectGroupPtr()->getDocument()) {
 
100
        PyErr_SetString(PyExc_Exception, "Cannot remove an object from another document from this group");
 
101
        return NULL;
 
102
    }
 
103
 
 
104
    getDocumentObjectGroupPtr()->removeObject(docObj->getDocumentObjectPtr());
 
105
    Py_Return;
 
106
}
 
107
 
 
108
PyObject*  DocumentObjectGroupPy::removeObjectsFromDocument(PyObject *args)
 
109
{
 
110
    if (!PyArg_ParseTuple(args, ""))     // convert args: Python->C 
 
111
        return NULL;                    // NULL triggers exception 
 
112
 
 
113
    getDocumentObjectGroupPtr()->removeObjectsFromDocument();
 
114
    Py_Return;
 
115
}
 
116
 
 
117
PyObject*  DocumentObjectGroupPy::getObject(PyObject *args)
 
118
{
 
119
    char* pcName;
 
120
    if (!PyArg_ParseTuple(args, "s", &pcName))     // convert args: Python->C 
 
121
        return NULL;                    // NULL triggers exception 
 
122
 
 
123
    DocumentObject* obj = getDocumentObjectGroupPtr()->getObject(pcName);
 
124
    if ( obj ) {
 
125
        return obj->getPyObject();
 
126
    } else {
 
127
        Py_Return;
 
128
    }
 
129
}
 
130
 
 
131
PyObject*  DocumentObjectGroupPy::hasObject(PyObject *args)
 
132
{
 
133
    PyObject *object;
 
134
    if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object))     // convert args: Python->C 
 
135
        return NULL;                             // NULL triggers exception 
 
136
 
 
137
    DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
 
138
    if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) {
 
139
        PyErr_SetString(PyExc_Exception, "Cannot check an invalid object");
 
140
        return NULL;
 
141
    }
 
142
    if (docObj->getDocumentObjectPtr()->getDocument() != getDocumentObjectGroupPtr()->getDocument()) {
 
143
        PyErr_SetString(PyExc_Exception, "Cannot check an object from another document with this group");
 
144
        return NULL;
 
145
    }
 
146
 
 
147
    if (getDocumentObjectGroupPtr()->hasObject(docObj->getDocumentObjectPtr())) {
 
148
        Py_INCREF(Py_True);
 
149
        return Py_True;
 
150
    } 
 
151
    else {
 
152
        Py_INCREF(Py_False);
 
153
        return Py_False;
 
154
    }
 
155
}
 
156
 
 
157
PyObject *DocumentObjectGroupPy::getCustomAttributes(const char* /*attr*/) const
 
158
{
 
159
    return 0;
 
160
}
 
161
 
 
162
int DocumentObjectGroupPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
 
163
{
 
164
    return 0; 
 
165
}
 
166