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"
29
# include <GC_MakePlane.hxx>
30
# include <Geom_Plane.hxx>
31
# include <Standard_Failure.hxx>
34
#include <Base/GeometryPyCXX.h>
35
#include <Base/PyWrapParseTupleAndKeywords.h>
36
#include <Base/VectorPy.h>
45
extern const char* gce_ErrorStatusText(gce_ErrorType et);
47
// returns a string which represents the object e.g. when printed in python
48
std::string PlanePy::representation() const
50
return "<Plane object>";
53
PyObject *PlanePy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
55
// create a new instance of PlanePy and the Twin object
56
return new PlanePy(new GeomPlane);
60
int PlanePy::PyInit(PyObject* args, PyObject* kwds)
62
// plane and distance for offset
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);
72
PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
76
Handle(Geom_Plane) plan = Handle(Geom_Plane)::DownCast(getGeometryPtr()->handle());
77
plan->SetPln(mc.Value()->Pln());
81
// plane from equation
83
static const std::array<const char *, 5> keywords_abcd{"A", "B", "C", "D", nullptr};
85
if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "dddd", keywords_abcd,
87
GC_MakePlane mc(a,b,c,d);
89
PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
93
Handle(Geom_Plane) plane = Handle(Geom_Plane)::DownCast(getGeometryPtr()->handle());
94
plane->SetPln(mc.Value()->Pln());
98
PyObject *pV1, *pV2, *pV3;
99
static const std::array<const char *, 4> keywords_ppp{"Point1", "Point2", "Point3", nullptr};
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));
112
PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
116
Handle(Geom_Plane) plane = Handle(Geom_Plane)::DownCast(getGeometryPtr()->handle());
117
plane->SetPln(mc.Value()->Pln());
121
// location and normal
122
static const std::array<const char *, 3> keywords_cnr {"Location", "Normal", nullptr};
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));
132
PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(mc.Status()));
136
Handle(Geom_Plane) plane = Handle(Geom_Plane)::DownCast(getGeometryPtr()->handle());
137
plane->SetPln(mc.Value()->Pln());
141
static const std::array<const char *, 2> keywords_p {"Plane", nullptr};
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());
153
static const std::array<const char *, 1> keywords_n {nullptr};
155
if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "", keywords_n)) {
160
PyErr_SetString(PyExc_TypeError, "Plane constructor accepts:\n"
161
"-- empty parameter list\n"
163
"-- Plane, Distance\n"
164
"-- Location, Normal\n"
165
"-- Point1, Point2, Point3\n"
167
" (as equation: Ax + By + Cz + D = 0.0)");
171
Py::Object PlanePy::getPosition() const
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()));
179
void PlanePy::setPosition(Py::Object arg)
182
PyObject *p = arg.ptr();
183
if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
184
Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
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)));
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);
202
Handle(Geom_Plane) this_surf = Handle(Geom_Plane)::DownCast
203
(this->getGeomPlanePtr()->handle());
204
this_surf->SetLocation(loc);
206
catch (Standard_Failure& e) {
207
throw Py::RuntimeError(e.GetMessageString());
211
Py::Object PlanePy::getAxis() const
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()));
219
void PlanePy::setAxis(Py::Object arg)
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();
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));
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);
242
Handle(Geom_ElementarySurface) this_surf = Handle(Geom_ElementarySurface)::DownCast
243
(this->getGeometryPtr()->handle());
245
axis.SetLocation(this_surf->Location());
246
axis.SetDirection(gp_Dir(dir_x, dir_y, dir_z));
247
this_surf->SetAxis(axis);
249
catch (Standard_Failure&) {
250
throw Py::RuntimeError("cannot set axis");
254
PyObject *PlanePy::getCustomAttributes(const char* /*attr*/) const
259
int PlanePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)