FreeCAD

Форк
0
/
CurveConstraintPyImp.cpp 
421 строка · 13.2 Кб
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 <GeomAdaptor_Curve.hxx>
26
# include <Geom2dAdaptor_Curve.hxx>
27
# include <Standard_Failure.hxx>
28
# include <Standard_Version.hxx>
29
# if OCC_VERSION_HEX < 0x070600
30
# include <GeomAdaptor_HCurve.hxx>
31
# include <Geom2dAdaptor_HCurve.hxx>
32
# endif
33
#endif
34

35
#include "GeomPlate/CurveConstraintPy.h"
36
#include "GeomPlate/CurveConstraintPy.cpp"
37
#include "Geom2d/Curve2dPy.h"
38
#include "GeometryCurvePy.h"
39
#include <Base/PyWrapParseTupleAndKeywords.h>
40

41

42
using namespace Part;
43

44
PyObject *CurveConstraintPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
45
{
46
    // create a new instance of CurveConstraintPy and the Twin object
47
    return new CurveConstraintPy(nullptr);
48
}
49

50
// constructor method
51
int CurveConstraintPy::PyInit(PyObject* args, PyObject* kwds)
52
{
53
    PyObject *bound = nullptr;
54
    int order = 0;
55
    int nbPts = 10;
56
    double tolDist = 0.0001;
57
    double tolAng = 0.01;
58
    double tolCurv = 0.1;
59

60
    // GeomPlate_CurveConstraint has a default constructor but OCCT doesn't check
61
    // if neither a 2d, 3d or curve on surface is set when accessing the functions
62
    // Length(), FirstParameter(), LastParameter(), ...
63
    // Thus, we don't allow to create an empty GeomPlate_CurveConstraint instance
64

65
    static const std::array<const char *, 7> keywords{"Boundary", "Order", "NbPts", "TolDist", "TolAng", "TolCurv",
66
                                                      nullptr};
67
    if (!Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!|iiddd", keywords,
68
                                             &(GeometryCurvePy::Type), &bound, &order,
69
                                             &nbPts, &tolDist, &tolAng, &tolCurv)) {
70
        return -1;
71
    }
72

73
    try {
74
        std::unique_ptr<GeomPlate_CurveConstraint> ptr;
75
        if (bound) {
76
            GeomCurve* curve = static_cast<GeometryCurvePy*>(bound)->getGeomCurvePtr();
77
            Handle(Geom_Curve) handle = Handle(Geom_Curve)::DownCast(curve->handle());
78
            if (handle.IsNull()) {
79
                PyErr_SetString(PyExc_ReferenceError, "No valid curve handle");
80
                return -1;
81
            }
82

83
#if OCC_VERSION_HEX >= 0x070600
84
            Handle(Adaptor3d_Curve) hCurve;
85
            if (curve->isDerivedFrom<GeomTrimmedCurve>()) {
86
                GeomTrimmedCurve* trim = static_cast<GeomTrimmedCurve*>(curve);
87
                hCurve = new GeomAdaptor_Curve(handle, trim->getFirstParameter(), trim->getLastParameter());
88
            }
89
            else {
90
                hCurve = new GeomAdaptor_Curve(handle);
91
            }
92
#else
93
            Handle(Adaptor3d_HCurve) hCurve;
94
            if (curve->isDerivedFrom<GeomTrimmedCurve>()) {
95
                GeomTrimmedCurve* trim = static_cast<GeomTrimmedCurve*>(curve);
96
                GeomAdaptor_Curve adapt(handle, trim->getFirstParameter(), trim->getLastParameter());
97
                hCurve = new GeomAdaptor_HCurve(adapt);
98
            }
99
            else {
100
                GeomAdaptor_Curve adapt(handle);
101
                hCurve = new GeomAdaptor_HCurve(adapt);
102
            }
103
#endif
104

105
            ptr = std::make_unique<GeomPlate_CurveConstraint>(hCurve, order, nbPts, tolDist, tolAng, tolCurv);
106
        }
107
        else {
108
            ptr = std::make_unique<GeomPlate_CurveConstraint>();
109
        }
110

111
        setTwinPointer(ptr.release());
112

113
        return 0;
114
    }
115
    catch (const Standard_Failure& e) {
116
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
117
        return -1;
118
    }
119
}
120

121
// returns a string which represents the object e.g. when printed in python
122
std::string CurveConstraintPy::representation() const
123
{
124
    return {"<GeomPlate_CurveConstraint object>"};
125
}
126

127
PyObject* CurveConstraintPy::setOrder(PyObject *args)
128
{
129
    int order;
130
    if (!PyArg_ParseTuple(args, "i", &order))
131
        return nullptr;
132

133
    try {
134
        getGeomPlate_CurveConstraintPtr()->SetOrder(order);
135
        Py_Return;
136
    }
137
    catch (const Standard_Failure& e) {
138
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
139
        return nullptr;
140
    }
141
}
142

