-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
BezierCurve2dPyImp.cpp
422 lines (374 loc) · 12.9 KB
/
BezierCurve2dPyImp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/***************************************************************************
* Copyright (c) 2016 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <Geom2d_BezierCurve.hxx>
# include <gp_Pnt2d.hxx>
# include <TColStd_Array1OfReal.hxx>
# include <TColgp_Array1OfPnt2d.hxx>
#endif
#include <Base/GeometryPyCXX.h>
#include <Mod/Part/App/OCCError.h>
#include <Mod/Part/App/Geometry2d.h>
#include <Mod/Part/App/Geom2d/BezierCurve2dPy.h>
#include <Mod/Part/App/Geom2d/BezierCurve2dPy.cpp>
using namespace Part;
// returns a string which represents the object e.g. when printed in python
std::string BezierCurve2dPy::representation(void) const
{
return "<BezierCurve2d object>";
}
PyObject *BezierCurve2dPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
{
// create a new instance of BezierCurve2dPy and the Twin object
return new BezierCurve2dPy(new Geom2dBezierCurve);
}
// constructor method
int BezierCurve2dPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
{
return 0;
}
PyObject* BezierCurve2dPy::isRational(PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
Standard_Boolean val = curve->IsRational();
return PyBool_FromLong(val ? 1 : 0);
}
PyObject* BezierCurve2dPy::isPeriodic(PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
Standard_Boolean val = curve->IsPeriodic();
return PyBool_FromLong(val ? 1 : 0);
}
PyObject* BezierCurve2dPy::isClosed(PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
Standard_Boolean val = curve->IsClosed();
return PyBool_FromLong(val ? 1 : 0);
}
PyObject* BezierCurve2dPy::increase(PyObject * args)
{
int degree;
if (!PyArg_ParseTuple(args, "i", °ree))
return 0;
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
curve->Increase(degree);
Py_Return;
}
PyObject* BezierCurve2dPy::insertPoleAfter(PyObject * args)
{
int index;
double weight=1.0;
PyObject* p;
if (!PyArg_ParseTuple(args, "iO!|d", &index, Base::Vector2dPy::type_object(), &p, &weight))
return 0;
Base::Vector2d vec = Py::toVector2d(p);
gp_Pnt2d pnt(vec.x, vec.y);
try {
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
curve->InsertPoleAfter(index,pnt,weight);
Py_Return;
}
catch (Standard_Failure& e) {
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
return 0;
}
}
PyObject* BezierCurve2dPy::insertPoleBefore(PyObject * args)
{
int index;
double weight=1.0;
PyObject* p;
if (!PyArg_ParseTuple(args, "iO!|d", &index, Base::Vector2dPy::type_object(), &p, &weight))
return 0;
Base::Vector2d vec = Py::toVector2d(p);
gp_Pnt2d pnt(vec.x, vec.y);
try {
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
curve->InsertPoleBefore(index,pnt,weight);
Py_Return;
}
catch (Standard_Failure& e) {
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
return 0;
}
}
PyObject* BezierCurve2dPy::removePole(PyObject * args)
{
int index;
if (!PyArg_ParseTuple(args, "i", &index))
return 0;
try {
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
curve->RemovePole(index);
Py_Return;
}
catch (Standard_Failure& e) {
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
return 0;
}
}
PyObject* BezierCurve2dPy::segment(PyObject * args)
{
double u1,u2;
if (!PyArg_ParseTuple(args, "dd", &u1,&u2))
return 0;
try {
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
curve->Segment(u1,u2);
Py_Return;
}
catch (Standard_Failure& e) {
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
return 0;
}
}
PyObject* BezierCurve2dPy::setPole(PyObject * args)
{
int index;
double weight=-1.0;
PyObject* p;
if (!PyArg_ParseTuple(args, "iO!|d", &index, Base::Vector2dPy::type_object(), &p, &weight))
return 0;
Base::Vector2d vec = Py::toVector2d(p);
gp_Pnt2d pnt(vec.x, vec.y);
try {
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
if (weight < 0.0)
curve->SetPole(index,pnt);
else
curve->SetPole(index,pnt,weight);
Py_Return;
}
catch (Standard_Failure& e) {
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
return 0;
}
}
PyObject* BezierCurve2dPy::getPole(PyObject * args)
{
int index;
if (!PyArg_ParseTuple(args, "i", &index))
return 0;
try {
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
Standard_OutOfRange_Raise_if
(index < 1 || index > curve->NbPoles(), "Pole index out of range");
gp_Pnt2d pnt = curve->Pole(index);
Py::Module module("__FreeCADBase__");
Py::Callable method(module.getAttr("Vector2d"));
Py::Tuple arg(2);
arg.setItem(0, Py::Float(pnt.X()));
arg.setItem(1, Py::Float(pnt.Y()));
return Py::new_reference_to(method.apply(arg));
}
catch (Standard_Failure& e) {
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
return 0;
}
}
PyObject* BezierCurve2dPy::getPoles(PyObject * args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
try {
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
TColgp_Array1OfPnt2d p(1,curve->NbPoles());
curve->Poles(p);
Py::List poles;
Py::Module module("__FreeCADBase__");
Py::Callable method(module.getAttr("Vector2d"));
Py::Tuple arg(2);
for (Standard_Integer i=p.Lower(); i<=p.Upper(); i++) {
gp_Pnt2d pnt = p(i);
arg.setItem(0, Py::Float(pnt.X()));
arg.setItem(1, Py::Float(pnt.Y()));
poles.append(method.apply(arg));
}
return Py::new_reference_to(poles);
}
catch (Standard_Failure& e) {
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
return 0;
}
}
PyObject* BezierCurve2dPy::setPoles(PyObject * args)
{
PyObject* plist;
if (!PyArg_ParseTuple(args, "O", &plist))
return 0;
try {
Py::Sequence list(plist);
TColgp_Array1OfPnt2d poles(1,list.size());
int index = poles.Lower();
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
Base::Vector2d pole = Py::toVector2d(*it);
poles.SetValue(index++, gp_Pnt2d(pole.x,pole.y));
}
Handle(Geom2d_BezierCurve) bezier = new Geom2d_BezierCurve(poles);
this->getGeom2dBezierCurvePtr()->setHandle(bezier);
Py_Return;
}
catch (Standard_Failure& e) {
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
return 0;
}
}
PyObject* BezierCurve2dPy::setWeight(PyObject * args)
{
int index;
double weight;
if (!PyArg_ParseTuple(args, "id", &index,&weight))
return 0;
try {
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
curve->SetWeight(index,weight);
Py_Return;
}
catch (Standard_Failure& e) {
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
return 0;
}
}
PyObject* BezierCurve2dPy::getWeight(PyObject * args)
{
int index;
if (!PyArg_ParseTuple(args, "i", &index))
return 0;
try {
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
Standard_OutOfRange_Raise_if
(index < 1 || index > curve->NbPoles() , "Weight index out of range");
double weight = curve->Weight(index);
return Py_BuildValue("d", weight);
}
catch (Standard_Failure& e) {
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
return 0;
}
}
PyObject* BezierCurve2dPy::getWeights(PyObject * args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
try {
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
TColStd_Array1OfReal w(1,curve->NbPoles());
curve->Weights(w);
Py::List weights;
for (Standard_Integer i=w.Lower(); i<=w.Upper(); i++) {
weights.append(Py::Float(w(i)));
}
return Py::new_reference_to(weights);
}
catch (Standard_Failure& e) {
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
return 0;
}
}
PyObject* BezierCurve2dPy::getResolution(PyObject* args)
{
double tol;
if (!PyArg_ParseTuple(args, "d", &tol))
return 0;
try {
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
double utol;
curve->Resolution(tol,utol);
return Py_BuildValue("d",utol);
}
catch (Standard_Failure& e) {
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
return 0;
}
}
Py::Long BezierCurve2dPy::getDegree(void) const
{
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
return Py::Long(curve->Degree());
}
Py::Long BezierCurve2dPy::getMaxDegree(void) const
{
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
return Py::Long(curve->MaxDegree());
}
Py::Long BezierCurve2dPy::getNbPoles(void) const
{
Handle(Geom2d_BezierCurve) curve = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
return Py::Long(curve->NbPoles());
}
Py::Object BezierCurve2dPy::getStartPoint(void) const
{
Handle(Geom2d_BezierCurve) c = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
gp_Pnt2d pnt = c->StartPoint();
Py::Module module("__FreeCADBase__");
Py::Callable method(module.getAttr("Vector2d"));
Py::Tuple arg(2);
arg.setItem(0, Py::Float(pnt.X()));
arg.setItem(1, Py::Float(pnt.Y()));
return method.apply(arg);
}
Py::Object BezierCurve2dPy::getEndPoint(void) const
{
Handle(Geom2d_BezierCurve) c = Handle(Geom2d_BezierCurve)::DownCast
(getGeometry2dPtr()->handle());
gp_Pnt2d pnt = c->EndPoint();
Py::Module module("__FreeCADBase__");
Py::Callable method(module.getAttr("Vector2d"));
Py::Tuple arg(2);
arg.setItem(0, Py::Float(pnt.X()));
arg.setItem(1, Py::Float(pnt.Y()));
return method.apply(arg);
}
PyObject *BezierCurve2dPy::getCustomAttributes(const char* /*attr*/) const
{
return 0;
}
int BezierCurve2dPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
{
return 0;
}