FreeCAD

Форк
0
/
PlanePyImp.cpp 
262 строки · 9.9 Кб
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
#ifndef _PreComp_
25
# include <gp_Ax1.hxx>
26
# include <gp_Dir.hxx>
27
# include <gp_Pln.hxx>
28
# include <gp_Pnt.hxx>
29
# include <GC_MakePlane.hxx>
30
# include <Geom_Plane.hxx>
31
# include <Standard_Failure.hxx>
32
#endif
33

34
#include <Base/GeometryPyCXX.h>
35
#include <Base/PyWrapParseTupleAndKeywords.h>
36
#include <Base/VectorPy.h>
37

38
#include "PlanePy.h"
39
#include "PlanePy.cpp"
40
#include "OCCError.h"
41

42

43
using namespace Part;
44

45
extern const char* gce_ErrorStatusText(gce_ErrorType et);
46

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

53
PyObject *PlanePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
54
{
55
    // create a new instance of PlanePy and the Twin object
56
    return new PlanePy(new GeomPlane);
57
}
58

59
// constructor method
60
int PlanePy::PyInit(PyObject* args, PyObject* kwds)
61
{
62
    // plane and distance for offset
63
    PyObject *pPlane;
64
    double dist;
65
    static const std::array<const char *, 3> keywords_pd {"Plane", "Distance", nullptr};
66
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!d", keywords_pd, &(PlanePy::Type), &pPlane, &dist)) {
67
        PlanePy* pcPlane = static_cast<PlanePy*>(pPlane);
68
        Handle(Geom_Plane) plane = Handle(Geom_Plane)::DownCast
69
            (pcPlane->getGeometryPtr()->handle());
70
        GC_MakePlane mc(plane->Pln(), dist);
71
        if (!mc.IsDone()) {
72
            PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
73
            return -1;
74
        }
75

76
        Handle(Geom_Plane) plan = Handle(Geom_Plane)::DownCast(getGeometryPtr()->handle());
77
        plan->SetPln(mc.Value()->Pln());
78
        return 0;
79
    }
80

81
    // plane from equation
82
    double a,b,c,d;
83
    static const std::array<const char *, 5> keywords_abcd{"A", "B", "C", "D", nullptr};
84
    PyErr_Clear();
85
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "dddd", keywords_abcd,
86
                                            &a,&b,&c,&d)) {
87
        GC_MakePlane mc(a,b,c,d);
88
        if (!mc.IsDone()) {
89
            PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
90
            return -1;
91
        }
92

93
        Handle(Geom_Plane) plane = Handle(Geom_Plane)::DownCast(getGeometryPtr()->handle());
94
        plane->SetPln(mc.Value()->Pln());
95
        return 0;
96
    }
97

98
    PyObject *pV1, *pV2, *pV3;
99
    static const std::array<const char *, 4> keywords_ppp{"Point1", "Point2", "Point3", nullptr};
100
    PyErr_Clear();
101
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!O!O!", keywords_ppp,
102
                                            &(Base::VectorPy::Type), &pV1,
103
                                            &(Base::VectorPy::Type), &pV2,
104
                                            &(Base::VectorPy::Type), &pV3)) {
105
        Base::Vector3d v1 = static_cast<Base::VectorPy*>(pV1)->value();
106
        Base::Vector3d v2 = static_cast<Base::VectorPy*>(pV2)->value();
107
        Base::Vector3d v3 = static_cast<Base::VectorPy*>(pV3)->value();
108
        GC_MakePlane mc(gp_Pnt(v1.x,v1.y,v1.z),
109
                        gp_Pnt(v2.x,v2.y,v2.z),
110
                        gp_Pnt(v3.x,v3.y,v3.z));
111
        if (!mc.IsDone()) {
112
            PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
113
            return -1;
114
        }
115

116
        Handle(Geom_Plane) plane = Handle(Geom_Plane)::DownCast(getGeometryPtr()->handle());
117
        plane->SetPln(mc.Value()->Pln());
118
        return 0;
119
    }
120

121
    // location and normal
122
    static const std::array<const char *, 3> keywords_cnr {"Location", "Normal", nullptr};
123
    PyErr_Clear();
124
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!O!", keywords_cnr,
125
                                            &(Base::VectorPy::Type), &pV1,
126
                                            &(Base::VectorPy::Type), &pV2)) {
127
        Base::Vector3d v1 = static_cast<Base::VectorPy*>(pV1)->value();
128
        Base::Vector3d v2 = static_cast<Base::VectorPy*>(pV2)->value();
129
        GC_MakePlane mc(gp_Pnt(v1.x,v1.y,v1.z),
130
                        gp_Dir(v2.x,v2.y,v2.z));
131
        if (!mc.IsDone()) {
132
            PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
133
            return -1;
134
        }
135

136
        Handle(Geom_Plane) plane = Handle(Geom_Plane)::DownCast(getGeometryPtr()->handle());
137
        plane->SetPln(mc.Value()->Pln());
138
        return 0;
139
    }
