FreeCAD

Форк
0
/
BuildPlateSurfacePyImp.cpp 
507 строк · 15.6 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2020 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
#ifndef _PreComp_
25
# include <Standard_Failure.hxx>
26
#endif
27

28
#include "GeomPlate/BuildPlateSurfacePy.h"
29
#include "GeomPlate/BuildPlateSurfacePy.cpp"
30
#include "GeomPlate/CurveConstraintPy.h"
31
#include "GeomPlate/PointConstraintPy.h"
32
#include "Geometry2d.h"
33
#include "GeometrySurfacePy.h"
34

35
#include <Base/PyWrapParseTupleAndKeywords.h>
36

37

38
using namespace Part;
39

40
/*!
41
 * \brief BuildPlateSurfacePy::PyMake
42
 * \code
43
v1=App.Vector(0,0,0)
44
v2=App.Vector(10,0,0)
45
v3=App.Vector(10,10,3)
46
v4=App.Vector(0,10,0)
47
v5=App.Vector(5,5,5)
48

49
l1=Part.LineSegment(v1, v2)
50
l2=Part.LineSegment(v2, v3)
51
l3=Part.LineSegment(v3, v4)
52
l4=Part.LineSegment(v4, v1)
53

54
c1=Part.GeomPlate.CurveConstraint(l1)
55
c2=Part.GeomPlate.CurveConstraint(l2)
56
c3=Part.GeomPlate.CurveConstraint(l3)
57
c4=Part.GeomPlate.CurveConstraint(l4)
58
c5=Part.GeomPlate.PointConstraint(v5)
59

60
bp=Part.GeomPlate.BuildPlateSurface()
61
bp.add(c1)
62
bp.add(c2)
63
bp.add(c3)
64
bp.add(c4)
65
bp.add(c5)
66
bp.perform()
67
s=bp.surface()
68
bs=s.makeApprox()
69
Part.show(bs.toShape())
70
Part.show(l1.toShape())
71
Part.show(l2.toShape())
72
Part.show(l3.toShape())
73
Part.show(l4.toShape())
74

75
bp.surfInit()
76
 * \endcode
77
 */
78
PyObject *BuildPlateSurfacePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
79
{
80
    // create a new instance of BuildPlateSurfacePy
81
    return new BuildPlateSurfacePy(nullptr);
82
}
83

84
// constructor method
85
int BuildPlateSurfacePy::PyInit(PyObject* args, PyObject* kwds)
86
{
87
    PyObject *surf = nullptr;
88
    int degree = 3;
89
    int nbPtsOnCur = 10;
90
    int nbIter = 3;
91
    double tol2d = 0.00001;
92
    double tol3d = 0.0001;
93
    double tolAng = 0.01;
94
    double tolCurv = 0.1;
95
    PyObject* anisotropy = Py_False;
96

97
    static const std::array<const char *, 10> keywords{"Surface", "Degree", "NbPtsOnCur", "NbIter", "Tol2d", "Tol3d",
98
                                                       "TolAng", "TolCurv", "Anisotropy", nullptr};
99
    if (!Base::Wrapped_ParseTupleAndKeywords(args, kwds, "|O!iiiddddO!", keywords,
100
                                             &(GeometrySurfacePy::Type), &surf, &degree,
101
                                             &nbPtsOnCur, &nbIter, &tol2d, &tol3d,
102
                                             &tolAng, &tolCurv, &PyBool_Type, &anisotropy)) {
103
        return -1;
104
    }
105

106
    try {
107
        std::unique_ptr<GeomPlate_BuildPlateSurface> ptr(new GeomPlate_BuildPlateSurface
108
                                                         (degree, nbPtsOnCur, nbIter, tol2d, tol3d, tolAng, tolCurv,
109
                                                          Base::asBoolean(anisotropy)));
110

111
        if (surf) {
112
            GeomSurface* surface = static_cast<GeometrySurfacePy*>(surf)->getGeomSurfacePtr();
113
            Handle(Geom_Surface) handle = Handle(Geom_Surface)::DownCast(surface->handle());
114
            if (handle.IsNull()) {
115
                PyErr_SetString(PyExc_ReferenceError, "No valid surface handle");
116
                return -1;
117
            }
118
            ptr->LoadInitSurface(handle);
119
        }
120

121
        setTwinPointer(ptr.release());
122

123
        return 0;
124
    }
125
    catch (const Standard_Failure& e) {
126
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
127
        return -1;
128
    }
129
}
130

