FreeCAD

Форк
0
/
Geometry2dPyImp.cpp 
189 строк · 6.3 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2016 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
#ifndef _PreComp_
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>
30
#endif
31

32
#include <Base/GeometryPyCXX.h>
33

34
#include "Geom2d/Geometry2dPy.h"
35
#include "Geom2d/Geometry2dPy.cpp"
36
#include "OCCError.h"
37

38

39
using namespace Part;
40

41
// returns a string which represents the object e.g. when printed in python
42
std::string Geometry2dPy::representation() const
43
{
44
    return "<Geometry2d object>";
45
}
46

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

55
// constructor method
56
int Geometry2dPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
57
{
58
    return 0;
59
}
60

61
PyObject* Geometry2dPy::mirror(PyObject *args)
62
{
63
    PyObject* o;
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);
68
        Py_Return;
69
    }
70

71
    PyErr_Clear();
72
    PyObject* axis;
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);
79
        Py_Return;
80
    }
81

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

86
PyObject* Geometry2dPy::rotate(PyObject *args)
87
{
88
    PyObject* o;
89
    double angle;
90
    Base::Vector2d vec;
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);
95
        Py_Return;
96
    }
97

98
    PyErr_SetString(PartExceptionOCCError, "Vector2d and float expected");
99
    return nullptr;
100
}
101

102
PyObject* Geometry2dPy::scale(PyObject *args)
103
{
104
    PyObject* o;
105
    double scale;
106
    Base::Vector2d vec;
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);
111
        Py_Return;
112
    }
113

114
    PyErr_SetString(PartExceptionOCCError, "Vector2d and float expected");
115
    return nullptr;
116
}
117

118
PyObject* Geometry2dPy::transform(PyObject *args)
119
{
120
    PyObject* o;
121
    if (!PyArg_ParseTuple(args, "O", &o))
122
        return nullptr;
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)));
130

131
    gp_Trsf mat;
132
    mat.SetValues(a11, a12, 0, a13, a21, a22, 0, a23, 0, 0, 1, 0);
133
    gp_Trsf2d trf(mat);
134

135
    getGeometry2dPtr()->handle()->Transform(trf);
136
    Py_Return;
137
}
138

139
PyObject* Geometry2dPy::translate(PyObject *args)
140
{
141
    PyObject* o;
142
    Base::Vector2d vec;
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);
147
        Py_Return;
148
    }
149

150
    PyErr_SetString(PartExceptionOCCError, "Vector2d expected");
151
    return nullptr;
152
}
153

154
PyObject* Geometry2dPy::copy(PyObject *args)
155
{
156
    if (!PyArg_ParseTuple(args, ""))
157
        return nullptr;
158

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

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);
175
        delete clone;
176
    }
177
    geompy->_pcTwinPointer = geom->clone();
178
    return cpy;
179
}
180

181
PyObject *Geometry2dPy::getCustomAttributes(const char* /*attr*/) const
182
{
183
    return nullptr;
184
}
185

186
int Geometry2dPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
187
{
188
    return 0;
189
}
190

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

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

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

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