FreeCAD

Форк
0
/
GeometryPyImp.cpp 
442 строки · 14.2 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2008 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
#include "PreCompiled.h"
24

25
#ifndef _PreComp_
26
# include <boost/uuid/uuid_io.hpp>
27
#endif
28

29
#include <Base/GeometryPyCXX.h>
30
#include <Base/Matrix.h>
31
#include <Base/MatrixPy.h>
32
#include <Base/Placement.h>
33
#include <Base/PlacementPy.h>
34
#include <Base/Vector3D.h>
35
#include <Base/VectorPy.h>
36

37
#include "GeometryPy.h"
38
#include "GeometryPy.cpp"
39
#include "GeometryExtensionPy.h"
40
#include "OCCError.h"
41

42

43
using namespace Part;
44

45
// returns a string which represents the object e.g. when printed in python
46
std::string GeometryPy::representation() const
47
{
48
    return "<Geometry object>";
49
}
50

51
PyObject *GeometryPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
52
{
53
    // never create such objects with the constructor
54
    PyErr_SetString(PyExc_RuntimeError,
55
        "You cannot create an instance of the abstract class 'Geometry'.");
56
    return nullptr;
57
}
58

59
// constructor method
60
int GeometryPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
61
{
62
    return 0;
63
}
64

65
PyObject* GeometryPy::mirror(PyObject *args)
66
{
67
    PyObject* o;
68
    if (PyArg_ParseTuple(args, "O!", &(Base::VectorPy::Type),&o)) {
69
        Base::Vector3d vec = static_cast<Base::VectorPy*>(o)->value();
70
        getGeometryPtr()->mirror(vec);
71
        Py_Return;
72
    }
73

74
    PyErr_Clear();
75
    PyObject* axis;
76
    if (PyArg_ParseTuple(args, "O!O!", &(Base::VectorPy::Type),&o,
77
                                       &(Base::VectorPy::Type),&axis)) {
78
        Base::Vector3d pnt = static_cast<Base::VectorPy*>(o)->value();
79
        Base::Vector3d dir = static_cast<Base::VectorPy*>(axis)->value();
80
        getGeometryPtr()->mirror(pnt, dir);
81
        Py_Return;
82
    }
83

84
    PyErr_SetString(PartExceptionOCCError, "either a point (vector) or axis (vector, vector) must be given");
85
    return nullptr;
86
}
87

88
PyObject* GeometryPy::rotate(PyObject *args)
89
{
90
    PyObject* o;
91
    if (!PyArg_ParseTuple(args, "O!", &(Base::PlacementPy::Type),&o))
92
        return nullptr;
93

94
    Base::Placement* plm = static_cast<Base::PlacementPy*>(o)->getPlacementPtr();
95
    getGeometryPtr()->rotate(*plm);
96
    Py_Return;
97
}
98

99
PyObject* GeometryPy::scale(PyObject *args)
100
{
101
    PyObject* o;
102
    double scale;
103
    Base::Vector3d vec;
104
    if (PyArg_ParseTuple(args, "O!d", &(Base::VectorPy::Type),&o, &scale)) {
105
        vec = static_cast<Base::VectorPy*>(o)->value();
106
        getGeometryPtr()->scale(vec, scale);
107
        Py_Return;
108
    }
109

110
    PyErr_Clear();
111
    if (PyArg_ParseTuple(args, "O!d", &PyTuple_Type,&o, &scale)) {
112
        vec = Base::getVectorFromTuple<double>(o);
113
        getGeometryPtr()->scale(vec, scale);
114
        Py_Return;
115
    }
116

117
    PyErr_SetString(PartExceptionOCCError, "either vector or tuple and float expected");
118
    return nullptr;
119
}
120

121
PyObject* GeometryPy::transform(PyObject *args)
122
{
123
    PyObject* o;
124
    if (!PyArg_ParseTuple(args, "O!", &(Base::MatrixPy::Type),&o))
125
        return nullptr;
126
    Base::Matrix4D mat = static_cast<Base::MatrixPy*>(o)->value();
127
    getGeometryPtr()->transform(mat);
128
    Py_Return;
129
}
130

131
PyObject* GeometryPy::translate(PyObject *args)
132
{
133
    PyObject* o;
134
    Base::Vector3d vec;
135
    if (PyArg_ParseTuple(args, "O!", &(Base::VectorPy::Type),&o)) {
136
        vec = static_cast<Base::VectorPy*>(o)->value();
137
        getGeometryPtr()->translate(vec);
138
        Py_Return;
139
    }
140

141
    PyErr_Clear();
142
    if (PyArg_ParseTuple(args, "O!", &PyTuple_Type,&o)) {
143
        vec = Base::getVectorFromTuple<double>(o);
144
        getGeometryPtr()->translate(vec);
145
        Py_Return;
146
    }
147

148
    PyErr_SetString(PartExceptionOCCError, "either vector or tuple expected");
149
    return nullptr;
150
}
151

