FreeCAD

Форк
0
/
GeometrySurfacePyImp.cpp 
894 строки · 28.3 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
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.      *
10
 *                                                                         *
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.                  *
15
 *                                                                         *
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                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24

25
#ifndef _PreComp_
26
# include <BRepBuilderAPI_MakeFace.hxx>
27
# include <BRepBuilderAPI_MakeShell.hxx>
28
# include <Geom_BSplineSurface.hxx>
29
# include <Geom_Geometry.hxx>
30
# include <Geom_Surface.hxx>
31
# include <GeomAPI_IntSS.hxx>
32
# include <GeomAPI_ProjectPointOnSurf.hxx>
33
# include <GeomConvert_ApproxSurface.hxx>
34
# include <GeomLib_IsPlanarSurface.hxx>
35
# include <GeomLProp_SLProps.hxx>
36
# include <gp_Dir.hxx>
37
# include <gp_Quaternion.hxx>
38
# include <gp_Vec.hxx>
39
# include <Precision.hxx>
40
# include <ShapeAnalysis_Surface.hxx>
41
# include <Standard_Failure.hxx>
42
# include <Standard_Version.hxx>
43
#endif
44

45
#include <Base/GeometryPyCXX.h>
46
#include <Base/PyWrapParseTupleAndKeywords.h>
47
#include <Base/VectorPy.h>
48

49
#include "GeometrySurfacePy.h"
50
#include "GeometrySurfacePy.cpp"
51
#include "BSplineSurfacePy.h"
52
#include "GeometryCurvePy.h"
53
#include "LinePy.h"
54
#include "OCCError.h"
55
#include "TopoShapeFacePy.h"
56
#include "TopoShapeShellPy.h"
57

58

59
namespace Part {
60
const Py::Object makeTrimmedCurvePy(const Handle(Geom_Curve)& c, double f, double l)
61
{
62
    try {
63
        std::unique_ptr<GeomCurve> gc(makeFromTrimmedCurve(c, f, l));
64
        return Py::asObject(gc->getPyObject());
65
    }
66
    catch (const Base::Exception& e) {
67
        throw Py::TypeError(e.what());
68
    }
69
}
70

71
const Py::Object makeGeometryCurvePy(const Handle(Geom_Curve)& c)
72
{
73
    try {
74
        std::unique_ptr<GeomCurve> gc(makeFromCurve(c));
75
        return Py::asObject(gc->getPyObject());
76
    }
77
    catch (const Base::Exception& e) {
78
        throw Py::TypeError(e.what());
79
    }
80
}
81

82
} // Part
83

84
// ---------------------------------------
85

86
using namespace Part;
87

88
// returns a string which represents the object e.g. when printed in python
89
std::string GeometrySurfacePy::representation() const
90
{
91
    return "<Surface object>";
92
}
93

94
PyObject *GeometrySurfacePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
95
{
96
    // never create such objects with the constructor
97
    PyErr_SetString(PyExc_RuntimeError,
98
        "You cannot create an instance of the abstract class 'GeometrySurface'.");
99
    return nullptr;
100
}
101

102
// constructor method
103
int GeometrySurfacePy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
104
{
105
    return 0;
106
}
107

108
PyObject* GeometrySurfacePy::toShape(PyObject *args)
109
{
110
    Handle(Geom_Geometry) g = getGeometryPtr()->handle();
111
    Handle(Geom_Surface) s = Handle(Geom_Surface)::DownCast(g);
112
    try {
113
        if (!s.IsNull()) {
114
            double u1,u2,v1,v2;
115
            s->Bounds(u1,u2,v1,v2);
116
            if (!PyArg_ParseTuple(args, "|dddd", &u1,&u2,&v1,&v2))
117
                return nullptr;
118
            BRepBuilderAPI_MakeFace mkBuilder(s, u1, u2, v1, v2, Precision::Confusion() );
119
            TopoDS_Shape sh = mkBuilder.Shape();
120
            return new TopoShapeFacePy(new TopoShape(sh));
121
        }
122
    }
123
    catch (Standard_Failure& e) {
124

125
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
126
        return nullptr;
127
    }
128

129
    PyErr_SetString(PartExceptionOCCError, "Geometry is not a surface");
130
    return nullptr;
131
}
132