131
// returns a string which represents the object e.g. when printed in python
132
std::string BuildPlateSurfacePy::representation() const
133
{
134
    return {"<GeomPlate_BuildPlateSurface object>"};
135
}
136

137
PyObject* BuildPlateSurfacePy::init(PyObject *args)
138
{
139
    if (!PyArg_ParseTuple(args, ""))
140
        return nullptr;
141

142
    try {
143
        getGeomPlate_BuildPlateSurfacePtr()->Init();
144
        Py_Return;
145
    }
146
    catch (const Standard_Failure& e) {
147
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
148
        return nullptr;
149
    }
150
}
151

152
PyObject* BuildPlateSurfacePy::loadInitSurface(PyObject *args)
153
{
154
    PyObject* surf;
155
    if (!PyArg_ParseTuple(args, "O!", &(GeometrySurfacePy::Type), &surf))
156
        return nullptr;
157

158
    GeomSurface* surface = static_cast<GeometrySurfacePy*>(surf)->getGeomSurfacePtr();
159
    Handle(Geom_Surface) handle = Handle(Geom_Surface)::DownCast(surface->handle());
160
    if (handle.IsNull()) {
161
        PyErr_SetString(PyExc_ReferenceError, "No valid surface handle");
162
        return nullptr;
163
    }
164

165
    try {
166
        getGeomPlate_BuildPlateSurfacePtr()->LoadInitSurface(handle);
167
        Py_Return;
168
    }
169
    catch (const Standard_Failure& e) {
170
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
171
        return nullptr;
172
    }
173
}
174

175
PyObject* BuildPlateSurfacePy::add(PyObject *args)
176
{
177
    PyObject* cont;
178
    if (!PyArg_ParseTuple(args, "O", &cont))
179
        return nullptr;
180

181
    try {
182
        if (PyObject_TypeCheck(cont, &PointConstraintPy::Type)) {
183
            GeomPlate_PointConstraint* pc = static_cast<PointConstraintPy*>(cont)->getGeomPlate_PointConstraintPtr();
184
            getGeomPlate_BuildPlateSurfacePtr()->Add(new GeomPlate_PointConstraint(*pc));
185
            Py_Return;
186
        }
187
        else if (PyObject_TypeCheck(cont, &CurveConstraintPy::Type)) {
188
            GeomPlate_CurveConstraint* cc = static_cast<CurveConstraintPy*>(cont)->getGeomPlate_CurveConstraintPtr();
189
            getGeomPlate_BuildPlateSurfacePtr()->Add(new GeomPlate_CurveConstraint(*cc));
190
            Py_Return;
191
        }
192
        else {
193
            PyErr_SetString(PyExc_TypeError, "PointConstraint or CurveConstraint expected");
194
            return nullptr;
195
        }
196
    }
197
    catch (const Standard_Failure& e) {
198
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
199
        return nullptr;
200
    }
201
}
202

203
PyObject* BuildPlateSurfacePy::setNbBounds(PyObject *args)
204
{
205
    int count;
206
    if (!PyArg_ParseTuple(args, "i", &count))
207
        return nullptr;
208

209
    try {
210
        getGeomPlate_BuildPlateSurfacePtr()->SetNbBounds(count);
211
        Py_Return;
212
    }
213
    catch (const Standard_Failure& e) {
214
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
215
        return nullptr;
216
    }
217
}
218

219
PyObject* BuildPlateSurfacePy::perform(PyObject *args)
220
{
221
    if (!PyArg_ParseTuple(args, ""))
222
        return nullptr;
223

224
    try {
225
        getGeomPlate_BuildPlateSurfacePtr()->Perform();
226
        Py_Return;
227
    }
228
    catch (const Standard_Failure& e) {
229
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
230
        return nullptr;
231
    }
232
}
233

234
PyObject* BuildPlateSurfacePy::isDone(PyObject *args)
235
{
236
    if (!PyArg_ParseTuple(args, ""))
237
        return nullptr;
238

239
    try {
240
        Standard_Boolean ok = getGeomPlate_BuildPlateSurfacePtr()->IsDone();
241
        return Py_BuildValue("O", (ok ? Py_True : Py_False));
242
    }
243
    catch (const Standard_Failure& e) {
244
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
245
        return nullptr;
246
    }
247
}
248

249
PyObject* BuildPlateSurfacePy::surface(PyObject *args)
250
{
251
    if (!PyArg_ParseTuple(args, ""))
252
        return nullptr;
253

254
    try {
255
        Handle(Geom_Surface) hSurf = getGeomPlate_BuildPlateSurfacePtr()->Surface();
256
        if (hSurf.IsNull())
257
            Py_Return;
258

259
        std::unique_ptr<GeomSurface> geo(makeFromSurface(hSurf));
260
        return geo->getPyObject();
261
    }
262
    catch (const Standard_Failure& e) {
263
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
264
        return nullptr;
265
    }
266
}
267

