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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************
 *   Copyright (c) 2020 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 <Mod/Part/PartGlobal.h>

#include <GeomAdaptor_Curve.hxx>
#include <Geom2dAdaptor_Curve.hxx>
#include <Standard_Failure.hxx>
#include <Standard_Version.hxx>
#if OCC_VERSION_HEX < 0x070600
# include <GeomAdaptor_HCurve.hxx>
# include <Geom2dAdaptor_HCurve.hxx>
#endif

#include "GeomPlate/CurveConstraintPy.h"
#include "GeomPlate/CurveConstraintPy.cpp"
#include "Geom2d/Curve2dPy.h"
#include "GeometryCurvePy.h"
#include <Base/PyWrapParseTupleAndKeywords.h>


using namespace Part;

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

// constructor method
int CurveConstraintPy::PyInit(PyObject* args, PyObject* kwds)
{
    PyObject* bound = nullptr;
    int order = 0;
    int nbPts = 10;
    double tolDist = 0.0001;
    double tolAng = 0.01;
    double tolCurv = 0.1;

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

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

    try {
        std::unique_ptr<GeomPlate_CurveConstraint> ptr;
        if (bound) {
            GeomCurve* curve = static_cast<GeometryCurvePy*>(bound)->getGeomCurvePtr();
            Handle(Geom_Curve) handle = Handle(Geom_Curve)::DownCast(curve->handle());
            if (handle.IsNull()) {
                PyErr_SetString(PyExc_ReferenceError, "No valid curve handle");
                return -1;
            }

#if OCC_VERSION_HEX >= 0x070600
            Handle(Adaptor3d_Curve) hCurve;
            if (curve->isDerivedFrom<GeomTrimmedCurve>()) {
                GeomTrimmedCurve* trim = static_cast<GeomTrimmedCurve*>(curve);
                hCurve = new GeomAdaptor_Curve(
                    handle,
                    trim->getFirstParameter(),
                    trim->getLastParameter()
                );
            }
            else {
                hCurve = new GeomAdaptor_Curve(handle);
            }
#else
            Handle(Adaptor3d_HCurve) hCurve;
            if (curve->isDerivedFrom<GeomTrimmedCurve>()) {
                GeomTrimmedCurve* trim = static_cast<GeomTrimmedCurve*>(curve);
                GeomAdaptor_Curve adapt(handle, trim->getFirstParameter(), trim->getLastParameter());
                hCurve = new GeomAdaptor_HCurve(adapt);
            }
            else {
                GeomAdaptor_Curve adapt(handle);
                hCurve = new GeomAdaptor_HCurve(adapt);
            }
#endif

            ptr = std::make_unique<GeomPlate_CurveConstraint>(hCurve, order, nbPts, tolDist, tolAng, tolCurv);
        }
        else {
            ptr = std::make_unique<GeomPlate_CurveConstraint>();
        }

        setTwinPointer(ptr.release());

        return 0;
    }
    catch (const Standard_Failure& e) {
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
        return -1;
    }
}

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

PyObject* CurveConstraintPy::setOrder(PyObject* args)
{
    int order;
    if (!PyArg_ParseTuple(args, "i", &order)) {
        return nullptr;
    }

    try {
        getGeomPlate_CurveConstraintPtr()->SetOrder(order);
        Py_Return;
    }
    catch (const Standard_Failure& e) {
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
        return nullptr;
    }
}

PyObject* CurveConstraintPy::order(PyObject* args)
{
    if (!PyArg_ParseTuple(args, "")) {
        return nullptr;
    }

    try {
        Standard_Integer v = getGeomPlate_CurveConstraintPtr()->Order();
        return PyLong_FromLong(v);
    }
    catch (const Standard_Failure& e) {
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
        return nullptr;
    }
}

PyObject* CurveConstraintPy::G0Criterion(PyObject* args)
{
    double u;
    if (!PyArg_ParseTuple(args, "d", &u)) {
        return nullptr;
    }

    try {
        Standard_Real v = getGeomPlate_CurveConstraintPtr()->G0Criterion(u);
        return PyFloat_FromDouble(v);
    }
    catch (const Standard_Failure& e) {
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
        return nullptr;
    }
}

PyObject* CurveConstraintPy::G1Criterion(PyObject* args)
{
    double u;
    if (!PyArg_ParseTuple(args, "d", &u)) {
        return nullptr;
    }

    try {
        Standard_Real v = getGeomPlate_CurveConstraintPtr()->G1Criterion(u);
        return PyFloat_FromDouble(v);
    }
    catch (const Standard_Failure& e) {
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
        return nullptr;
    }
}

PyObject* CurveConstraintPy::G2Criterion(PyObject* args)
{
    double u;
    if (!PyArg_ParseTuple(args, "d", &u)) {
        return nullptr;
    }

    try {
        Standard_Real v = getGeomPlate_CurveConstraintPtr()->G2Criterion(u);
        return PyFloat_FromDouble(v);
    }
    catch (const Standard_Failure& e) {
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
        return nullptr;
    }
}

PyObject* CurveConstraintPy::setG0Criterion(PyObject*)
{
    PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
    return nullptr;
}

PyObject* CurveConstraintPy::setG1Criterion(PyObject*)
{
    PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
    return nullptr;
}

PyObject* CurveConstraintPy::setG2Criterion(PyObject*)
{
    PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
    return nullptr;
}