133
PyObject* GeometrySurfacePy::toShell(PyObject *args, PyObject* kwds)
134
{
135
    PyObject* bound = nullptr;
136
    PyObject* segm = nullptr;
137
    static const std::array<const char *, 3> kwlist {"Bounds", "Segment", nullptr};
138
    if (!Base::Wrapped_ParseTupleAndKeywords(args, kwds, "|O!O!", kwlist,
139
                                             &PyTuple_Type, &bound, &PyBool_Type, &segm)) {
140
        return nullptr;
141
    }
142

143
    Handle(Geom_Geometry) g = getGeometryPtr()->handle();
144
    Handle(Geom_Surface) s = Handle(Geom_Surface)::DownCast(g);
145
    try {
146
        if (!s.IsNull()) {
147
            if (segm) {
148
                Standard_Boolean segment = Base::asBoolean(segm);
149
                BRepBuilderAPI_MakeShell mkBuilder(s, segment);
150
                TopoDS_Shape sh = mkBuilder.Shape();
151
                return new TopoShapeShellPy(new TopoShape(sh));
152
            }
153
            else {
154
                double u1,u2,v1,v2;
155
                s->Bounds(u1,u2,v1,v2);
156

157
                if (bound) {
158
                    Py::Tuple tuple(bound);
159
                    u1 = double(Py::Float(tuple[0]));
160
                    u2 = double(Py::Float(tuple[1]));
161
                    v1 = double(Py::Float(tuple[2]));
162
                    v2 = double(Py::Float(tuple[3]));
163
                }
164

165
                BRepBuilderAPI_MakeShell mkBuilder(s, u1, u2, v1, v2);
166
                TopoDS_Shape sh = mkBuilder.Shape();
167
                return new TopoShapeShellPy(new TopoShape(sh));
168
            }
169
        }
170
    }
171
    catch (Standard_Failure& e) {
172
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
173
        return nullptr;
174
    }
175

176
    PyErr_SetString(PartExceptionOCCError, "Geometry is not a surface");
177
    return nullptr;
178
}
179

180
PyObject* GeometrySurfacePy::getD0(PyObject *args)
181
{
182
    Handle(Geom_Geometry) g = getGeometryPtr()->handle();
183
    Handle(Geom_Surface) s = Handle(Geom_Surface)::DownCast(g);
184
    try {
185
        if (!s.IsNull()) {
186
            double u,v;
187
            if (!PyArg_ParseTuple(args, "dd", &u, &v))
188
                return nullptr;
189
            gp_Pnt p;
190
            s->D0(u, v, p);
191
            return new Base::VectorPy(Base::Vector3d(p.X(),p.Y(),p.Z()));
192
        }
193
    }
194
    catch (Standard_Failure& e) {
195
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
196
        return nullptr;
197
    }
198

199
    PyErr_SetString(PartExceptionOCCError, "Geometry is not a surface");
200
    return nullptr;
201
}
202

203
PyObject* GeometrySurfacePy::getDN(PyObject *args)
204
{
205
    try {
206
        int nu, nv;
207
        double u,v;
208
        if (!PyArg_ParseTuple(args, "ddii", &u, &v, &nu, &nv))
209
            return nullptr;
210
        gp_Vec v1 = getGeomSurfacePtr()->getDN(u, v, nu, nv);
211
        return new Base::VectorPy(Base::Vector3d(v1.X(),v1.Y(),v1.Z()));
212
    }
213
    catch (Standard_Failure& e) {
214
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
215
        return nullptr;
216
    }
217
}
218

219
PyObject* GeometrySurfacePy::value(PyObject *args)
220
{
221
    Handle(Geom_Geometry) g = getGeometryPtr()->handle();
222
    Handle(Geom_Surface) s = Handle(Geom_Surface)::DownCast(g);
223
    try {
224
        if (!s.IsNull()) {
225
            double u,v;
226
            if (!PyArg_ParseTuple(args, "dd", &u,&v))
227
                return nullptr;
228
            gp_Pnt p = s->Value(u,v);
229
            return new Base::VectorPy(Base::Vector3d(p.X(),p.Y(),p.Z()));
230
        }
231
    }
232
    catch (Standard_Failure& e) {
233

234
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
235
        return nullptr;
236
    }
237

238
    PyErr_SetString(PartExceptionOCCError, "Geometry is not a surface");
239
    return nullptr;
240
}
241

