1
/***************************************************************************
2
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
26
# include <boost/uuid/uuid_io.hpp>
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>
37
#include "GeometryPy.h"
38
#include "GeometryPy.cpp"
39
#include "GeometryExtensionPy.h"
45
// returns a string which represents the object e.g. when printed in python
46
std::string GeometryPy::representation() const
48
return "<Geometry object>";
51
PyObject *GeometryPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
53
// never create such objects with the constructor
54
PyErr_SetString(PyExc_RuntimeError,
55
"You cannot create an instance of the abstract class 'Geometry'.");
60
int GeometryPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
65
PyObject* GeometryPy::mirror(PyObject *args)
68
if (PyArg_ParseTuple(args, "O!", &(Base::VectorPy::Type),&o)) {
69
Base::Vector3d vec = static_cast<Base::VectorPy*>(o)->value();
70
getGeometryPtr()->mirror(vec);
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);
84
PyErr_SetString(PartExceptionOCCError, "either a point (vector) or axis (vector, vector) must be given");
88
PyObject* GeometryPy::rotate(PyObject *args)
91
if (!PyArg_ParseTuple(args, "O!", &(Base::PlacementPy::Type),&o))
94
Base::Placement* plm = static_cast<Base::PlacementPy*>(o)->getPlacementPtr();
95
getGeometryPtr()->rotate(*plm);
99
PyObject* GeometryPy::scale(PyObject *args)
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);
111
if (PyArg_ParseTuple(args, "O!d", &PyTuple_Type,&o, &scale)) {
112
vec = Base::getVectorFromTuple<double>(o);
113
getGeometryPtr()->scale(vec, scale);
117
PyErr_SetString(PartExceptionOCCError, "either vector or tuple and float expected");
121
PyObject* GeometryPy::transform(PyObject *args)
124
if (!PyArg_ParseTuple(args, "O!", &(Base::MatrixPy::Type),&o))
126
Base::Matrix4D mat = static_cast<Base::MatrixPy*>(o)->value();
127
getGeometryPtr()->transform(mat);
131
PyObject* GeometryPy::translate(PyObject *args)
135
if (PyArg_ParseTuple(args, "O!", &(Base::VectorPy::Type),&o)) {
136
vec = static_cast<Base::VectorPy*>(o)->value();
137
getGeometryPtr()->translate(vec);
142
if (PyArg_ParseTuple(args, "O!", &PyTuple_Type,&o)) {
143
vec = Base::getVectorFromTuple<double>(o);
144
getGeometryPtr()->translate(vec);
148
PyErr_SetString(PartExceptionOCCError, "either vector or tuple expected");
152
PyObject* GeometryPy::copy(PyObject *args)
154
if (!PyArg_ParseTuple(args, ""))
157
Part::Geometry* geom = this->getGeometryPtr();
158
PyTypeObject* type = this->GetType();
159
PyObject* cpy = nullptr;
160
// let the type object decide
162
cpy = type->tp_new(type, this, nullptr);
164
PyErr_SetString(PyExc_TypeError, "failed to create copy of geometry");
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);
175
geompy->_pcTwinPointer = geom->copy();
179
PyObject* GeometryPy::clone(PyObject *args)
181
if (!PyArg_ParseTuple(args, ""))
184
Part::Geometry* geom = this->getGeometryPtr();
185
PyTypeObject* type = this->GetType();
186
PyObject* cpy = nullptr;
187
// let the type object decide
189
cpy = type->tp_new(type, this, nullptr);
191
PyErr_SetString(PyExc_TypeError, "failed to create clone of geometry");
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);
202
geompy->_pcTwinPointer = geom->clone();
206
PyObject* GeometryPy::setExtension(PyObject *args)
209
if (PyArg_ParseTuple(args, "O!", &(GeometryExtensionPy::Type),&o)) {
210
Part::GeometryExtension * ext;
211
ext = static_cast<GeometryExtensionPy *>(o)->getGeometryExtensionPtr();
213
// make copy of Python managed memory and wrap it in smart pointer
214
auto cpy = ext->copy();
216
this->getGeometryPtr()->setExtension(std::move(cpy));
220
PyErr_SetString(PartExceptionOCCError, "A geometry extension object was expected");
224
PyObject* GeometryPy::getExtensionOfType(PyObject *args)
227
if (PyArg_ParseTuple(args, "s", &o)) {
229
Base::Type type = Base::Type::fromName(o);
231
if(type != Base::Type::badType()) {
233
std::shared_ptr<const GeometryExtension> ext(this->getGeometryPtr()->getExtension(type));
235
// we create a copy and transfer this copy's memory management responsibility to Python
236
PyObject* cpy = ext->copyPyObject();
239
catch(const Base::ValueError& e) {
240
PyErr_SetString(PartExceptionOCCError, e.what());
243
catch(const std::bad_weak_ptr&) {
244
PyErr_SetString(PartExceptionOCCError, "Geometry extension does not exist anymore.");
247
catch(Base::NotImplementedError&) {
248
PyErr_SetString(Part::PartExceptionOCCError, "Geometry extension does not implement a Python counterpart.");
254
PyErr_SetString(PartExceptionOCCError, "Exception type does not exist");
260
PyErr_SetString(PartExceptionOCCError, "A string with the name of the geometry extension type was expected");
264
PyObject* GeometryPy::getExtensionOfName(PyObject *args)
267
if (PyArg_ParseTuple(args, "s", &o)) {
270
std::shared_ptr<const GeometryExtension> ext(this->getGeometryPtr()->getExtension(std::string(o)));
272
// we create a copy and transfer this copy's memory management responsibility to Python
273
PyObject* cpy = ext->copyPyObject();
276
catch(const Base::ValueError& e) {
277
PyErr_SetString(PartExceptionOCCError, e.what());
280
catch(const std::bad_weak_ptr&) {
281
PyErr_SetString(PartExceptionOCCError, "Geometry extension does not exist anymore.");
284
catch(Base::NotImplementedError&) {
285
PyErr_SetString(Part::PartExceptionOCCError, "Geometry extension does not implement a Python counterpart.");
291
PyErr_SetString(PartExceptionOCCError, "A string with the name of the geometry extension was expected");
295
PyObject* GeometryPy::hasExtensionOfType(PyObject *args)
298
if (PyArg_ParseTuple(args, "s", &o)) {
300
Base::Type type = Base::Type::fromName(o);
302
if(type != Base::Type::badType()) {
304
return Py::new_reference_to(Py::Boolean(this->getGeometryPtr()->hasExtension(type)));
306
catch(const Base::ValueError& e) {
307
PyErr_SetString(PartExceptionOCCError, e.what());
313
PyErr_SetString(PartExceptionOCCError, "Exception type does not exist");
319
PyErr_SetString(PartExceptionOCCError, "A string with the type of the geometry extension was expected");
323
PyObject* GeometryPy::hasExtensionOfName(PyObject *args)
326
if (PyArg_ParseTuple(args, "s", &o)) {
329
return Py::new_reference_to(Py::Boolean(this->getGeometryPtr()->hasExtension(std::string(o))));
331
catch(const Base::ValueError& e) {
332
PyErr_SetString(PartExceptionOCCError, e.what());
338
PyErr_SetString(PartExceptionOCCError, "A string with the type of the geometry extension was expected");
342
PyObject* GeometryPy::deleteExtensionOfType(PyObject *args)
345
if (PyArg_ParseTuple(args, "s", &o)) {
347
Base::Type type = Base::Type::fromName(o);
349
if(type != Base::Type::badType()) {
351
this->getGeometryPtr()->deleteExtension(type);
354
catch(const Base::ValueError& e) {
355
PyErr_SetString(PartExceptionOCCError, e.what());
361
PyErr_SetString(PartExceptionOCCError, "Type does not exist");
367
PyErr_SetString(PartExceptionOCCError, "A string with a type object was expected");
371
PyObject* GeometryPy::deleteExtensionOfName(PyObject *args)
374
if (PyArg_ParseTuple(args, "s", &o)) {
377
this->getGeometryPtr()->deleteExtension(std::string(o));
380
catch(const Base::ValueError& e) {
381
PyErr_SetString(PartExceptionOCCError, e.what());
386
PyErr_SetString(PartExceptionOCCError, "A string with the name of the extension was expected");
390
PyObject* GeometryPy::getExtensions(PyObject *args)
392
if (!PyArg_ParseTuple(args, "")){
393
PyErr_SetString(PartExceptionOCCError, "No arguments were expected");
398
const std::vector<std::weak_ptr<const GeometryExtension>> ext = this->getGeometryPtr()->getExtensions();
402
for (const auto & it : ext) {
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());
408
// we create a python copy and add it to the list
411
list.append(Py::asObject(p->copyPyObject()));
413
catch(Base::NotImplementedError&) {
414
// silently ignoring extensions not having a Python object
419
return Py::new_reference_to(list);
421
catch(const Base::ValueError& e) {
422
PyErr_SetString(PartExceptionOCCError, e.what());
428
Py::String GeometryPy::getTag() const
430
std::string tmp = boost::uuids::to_string(getGeometryPtr()->getTag());
434
PyObject *GeometryPy::getCustomAttributes(const char* /*attr*/) const
439
int GeometryPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)