PyObject* CurveConstraintPy::curve3d(PyObject* args)
{
    if (!PyArg_ParseTuple(args, "")) {
        return nullptr;
    }

    try {
        auto hAdapt = getGeomPlate_CurveConstraintPtr()->Curve3d();
        if (hAdapt.IsNull()) {
            Py_Return;
        }

#if OCC_VERSION_HEX >= 0x070600
        const Adaptor3d_Curve& a3d = *hAdapt;
#else
        const Adaptor3d_Curve& a3d = hAdapt->Curve();
#endif
        std::unique_ptr<GeomCurve> ptr(Part::makeFromCurveAdaptor(a3d));
        return ptr->getPyObject();
    }
    catch (const Standard_Failure& e) {
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
        return nullptr;
    }
}

PyObject* CurveConstraintPy::setCurve2dOnSurf(PyObject* args)
{
    PyObject* c;
    if (!PyArg_ParseTuple(args, "O!", &Part::Curve2dPy::Type, &c)) {
        return nullptr;
    }

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

        getGeomPlate_CurveConstraintPtr()->SetCurve2dOnSurf(curve2);
        Py_Return;
    }
    catch (const Standard_Failure& e) {
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
        return nullptr;
    }
}

PyObject* CurveConstraintPy::curve2dOnSurf(PyObject* args)
{
    if (!PyArg_ParseTuple(args, "")) {
        return nullptr;
    }

    try {
        Handle(Geom2d_Curve) curve2 = getGeomPlate_CurveConstraintPtr()->Curve2dOnSurf();
        if (curve2.IsNull()) {
            Py_Return;
        }

        std::unique_ptr<Part::Geom2dCurve> ptr(Part::makeFromCurve2d(curve2));
        return ptr->getPyObject();
    }
    catch (const Standard_Failure& e) {
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
        return nullptr;
    }
}

PyObject* CurveConstraintPy::setProjectedCurve(PyObject* args)
{
    PyObject* c;
    double tolU, tolV;
    if (!PyArg_ParseTuple(args, "O!dd", &Part::Curve2dPy::Type, &c, &tolU, &tolV)) {
        return nullptr;
    }

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

#if OCC_VERSION_HEX >= 0x070600
        Handle(Adaptor2d_Curve2d) hCurve;
        if (handle->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
            Handle(Geom2d_TrimmedCurve) aTC(Handle(Geom2d_TrimmedCurve)::DownCast(handle));
            hCurve = new Geom2dAdaptor_Curve(handle, aTC->FirstParameter(), aTC->LastParameter());
        }
        else {
            hCurve = new Geom2dAdaptor_Curve(handle);
        }
#else
        Handle(Adaptor2d_HCurve2d) hCurve;
        if (handle->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
            Handle(Geom2d_TrimmedCurve) aTC(Handle(Geom2d_TrimmedCurve)::DownCast(handle));
            Geom2dAdaptor_Curve adapt(handle, aTC->FirstParameter(), aTC->LastParameter());
            hCurve = new Geom2dAdaptor_HCurve(adapt);
        }
        else {
            Geom2dAdaptor_Curve adapt(handle);
            hCurve = new Geom2dAdaptor_HCurve(adapt);
        }
#endif

        getGeomPlate_CurveConstraintPtr()->SetProjectedCurve(hCurve, tolU, tolV);
        Py_Return;
    }
    catch (const Standard_Failure& e) {
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
        return nullptr;
    }
}

PyObject* CurveConstraintPy::projectedCurve(PyObject* args)
{
    if (!PyArg_ParseTuple(args, "")) {
        return nullptr;
    }

    try {
        auto hAdapt = getGeomPlate_CurveConstraintPtr()->ProjectedCurve();
        if (hAdapt.IsNull()) {
            Py_Return;
        }

#if OCC_VERSION_HEX >= 0x070600
        const Adaptor2d_Curve2d& a2d = *hAdapt;
#else
        const Adaptor2d_Curve2d& a2d = hAdapt->Curve2d();
#endif
        std::unique_ptr<Geom2dCurve> ptr(Part::makeFromCurveAdaptor2d(a2d));
        return ptr->getPyObject();
    }
    catch (const Standard_Failure& e) {
        PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
        return nullptr;
    }
}

Py::Long CurveConstraintPy::getNbPoints() const
{
    try {
        Standard_Integer v = getGeomPlate_CurveConstraintPtr()->NbPoints();
        return Py::Long(v);
    }
    catch (const Standard_Failure& e) {
        throw Py::RuntimeError(e.GetMessageString());
    }
}

void CurveConstraintPy::setNbPoints(Py::Long arg)
{
    try {
        getGeomPlate_CurveConstraintPtr()->SetNbPoints(static_cast<long>(arg));
    }
    catch (const Standard_Failure& e) {
        throw Py::RuntimeError(e.GetMessageString());
    }
}

Py::Float CurveConstraintPy::getFirstParameter() const
{
    try {
        Standard_Real v = getGeomPlate_CurveConstraintPtr()->FirstParameter();
        return Py::Float(v);
    }
    catch (const Standard_Failure& e) {
        throw Py::RuntimeError(e.GetMessageString());
    }
}

Py::Float CurveConstraintPy::getLastParameter() const
{
    try {
        Standard_Real v = getGeomPlate_CurveConstraintPtr()->LastParameter();
        return Py::Float(v);
    }
    catch (const Standard_Failure& e) {
        throw Py::RuntimeError(e.GetMessageString());
    }
}

Py::Float CurveConstraintPy::getLength() const
{
    try {
        Standard_Real v = getGeomPlate_CurveConstraintPtr()->Length();
        return Py::Float(v);
    }
    catch (const Standard_Failure& e) {
        throw Py::RuntimeError(e.GetMessageString());
    }
}

PyObject* CurveConstraintPy::getCustomAttributes(const char* /*attr*/) const
{
    return nullptr;
}

int CurveConstraintPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
{
    return 0;
}