FreeCAD

Форк
0
/
GroupExtensionPyImp.cpp 
286 строк · 10.8 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2007 Werner Mayer <wmayer[at]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 "DocumentObject.h"
27

28
// inclusion of the generated files (generated out of GroupExtensionPy.xml)
29
#include "GroupExtensionPy.h"
30
#include "GroupExtensionPy.cpp"
31
#include "DocumentObjectPy.h"
32

33

34
using namespace App;
35

36
// returns a string which represent the object e.g. when printed in python
37
std::string GroupExtensionPy::representation() const
38
{
39
    return {"<group extension object>"};
40
}
41

42
PyObject*  GroupExtensionPy::newObject(PyObject *args)
43
{
44
    char *sType,*sName=nullptr;
45
    if (!PyArg_ParseTuple(args, "s|s", &sType,&sName))
46
        return nullptr;
47

48
    DocumentObject *object = getGroupExtensionPtr()->addObject(sType, sName);
49
    if ( object ) {
50
        return object->getPyObject();
51
    }
52
    else {
53
        PyErr_Format(PyExc_TypeError, "Cannot create object of type '%s'", sType);
54
        return nullptr;
55
    }
56
}
57

58
PyObject*  GroupExtensionPy::addObject(PyObject *args)
59
{
60
    PyObject *object;
61
    if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object))
62
        return nullptr;
63

64
    DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
65
    if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->isAttachedToDocument()) {
66
        PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot add an invalid object");
67
        return nullptr;
68
    }
69
    
70
    if (docObj->getDocumentObjectPtr()->getDocument() != getGroupExtensionPtr()->getExtendedObject()->getDocument()) {
71
        PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot add an object from another document to this group");
72
        return nullptr;
73
    }
74
    if (docObj->getDocumentObjectPtr() == this->getGroupExtensionPtr()->getExtendedObject()) {
75
        PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot add a group object to itself");
76
        return nullptr;
77
    }
78
    if (docObj->getDocumentObjectPtr()->hasExtension(GroupExtension::getExtensionClassTypeId())) {
79
        App::GroupExtension* docGrp = docObj->getDocumentObjectPtr()->getExtensionByType<GroupExtension>();
80
        if (docGrp->hasObject(getGroupExtensionPtr()->getExtendedObject())) {
81
            PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot add a group object to a child group");
82
            return nullptr;
83
        }
84
    }
85

86
    GroupExtension* grp = getGroupExtensionPtr();
87

88
    auto vec = grp->addObject(docObj->getDocumentObjectPtr()); 
89
    Py::List list;
90
    for (App::DocumentObject* obj : vec)
91
        list.append(Py::asObject(obj->getPyObject()));
92

93
    return Py::new_reference_to(list);
94
}
95

96
PyObject* GroupExtensionPy::addObjects(PyObject *args) {
97
    
98
    PyObject *object;
99
    if (!PyArg_ParseTuple(args, "O", &object))
100
        return nullptr;
101
        
102
    if (PyTuple_Check(object) || PyList_Check(object)) {
103
        Py::Sequence list(object);
104
        Py::Sequence::size_type size = list.size();
105
        std::vector<DocumentObject*> values;
106
        values.resize(size);
107

108
        for (Py::Sequence::size_type i = 0; i < size; i++) {
109
            Py::Object item = list[i];
110
            if (!PyObject_TypeCheck(*item, &(DocumentObjectPy::Type))) {
111
                std::string error = std::string("type in list must be 'DocumentObject', not ");
112
                error += (*item)->ob_type->tp_name;
113
                throw Base::TypeError(error);
114
            }
115

116
            values[i] = static_cast<DocumentObjectPy*>(*item)->getDocumentObjectPtr();
117
        }
118

119
        GroupExtension* grp = getGroupExtensionPtr();
120
        auto vec = grp->addObjects(values); 
121
        Py::List result;
122
        for (App::DocumentObject* obj : vec)
123
            result.append(Py::asObject(obj->getPyObject()));
124

125
        return Py::new_reference_to(result);
126
    }
127
    
128
    std::string error = std::string("type must be list of 'DocumentObject', not ");
129
    error += object->ob_type->tp_name;
130
    throw Base::TypeError(error);
131
}
132

133
PyObject* GroupExtensionPy::setObjects(PyObject *args) {
134

135
    PyObject *object;
136
    if (!PyArg_ParseTuple(args, "O", &object))
137
        return nullptr;
138

139
    if (PyTuple_Check(object) || PyList_Check(object)) {
140
        Py::Sequence list(object);
141
        Py::Sequence::size_type size = list.size();
142
        std::vector<DocumentObject*> values;
143
        values.resize(size);
144

145
        for (Py::Sequence::size_type i = 0; i < size; i++) {
146
            Py::Object item = list[i];
147
            if (!PyObject_TypeCheck(*item, &(DocumentObjectPy::Type))) {
148
                std::string error = std::string("type in list must be 'DocumentObject', not ");
149
                error += (*item)->ob_type->tp_name;
150
                throw Base::TypeError(error);
151
            }
152

153
            values[i] = static_cast<DocumentObjectPy*>(*item)->getDocumentObjectPtr();
154
        }
155

156
        GroupExtension* grp = getGroupExtensionPtr();
157
        auto vec = grp->setObjects(values); 
158
        Py::List result;
159
        for (App::DocumentObject* obj : vec)
160
            result.append(Py::asObject(obj->getPyObject()));
161

162
        return Py::new_reference_to(result);
163
    }
164
    
165
    std::string error = std::string("type must be list of 'DocumentObject', not ");
166
    error += object->ob_type->tp_name;
167
    throw Base::TypeError(error);
168
}
169