140

141
    static const std::array<const char *, 2> keywords_p {"Plane", nullptr};
142
    PyErr_Clear();
143
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!", keywords_p, &(PlanePy::Type), &pPlane)) {
144
        PlanePy* pcPlane = static_cast<PlanePy*>(pPlane);
145
        Handle(Geom_Plane) plane1 = Handle(Geom_Plane)::DownCast
146
            (pcPlane->getGeometryPtr()->handle());
147
        Handle(Geom_Plane) plane2 = Handle(Geom_Plane)::DownCast
148
            (this->getGeometryPtr()->handle());
149
        plane2->SetPln(plane1->Pln());
150
        return 0;
151
    }
152

153
    static const std::array<const char *, 1> keywords_n {nullptr};
154
    PyErr_Clear();
155
    if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "", keywords_n)) {
156
        // do nothing
157
        return 0;
158
    }
159

160
    PyErr_SetString(PyExc_TypeError, "Plane constructor accepts:\n"
161
        "-- empty parameter list\n"
162
        "-- Plane\n"
163
        "-- Plane, Distance\n"
164
        "-- Location, Normal\n"
165
        "-- Point1, Point2, Point3\n"
166
        "-- A, B, C, D\n"
167
        "   (as equation: Ax + By + Cz + D = 0.0)");
168
    return -1;
169
}
170

171
Py::Object PlanePy::getPosition() const
172
{
173
    Handle(Geom_Plane) this_surf = Handle(Geom_Plane)::DownCast
174
        (this->getGeomPlanePtr()->handle());
175
    gp_Pnt pnt = this_surf->Location();
176
    return Py::Vector(Base::Vector3d(pnt.X(), pnt.Y(), pnt.Z()));
177
}
178

179
void PlanePy::setPosition(Py::Object arg)
180
{
181
    gp_Pnt loc;
182
    PyObject *p = arg.ptr();
183
    if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
184
        Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
185
        loc.SetX(v.x);
186
        loc.SetY(v.y);
187
        loc.SetZ(v.z);
188
    }
189
    else if (PyTuple_Check(p)) {
190
        Py::Tuple tuple(arg);
191
        loc.SetX((double)Py::Float(tuple.getItem(0)));
192
        loc.SetY((double)Py::Float(tuple.getItem(1)));
193
        loc.SetZ((double)Py::Float(tuple.getItem(2)));
194
    }
195
    else {
196
        std::string error = std::string("type must be 'Vector' or tuple, not ");
197
        error += p->ob_type->tp_name;
198
        throw Py::TypeError(error);
199
    }
200

201
    try {
202
        Handle(Geom_Plane) this_surf = Handle(Geom_Plane)::DownCast
203
            (this->getGeomPlanePtr()->handle());
204
        this_surf->SetLocation(loc);
205
    }
206
    catch (Standard_Failure& e) {
207
        throw Py::RuntimeError(e.GetMessageString());
208
    }
209
}
210

211
Py::Object PlanePy::getAxis() const
212
{
213
    Handle(Geom_ElementarySurface) s = Handle(Geom_ElementarySurface)::DownCast
214
        (getGeometryPtr()->handle());
215
    gp_Dir dir = s->Axis().Direction();
216
    return Py::Vector(Base::Vector3d(dir.X(), dir.Y(), dir.Z()));
217
}
218

219
void PlanePy::setAxis(Py::Object arg)
220
{
221
    Standard_Real dir_x, dir_y, dir_z;
222
    PyObject *p = arg.ptr();
223
    if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
224
        Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
225
        dir_x = v.x;
226
        dir_y = v.y;
227
        dir_z = v.z;
228
    }
229
    else if (PyTuple_Check(p)) {
230
        Py::Tuple tuple(arg);
231
        dir_x = (double)Py::Float(tuple.getItem(0));
232
        dir_y = (double)Py::Float(tuple.getItem(1));
233
        dir_z = (double)Py::Float(tuple.getItem(2));
234
    }
235
    else {
236
        std::string error = std::string("type must be 'Vector' or tuple, not ");
237
        error += p->ob_type->tp_name;
238
        throw Py::TypeError(error);
239
    }
240

241
    try {
242
        Handle(Geom_ElementarySurface) this_surf = Handle(Geom_ElementarySurface)::DownCast
243
            (this->getGeometryPtr()->handle());
244
        gp_Ax1 axis;
245
        axis.SetLocation(this_surf->Location());
246
        axis.SetDirection(gp_Dir(dir_x, dir_y, dir_z));
247
        this_surf->SetAxis(axis);
248
    }
249
    catch (Standard_Failure&) {
250
        throw Py::RuntimeError("cannot set axis");
251
    }
252
}
253

254
PyObject *PlanePy::getCustomAttributes(const char* /*attr*/) const
255
{
256
    return nullptr;
257
}
258

259
int PlanePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
260
{
261
    return 0;
262
}
263

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

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

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

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