1
/***************************************************************************
2
* Copyright (c) 2016 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"
25
# include <gp_Dir2d.hxx>
26
# include <gp_Pnt2d.hxx>
27
# include <gp_Trsf.hxx>
28
# include <gp_Trsf2d.hxx>
29
# include <gp_Vec2d.hxx>
32
#include <Base/GeometryPyCXX.h>
34
#include "Geom2d/Geometry2dPy.h"
35
#include "Geom2d/Geometry2dPy.cpp"
41
// returns a string which represents the object e.g. when printed in python
42
std::string Geometry2dPy::representation() const
44
return "<Geometry2d object>";
47
PyObject *Geometry2dPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
49
// never create such objects with the constructor
50
PyErr_SetString(PyExc_RuntimeError,
51
"You cannot create an instance of the abstract class 'Geometry'.");
56
int Geometry2dPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
61
PyObject* Geometry2dPy::mirror(PyObject *args)
64
if (PyArg_ParseTuple(args, "O!", Base::Vector2dPy::type_object(),&o)) {
65
Base::Vector2d vec = Py::toVector2d(o);
66
gp_Pnt2d pnt(vec.x, vec.y);
67
getGeometry2dPtr()->handle()->Mirror(pnt);
73
if (PyArg_ParseTuple(args, "O!O!", Base::Vector2dPy::type_object(),&o,
74
Base::Vector2dPy::type_object(),&axis)) {
75
Base::Vector2d pnt = Py::toVector2d(o);
76
Base::Vector2d dir = Py::toVector2d(axis);
77
gp_Ax2d ax1(gp_Pnt2d(pnt.x,pnt.y), gp_Dir2d(dir.x,dir.y));
78
getGeometry2dPtr()->handle()->Mirror(ax1);
82
PyErr_SetString(PartExceptionOCCError, "either a point (vector) or axis (vector, vector) must be given");
86
PyObject* Geometry2dPy::rotate(PyObject *args)
91
if (PyArg_ParseTuple(args, "O!d", Base::Vector2dPy::type_object(), &o, &angle)) {
92
vec = Py::toVector2d(o);
93
gp_Pnt2d pnt(vec.x, vec.y);
94
getGeometry2dPtr()->handle()->Rotate(pnt, angle);
98
PyErr_SetString(PartExceptionOCCError, "Vector2d and float expected");
102
PyObject* Geometry2dPy::scale(PyObject *args)
107
if (PyArg_ParseTuple(args, "O!d", Base::Vector2dPy::type_object(), &o, &scale)) {
108
vec = Py::toVector2d(o);
109
gp_Pnt2d pnt(vec.x, vec.y);
110
getGeometry2dPtr()->handle()->Scale(pnt, scale);
114
PyErr_SetString(PartExceptionOCCError, "Vector2d and float expected");
118
PyObject* Geometry2dPy::transform(PyObject *args)
121
if (!PyArg_ParseTuple(args, "O", &o))
123
Py::Sequence list(o);
124
double a11 = static_cast<double>(Py::Float(list.getItem(0)));
125
double a12 = static_cast<double>(Py::Float(list.getItem(1)));
126
double a13 = static_cast<double>(Py::Float(list.getItem(2)));
127
double a21 = static_cast<double>(Py::Float(list.getItem(3)));
128
double a22 = static_cast<double>(Py::Float(list.getItem(4)));
129
double a23 = static_cast<double>(Py::Float(list.getItem(5)));
132
mat.SetValues(a11, a12, 0, a13, a21, a22, 0, a23, 0, 0, 1, 0);
135
getGeometry2dPtr()->handle()->Transform(trf);
139
PyObject* Geometry2dPy::translate(PyObject *args)
143
if (PyArg_ParseTuple(args, "O!", Base::Vector2dPy::type_object(),&o)) {
144
vec = Py::toVector2d(o);
145
gp_Vec2d trl(vec.x, vec.y);
146
getGeometry2dPtr()->handle()->Translate(trl);
150
PyErr_SetString(PartExceptionOCCError, "Vector2d expected");
154
PyObject* Geometry2dPy::copy(PyObject *args)
156
if (!PyArg_ParseTuple(args, ""))
159
Part::Geometry2d* geom = this->getGeometry2dPtr();
160
PyTypeObject* type = this->GetType();
161
PyObject* cpy = nullptr;
162
// let the type object decide
164
cpy = type->tp_new(type, this, nullptr);
166
PyErr_SetString(PyExc_TypeError, "failed to create copy of geometry");
170
Part::Geometry2dPy* geompy = static_cast<Part::Geometry2dPy*>(cpy);
171
// the PyMake function must have created the corresponding instance of the 'Geometry' subclass
172
// so delete it now to avoid a memory leak
173
if (geompy->_pcTwinPointer) {
174
Part::Geometry2d* clone = static_cast<Part::Geometry2d*>(geompy->_pcTwinPointer);
177
geompy->_pcTwinPointer = geom->clone();
181
PyObject *Geometry2dPy::getCustomAttributes(const char* /*attr*/) const
186
int Geometry2dPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)