170
PyObject*  GroupExtensionPy::removeObject(PyObject *args)
171
{
172
    PyObject *object;
173
    if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object))
174
        return nullptr;
175

176
    DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
177
    if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->isAttachedToDocument()) {
178
        PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot remove an invalid object");
179
        return nullptr;
180
    }
181
    if (docObj->getDocumentObjectPtr()->getDocument() != getGroupExtensionPtr()->getExtendedObject()->getDocument()) {
182
        PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot remove an object from another document from this group");
183
        return nullptr;
184
    }
185

186
    GroupExtension* grp = getGroupExtensionPtr();
187

188
    auto vec = grp->removeObject(docObj->getDocumentObjectPtr());
189
    Py::List list;
190
    for (App::DocumentObject* obj : vec)
191
        list.append(Py::asObject(obj->getPyObject()));
192

193
    return Py::new_reference_to(list);
194
}
195

196
PyObject* GroupExtensionPy::removeObjects(PyObject *args) {
197

198
    PyObject *object;
199
    if (!PyArg_ParseTuple(args, "O", &object))
200
        return nullptr;
201
        
202
    if (PyTuple_Check(object) || PyList_Check(object)) {
203
        Py::Sequence list(object);
204
        Py::Sequence::size_type size = list.size();
205
        std::vector<DocumentObject*> values;
206
        values.resize(size);
207

208
        for (Py::Sequence::size_type i = 0; i < size; i++) {
209
            Py::Object item = list[i];
210
            if (!PyObject_TypeCheck(*item, &(DocumentObjectPy::Type))) {
211
                std::string error = std::string("type in list must be 'DocumentObject', not ");
212
                error += (*item)->ob_type->tp_name;
213
                throw Base::TypeError(error);
214
            }
215

216
            values[i] = static_cast<DocumentObjectPy*>(*item)->getDocumentObjectPtr();
217
        }
218

219
        GroupExtension* grp = getGroupExtensionPtr();
220
        auto vec = grp->removeObjects(values); 
221
        Py::List result;
222
        for (App::DocumentObject* obj : vec)
223
            result.append(Py::asObject(obj->getPyObject()));
224

225
        return Py::new_reference_to(result);
226
    }
227

228
    std::string error = std::string("type must be list of 'DocumentObject', not ");
229
    error += object->ob_type->tp_name;
230
    throw Base::TypeError(error);
231
}
232

233
PyObject*  GroupExtensionPy::removeObjectsFromDocument(PyObject *args)
234
{
235
    if (!PyArg_ParseTuple(args, ""))
236
        return nullptr;
237

238
    getGroupExtensionPtr()->removeObjectsFromDocument();
239
    Py_Return;
240
}
241

242
PyObject*  GroupExtensionPy::getObject(PyObject *args)
243
{
244
    char* pcName;
245
    if (!PyArg_ParseTuple(args, "s", &pcName))
246
        return nullptr;
247

248
    DocumentObject* obj = getGroupExtensionPtr()->getObject(pcName);
249
    if ( obj ) {
250
        return obj->getPyObject();
251
    } else {
252
        Py_Return;
253
    }
254
}
255

256
PyObject*  GroupExtensionPy::hasObject(PyObject *args)
257
{
258
    PyObject *object;
259
    PyObject *recursivePy = Py_False;
260
    if (!PyArg_ParseTuple(args, "O!|O!", &(DocumentObjectPy::Type), &object, &PyBool_Type, &recursivePy))
261
        return nullptr;
262

263
    DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
264
    bool recursive = Base::asBoolean(recursivePy);
265
    if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->isAttachedToDocument()) {
266
        PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot check an invalid object");
267
        return nullptr;
268
    }
269
    if (docObj->getDocumentObjectPtr()->getDocument() != getGroupExtensionPtr()->getExtendedObject()->getDocument()) {
270
        PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot check an object from another document with this group");
271
        return nullptr;
272
    }
273

274
    bool v = getGroupExtensionPtr()->hasObject(docObj->getDocumentObjectPtr(), recursive);
275
    return PyBool_FromLong(v ? 1 : 0);
276
}
277

278
PyObject *GroupExtensionPy::getCustomAttributes(const char* /*attr*/) const
279
{
280
    return nullptr;
281
}
282

283
int GroupExtensionPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
284
{
285
    return 0;
286
}
287

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.