268
PyObject* BuildPlateSurfacePy::surfInit(PyObject *args)
269
{
270
    if (!PyArg_ParseTuple(args, ""))
271
        return nullptr;
272

273
    try {
274
        Handle(Geom_Surface) hSurf = getGeomPlate_BuildPlateSurfacePtr()->SurfInit();
275
        if (hSurf.IsNull())
276
            Py_Return;
277

278
        std::unique_ptr<GeomSurface> geo(makeFromSurface(hSurf));
279
        return geo->getPyObject();
280
    }
281
    catch (const Standard_Failure& e) {
282
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
283
        return nullptr;
284
    }
285
}
286

287
PyObject* BuildPlateSurfacePy::curveConstraint(PyObject *args)
288
{
289
    int index;
290
    if (!PyArg_ParseTuple(args, "i", &index))
291
        return nullptr;
292

293
    try {
294
        Handle(GeomPlate_CurveConstraint) hCC = getGeomPlate_BuildPlateSurfacePtr()->CurveConstraint(index);
295
        if (hCC.IsNull())
296
            Py_Return;
297

298
        std::unique_ptr<GeomPlate_CurveConstraint> ptr(new GeomPlate_CurveConstraint(*hCC));
299
        return new CurveConstraintPy(ptr.release());
300
    }
301
    catch (const Standard_Failure& e) {
302
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
303
        return nullptr;
304
    }
305
}
306

307
PyObject* BuildPlateSurfacePy::pointConstraint(PyObject *args)
308
{
309
    int index;
310
    if (!PyArg_ParseTuple(args, "i", &index))
311
        return nullptr;
312

313
    try {
314
        Handle(GeomPlate_PointConstraint) hPC = getGeomPlate_BuildPlateSurfacePtr()->PointConstraint(index);
315
        if (hPC.IsNull())
316
            Py_Return;
317

318
        std::unique_ptr<GeomPlate_PointConstraint> ptr(new GeomPlate_PointConstraint(*hPC));
319
        return new PointConstraintPy(ptr.release());
320
    }
321
    catch (const Standard_Failure& e) {
322
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
323
        return nullptr;
324
    }
325
}
326

327
PyObject* BuildPlateSurfacePy::disc2dContour(PyObject *args)
328
{
329
    int index;
330
    if (!PyArg_ParseTuple(args, "i", &index))
331
        return nullptr;
332

333
    try {
334
        TColgp_SequenceOfXY seq2d;
335
        getGeomPlate_BuildPlateSurfacePtr()->Disc2dContour(index, seq2d);
336

337
        Py::List list;
338
        for (int i = seq2d.Lower(); i <= seq2d.Upper(); ++i) {
339
            const gp_XY& pnt = seq2d.Value(i);
340
            Py::Tuple coord(2);
341
            coord.setItem(0, Py::Float(pnt.X()));
342
            coord.setItem(1, Py::Float(pnt.Y()));
343
            list.append(coord);
344
        }
345

346
        return Py::new_reference_to(list);
347
    }
348
    catch (const Standard_Failure& e) {
349
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
350
        return nullptr;
351
    }
352
}
353

354
PyObject* BuildPlateSurfacePy::disc3dContour(PyObject *args)
355
{
356
    int index, order;
357
    if (!PyArg_ParseTuple(args, "ii", &index, &order))
358
        return nullptr;
359

360
    try {
361
        TColgp_SequenceOfXYZ seq3d;
362
        getGeomPlate_BuildPlateSurfacePtr()->Disc3dContour(index, order, seq3d);
363

364
        Py::List list;
365
        for (int i = seq3d.Lower(); i <= seq3d.Upper(); ++i) {
366
            const gp_XYZ& pnt = seq3d.Value(i);
367
            Py::Tuple coord(3);
368
            coord.setItem(0, Py::Float(pnt.X()));
369
            coord.setItem(1, Py::Float(pnt.Y()));
370
            coord.setItem(2, Py::Float(pnt.Z()));
371
            list.append(coord);
372
        }
373

374
        return Py::new_reference_to(list);
375
    }
376
    catch (const Standard_Failure& e) {
377
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
378
        return nullptr;
379
    }
380
}
381