143
PyObject* CurveConstraintPy::order(PyObject *args)
144
{
145
    if (!PyArg_ParseTuple(args, ""))
146
        return nullptr;
147

148
    try {
149
        Standard_Integer v = getGeomPlate_CurveConstraintPtr()->Order();
150
        return PyLong_FromLong(v);
151
    }
152
    catch (const Standard_Failure& e) {
153
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
154
        return nullptr;
155
    }
156
}
157

158
PyObject* CurveConstraintPy::G0Criterion(PyObject *args)
159
{
160
    double u;
161
    if (!PyArg_ParseTuple(args, "d", &u))
162
        return nullptr;
163

164
    try {
165
        Standard_Real v = getGeomPlate_CurveConstraintPtr()->G0Criterion(u);
166
        return PyFloat_FromDouble(v);
167
    }
168
    catch (const Standard_Failure& e) {
169
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
170
        return nullptr;
171
    }
172
}
173

174
PyObject* CurveConstraintPy::G1Criterion(PyObject *args)
175
{
176
    double u;
177
    if (!PyArg_ParseTuple(args, "d", &u))
178
        return nullptr;
179

180
    try {
181
        Standard_Real v = getGeomPlate_CurveConstraintPtr()->G1Criterion(u);
182
        return PyFloat_FromDouble(v);
183
    }
184
    catch (const Standard_Failure& e) {
185
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
186
        return nullptr;
187
    }
188
}
189

190
PyObject* CurveConstraintPy::G2Criterion(PyObject *args)
191
{
192
    double u;
193
    if (!PyArg_ParseTuple(args, "d", &u))
194
        return nullptr;
195

196
    try {
197
        Standard_Real v = getGeomPlate_CurveConstraintPtr()->G2Criterion(u);
198
        return PyFloat_FromDouble(v);
199
    }
200
    catch (const Standard_Failure& e) {
201
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
202
        return nullptr;
203
    }
204
}
205

206
PyObject* CurveConstraintPy::setG0Criterion(PyObject *)
207
{
208
    PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
209
    return nullptr;
210
}
211

212
PyObject* CurveConstraintPy::setG1Criterion(PyObject *)
213
{
214
    PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
215
    return nullptr;
216
}
217

218
PyObject* CurveConstraintPy::setG2Criterion(PyObject *)
219
{
220
    PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
221
    return nullptr;
222
}
223

224
PyObject* CurveConstraintPy::curve3d(PyObject *args)
225
{
226
    if (!PyArg_ParseTuple(args, ""))
227
        return nullptr;
228

229
    try {
230
        auto hAdapt = getGeomPlate_CurveConstraintPtr()->Curve3d();
231
        if (hAdapt.IsNull())
232
            Py_Return;
233

234
#if OCC_VERSION_HEX >= 0x070600
235
        const Adaptor3d_Curve& a3d = *hAdapt;
236
#else
237
        const Adaptor3d_Curve& a3d = hAdapt->Curve();
238
#endif
239
        std::unique_ptr<GeomCurve> ptr(Part::makeFromCurveAdaptor(a3d));
240
        return ptr->getPyObject();
241
    }
242
    catch (const Standard_Failure& e) {
243
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
244
        return nullptr;
245
    }
246
}
247

248
PyObject* CurveConstraintPy::setCurve2dOnSurf(PyObject *args)
249
{
250
    PyObject* c;
251
    if (!PyArg_ParseTuple(args, "O!", &Part::Curve2dPy::Type, &c))
252
        return nullptr;
253

254
    try {
255
        Handle(Geom2d_Curve) curve2 = Handle(Geom2d_Curve)::DownCast(static_cast<Geometry2dPy*>(c)->getGeometry2dPtr()->handle());
256
        if (curve2.IsNull()) {
257
            PyErr_SetString(PyExc_ReferenceError, "No valid curve handle");
258
            return nullptr;
259
        }
260

261
        getGeomPlate_CurveConstraintPtr()->SetCurve2dOnSurf(curve2);
262
        Py_Return;
263
    }
264
    catch (const Standard_Failure& e) {
265
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
266
        return nullptr;
267
    }
268
}
269

270
PyObject* CurveConstraintPy::curve2dOnSurf(PyObject *args)
271
{
272
    if (!PyArg_ParseTuple(args, ""))
273
        return nullptr;
274

275
    try {
276
        Handle(Geom2d_Curve) curve2 = getGeomPlate_CurveConstraintPtr()->Curve2dOnSurf();
277
        if (curve2.IsNull())
278
            Py_Return;
279

280
        std::unique_ptr<Part::Geom2dCurve> ptr(Part::makeFromCurve2d(curve2));
281
        return ptr->getPyObject();
282
    }
283
    catch (const Standard_Failure& e) {
284
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
285
        return nullptr;
286
    }
287
}
288