242
PyObject* GeometrySurfacePy::tangent(PyObject *args)
243
{
244
    Handle(Geom_Geometry) g = getGeometryPtr()->handle();
245
    Handle(Geom_Surface) s = Handle(Geom_Surface)::DownCast(g);
246
    try {
247
        if (!s.IsNull()) {
248
            double u,v;
249
            if (!PyArg_ParseTuple(args, "dd", &u,&v))
250
                return nullptr;
251
            gp_Dir dir;
252
            Py::Tuple tuple(2);
253
            GeomLProp_SLProps prop(s,u,v,2,Precision::Confusion());
254
            if (prop.IsTangentUDefined()) {
255
                prop.TangentU(dir);
256
                tuple.setItem(0, Py::Vector(Base::Vector3d(dir.X(),dir.Y(),dir.Z())));
257
            }
258
            if (prop.IsTangentVDefined()) {
259
                prop.TangentV(dir);
260
                tuple.setItem(1, Py::Vector(Base::Vector3d(dir.X(),dir.Y(),dir.Z())));
261
            }
262

263
            return Py::new_reference_to(tuple);
264
        }
265
    }
266
    catch (Standard_Failure& e) {
267

268
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
269
        return nullptr;
270
    }
271

272
    PyErr_SetString(PartExceptionOCCError, "Geometry is not a surface");
273
    return nullptr;
274
}
275

276
PyObject* GeometrySurfacePy::normal(PyObject *args)
277
{
278
    try {
279
        GeomSurface* s = getGeomSurfacePtr();
280
        if (s) {
281
            double u,v;
282
            if (!PyArg_ParseTuple(args, "dd", &u,&v))
283
                return nullptr;
284
            gp_Dir d;
285
            if (s->normal(u,v,d)) {
286
                return new Base::VectorPy(Base::Vector3d(d.X(),d.Y(),d.Z()));
287
            }
288
            else {
289
                PyErr_SetString(PyExc_RuntimeError, "normal at this point is not defined");
290
                return nullptr;
291
            }
292
        }
293
    }
294
    catch (Standard_Failure& e) {
295
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
296
        return nullptr;
297
    }
298

299
    PyErr_SetString(PartExceptionOCCError, "Geometry is not a surface");
300
    return nullptr;
301
}
302