382
PyObject* BuildPlateSurfacePy::sense(PyObject *args)
383
{
384
    if (!PyArg_ParseTuple(args, ""))
385
        return nullptr;
386

387
    try {
388
        Handle(TColStd_HArray1OfInteger) hOrder = getGeomPlate_BuildPlateSurfacePtr()->Sense();
389
        Py::List list;
390
        if (!hOrder.IsNull()) {
391
            for (auto i = hOrder->Lower(); i <= hOrder->Upper(); ++i) {
392
                list.append(Py::Long(hOrder->Value(i)));
393
            }
394
        }
395
        return Py::new_reference_to(list);
396
    }
397
    catch (const Standard_Failure& e) {
398
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
399
        return nullptr;
400
    }
401
}
402

403
PyObject* BuildPlateSurfacePy::curves2d(PyObject *args)
404
{
405
    if (!PyArg_ParseTuple(args, ""))
406
        return nullptr;
407

408
    try {
409
        Handle(TColGeom2d_HArray1OfCurve) hCurves = getGeomPlate_BuildPlateSurfacePtr()->Curves2d();
410
        Py::List list;
411
        if (!hCurves.IsNull()) {
412
            for (auto i = hCurves->Lower(); i <= hCurves->Upper(); ++i) {
413
                Handle(Geom2d_Curve) hCurve = hCurves->Value(i);
414
                std::unique_ptr<Geom2dCurve> ptr(makeFromCurve2d(hCurve));
415
                if (ptr)
416
                    list.append(Py::asObject(ptr->getPyObject()));
417
            }
418
        }
419
        return Py::new_reference_to(list);
420
    }
421
    catch (const Standard_Failure& e) {
422
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
423
        return nullptr;
424
    }
425
}
426

427
PyObject* BuildPlateSurfacePy::order(PyObject *args)
428
{
429
    if (!PyArg_ParseTuple(args, ""))
430
        return nullptr;
431

432
    try {
433
        Handle(TColStd_HArray1OfInteger) hOrder = getGeomPlate_BuildPlateSurfacePtr()->Order();
434
        Py::List list;
435
        if (!hOrder.IsNull()) {
436
            for (auto i = hOrder->Lower(); i <= hOrder->Upper(); ++i) {
437
                list.append(Py::Long(hOrder->Value(i)));
438
            }
439
        }
440
        return Py::new_reference_to(list);
441
    }
442
    catch (const Standard_Failure& e) {
443
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
444
        return nullptr;
445
    }
446
}
447

448
PyObject* BuildPlateSurfacePy::G0Error(PyObject *args)
449
{
450
    int index = 0;
451
    if (!PyArg_ParseTuple(args, "|i", &index))
452
        return nullptr;
453

454
    try {
455
        Standard_Real v = index < 1 ? getGeomPlate_BuildPlateSurfacePtr()->G0Error()
456
                                    : getGeomPlate_BuildPlateSurfacePtr()->G0Error(index);
457
        return PyFloat_FromDouble(v);
458
    }
459
    catch (const Standard_Failure& e) {
460
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
461
        return nullptr;
462
    }
463
}
464

465
PyObject* BuildPlateSurfacePy::G1Error(PyObject *args)
466
{
467
    int index = 0;
468
    if (!PyArg_ParseTuple(args, "|i", &index))
469
        return nullptr;
470

471
    try {
472
        Standard_Real v = index < 1 ? getGeomPlate_BuildPlateSurfacePtr()->G1Error()
473
                                    : getGeomPlate_BuildPlateSurfacePtr()->G1Error(index);
474
        return PyFloat_FromDouble(v);
475
    }
476
    catch (const Standard_Failure& e) {
477
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
478
        return nullptr;
479
    }
480
}
481

482
PyObject* BuildPlateSurfacePy::G2Error(PyObject *args)
483
{
484
    int index = 0;
485
    if (!PyArg_ParseTuple(args, "|i", &index))
486
        return nullptr;
487

488
    try {
489
        Standard_Real v = index < 1 ? getGeomPlate_BuildPlateSurfacePtr()->G2Error()
490
                                    : getGeomPlate_BuildPlateSurfacePtr()->G2Error(index);
491
        return PyFloat_FromDouble(v);
492
    }
493
    catch (const Standard_Failure& e) {
494
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
495
        return nullptr;
496
    }
497
}
498

499
PyObject *BuildPlateSurfacePy::getCustomAttributes(const char* /*attr*/) const
500
{
501
    return nullptr;
502
}
503

504
int BuildPlateSurfacePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
505
{
506
    return 0;
507
}
508

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

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

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

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