/
GeometrySurfacePyImp.cpp
417 lines (377 loc) · 12.5 KB
/
GeometrySurfacePyImp.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
/***************************************************************************
* Copyright (c) 2009 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 <BRepBuilderAPI_MakeFace.hxx>
# include <gp_Dir.hxx>
# include <gp_Vec.hxx>
# include <Geom_Geometry.hxx>
# include <Geom_Surface.hxx>
# include <GeomConvert_ApproxSurface.hxx>
# include <GeomLProp_SLProps.hxx>
# include <Precision.hxx>
# include <Standard_Failure.hxx>
# include <Standard_Version.hxx>
# include <ShapeAnalysis_Surface.hxx>
#endif
#include <Base/GeometryPyCXX.h>
#include <Base/VectorPy.h>
#include "Geometry.h"
#include "GeometrySurfacePy.h"
#include "GeometrySurfacePy.cpp"
#include "BSplineSurfacePy.h"
#include "TopoShape.h"
#include "TopoShapePy.h"
#include "TopoShapeFacePy.h"
using namespace Part;
// returns a string which represents the object e.g. when printed in python
std::string GeometrySurfacePy::representation(void) const
{
return "<Surface object>";
}
PyObject *GeometrySurfacePy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
{
// never create such objects with the constructor
PyErr_SetString(PyExc_RuntimeError,
"You cannot create an instance of the abstract class 'GeometrySurface'.");
return 0;
}
// constructor method
int GeometrySurfacePy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
{
return 0;
}
PyObject* GeometrySurfacePy::toShape(PyObject *args)
{
Handle_Geom_Geometry g = getGeometryPtr()->handle();
Handle_Geom_Surface s = Handle_Geom_Surface::DownCast(g);
try {
if (!s.IsNull()) {
double u1,u2,v1,v2;
s->Bounds(u1,u2,v1,v2);
if (!PyArg_ParseTuple(args, "|dddd", &u1,&u2,&v1,&v2))
return 0;
BRepBuilderAPI_MakeFace mkBuilder(s, u1, u2, v1, v2
#if OCC_VERSION_HEX >= 0x060502
, Precision::Confusion()
#endif
);
TopoDS_Shape sh = mkBuilder.Shape();
return new TopoShapeFacePy(new TopoShape(sh));
}
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
PyErr_SetString(PyExc_Exception, e->GetMessageString());
return 0;
}
PyErr_SetString(PyExc_Exception, "Geometry is not a surface");
return 0;
}
PyObject* GeometrySurfacePy::value(PyObject *args)
{
Handle_Geom_Geometry g = getGeometryPtr()->handle();
Handle_Geom_Surface s = Handle_Geom_Surface::DownCast(g);
try {
if (!s.IsNull()) {
double u,v;
if (!PyArg_ParseTuple(args, "dd", &u,&v))
return 0;
gp_Pnt p = s->Value(u,v);
return new Base::VectorPy(Base::Vector3d(p.X(),p.Y(),p.Z()));
}
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
PyErr_SetString(PyExc_Exception, e->GetMessageString());
return 0;
}
PyErr_SetString(PyExc_Exception, "Geometry is not a surface");
return 0;
}
PyObject* GeometrySurfacePy::tangent(PyObject *args)
{
Handle_Geom_Geometry g = getGeometryPtr()->handle();
Handle_Geom_Surface s = Handle_Geom_Surface::DownCast(g);
try {
if (!s.IsNull()) {
double u,v;
if (!PyArg_ParseTuple(args, "dd", &u,&v))
return 0;
gp_Dir dir;
Py::Tuple tuple(2);
GeomLProp_SLProps prop(s,u,v,1,Precision::Confusion());
if (prop.IsTangentUDefined()) {
prop.TangentU(dir);
tuple.setItem(0, Py::Vector(Base::Vector3d(dir.X(),dir.Y(),dir.Z())));
}
if (prop.IsTangentVDefined()) {
prop.TangentV(dir);
tuple.setItem(1, Py::Vector(Base::Vector3d(dir.X(),dir.Y(),dir.Z())));
}
return Py::new_reference_to(tuple);
}
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
PyErr_SetString(PyExc_Exception, e->GetMessageString());
return 0;
}
PyErr_SetString(PyExc_Exception, "Geometry is not a surface");
return 0;
}
PyObject* GeometrySurfacePy::parameter(PyObject *args)
{
Handle_Geom_Surface surf = Handle_Geom_Surface
::DownCast(getGeometryPtr()->handle());
try {
if (!surf.IsNull()) {
PyObject *p;
double prec = Precision::Confusion();
if (!PyArg_ParseTuple(args, "O!|d", &(Base::VectorPy::Type), &p, &prec))
return 0;
Base::Vector3d v = Py::Vector(p, false).toVector();
gp_Pnt pnt(v.x,v.y,v.z);
ShapeAnalysis_Surface as(surf);
gp_Pnt2d uv = as.ValueOfUV(pnt, prec);
Py::Tuple tuple(2);
tuple.setItem(0, Py::Float(uv.X()));
tuple.setItem(1, Py::Float(uv.Y()));
return Py::new_reference_to(tuple);
}
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
PyErr_SetString(PyExc_Exception, e->GetMessageString());
return 0;
}
PyErr_SetString(PyExc_Exception, "Geometry is not a surface");
return 0;
}
PyObject* GeometrySurfacePy::bounds(PyObject * args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
Handle_Geom_Surface surf = Handle_Geom_Surface
::DownCast(getGeometryPtr()->handle());
Py::Tuple bound(4);
Standard_Real u1,u2,v1,v2;
surf->Bounds(u1,u2,v1,v2);
bound.setItem(0,Py::Float(u1));
bound.setItem(1,Py::Float(u2));
bound.setItem(2,Py::Float(v1));
bound.setItem(3,Py::Float(v2));
return Py::new_reference_to(bound);
}
PyObject* GeometrySurfacePy::isUPeriodic(PyObject * args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast
(getGeometryPtr()->handle());
Standard_Boolean val = surf->IsUPeriodic();
if (val) {
Py_INCREF(Py_True);
return Py_True;
}
else {
Py_INCREF(Py_False);
return Py_False;
}
}
PyObject* GeometrySurfacePy::isVPeriodic(PyObject * args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast
(getGeometryPtr()->handle());
Standard_Boolean val = surf->IsVPeriodic();
if (val) {
Py_INCREF(Py_True);
return Py_True;
}
else {
Py_INCREF(Py_False);
return Py_False;
}
}
PyObject* GeometrySurfacePy::isUClosed(PyObject * args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast
(getGeometryPtr()->handle());
Standard_Boolean val = surf->IsUClosed();
if (val) {
Py_INCREF(Py_True);
return Py_True;
}
else {
Py_INCREF(Py_False);
return Py_False;
}
}
PyObject* GeometrySurfacePy::isVClosed(PyObject * args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast
(getGeometryPtr()->handle());
Standard_Boolean val = surf->IsVClosed();
if (val) {
Py_INCREF(Py_True);
return Py_True;
}
else {
Py_INCREF(Py_False);
return Py_False;
}
}
PyObject* GeometrySurfacePy::UPeriod(PyObject * args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
try {
Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast
(getGeometryPtr()->handle());
Standard_Real val = surf->UPeriod();
return PyFloat_FromDouble(val);
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
PyErr_SetString(PyExc_Exception, e->GetMessageString());
return 0;
}
}
PyObject* GeometrySurfacePy::VPeriod(PyObject * args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;
try {
Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast
(getGeometryPtr()->handle());
Standard_Real val = surf->VPeriod();
return PyFloat_FromDouble(val);
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
PyErr_SetString(PyExc_Exception, e->GetMessageString());
return 0;
}
}
Py::String GeometrySurfacePy::getContinuity(void) const
{
GeomAbs_Shape c = Handle_Geom_Surface::DownCast
(getGeometryPtr()->handle())->Continuity();
std::string str;
switch (c) {
case GeomAbs_C0:
str = "C0";
break;
case GeomAbs_G1:
str = "G1";
break;
case GeomAbs_C1:
str = "C1";
break;
case GeomAbs_G2:
str = "G2";
break;
case GeomAbs_C2:
str = "C2";
break;
case GeomAbs_C3:
str = "C3";
break;
case GeomAbs_CN:
str = "CN";
break;
default:
str = "Unknown";
break;
}
return Py::String(str);
}
PyObject* GeometrySurfacePy::toBSpline(PyObject * args)
{
double tol3d;
char *ucont, *vcont;
int maxDegU,maxDegV,maxSegm,prec=0;
if (!PyArg_ParseTuple(args, "dssiii|i",&tol3d,&ucont,&vcont,
&maxDegU,&maxDegV,&maxSegm,&prec))
return 0;
std::string uc = ucont;
GeomAbs_Shape absU, absV;
if (uc == "C0")
absU = GeomAbs_C0;
else if (uc == "C1")
absU = GeomAbs_C1;
else if (uc == "C2")
absU = GeomAbs_C2;
else if (uc == "C3")
absU = GeomAbs_C3;
else if (uc == "CN")
absU = GeomAbs_CN;
else if (uc == "G1")
absU = GeomAbs_G1;
else
absU = GeomAbs_G2;
std::string vc = vcont;
if (vc == "C0")
absV = GeomAbs_C0;
else if (vc == "C1")
absV = GeomAbs_C1;
else if (vc == "C2")
absV = GeomAbs_C2;
else if (vc == "C3")
absV = GeomAbs_C3;
else if (vc == "CN")
absV = GeomAbs_CN;
else if (vc == "G1")
absV = GeomAbs_G1;
else
absV = GeomAbs_G2;
try {
Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast
(getGeometryPtr()->handle());
GeomConvert_ApproxSurface cvt(surf, tol3d, absU, absV, maxDegU, maxDegV, maxSegm, prec);
if (cvt.IsDone() && cvt.HasResult()) {
return new BSplineSurfacePy(new GeomBSplineSurface(cvt.Surface()));
}
else {
Standard_Failure::Raise("Cannot convert to B-Spline surface");
}
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
PyErr_SetString(PyExc_Exception, e->GetMessageString());
}
return 0;
}
PyObject *GeometrySurfacePy::getCustomAttributes(const char* /*attr*/) const
{
return 0;
}
int GeometrySurfacePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
{
return 0;
}