303
PyObject* GeometrySurfacePy::projectPoint(PyObject *args, PyObject* kwds)
304
{
305
    PyObject* v;
306
    const char* meth = "NearestPoint";
307
    static const std::array<const char *, 3> kwlist {"Point", "Method", nullptr};
308
    if (!Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!|s", kwlist, &Base::VectorPy::Type, &v, &meth)) {
309
        return nullptr;
310
    }
311

312
    try {
313
        Base::Vector3d vec = Py::Vector(v, false).toVector();
314
        gp_Pnt pnt(vec.x, vec.y, vec.z);
315
        std::string method = meth;
316

317
        Handle(Geom_Geometry) geom = getGeometryPtr()->handle();
318
        Handle(Geom_Surface) surf = Handle(Geom_Surface)::DownCast(geom);
319

320
        GeomAPI_ProjectPointOnSurf proj(pnt, surf);
321
        if (method == "NearestPoint") {
322
            pnt = proj.NearestPoint();
323
            vec.Set(pnt.X(), pnt.Y(), pnt.Z());
324
            return new Base::VectorPy(vec);
325
        }
326
        else if (method == "LowerDistance") {
327
            Py::Float dist(proj.LowerDistance());
328
            return Py::new_reference_to(dist);
329
        }
330
        else if (method == "LowerDistanceParameters") {
331
            Standard_Real u, v;
332
            proj.LowerDistanceParameters(u, v);
333
            Py::Tuple par(2);
334
            par.setItem(0, Py::Float(u));
335
            par.setItem(1, Py::Float(v));
336
            return Py::new_reference_to(par);
337
        }
338
        else if (method == "Distance") {
339
            Standard_Integer num = proj.NbPoints();
340
            Py::List list;
341
            for (Standard_Integer i=1; i <= num; i++) {
342
                list.append(Py::Float(proj.Distance(i)));
343
            }
344
            return Py::new_reference_to(list);
345
        }
346
        else if (method == "Parameters") {
347
            Standard_Integer num = proj.NbPoints();
348
            Py::List list;
349
            for (Standard_Integer i=1; i <= num; i++) {
350
                Standard_Real u, v;
351
                proj.Parameters(i, u, v);
352
                Py::Tuple par(2);
353
                par.setItem(0, Py::Float(u));
354
                par.setItem(1, Py::Float(v));
355
                list.append(par);
356
            }
357
            return Py::new_reference_to(list);
358
        }
359
        else if (method == "Point") {
360
            Standard_Integer num = proj.NbPoints();
361
            Py::List list;
362
            for (Standard_Integer i=1; i <= num; i++) {
363
                gp_Pnt pnt = proj.Point(i);
364
                Base::Vector3d vec(pnt.X(), pnt.Y(), pnt.Z());
365
                list.append(Py::Vector(vec));
366
            }
367
            return Py::new_reference_to(list);
368
        }
369
        else {
370
            PyErr_SetString(PartExceptionOCCError, "Unsupported method");
371
            return nullptr;
372
        }
373
    }
374
    catch (Standard_Failure& e) {
375
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
376
        return nullptr;
377
    }
378
}
379

380
PyObject* GeometrySurfacePy::isUmbillic(PyObject *args)
381
{
382
    try {
383
        GeomSurface* s = getGeomSurfacePtr();
384
        if (s) {
385
            double u,v;
386
            if (!PyArg_ParseTuple(args, "dd", &u,&v))
387
                return nullptr;
388

389
            bool val = s->isUmbillic(u,v);
390
            return PyBool_FromLong(val ? 1 : 0);
391
        }
392
    }
393
    catch (Standard_Failure& e) {
394

395
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
396
        return nullptr;
397
    }
398

399
    PyErr_SetString(PartExceptionOCCError, "Geometry is not a surface");
400
    return nullptr;
401
}
402

403
PyObject* GeometrySurfacePy::curvatureDirections(PyObject *args)
404
{
405
    try {
406
        GeomSurface* s = getGeomSurfacePtr();
407
        if (s) {
408
            double u,v;
409
            if (!PyArg_ParseTuple(args, "dd", &u,&v))
410
                return nullptr;
411

412
            gp_Dir maxd, mind;
413
            s->curvatureDirections(u,v,maxd,mind);
414

415
            Py::Tuple tuple(2);
416
            tuple.setItem(0, Py::Vector(Base::Vector3d(maxd.X(),maxd.Y(),maxd.Z())));
417
            tuple.setItem(1, Py::Vector(Base::Vector3d(mind.X(),mind.Y(),mind.Z())));
418
            return Py::new_reference_to(tuple);
419
        }
420
    }
421
    catch (Standard_Failure& e) {
422

423
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
424
        return nullptr;
425
    }
426

427
    PyErr_SetString(PartExceptionOCCError, "Geometry is not a surface");
428
    return nullptr;
429
}
430

431
PyObject* GeometrySurfacePy::curvature(PyObject *args)
432
{
433
    try {
434
        GeomSurface* s = getGeomSurfacePtr();
435
        if (s) {
436
            double u,v;
437
            char* type;
438
            if (!PyArg_ParseTuple(args, "dds", &u,&v,&type))
439
                return nullptr;
440

441
            GeomSurface::Curvature t;
442
            if (strcmp(type,"Max") == 0) {
443
                t = GeomSurface::Maximum;
444
            }
445
            else if (strcmp(type,"Min") == 0) {
446
                t = GeomSurface::Minimum;
447
            }
448
            else if (strcmp(type,"Mean") == 0) {
449
                t = GeomSurface::Mean;
450
            }
451
            else if (strcmp(type,"Gauss") == 0) {
452
                t = GeomSurface::Gaussian;
453
            }
454
            else {
455
                PyErr_SetString(PyExc_ValueError, "unknown curvature type");
456
                return nullptr;
457
            }
458

459
            double c = s->curvature(u,v,t);
460
            return PyFloat_FromDouble(c);
461
        }
462
    }
463
    catch (Standard_Failure& e) {
464

465
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
466
        return nullptr;
467
    }
468

469
    PyErr_SetString(PartExceptionOCCError, "Geometry is not a surface");
470
    return nullptr;
471
}
472