152
PyObject* GeometryPy::copy(PyObject *args)
153
{
154
    if (!PyArg_ParseTuple(args, ""))
155
        return nullptr;
156

157
    Part::Geometry* geom = this->getGeometryPtr();
158
    PyTypeObject* type = this->GetType();
159
    PyObject* cpy = nullptr;
160
    // let the type object decide
161
    if (type->tp_new)
162
        cpy = type->tp_new(type, this, nullptr);
163
    if (!cpy) {
164
        PyErr_SetString(PyExc_TypeError, "failed to create copy of geometry");
165
        return nullptr;
166
    }
167

168
    Part::GeometryPy* geompy = static_cast<Part::GeometryPy*>(cpy);
169
    // the PyMake function must have created the corresponding instance of the 'Geometry' subclass
170
    // so delete it now to avoid a memory leak
171
    if (geompy->_pcTwinPointer) {
172
        Part::Geometry* clone = static_cast<Part::Geometry*>(geompy->_pcTwinPointer);
173
        delete clone;
174
    }
175
    geompy->_pcTwinPointer = geom->copy();
176
    return cpy;
177
}
178

179
PyObject* GeometryPy::clone(PyObject *args)
180
{
181
    if (!PyArg_ParseTuple(args, ""))
182
        return nullptr;
183

184
    Part::Geometry* geom = this->getGeometryPtr();
185
    PyTypeObject* type = this->GetType();
186
    PyObject* cpy = nullptr;
187
    // let the type object decide
188
    if (type->tp_new)
189
        cpy = type->tp_new(type, this, nullptr);
190
    if (!cpy) {
191
        PyErr_SetString(PyExc_TypeError, "failed to create clone of geometry");
192
        return nullptr;
193
    }
194

195
    Part::GeometryPy* geompy = static_cast<Part::GeometryPy*>(cpy);
196
    // the PyMake function must have created the corresponding instance of the 'Geometry' subclass
197
    // so delete it now to avoid a memory leak
198
    if (geompy->_pcTwinPointer) {
199
        Part::Geometry* clone = static_cast<Part::Geometry*>(geompy->_pcTwinPointer);
200
        delete clone;
201
    }
202
    geompy->_pcTwinPointer = geom->clone();
203
    return cpy;
204
}
205

206
PyObject* GeometryPy::setExtension(PyObject *args)
207
{
208
    PyObject* o;
209
    if (PyArg_ParseTuple(args, "O!", &(GeometryExtensionPy::Type),&o)) {
210
        Part::GeometryExtension * ext;
211
        ext = static_cast<GeometryExtensionPy *>(o)->getGeometryExtensionPtr();
212

213
        // make copy of Python managed memory and wrap it in smart pointer
214
        auto cpy = ext->copy();
215

216
        this->getGeometryPtr()->setExtension(std::move(cpy));
217
        Py_Return;
218
    }
219

220
    PyErr_SetString(PartExceptionOCCError, "A geometry extension object was expected");
221
    return nullptr;
222
}
223

224
PyObject* GeometryPy::getExtensionOfType(PyObject *args)
225
{
226
    char* o;
227
    if (PyArg_ParseTuple(args, "s", &o)) {
228

229
        Base::Type type = Base::Type::fromName(o);
230

231
        if(type != Base::Type::badType()) {
232
            try {
233
                std::shared_ptr<const GeometryExtension> ext(this->getGeometryPtr()->getExtension(type));
234

235
                // we create a copy and transfer this copy's memory management responsibility to Python
236
                PyObject* cpy = ext->copyPyObject();
237
                return cpy;
238
            }
239
            catch(const Base::ValueError& e) {
240
                PyErr_SetString(PartExceptionOCCError, e.what());
241
                return nullptr;
242
            }
243
            catch(const std::bad_weak_ptr&) {
244
                PyErr_SetString(PartExceptionOCCError, "Geometry extension does not exist anymore.");
245
                return nullptr;
246
            }
247
            catch(Base::NotImplementedError&) {
248
                PyErr_SetString(Part::PartExceptionOCCError, "Geometry extension does not implement a Python counterpart.");
249
                return nullptr;
250
            }
251
        }
252
        else
253
        {
254
            PyErr_SetString(PartExceptionOCCError, "Exception type does not exist");
255
            return nullptr;
256
        }
257

258
    }
259

260
    PyErr_SetString(PartExceptionOCCError, "A string with the name of the geometry extension type was expected");
261
    return nullptr;
262
}
263

264
PyObject* GeometryPy::getExtensionOfName(PyObject *args)
265
{
266
    char* o;
267
    if (PyArg_ParseTuple(args, "s", &o)) {
268

269
        try {
270
            std::shared_ptr<const GeometryExtension> ext(this->getGeometryPtr()->getExtension(std::string(o)));
271

272
            // we create a copy and transfer this copy's memory management responsibility to Python
273
            PyObject* cpy = ext->copyPyObject();
274
            return cpy;
275
        }
276
        catch(const Base::ValueError& e) {
277
            PyErr_SetString(PartExceptionOCCError, e.what());
278
            return nullptr;
279
        }
280
        catch(const std::bad_weak_ptr&) {
281
            PyErr_SetString(PartExceptionOCCError, "Geometry extension does not exist anymore.");
282
            return nullptr;
283
        }
284
        catch(Base::NotImplementedError&) {
285
            PyErr_SetString(Part::PartExceptionOCCError, "Geometry extension does not implement a Python counterpart.");
286
            return nullptr;
287
        }
288

289
    }
290

291
    PyErr_SetString(PartExceptionOCCError, "A string with the name of the geometry extension was expected");
292
    return nullptr;
293
}
294