289
PyObject* CurveConstraintPy::setProjectedCurve(PyObject *args)
290
{
291
    PyObject* c;
292
    double tolU, tolV;
293
    if (!PyArg_ParseTuple(args, "O!dd", &Part::Curve2dPy::Type, &c, &tolU, &tolV))
294
        return nullptr;
295

296
    try {
297
        Geom2dCurve* curve2 = static_cast<Curve2dPy*>(c)->getGeom2dCurvePtr();
298
        Handle(Geom2d_Curve) handle = Handle(Geom2d_Curve)::DownCast(curve2->handle());
299
        if (handle.IsNull()) {
300
            PyErr_SetString(PyExc_ReferenceError, "No valid curve handle");
301
            return nullptr;
302
        }
303

304
#if OCC_VERSION_HEX >= 0x070600
305
        Handle(Adaptor2d_Curve2d) hCurve;
306
        if (handle->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
307
            Handle(Geom2d_TrimmedCurve) aTC (Handle(Geom2d_TrimmedCurve)::DownCast (handle));
308
            hCurve = new Geom2dAdaptor_Curve(handle, aTC->FirstParameter(), aTC->LastParameter());
309
        }
310
        else {
311
            hCurve = new Geom2dAdaptor_Curve(handle);
312
        }
313
#else
314
        Handle(Adaptor2d_HCurve2d) hCurve;
315
        if (handle->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
316
            Handle(Geom2d_TrimmedCurve) aTC (Handle(Geom2d_TrimmedCurve)::DownCast (handle));
317
            Geom2dAdaptor_Curve adapt(handle, aTC->FirstParameter(), aTC->LastParameter());
318
            hCurve = new Geom2dAdaptor_HCurve(adapt);
319
        }
320
        else {
321
            Geom2dAdaptor_Curve adapt(handle);
322
            hCurve = new Geom2dAdaptor_HCurve(adapt);
323
        }
324
#endif
325

326
        getGeomPlate_CurveConstraintPtr()->SetProjectedCurve(hCurve, tolU, tolV);
327
        Py_Return;
328
    }
329
    catch (const Standard_Failure& e) {
330
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
331
        return nullptr;
332
    }
333
}
334

335
PyObject* CurveConstraintPy::projectedCurve(PyObject *args)
336
{
337
    if (!PyArg_ParseTuple(args, ""))
338
        return nullptr;
339

340
    try {
341
        auto hAdapt = getGeomPlate_CurveConstraintPtr()->ProjectedCurve();
342
        if (hAdapt.IsNull())
343
            Py_Return;
344

345
#if OCC_VERSION_HEX >= 0x070600
346
        const Adaptor2d_Curve2d& a2d = *hAdapt;
347
#else
348
        const Adaptor2d_Curve2d& a2d = hAdapt->Curve2d();
349
#endif
350
        std::unique_ptr<Geom2dCurve> ptr(Part::makeFromCurveAdaptor2d(a2d));
351
        return ptr->getPyObject();
352
    }
353
    catch (const Standard_Failure& e) {
354
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
355
        return nullptr;
356
    }
357
}
358

359
Py::Long CurveConstraintPy::getNbPoints() const
360
{
361
    try {
362
        Standard_Integer v = getGeomPlate_CurveConstraintPtr()->NbPoints();
363
        return Py::Long(v);
364
    }
365
    catch (const Standard_Failure& e) {
366
        throw Py::RuntimeError(e.GetMessageString());
367
    }
368
}
369

370
void  CurveConstraintPy::setNbPoints(Py::Long arg)
371
{
372
    try {
373
        getGeomPlate_CurveConstraintPtr()->SetNbPoints(static_cast<long>(arg));
374
    }
375
    catch (const Standard_Failure& e) {
376
        throw Py::RuntimeError(e.GetMessageString());
377
    }
378
}
379

380
Py::Float CurveConstraintPy::getFirstParameter() const
381
{
382
    try {
383
        Standard_Real v = getGeomPlate_CurveConstraintPtr()->FirstParameter();
384
        return Py::Float(v);
385
    }
386
    catch (const Standard_Failure& e) {
387
        throw Py::RuntimeError(e.GetMessageString());
388
    }
389
}
390

391
Py::Float CurveConstraintPy::getLastParameter() const
392
{
393
    try {
394
        Standard_Real v = getGeomPlate_CurveConstraintPtr()->LastParameter();
395
        return Py::Float(v);
396
    }
397
    catch (const Standard_Failure& e) {
398
        throw Py::RuntimeError(e.GetMessageString());
399
    }
400
}
401

402
Py::Float CurveConstraintPy::getLength() const
403
{
404
    try {
405
        Standard_Real v = getGeomPlate_CurveConstraintPtr()->Length();
406
        return Py::Float(v);
407
    }
408
    catch (const Standard_Failure& e) {
409
        throw Py::RuntimeError(e.GetMessageString());
410
    }
411
}
412

413
PyObject *CurveConstraintPy::getCustomAttributes(const char* /*attr*/) const
414
{
415
    return nullptr;
416
}
417

418
int CurveConstraintPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
419
{
420
    return 0;
421
}
422

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

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

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

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