473
PyObject* GeometrySurfacePy::isPlanar(PyObject *args)
474
{
475
    try {
476
        Handle(Geom_Surface) surf = Handle(Geom_Surface)
477
            ::DownCast(getGeometryPtr()->handle());
478
        if (!surf.IsNull()) {
479
            double tol = Precision::Confusion();
480
            if (!PyArg_ParseTuple(args, "|d", &tol))
481
                return nullptr;
482

483
            GeomLib_IsPlanarSurface check(surf, tol);
484
            Standard_Boolean val = check.IsPlanar();
485
            return PyBool_FromLong(val ? 1 : 0);
486
        }
487
    }
488
    catch (Standard_Failure& e) {
489
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
490
        return nullptr;
491
    }
492

493
    PyErr_SetString(PartExceptionOCCError, "Geometry is not a surface");
494
    return nullptr;
495
}
496

497
PyObject* GeometrySurfacePy::parameter(PyObject *args)
498
{
499
    Handle(Geom_Surface) surf = Handle(Geom_Surface)
500
        ::DownCast(getGeometryPtr()->handle());
501
    try {
502
        if (!surf.IsNull()) {
503
            PyObject *p;
504
            double prec = Precision::Confusion();
505
            if (!PyArg_ParseTuple(args, "O!|d", &(Base::VectorPy::Type), &p, &prec))
506
                return nullptr;
507
            Base::Vector3d v = Py::Vector(p, false).toVector();
508
            gp_Pnt pnt(v.x,v.y,v.z);
509
            ShapeAnalysis_Surface as(surf);
510
            gp_Pnt2d uv = as.ValueOfUV(pnt, prec);
511
            Py::Tuple tuple(2);
512
            tuple.setItem(0, Py::Float(uv.X()));
513
            tuple.setItem(1, Py::Float(uv.Y()));
514
            return Py::new_reference_to(tuple);
515
        }
516
    }
517
    catch (Standard_Failure& e) {
518

519
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
520
        return nullptr;
521
    }
522

523
    PyErr_SetString(PartExceptionOCCError, "Geometry is not a surface");
524
    return nullptr;
525
}
526

527
PyObject* GeometrySurfacePy::bounds(PyObject * args)
528
{
529
    if (!PyArg_ParseTuple(args, ""))
530
        return nullptr;
531

532
    Handle(Geom_Surface) surf = Handle(Geom_Surface)
533
        ::DownCast(getGeometryPtr()->handle());
534
    Py::Tuple bound(4);
535
    Standard_Real u1,u2,v1,v2;
536
    surf->Bounds(u1,u2,v1,v2);
537
    bound.setItem(0,Py::Float(u1));
538
    bound.setItem(1,Py::Float(u2));
539
    bound.setItem(2,Py::Float(v1));
540
    bound.setItem(3,Py::Float(v2));
541
    return Py::new_reference_to(bound);
542
}
543

544
PyObject* GeometrySurfacePy::uIso(PyObject * args)
545
{
546
    double v;
547
    if (!PyArg_ParseTuple(args, "d", &v))
548
        return nullptr;
549

550
    try {
551
        Handle(Geom_Surface) surf = Handle(Geom_Surface)::DownCast
552
            (getGeometryPtr()->handle());
553
        Handle(Geom_Curve) c = surf->UIso(v);
554
        if (c.IsNull()) {
555
            PyErr_SetString(PyExc_RuntimeError, "failed to create u iso curve");
556
            return nullptr;
557
        }
558

559
        if (c->IsKind(STANDARD_TYPE(Geom_Line))) {
560
            Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(c);
561
            GeomLine* line = new GeomLine();
562
            Handle(Geom_Line) this_curv = Handle(Geom_Line)::DownCast
563
                (line->handle());
564
            this_curv->SetLin(aLine->Lin());
565
            return new LinePy(line);
566
        }
567
        else {
568
            return Py::new_reference_to(makeGeometryCurvePy(c));
569
        }
570
    }
571
    catch (Standard_Failure& e) {
572

573
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
574
        return nullptr;
575
    }
576
}
577