295
PyObject* GeometryPy::hasExtensionOfType(PyObject *args)
296
{
297
    char* o;
298
    if (PyArg_ParseTuple(args, "s", &o)) {
299

300
        Base::Type type = Base::Type::fromName(o);
301

302
        if(type != Base::Type::badType()) {
303
            try {
304
                return Py::new_reference_to(Py::Boolean(this->getGeometryPtr()->hasExtension(type)));
305
            }
306
            catch(const Base::ValueError& e) {
307
                PyErr_SetString(PartExceptionOCCError, e.what());
308
                return nullptr;
309
            }
310
        }
311
        else
312
        {
313
            PyErr_SetString(PartExceptionOCCError, "Exception type does not exist");
314
            return nullptr;
315
        }
316

317
    }
318

319
    PyErr_SetString(PartExceptionOCCError, "A string with the type of the geometry extension was expected");
320
    return nullptr;
321
}
322

323
PyObject* GeometryPy::hasExtensionOfName(PyObject *args)
324
{
325
    char* o;
326
    if (PyArg_ParseTuple(args, "s", &o)) {
327

328
        try {
329
            return Py::new_reference_to(Py::Boolean(this->getGeometryPtr()->hasExtension(std::string(o))));
330
        }
331
        catch(const Base::ValueError& e) {
332
            PyErr_SetString(PartExceptionOCCError, e.what());
333
            return nullptr;
334
        }
335

336
    }
337

338
    PyErr_SetString(PartExceptionOCCError, "A string with the type of the geometry extension was expected");
339
    return nullptr;
340
}
341

342
PyObject* GeometryPy::deleteExtensionOfType(PyObject *args)
343
{
344
    char* o;
345
    if (PyArg_ParseTuple(args, "s", &o)) {
346

347
        Base::Type type = Base::Type::fromName(o);
348

349
        if(type != Base::Type::badType()) {
350
            try {
351
                this->getGeometryPtr()->deleteExtension(type);
352
                Py_Return;
353
            }
354
            catch(const Base::ValueError& e) {
355
                PyErr_SetString(PartExceptionOCCError, e.what());
356
                return nullptr;
357
            }
358
        }
359
        else
360
        {
361
            PyErr_SetString(PartExceptionOCCError, "Type does not exist");
362
            return nullptr;
363
        }
364

365
    }
366

367
    PyErr_SetString(PartExceptionOCCError, "A string with a type object was expected");
368
    return nullptr;
369
}
370

371
PyObject* GeometryPy::deleteExtensionOfName(PyObject *args)
372
{
373
    char* o;
374
    if (PyArg_ParseTuple(args, "s", &o)) {
375

376
        try {
377
            this->getGeometryPtr()->deleteExtension(std::string(o));
378
            Py_Return;
379
        }
380
        catch(const Base::ValueError& e) {
381
            PyErr_SetString(PartExceptionOCCError, e.what());
382
            return nullptr;
383
        }
384
    }
385

386
    PyErr_SetString(PartExceptionOCCError, "A string with the name of the extension was expected");
387
    return nullptr;
388
}
389

390
PyObject* GeometryPy::getExtensions(PyObject *args)
391
{
392
    if (!PyArg_ParseTuple(args, "")){
393
        PyErr_SetString(PartExceptionOCCError, "No arguments were expected");
394
        return nullptr;
395
    }
396

397
    try {
398
        const std::vector<std::weak_ptr<const GeometryExtension>> ext = this->getGeometryPtr()->getExtensions();
399

400
        Py::List list;
401

402
        for (const auto & it : ext) {
403

404
            // const casting only to get the Python object to make a copy
405
            std::shared_ptr<GeometryExtension> p = std::const_pointer_cast<GeometryExtension>(it.lock());
406

407
            if(p) {
408
                // we create a python copy and add it to the list
409

410
                try {
411
                    list.append(Py::asObject(p->copyPyObject()));
412
                }
413
                catch(Base::NotImplementedError&) {
414
                    // silently ignoring extensions not having a Python object
415
                }
416
            }
417
        }
418

419
        return Py::new_reference_to(list);
420
    }
421
    catch(const Base::ValueError& e) {
422
        PyErr_SetString(PartExceptionOCCError, e.what());
423
        return nullptr;
424
    }
425

426
}
427

428
Py::String GeometryPy::getTag() const
429
{
430
    std::string tmp = boost::uuids::to_string(getGeometryPtr()->getTag());
431
    return {tmp};
432
}
433

434
PyObject *GeometryPy::getCustomAttributes(const char* /*attr*/) const
435
{
436
    return nullptr;
437
}
438

439
int GeometryPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
440
{
441
    return 0;
442
}
443

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

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

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

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