1
/***************************************************************************
2
* Copyright (c) 2020 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
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>
35
#include "GeomPlate/CurveConstraintPy.h"
36
#include "GeomPlate/CurveConstraintPy.cpp"
37
#include "Geom2d/Curve2dPy.h"
38
#include "GeometryCurvePy.h"
39
#include <Base/PyWrapParseTupleAndKeywords.h>
44
PyObject *CurveConstraintPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
46
// create a new instance of CurveConstraintPy and the Twin object
47
return new CurveConstraintPy(nullptr);
51
int CurveConstraintPy::PyInit(PyObject* args, PyObject* kwds)
53
PyObject *bound = nullptr;
56
double tolDist = 0.0001;
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
65
static const std::array<const char *, 7> keywords{"Boundary", "Order", "NbPts", "TolDist", "TolAng", "TolCurv",
67
if (!Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!|iiddd", keywords,
68
&(GeometryCurvePy::Type), &bound, &order,
69
&nbPts, &tolDist, &tolAng, &tolCurv)) {
74
std::unique_ptr<GeomPlate_CurveConstraint> ptr;
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");
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());
90
hCurve = new GeomAdaptor_Curve(handle);
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);
100
GeomAdaptor_Curve adapt(handle);
101
hCurve = new GeomAdaptor_HCurve(adapt);
105
ptr = std::make_unique<GeomPlate_CurveConstraint>(hCurve, order, nbPts, tolDist, tolAng, tolCurv);
108
ptr = std::make_unique<GeomPlate_CurveConstraint>();
111
setTwinPointer(ptr.release());
115
catch (const Standard_Failure& e) {
116
PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
121
// returns a string which represents the object e.g. when printed in python
122
std::string CurveConstraintPy::representation() const
124
return {"<GeomPlate_CurveConstraint object>"};
127
PyObject* CurveConstraintPy::setOrder(PyObject *args)
130
if (!PyArg_ParseTuple(args, "i", &order))
134
getGeomPlate_CurveConstraintPtr()->SetOrder(order);
137
catch (const Standard_Failure& e) {
138
PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
143
PyObject* CurveConstraintPy::order(PyObject *args)
145
if (!PyArg_ParseTuple(args, ""))
149
Standard_Integer v = getGeomPlate_CurveConstraintPtr()->Order();
150
return PyLong_FromLong(v);
152
catch (const Standard_Failure& e) {
153
PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
158
PyObject* CurveConstraintPy::G0Criterion(PyObject *args)
161
if (!PyArg_ParseTuple(args, "d", &u))
165
Standard_Real v = getGeomPlate_CurveConstraintPtr()->G0Criterion(u);
166
return PyFloat_FromDouble(v);
168
catch (const Standard_Failure& e) {
169
PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
174
PyObject* CurveConstraintPy::G1Criterion(PyObject *args)
177
if (!PyArg_ParseTuple(args, "d", &u))
181
Standard_Real v = getGeomPlate_CurveConstraintPtr()->G1Criterion(u);
182
return PyFloat_FromDouble(v);
184
catch (const Standard_Failure& e) {
185
PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
190
PyObject* CurveConstraintPy::G2Criterion(PyObject *args)
193
if (!PyArg_ParseTuple(args, "d", &u))
197
Standard_Real v = getGeomPlate_CurveConstraintPtr()->G2Criterion(u);
198
return PyFloat_FromDouble(v);
200
catch (const Standard_Failure& e) {
201
PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
206
PyObject* CurveConstraintPy::setG0Criterion(PyObject *)
208
PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
212
PyObject* CurveConstraintPy::setG1Criterion(PyObject *)
214
PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
218
PyObject* CurveConstraintPy::setG2Criterion(PyObject *)
220
PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
224
PyObject* CurveConstraintPy::curve3d(PyObject *args)
226
if (!PyArg_ParseTuple(args, ""))
230
auto hAdapt = getGeomPlate_CurveConstraintPtr()->Curve3d();
234
#if OCC_VERSION_HEX >= 0x070600
235
const Adaptor3d_Curve& a3d = *hAdapt;
237
const Adaptor3d_Curve& a3d = hAdapt->Curve();
239
std::unique_ptr<GeomCurve> ptr(Part::makeFromCurveAdaptor(a3d));
240
return ptr->getPyObject();
242
catch (const Standard_Failure& e) {
243
PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
248
PyObject* CurveConstraintPy::setCurve2dOnSurf(PyObject *args)
251
if (!PyArg_ParseTuple(args, "O!", &Part::Curve2dPy::Type, &c))
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");
261
getGeomPlate_CurveConstraintPtr()->SetCurve2dOnSurf(curve2);
264
catch (const Standard_Failure& e) {
265
PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
270
PyObject* CurveConstraintPy::curve2dOnSurf(PyObject *args)
272
if (!PyArg_ParseTuple(args, ""))
276
Handle(Geom2d_Curve) curve2 = getGeomPlate_CurveConstraintPtr()->Curve2dOnSurf();
280
std::unique_ptr<Part::Geom2dCurve> ptr(Part::makeFromCurve2d(curve2));
281
return ptr->getPyObject();
283
catch (const Standard_Failure& e) {
284
PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
289
PyObject* CurveConstraintPy::setProjectedCurve(PyObject *args)
293
if (!PyArg_ParseTuple(args, "O!dd", &Part::Curve2dPy::Type, &c, &tolU, &tolV))
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");
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());
311
hCurve = new Geom2dAdaptor_Curve(handle);
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);
321
Geom2dAdaptor_Curve adapt(handle);
322
hCurve = new Geom2dAdaptor_HCurve(adapt);
326
getGeomPlate_CurveConstraintPtr()->SetProjectedCurve(hCurve, tolU, tolV);
329
catch (const Standard_Failure& e) {
330
PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
335
PyObject* CurveConstraintPy::projectedCurve(PyObject *args)
337
if (!PyArg_ParseTuple(args, ""))
341
auto hAdapt = getGeomPlate_CurveConstraintPtr()->ProjectedCurve();
345
#if OCC_VERSION_HEX >= 0x070600
346
const Adaptor2d_Curve2d& a2d = *hAdapt;
348
const Adaptor2d_Curve2d& a2d = hAdapt->Curve2d();
350
std::unique_ptr<Geom2dCurve> ptr(Part::makeFromCurveAdaptor2d(a2d));
351
return ptr->getPyObject();
353
catch (const Standard_Failure& e) {
354
PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
359
Py::Long CurveConstraintPy::getNbPoints() const
362
Standard_Integer v = getGeomPlate_CurveConstraintPtr()->NbPoints();
365
catch (const Standard_Failure& e) {
366
throw Py::RuntimeError(e.GetMessageString());
370
void CurveConstraintPy::setNbPoints(Py::Long arg)
373
getGeomPlate_CurveConstraintPtr()->SetNbPoints(static_cast<long>(arg));
375
catch (const Standard_Failure& e) {
376
throw Py::RuntimeError(e.GetMessageString());
380
Py::Float CurveConstraintPy::getFirstParameter() const
383
Standard_Real v = getGeomPlate_CurveConstraintPtr()->FirstParameter();
386
catch (const Standard_Failure& e) {
387
throw Py::RuntimeError(e.GetMessageString());
391
Py::Float CurveConstraintPy::getLastParameter() const
394
Standard_Real v = getGeomPlate_CurveConstraintPtr()->LastParameter();
397
catch (const Standard_Failure& e) {
398
throw Py::RuntimeError(e.GetMessageString());
402
Py::Float CurveConstraintPy::getLength() const
405
Standard_Real v = getGeomPlate_CurveConstraintPtr()->Length();
408
catch (const Standard_Failure& e) {
409
throw Py::RuntimeError(e.GetMessageString());
413
PyObject *CurveConstraintPy::getCustomAttributes(const char* /*attr*/) const
418
int CurveConstraintPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)