578
PyObject* GeometrySurfacePy::vIso(PyObject * args)
579
{
580
    double v;
581
    if (!PyArg_ParseTuple(args, "d", &v))
582
        return nullptr;
583

584
    try {
585
        Handle(Geom_Surface) surf = Handle(Geom_Surface)::DownCast
586
            (getGeometryPtr()->handle());
587
        Handle(Geom_Curve) c = surf->VIso(v);
588
        if (c.IsNull()) {
589
            PyErr_SetString(PyExc_RuntimeError, "failed to create v iso curve");
590
            return nullptr;
591
        }
592

593
        if (c->IsKind(STANDARD_TYPE(Geom_Line))) {
594
            Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(c);
595
            GeomLine* line = new GeomLine();
596
            Handle(Geom_Line) this_curv = Handle(Geom_Line)::DownCast
597
                (line->handle());
598
            this_curv->SetLin(aLine->Lin());
599
            return new LinePy(line);
600
        }
601
        else {
602
            return Py::new_reference_to(makeGeometryCurvePy(c));
603
        }
604
    }
605
    catch (Standard_Failure& e) {
606

607
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
608
        return nullptr;
609
    }
610
}
611

612
PyObject* GeometrySurfacePy::isUPeriodic(PyObject * args)
613
{
614
    if (!PyArg_ParseTuple(args, ""))
615
        return nullptr;
616

617
    Handle(Geom_Surface) surf = Handle(Geom_Surface)::DownCast
618
        (getGeometryPtr()->handle());
619
    Standard_Boolean val = surf->IsUPeriodic();
620
    return PyBool_FromLong(val ? 1 : 0);
621
}
622

623
PyObject* GeometrySurfacePy::isVPeriodic(PyObject * args)
624
{
625
    if (!PyArg_ParseTuple(args, ""))
626
        return nullptr;
627

628
    Handle(Geom_Surface) surf = Handle(Geom_Surface)::DownCast
629
        (getGeometryPtr()->handle());
630
    Standard_Boolean val = surf->IsVPeriodic();
631
    return PyBool_FromLong(val ? 1 : 0);
632
}
633

634
PyObject* GeometrySurfacePy::isUClosed(PyObject * args)
635
{
636
    if (!PyArg_ParseTuple(args, ""))
637
        return nullptr;
638

639
    Handle(Geom_Surface) surf = Handle(Geom_Surface)::DownCast
640
        (getGeometryPtr()->handle());
641
    Standard_Boolean val = surf->IsUClosed();
642
    return PyBool_FromLong(val ? 1 : 0);
643
}
644

645
PyObject* GeometrySurfacePy::isVClosed(PyObject * args)
646
{
647
    if (!PyArg_ParseTuple(args, ""))
648
        return nullptr;
649

650
    Handle(Geom_Surface) surf = Handle(Geom_Surface)::DownCast
651
        (getGeometryPtr()->handle());
652
    Standard_Boolean val = surf->IsVClosed();
653
    return PyBool_FromLong(val ? 1 : 0);
654
}
655

656
PyObject* GeometrySurfacePy::UPeriod(PyObject * args)
657
{
658
    if (!PyArg_ParseTuple(args, ""))
659
        return nullptr;
660

661
    try {
662
        Handle(Geom_Surface) surf = Handle(Geom_Surface)::DownCast
663
            (getGeometryPtr()->handle());
664
        Standard_Real val = surf->UPeriod();
665
        return PyFloat_FromDouble(val);
666
    }
667
    catch (Standard_Failure& e) {
668

669
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
670
        return nullptr;
671
    }
672
}
673

