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_BezierSurface.hxx>
26
# include <TColgp_Array1OfPnt.hxx>
27
# include <TColgp_Array2OfPnt.hxx>
28
# include <TColStd_Array1OfReal.hxx>
29
# include <TColStd_Array2OfReal.hxx>
32
#include <Base/GeometryPyCXX.h>
33
#include <Base/VectorPy.h>
35
#include "BezierCurvePy.h"
36
#include "BezierSurfacePy.h"
37
#include "BezierSurfacePy.cpp"
43
// returns a string which represents the object e.g. when printed in python
44
std::string BezierSurfacePy::representation() const
46
return "<BezierSurface object>";
49
PyObject *BezierSurfacePy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
51
// create a new instance of BezierSurfacePy and the Twin object
52
return new BezierSurfacePy(new GeomBezierSurface);
56
int BezierSurfacePy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
61
PyObject* BezierSurfacePy::bounds(PyObject *args)
63
if (!PyArg_ParseTuple(args, ""))
66
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
67
(getGeometryPtr()->handle());
69
Standard_Real u1,u2,v1,v2;
70
surf->Bounds(u1,u2,v1,v2);
71
bound.setItem(0,Py::Float(u1));
72
bound.setItem(1,Py::Float(u2));
73
bound.setItem(2,Py::Float(v1));
74
bound.setItem(3,Py::Float(v2));
75
return Py::new_reference_to(bound);
78
PyObject* BezierSurfacePy::isURational(PyObject *args)
80
if (!PyArg_ParseTuple(args, ""))
83
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
84
(getGeometryPtr()->handle());
85
Standard_Boolean val = surf->IsURational();
86
return PyBool_FromLong(val ? 1 : 0);
89
PyObject* BezierSurfacePy::isVRational(PyObject *args)
91
if (!PyArg_ParseTuple(args, ""))
94
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
95
(getGeometryPtr()->handle());
96
Standard_Boolean val = surf->IsVRational();
97
return PyBool_FromLong(val ? 1 : 0);
100
PyObject* BezierSurfacePy::isUPeriodic(PyObject *args)
102
if (!PyArg_ParseTuple(args, ""))
105
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
106
(getGeometryPtr()->handle());
107
Standard_Boolean val = surf->IsUPeriodic();
108
return PyBool_FromLong(val ? 1 : 0);
111
PyObject* BezierSurfacePy::isVPeriodic(PyObject *args)
113
if (!PyArg_ParseTuple(args, ""))
116
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
117
(getGeometryPtr()->handle());
118
Standard_Boolean val = surf->IsVPeriodic();
119
return PyBool_FromLong(val ? 1 : 0);
122
PyObject* BezierSurfacePy::isUClosed(PyObject *args)
124
if (!PyArg_ParseTuple(args, ""))
127
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
128
(getGeometryPtr()->handle());
129
Standard_Boolean val = surf->IsUClosed();
130
return PyBool_FromLong(val ? 1 : 0);
133
PyObject* BezierSurfacePy::isVClosed(PyObject *args)
135
if (!PyArg_ParseTuple(args, ""))
138
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
139
(getGeometryPtr()->handle());
140
Standard_Boolean val = surf->IsVPeriodic();
141
return PyBool_FromLong(val ? 1 : 0);
144
PyObject* BezierSurfacePy::increase(PyObject *args)
147
if (!PyArg_ParseTuple(args, "ii", &udegree, &vdegree))
150
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
151
(getGeometryPtr()->handle());
152
surf->Increase(udegree, vdegree);
155
catch (Standard_Failure& e) {
157
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
162
PyObject* BezierSurfacePy::insertPoleColAfter(PyObject *args)
166
PyObject* obj2=nullptr;
167
if (!PyArg_ParseTuple(args, "iO|O",&vindex,&obj,&obj2))
170
Py::Sequence list(obj);
171
TColgp_Array1OfPnt poles(1, list.size());
173
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
175
Base::Vector3d v = p.toVector();
176
poles(index++) = gp_Pnt(v.x,v.y,v.z);
179
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
180
(getGeometryPtr()->handle());
182
surf->InsertPoleColAfter(vindex, poles);
185
Py::Sequence list(obj2);
186
TColStd_Array1OfReal weights(1, list.size());
188
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
189
weights(index++) = (double)Py::Float(*it);
191
surf->InsertPoleColAfter(vindex, poles, weights);
196
catch (Standard_Failure& e) {
197
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
202
PyObject* BezierSurfacePy::insertPoleRowAfter(PyObject *args)
206
PyObject* obj2=nullptr;
207
if (!PyArg_ParseTuple(args, "iO|O",&uindex,&obj,&obj2))
210
Py::Sequence list(obj);
211
TColgp_Array1OfPnt poles(1, list.size());
213
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
215
Base::Vector3d v = p.toVector();
216
poles(index++) = gp_Pnt(v.x,v.y,v.z);
219
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
220
(getGeometryPtr()->handle());
222
surf->InsertPoleRowAfter(uindex, poles);
225
Py::Sequence list(obj2);
226
TColStd_Array1OfReal weights(1, list.size());
228
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
229
weights(index++) = (double)Py::Float(*it);
231
surf->InsertPoleRowAfter(uindex, poles, weights);
236
catch (Standard_Failure& e) {
237
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
242
PyObject* BezierSurfacePy::insertPoleColBefore(PyObject *args)
246
PyObject* obj2=nullptr;
247
if (!PyArg_ParseTuple(args, "iO|O",&vindex,&obj,&obj2))
250
Py::Sequence list(obj);
251
TColgp_Array1OfPnt poles(1, list.size());
253
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
255
Base::Vector3d v = p.toVector();
256
poles(index++) = gp_Pnt(v.x,v.y,v.z);
259
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
260
(getGeometryPtr()->handle());
262
surf->InsertPoleColBefore(vindex, poles);
265
Py::Sequence list(obj2);
266
TColStd_Array1OfReal weights(1, list.size());
268
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
269
weights(index++) = (double)Py::Float(*it);
271
surf->InsertPoleColBefore(vindex, poles, weights);
276
catch (Standard_Failure& e) {
277
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
282
PyObject* BezierSurfacePy::insertPoleRowBefore(PyObject *args)
286
PyObject* obj2=nullptr;
287
if (!PyArg_ParseTuple(args, "iO|O",&uindex,&obj,&obj2))
290
Py::Sequence list(obj);
291
TColgp_Array1OfPnt poles(1, list.size());
293
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
295
Base::Vector3d v = p.toVector();
296
poles(index++) = gp_Pnt(v.x,v.y,v.z);
299
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
300
(getGeometryPtr()->handle());
302
surf->InsertPoleRowBefore(uindex, poles);
305
Py::Sequence list(obj2);
306
TColStd_Array1OfReal weights(1, list.size());
308
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
309
weights(index++) = (double)Py::Float(*it);
311
surf->InsertPoleRowBefore(uindex, poles, weights);
316
catch (Standard_Failure& e) {
317
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
322
PyObject* BezierSurfacePy::removePoleCol(PyObject *args)
325
if (!PyArg_ParseTuple(args, "i",&vindex))
328
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
329
(getGeometryPtr()->handle());
330
surf->RemovePoleCol(vindex);
333
catch (Standard_Failure& e) {
334
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
339
PyObject* BezierSurfacePy::removePoleRow(PyObject *args)
342
if (!PyArg_ParseTuple(args, "i",&uindex))
345
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
346
(getGeometryPtr()->handle());
347
surf->RemovePoleRow(uindex);
350
catch (Standard_Failure& e) {
351
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
356
PyObject* BezierSurfacePy::segment(PyObject *args)
358
Standard_Real u1,u2,v1,v2;
359
if (!PyArg_ParseTuple(args, "dddd",&u1,&u2,&v1,&v2))
362
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
363
(getGeometryPtr()->handle());
364
surf->Segment(u1,u2,v1,v2);
367
catch (Standard_Failure& e) {
368
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
373
PyObject* BezierSurfacePy::setPole(PyObject *args)
378
if (!PyArg_ParseTuple(args, "iiO!|d",&uindex,&vindex,&(Base::VectorPy::Type),&obj,&weight))
381
Base::Vector3d pole = static_cast<Base::VectorPy*>(obj)->value();
382
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
383
(getGeometryPtr()->handle());
384
if (weight <= gp::Resolution())
385
surf->SetPole(uindex,vindex,gp_Pnt(pole.x,pole.y,pole.z));
387
surf->SetPole(uindex,vindex,gp_Pnt(pole.x,pole.y,pole.z),weight);
390
catch (Standard_Failure& e) {
391
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
396
PyObject* BezierSurfacePy::setPoleCol(PyObject *args)
400
PyObject* obj2=nullptr;
401
if (!PyArg_ParseTuple(args, "iO|O",&vindex,&obj,&obj2))
404
Py::Sequence list(obj);
405
TColgp_Array1OfPnt poles(1, list.size());
407
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
409
Base::Vector3d v = p.toVector();
410
poles(index++) = gp_Pnt(v.x,v.y,v.z);
413
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
414
(getGeometryPtr()->handle());
416
surf->SetPoleCol(vindex, poles);
419
Py::Sequence list(obj2);
420
TColStd_Array1OfReal weights(1, list.size());
422
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
423
weights(index++) = (double)Py::Float(*it);
425
surf->SetPoleCol(vindex, poles, weights);
430
catch (Standard_Failure& e) {
431
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
436
PyObject* BezierSurfacePy::setPoleRow(PyObject *args)
440
PyObject* obj2=nullptr;
441
if (!PyArg_ParseTuple(args, "iO|O",&uindex,&obj,&obj2))
444
Py::Sequence list(obj);
445
TColgp_Array1OfPnt poles(1, list.size());
447
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
449
Base::Vector3d v = p.toVector();
450
poles(index++) = gp_Pnt(v.x,v.y,v.z);
453
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
454
(getGeometryPtr()->handle());
456
surf->SetPoleRow(uindex, poles);
459
Py::Sequence list(obj2);
460
TColStd_Array1OfReal weights(1, list.size());
462
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
463
weights(index++) = (double)Py::Float(*it);
465
surf->SetPoleRow(uindex, poles, weights);
470
catch (Standard_Failure& e) {
471
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
476
PyObject* BezierSurfacePy::getPole(PyObject *args)
479
if (!PyArg_ParseTuple(args, "ii",&uindex,&vindex))
482
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
483
(getGeometryPtr()->handle());
484
Standard_OutOfRange_Raise_if
485
(uindex < 1 || uindex > surf->NbUPoles() ||
486
vindex < 1 || vindex > surf->NbVPoles(), "Pole index out of range");
487
gp_Pnt p = surf->Pole(uindex,vindex);
488
return new Base::VectorPy(Base::Vector3d(p.X(),p.Y(),p.Z()));
490
catch (Standard_Failure& e) {
491
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
496
PyObject* BezierSurfacePy::getPoles(PyObject *args)
498
if (!PyArg_ParseTuple(args, ""))
501
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
502
(getGeometryPtr()->handle());
503
TColgp_Array2OfPnt p(1,surf->NbUPoles(),1,surf->NbVPoles());
506
for (Standard_Integer i=p.LowerRow(); i<=p.UpperRow(); i++) {
508
for (Standard_Integer j=p.LowerCol(); j<=p.UpperCol(); j++) {
509
const gp_Pnt& pole = p(i,j);
510
row.append(Py::asObject(new Base::VectorPy(
511
Base::Vector3d(pole.X(),pole.Y(),pole.Z()))));
515
return Py::new_reference_to(poles);
517
catch (Standard_Failure& e) {
518
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
523
PyObject* BezierSurfacePy::setWeight(PyObject *args)
527
if (!PyArg_ParseTuple(args, "iid",&uindex,&vindex,&weight))
530
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
531
(getGeometryPtr()->handle());
532
surf->SetWeight(uindex,vindex,weight);
535
catch (Standard_Failure& e) {
536
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
541
PyObject* BezierSurfacePy::setWeightCol(PyObject *args)
545
if (!PyArg_ParseTuple(args, "iO",&vindex,&obj))
548
Py::Sequence list(obj);
549
TColStd_Array1OfReal weights(1, list.size());
551
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
552
weights(index++) = (double)Py::Float(*it);
555
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
556
(getGeometryPtr()->handle());
557
surf->SetWeightCol(vindex, weights);
560
catch (Standard_Failure& e) {
561
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
566
PyObject* BezierSurfacePy::setWeightRow(PyObject *args)
570
if (!PyArg_ParseTuple(args, "iO",&uindex,&obj))
573
Py::Sequence list(obj);
574
TColStd_Array1OfReal weights(1, list.size());
576
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
577
weights(index++) = (double)Py::Float(*it);
580
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
581
(getGeometryPtr()->handle());
582
surf->SetWeightRow(uindex, weights);
585
catch (Standard_Failure& e) {
586
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
591
PyObject* BezierSurfacePy::getWeight(PyObject *args)
594
if (!PyArg_ParseTuple(args, "ii",&uindex,&vindex))
597
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
598
(getGeometryPtr()->handle());
599
Standard_OutOfRange_Raise_if
600
(uindex < 1 || uindex > surf->NbUPoles() ||
601
vindex < 1 || vindex > surf->NbVPoles(), "Weight index out of range");
602
double w = surf->Weight(uindex,vindex);
603
return Py_BuildValue("d", w);
605
catch (Standard_Failure& e) {
606
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
611
PyObject* BezierSurfacePy::getWeights(PyObject *args)
613
if (!PyArg_ParseTuple(args, ""))
616
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
617
(getGeometryPtr()->handle());
618
TColStd_Array2OfReal w(1,surf->NbUPoles(),1,surf->NbVPoles());
621
for (Standard_Integer i=w.LowerRow(); i<=w.UpperRow(); i++) {
623
for (Standard_Integer j=w.LowerCol(); j<=w.UpperCol(); j++) {
624
row.append(Py::Float(w(i,j)));
628
return Py::new_reference_to(weights);
630
catch (Standard_Failure& e) {
631
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
636
PyObject* BezierSurfacePy::getResolution(PyObject *args)
639
if (!PyArg_ParseTuple(args, "d", &tol))
642
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
643
(getGeometryPtr()->handle());
645
surf->Resolution(tol,utol,vtol);
646
return Py_BuildValue("(dd)",utol,vtol);
648
catch (Standard_Failure& e) {
649
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
654
PyObject* BezierSurfacePy::exchangeUV(PyObject *args)
656
if (!PyArg_ParseTuple(args, ""))
660
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
661
(getGeometryPtr()->handle());
666
catch (Standard_Failure& e) {
667
PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
672
Py::Long BezierSurfacePy::getUDegree() const
674
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
675
(getGeometryPtr()->handle());
676
return Py::Long(surf->UDegree());
679
Py::Long BezierSurfacePy::getVDegree() const
681
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
682
(getGeometryPtr()->handle());
683
return Py::Long(surf->VDegree());
686
Py::Long BezierSurfacePy::getMaxDegree() const
688
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
689
(getGeometryPtr()->handle());
690
return Py::Long(surf->MaxDegree());
693
Py::Long BezierSurfacePy::getNbUPoles() const
695
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
696
(getGeometryPtr()->handle());
697
return Py::Long(surf->NbUPoles());
700
Py::Long BezierSurfacePy::getNbVPoles() const
702
Handle(Geom_BezierSurface) surf = Handle(Geom_BezierSurface)::DownCast
703
(getGeometryPtr()->handle());
704
return Py::Long(surf->NbVPoles());
707
PyObject *BezierSurfacePy::getCustomAttributes(const char* /*attr*/) const
712
int BezierSurfacePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)