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"
25
# include <Geom_Line.hxx>
26
# include <GC_MakeLine.hxx>
29
#include <Base/GeometryPyCXX.h>
30
#include <Base/VectorPy.h>
39
extern const char* gce_ErrorStatusText(gce_ErrorType et);
41
// returns a string which represents the object e.g. when printed in python
42
std::string LinePy::representation() const
44
return "<Line object>";
47
PyObject *LinePy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
49
// create a new instance of LinePy and the Twin object
50
return new LinePy(new GeomLine);
54
int LinePy::PyInit(PyObject* args, PyObject* /*kwd*/)
57
if (PyArg_ParseTuple(args, "")) {
64
if (PyArg_ParseTuple(args, "O!", &(LinePy::Type), &pLine)) {
66
LinePy* pcLine = static_cast<LinePy*>(pLine);
67
// get Geom_Line of line
68
Handle(Geom_Line) that_line = Handle(Geom_Line)::DownCast
69
(pcLine->getGeomLinePtr()->handle());
70
// get Geom_Line of line
71
Handle(Geom_Line) this_line = Handle(Geom_Line)::DownCast
72
(this->getGeomLinePtr()->handle());
75
this_line->SetLin(that_line->Lin());
81
if (PyArg_ParseTuple(args, "O!O!", &(Base::VectorPy::Type), &pV1,
82
&(Base::VectorPy::Type), &pV2)) {
83
Base::Vector3d v1 = static_cast<Base::VectorPy*>(pV1)->value();
84
Base::Vector3d v2 = static_cast<Base::VectorPy*>(pV2)->value();
86
// Create line out of two points
87
double distance = Base::Distance(v1, v2);
88
if (distance < gp::Resolution())
89
Standard_Failure::Raise("Both points are equal");
90
GC_MakeLine ms(gp_Pnt(v1.x,v1.y,v1.z),
91
gp_Pnt(v2.x,v2.y,v2.z));
93
PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(ms.Status()));
97
// get Geom_Line of line
98
Handle(Geom_Line) this_curv = Handle(Geom_Line)::DownCast
99
(this->getGeomLinePtr()->handle());
100
Handle(Geom_Line) that_curv = ms.Value();
101
this_curv->SetLin(that_curv->Lin());
104
catch (Standard_Failure& e) {
106
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
110
PyErr_SetString(PartExceptionOCCError, "creation of line failed");
115
PyErr_SetString(PyExc_TypeError, "Line constructor accepts:\n"
116
"-- empty parameter list\n"
122
Py::Object LinePy::getLocation() const
124
Handle(Geom_Line) this_curve = Handle(Geom_Line)::DownCast
125
(this->getGeomLinePtr()->handle());
126
gp_Pnt pnt = this_curve->Position().Location();
127
return Py::Vector(Base::Vector3d(pnt.X(), pnt.Y(), pnt.Z()));
130
void LinePy::setLocation(Py::Object arg)
134
Handle(Geom_Line) this_curv = Handle(Geom_Line)::DownCast
135
(this->getGeomLinePtr()->handle());
136
dir = this_curv->Position().Direction();
138
PyObject *p = arg.ptr();
139
if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
140
Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
145
else if (PyTuple_Check(p)) {
146
Py::Tuple tuple(arg);
147
pnt.SetX((double)Py::Float(tuple.getItem(0)));
148
pnt.SetY((double)Py::Float(tuple.getItem(1)));
149
pnt.SetZ((double)Py::Float(tuple.getItem(2)));
152
std::string error = std::string("type must be 'Vector' or tuple, not ");
153
error += p->ob_type->tp_name;
154
throw Py::TypeError(error);
158
GC_MakeLine ms(pnt, dir);
160
throw Py::RuntimeError(gce_ErrorStatusText(ms.Status()));
163
// get Geom_Line of line
164
Handle(Geom_Line) that_curv = ms.Value();
165
this_curv->SetLin(that_curv->Lin());
167
catch (Standard_Failure& e) {
168
throw Py::RuntimeError(e.GetMessageString());
172
Py::Object LinePy::getDirection() const
174
Handle(Geom_Line) this_curve = Handle(Geom_Line)::DownCast
175
(this->getGeomLinePtr()->handle());
176
gp_Dir dir = this_curve->Position().Direction();
177
return Py::Vector(Base::Vector3d(dir.X(), dir.Y(), dir.Z()));
180
void LinePy::setDirection(Py::Object arg)
184
Handle(Geom_Line) this_curv = Handle(Geom_Line)::DownCast
185
(this->getGeomLinePtr()->handle());
186
pnt = this_curv->Position().Location();
188
PyObject *p = arg.ptr();
189
if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
190
Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
191
dir = gp_Dir(v.x,v.y,v.z);
193
else if (PyTuple_Check(p)) {
194
Py::Tuple tuple(arg);
195
double x = (double)Py::Float(tuple.getItem(0));
196
double y = (double)Py::Float(tuple.getItem(1));
197
double z = (double)Py::Float(tuple.getItem(2));
201
std::string error = std::string("type must be 'Vector' or tuple, not ");
202
error += p->ob_type->tp_name;
203
throw Py::TypeError(error);
207
GC_MakeLine ms(pnt, dir);
209
throw Py::RuntimeError(gce_ErrorStatusText(ms.Status()));
212
// get Geom_Line of line
213
Handle(Geom_Line) that_curv = ms.Value();
214
this_curv->SetLin(that_curv->Lin());
216
catch (Standard_Failure& e) {
217
throw Py::RuntimeError(e.GetMessageString());
221
PyObject *LinePy::getCustomAttributes(const char* /*attr*/) const
226
int LinePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)