674
PyObject* GeometrySurfacePy::VPeriod(PyObject * args)
675
{
676
    if (!PyArg_ParseTuple(args, ""))
677
        return nullptr;
678

679
    try {
680
        Handle(Geom_Surface) surf = Handle(Geom_Surface)::DownCast
681
            (getGeometryPtr()->handle());
682
        Standard_Real val = surf->VPeriod();
683
        return PyFloat_FromDouble(val);
684
    }
685
    catch (Standard_Failure& e) {
686

687
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
688
        return nullptr;
689
    }
690
}
691

692
Py::String GeometrySurfacePy::getContinuity() const
693
{
694
    GeomAbs_Shape c = Handle(Geom_Surface)::DownCast
695
        (getGeometryPtr()->handle())->Continuity();
696
    std::string str;
697
    switch (c) {
698
    case GeomAbs_C0:
699
        str = "C0";
700
        break;
701
    case GeomAbs_G1:
702
        str = "G1";
703
        break;
704
    case GeomAbs_C1:
705
        str = "C1";
706
        break;
707
    case GeomAbs_G2:
708
        str = "G2";
709
        break;
710
    case GeomAbs_C2:
711
        str = "C2";
712
        break;
713
    case GeomAbs_C3:
714
        str = "C3";
715
        break;
716
    case GeomAbs_CN:
717
        str = "CN";
718
        break;
719
    default:
720
        str = "Unknown";
721
        break;
722
    }
723
    return Py::String(str);
724
}
725

726
PyObject* GeometrySurfacePy::toBSpline(PyObject * args, PyObject * kwds)
727
{
728
    double tol3d=Precision::Confusion();
729
    const char *ucont = "C1";
730
    const char *vcont = "C1";
731
    int maxDegU=Geom_BSplineSurface::MaxDegree();
732
    int maxDegV=Geom_BSplineSurface::MaxDegree();
733
    int maxSegm=1000, prec=0;
734

735
    static const std::array<const char *, 8> kwlist{"Tol3d", "UContinuity", "VContinuity", "MaxDegreeU", "MaxDegreeV",
736
                                                    "MaxSegments", "PrecisCode", nullptr};
737
    if (!Base::Wrapped_ParseTupleAndKeywords(args, kwds, "|dssiiii", kwlist,
738
                                             &tol3d, &ucont, &vcont,
739
                                             &maxDegU, &maxDegV, &maxSegm, &prec)) {
740
        return nullptr;
741
    }
742

743
    GeomAbs_Shape absU, absV;
744
    std::string uc = ucont;
745
    if (maxDegU <= 1)
746
        absU = GeomAbs_C0;
747
    else if (uc == "C0")
748
        absU = GeomAbs_C0;
749
    else if (uc == "C1")
750
        absU = GeomAbs_C1;
751
    else if (uc == "C2")
752
        absU = GeomAbs_C2;
753
    else if (uc == "C3")
754
        absU = GeomAbs_C3;
755
    else if (uc == "CN")
756
        absU = GeomAbs_CN;
757
    else if (uc == "G1")
758
        absU = GeomAbs_G1;
759
    else
760
        absU = GeomAbs_G2;
761

762
    std::string vc = vcont;
763
    if (maxDegV <= 1)
764
        absV = GeomAbs_C0;
765
    else if (vc == "C0")
766
        absV = GeomAbs_C0;
767
    else if (vc == "C1")
768
        absV = GeomAbs_C1;
769
    else if (vc == "C2")
770
        absV = GeomAbs_C2;
771
    else if (vc == "C3")
772
        absV = GeomAbs_C3;
773
    else if (vc == "CN")
774
        absV = GeomAbs_CN;
775
    else if (vc == "G1")
776
        absV = GeomAbs_G1;
777
    else
778
        absV = GeomAbs_G2;
779

780
    try {
781
        Handle(Geom_Surface) surf = Handle(Geom_Surface)::DownCast
782
            (getGeometryPtr()->handle());
783
        GeomConvert_ApproxSurface cvt(surf, tol3d, absU, absV, maxDegU, maxDegV, maxSegm, prec);
784
        if (cvt.IsDone() && cvt.HasResult()) {
785
            return new BSplineSurfacePy(new GeomBSplineSurface(cvt.Surface()));
786
        }
787
        else {
788
            Standard_Failure::Raise("Cannot convert to B-spline surface");
789
        }
790
    }
791
    catch (Standard_Failure& e) {
792

793
        PyErr_SetString(PartExceptionOCCError, e.GetMessageString());
794
    }
795

796
    return nullptr;
797
}
798

799
PyObject *GeometrySurfacePy::getCustomAttributes(const char* /*attr*/) const
800
{
801
    return nullptr;
802
}
803

804
int GeometrySurfacePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
805
{
806
    return 0;
807
}
808

809
// Specialized intersection functions
810

811
PyObject* GeometrySurfacePy::intersectSS(PyObject *args)
812
{
813
    Handle(Geom_Surface) surf1 = Handle(Geom_Surface)::DownCast(getGeometryPtr()->handle());
814
    try {
815
        if (!surf1.IsNull()) {
816
            PyObject *p;
817
            double prec = Precision::Confusion();
818
            if (!PyArg_ParseTuple(args, "O!|d", &(Part::GeometrySurfacePy::Type), &p, &prec))
819
                return nullptr;
820
            Handle(Geom_Surface) surf2 = Handle(Geom_Surface)::DownCast(static_cast<GeometryPy*>(p)->getGeometryPtr()->handle());
821
            GeomAPI_IntSS intersector(surf1, surf2, prec);
822
            if (!intersector.IsDone()) {
823
                PyErr_SetString(PyExc_RuntimeError, "Intersection of surfaces failed");
824
                return nullptr;
825
            }
826

827
            Py::List result;
828
            for (int i = 1; i <= intersector.NbLines(); i++) {
829
                Handle(Geom_Curve) line = intersector.Line(i);
830
                result.append(makeGeometryCurvePy(line));
831
            }
832

833
            return Py::new_reference_to(result);
834
        }
835
    }
836
    catch (Standard_Failure& e) {
837

838
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
839
        return nullptr;
840
    }
841

842
    PyErr_SetString(PyExc_TypeError, "intersectSS(): Geometry is not a surface");
843
    return nullptr;
844
}
845

846
// General intersection function
847

848
PyObject* GeometrySurfacePy::intersect(PyObject *args)
849
{
850
    Handle(Geom_Surface) surf = Handle(Geom_Surface)::DownCast(getGeometryPtr()->handle());
851
    try {
852
        if (!surf.IsNull()) {
853
            PyObject *p;
854
            double prec = Precision::Confusion();
855

856
            try {
857
                if (PyArg_ParseTuple(args, "O!|d", &(Part::GeometrySurfacePy::Type), &p, &prec))
858
                    return intersectSS(args);
859
            } catch(...) {};
860
            PyErr_Clear();
861

862
            if (PyArg_ParseTuple(args, "O!|d", &(Part::GeometryCurvePy::Type), &p, &prec)) {
863
                GeometryCurvePy* curve = static_cast<GeometryCurvePy*>(p);
864
                PyObject* t = PyTuple_New(2);
865
                Py_INCREF(this);
866
                PyTuple_SetItem(t, 0, this);
867
                PyTuple_SetItem(t, 1, PyFloat_FromDouble(prec));
868
                return curve->intersectCS(t);
869
            } else {
870
                return nullptr;
871
            }
872
        }
873
    }
874
    catch (Standard_Failure& e) {
875

876
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
877
        return nullptr;
878
    }
879

880
    PyErr_SetString(PyExc_TypeError, "intersect(): Geometry is not a surface");
881
    return nullptr;
882
}
883

884
Py::Object GeometrySurfacePy::getRotation() const
885
{
886
    Handle(Geom_ElementarySurface) s = Handle(Geom_ElementarySurface)::DownCast
887
        (getGeometryPtr()->handle());
888
    if(!s)
889
        return Py::Object();
890
    gp_Trsf trsf;
891
    trsf.SetTransformation(s->Position().Ax2(),gp_Ax3());
892
    auto q = trsf.GetRotation();
893
    return Py::Rotation(Base::Rotation(q.X(),q.Y(),q.Z(),q.W()));
894
}
895

896

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.