diff --git a/src/Mod/Part/App/Geom2d/Curve2dPy.xml b/src/Mod/Part/App/Geom2d/Curve2dPy.xml index e8a039d6ea..bedc9cd8d6 100644 --- a/src/Mod/Part/App/Geom2d/Curve2dPy.xml +++ b/src/Mod/Part/App/Geom2d/Curve2dPy.xml @@ -13,7 +13,7 @@ - The abstract class GeometryCurve is the root class of all curve objects. + The abstract class Geom2dCurve is the root class of all curve objects. @@ -26,7 +26,6 @@ Return the shape for the geometry. - Computes the point of parameter u on this curve - diff --git a/src/Mod/Part/App/Geom2d/Curve2dPyImp.cpp b/src/Mod/Part/App/Geom2d/Curve2dPyImp.cpp index 05c4a2b865..be04a062a1 100644 --- a/src/Mod/Part/App/Geom2d/Curve2dPyImp.cpp +++ b/src/Mod/Part/App/Geom2d/Curve2dPyImp.cpp @@ -33,35 +33,28 @@ # include # include # include -# include -# include -# include -# include -# include +# include +# include # include -# include -# include -# include -# include +# include # include -# include -# include +# include +# include # include # include # include -# include # include # include # include #endif #include -#include -#include +#include +#include +#include #include #include -#include #include #include @@ -188,7 +181,6 @@ PyObject* Curve2dPy::toShape(PyObject *args) return 0; } -#if 0 PyObject* Curve2dPy::discretize(PyObject *args, PyObject *kwds) { try { @@ -200,59 +192,57 @@ PyObject* Curve2dPy::discretize(PyObject *args, PyObject *kwds) } Geom2dAdaptor_Curve adapt(c); - bool uniformAbscissaPoints = false; - bool uniformAbscissaDistance = false; - int numPoints = -1; - double distance = -1; double first = adapt.FirstParameter(); double last = adapt.LastParameter(); - // use no kwds - PyObject* dist_or_num; - if (PyArg_ParseTuple(args, "O", &dist_or_num)) { - if (PyInt_Check(dist_or_num)) { - numPoints = PyInt_AsLong(dist_or_num); - uniformAbscissaPoints = true; - } - else if (PyFloat_Check(dist_or_num)) { - distance = PyFloat_AsDouble(dist_or_num); - uniformAbscissaDistance = true; - } - else { - PyErr_SetString(PyExc_TypeError, "Either int or float expected"); - return 0; - } - } - else { - // use Number kwds - static char* kwds_numPoints[] = {"Number","First","Last",NULL}; - PyErr_Clear(); - if (PyArg_ParseTupleAndKeywords(args, kwds, "i|dd", kwds_numPoints, &numPoints, &first, &last)) { - uniformAbscissaPoints = true; - } - else { - // use Abscissa kwds - static char* kwds_Distance[] = {"Distance","First","Last",NULL}; - PyErr_Clear(); - if (PyArg_ParseTupleAndKeywords(args, kwds, "d|dd", kwds_Distance, &distance, &first, &last)) { - uniformAbscissaDistance = true; - } - } - } - - if (uniformAbscissaPoints || uniformAbscissaDistance) { + // use Number kwds + static char* kwds_numPoints[] = {"Number","First","Last",NULL}; + PyErr_Clear(); + int numPoints = -1; + if (PyArg_ParseTupleAndKeywords(args, kwds, "i|dd", kwds_numPoints, &numPoints, &first, &last)) { GCPnts_UniformAbscissa discretizer; - if (uniformAbscissaPoints) - discretizer.Initialize (adapt, numPoints, first, last); - else - discretizer.Initialize (adapt, distance, first, last); + discretizer.Initialize (adapt, numPoints, first, last); if (discretizer.IsDone () && discretizer.NbPoints () > 0) { Py::List points; int nbPoints = discretizer.NbPoints (); for (int i=1; i<=nbPoints; i++) { gp_Pnt2d p = adapt.Value (discretizer.Parameter (i)); - points.append(Py::asObject(new Base::Vector2dPy(p.X(),p.Y()))); + Py::Module module("__FreeCADBase__"); + Py::Callable method(module.getAttr("Vector2d")); + Py::Tuple arg(2); + arg.setItem(0, Py::Float(p.X())); + arg.setItem(1, Py::Float(p.Y())); + points.append(method.apply(arg)); + } + + return Py::new_reference_to(points); + } + else { + PyErr_SetString(PartExceptionOCCError, "Discretization of curve failed"); + return 0; + } + } + + // use Distance kwds + static char* kwds_Distance[] = {"Distance","First","Last",NULL}; + PyErr_Clear(); + double distance = -1; + if (PyArg_ParseTupleAndKeywords(args, kwds, "d|dd", kwds_Distance, &distance, &first, &last)) { + GCPnts_UniformAbscissa discretizer; + discretizer.Initialize (adapt, distance, first, last); + + if (discretizer.IsDone () && discretizer.NbPoints () > 0) { + Py::List points; + int nbPoints = discretizer.NbPoints (); + for (int i=1; i<=nbPoints; i++) { + gp_Pnt2d p = adapt.Value (discretizer.Parameter (i)); + Py::Module module("__FreeCADBase__"); + Py::Callable method(module.getAttr("Vector2d")); + Py::Tuple arg(2); + arg.setItem(0, Py::Float(p.X())); + arg.setItem(1, Py::Float(p.Y())); + points.append(method.apply(arg)); } return Py::new_reference_to(points); @@ -274,7 +264,12 @@ PyObject* Curve2dPy::discretize(PyObject *args, PyObject *kwds) int nbPoints = discretizer.NbPoints (); for (int i=1; i<=nbPoints; i++) { gp_Pnt p = discretizer.Value (i); - points.append(Py::Vector(Base::Vector3d(p.X(),p.Y(),p.Z()))); + Py::Module module("__FreeCADBase__"); + Py::Callable method(module.getAttr("Vector2d")); + Py::Tuple arg(2); + arg.setItem(0, Py::Float(p.X())); + arg.setItem(1, Py::Float(p.Y())); + points.append(method.apply(arg)); } return Py::new_reference_to(points); @@ -298,7 +293,12 @@ PyObject* Curve2dPy::discretize(PyObject *args, PyObject *kwds) int nbPoints = discretizer.NbPoints (); for (int i=1; i<=nbPoints; i++) { gp_Pnt p = discretizer.Value (i); - points.append(Py::Vector(Base::Vector3d(p.X(),p.Y(),p.Z()))); + Py::Module module("__FreeCADBase__"); + Py::Callable method(module.getAttr("Vector2d")); + Py::Tuple arg(2); + arg.setItem(0, Py::Float(p.X())); + arg.setItem(1, Py::Float(p.Y())); + points.append(method.apply(arg)); } return Py::new_reference_to(points); @@ -320,7 +320,12 @@ PyObject* Curve2dPy::discretize(PyObject *args, PyObject *kwds) int nbPoints = discretizer.NbPoints (); for (int i=1; i<=nbPoints; i++) { gp_Pnt2d p = adapt.Value (discretizer.Parameter (i)); - points.append(Py::asObject(new Base::Vector2dPy(p.X(),p.Y()))); + Py::Module module("__FreeCADBase__"); + Py::Callable method(module.getAttr("Vector2d")); + Py::Tuple arg(2); + arg.setItem(0, Py::Float(p.X())); + arg.setItem(1, Py::Float(p.Y())); + points.append(method.apply(arg)); } return Py::new_reference_to(points); @@ -342,7 +347,12 @@ PyObject* Curve2dPy::discretize(PyObject *args, PyObject *kwds) int nbPoints = discretizer.NbPoints (); for (int i=1; i<=nbPoints; i++) { gp_Pnt p = discretizer.Value (i); - points.append(Py::Vector(Base::Vector3d(p.X(),p.Y(),p.Z()))); + Py::Module module("__FreeCADBase__"); + Py::Callable method(module.getAttr("Vector2d")); + Py::Tuple arg(2); + arg.setItem(0, Py::Float(p.X())); + arg.setItem(1, Py::Float(p.Y())); + points.append(method.apply(arg)); } return Py::new_reference_to(points); @@ -365,7 +375,7 @@ PyObject* Curve2dPy::discretize(PyObject *args, PyObject *kwds) PyObject* Curve2dPy::length(PyObject *args) { Handle_Geom2d_Geometry g = getGeometry2dPtr()->handle(); - Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(g); + Handle_Geom2d_Curve c = Handle_Geom2d_Curve::DownCast(g); try { if (!c.IsNull()) { double u=c->FirstParameter(); @@ -373,7 +383,7 @@ PyObject* Curve2dPy::length(PyObject *args) double t=Precision::Confusion(); if (!PyArg_ParseTuple(args, "|ddd", &u,&v,&t)) return 0; - GeomAdaptor_Curve adapt(c); + Geom2dAdaptor_Curve adapt(c); double len = GCPnts_AbscissaPoint::Length(adapt,u,v,t); return PyFloat_FromDouble(len); } @@ -391,14 +401,14 @@ PyObject* Curve2dPy::length(PyObject *args) PyObject* Curve2dPy::parameterAtDistance(PyObject *args) { Handle_Geom2d_Geometry g = getGeometry2dPtr()->handle(); - Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(g); + Handle_Geom2d_Curve c = Handle_Geom2d_Curve::DownCast(g); try { if (!c.IsNull()) { double abscissa; double u = 0; if (!PyArg_ParseTuple(args, "d|d", &abscissa,&u)) return 0; - GeomAdaptor_Curve adapt(c); + Geom2dAdaptor_Curve adapt(c); GCPnts_AbscissaPoint abscissaPoint(adapt,abscissa,u); double parm = abscissaPoint.Parameter(); return PyFloat_FromDouble(parm); @@ -413,7 +423,7 @@ PyObject* Curve2dPy::parameterAtDistance(PyObject *args) PyErr_SetString(PartExceptionOCCError, "Geometry is not a curve"); return 0; } -#endif + PyObject* Curve2dPy::value(PyObject *args) { Handle_Geom2d_Geometry g = getGeometry2dPtr()->handle(); @@ -442,25 +452,28 @@ PyObject* Curve2dPy::value(PyObject *args) PyErr_SetString(PartExceptionOCCError, "Geometry is not a curve"); return 0; } -#if 0 + PyObject* Curve2dPy::tangent(PyObject *args) { Handle_Geom2d_Geometry g = getGeometry2dPtr()->handle(); - Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(g); + Handle_Geom2d_Curve c = Handle_Geom2d_Curve::DownCast(g); try { if (!c.IsNull()) { double u; if (!PyArg_ParseTuple(args, "d", &u)) return 0; - gp_Dir dir; - Py::Tuple tuple(1); - GeomLProp_CLProps prop(c,u,2,Precision::Confusion()); + gp_Dir2d dir; + Geom2dLProp_CLProps2d prop(c,u,2,Precision::Confusion()); if (prop.IsTangentDefined()) { prop.Tangent(dir); - tuple.setItem(0, Py::Vector(Base::Vector3d(dir.X(),dir.Y(),dir.Z()))); } - return Py::new_reference_to(tuple); + Py::Module module("__FreeCADBase__"); + Py::Callable method(module.getAttr("Vector2d")); + Py::Tuple arg(2); + arg.setItem(0, Py::Float(dir.X())); + arg.setItem(1, Py::Float(dir.Y())); + return Py::new_reference_to(method.apply(arg)); } } catch (Standard_Failure) { @@ -476,16 +489,22 @@ PyObject* Curve2dPy::tangent(PyObject *args) PyObject* Curve2dPy::normal(PyObject *args) { Handle_Geom2d_Geometry g = getGeometry2dPtr()->handle(); - Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(g); + Handle_Geom2d_Curve c = Handle_Geom2d_Curve::DownCast(g); try { if (!c.IsNull()) { double u; if (!PyArg_ParseTuple(args, "d", &u)) return 0; - gp_Dir dir; - GeomLProp_CLProps prop(c,u,2,Precision::Confusion()); + gp_Dir2d dir; + Geom2dLProp_CLProps2d prop(c,u,2,Precision::Confusion()); prop.Normal(dir); - return new Base::VectorPy(new Base::Vector3d(dir.X(),dir.Y(),dir.Z())); + + Py::Module module("__FreeCADBase__"); + Py::Callable method(module.getAttr("Vector2d")); + Py::Tuple arg(2); + arg.setItem(0, Py::Float(dir.X())); + arg.setItem(1, Py::Float(dir.Y())); + return Py::new_reference_to(method.apply(arg)); } } catch (Standard_Failure) { @@ -501,13 +520,13 @@ PyObject* Curve2dPy::normal(PyObject *args) PyObject* Curve2dPy::curvature(PyObject *args) { Handle_Geom2d_Geometry g = getGeometry2dPtr()->handle(); - Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(g); + Handle_Geom2d_Curve c = Handle_Geom2d_Curve::DownCast(g); try { if (!c.IsNull()) { double u; if (!PyArg_ParseTuple(args, "d", &u)) return 0; - GeomLProp_CLProps prop(c,u,2,Precision::Confusion()); + Geom2dLProp_CLProps2d prop(c,u,2,Precision::Confusion()); double C = prop.Curvature(); return Py::new_reference_to(Py::Float(C)); } @@ -525,16 +544,22 @@ PyObject* Curve2dPy::curvature(PyObject *args) PyObject* Curve2dPy::centerOfCurvature(PyObject *args) { Handle_Geom2d_Geometry g = getGeometry2dPtr()->handle(); - Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(g); + Handle_Geom2d_Curve c = Handle_Geom2d_Curve::DownCast(g); try { if (!c.IsNull()) { double u; if (!PyArg_ParseTuple(args, "d", &u)) return 0; - GeomLProp_CLProps prop(c,u,2,Precision::Confusion()); - gp_Pnt V ; - prop.CentreOfCurvature(V); - return new Base::VectorPy(new Base::Vector3d(V.X(),V.Y(),V.Z())); + Geom2dLProp_CLProps2d prop(c,u,2,Precision::Confusion()); + gp_Pnt2d pnt ; + prop.CentreOfCurvature(pnt); + + Py::Module module("__FreeCADBase__"); + Py::Callable method(module.getAttr("Vector2d")); + Py::Tuple arg(2); + arg.setItem(0, Py::Float(pnt.X())); + arg.setItem(1, Py::Float(pnt.Y())); + return Py::new_reference_to(method.apply(arg)); } } catch (Standard_Failure) { @@ -550,15 +575,15 @@ PyObject* Curve2dPy::centerOfCurvature(PyObject *args) PyObject* Curve2dPy::parameter(PyObject *args) { Handle_Geom2d_Geometry g = getGeometry2dPtr()->handle(); - Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(g); + Handle_Geom2d_Curve c = Handle_Geom2d_Curve::DownCast(g); try { if (!c.IsNull()) { PyObject *p; - if (!PyArg_ParseTuple(args, "O!", &(Base::VectorPy::Type), &p)) + if (!PyArg_ParseTuple(args, "O!", Base::Vector2dPy::type_object(), &p)) return 0; - Base::Vector3d v = Py::Vector(p, false).toVector(); - gp_Pnt pnt(v.x,v.y,v.z); - GeomAPI_ProjectPointOnCurve ppc(pnt, c); + Base::Vector2d v = Py::Vector2d(p).getCxxObject()->value(); + gp_Pnt2d pnt(v.x,v.y); + Geom2dAPI_ProjectPointOnCurve ppc(pnt, c); double val = ppc.LowerDistanceParameter(); return Py::new_reference_to(Py::Float(val)); } @@ -576,7 +601,7 @@ PyObject* Curve2dPy::parameter(PyObject *args) PyObject* Curve2dPy::toBSpline(PyObject * args) { Handle_Geom2d_Geometry g = getGeometry2dPtr()->handle(); - Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(g); + Handle_Geom2d_Curve c = Handle_Geom2d_Curve::DownCast(g); try { if (!c.IsNull()) { double u,v; @@ -585,10 +610,10 @@ PyObject* Curve2dPy::toBSpline(PyObject * args) if (!PyArg_ParseTuple(args, "|dd", &u,&v)) return 0; ShapeConstruct_Curve scc; - Handle_Geom_BSplineCurve spline = scc.ConvertToBSpline(c, u, v, Precision::Confusion()); + Handle_Geom2d_BSplineCurve spline = scc.ConvertToBSpline(c, u, v, Precision::Confusion()); if (spline.IsNull()) Standard_NullValue::Raise("Conversion to B-Spline failed"); - return new BSplineCurvePy(new GeomBSplineCurve(spline)); + return new BSplineCurve2dPy(new Geom2dBSplineCurve(spline)); } } catch (Standard_Failure) { @@ -629,10 +654,10 @@ PyObject* Curve2dPy::approximateBSpline(PyObject *args) absShape = GeomAbs_C2; try { - Handle_Geom_Curve self = Handle_Geom_Curve::DownCast(getGeometry2dPtr()->handle()); - GeomConvert_ApproxCurve approx(self, tolerance, absShape, maxSegment, maxDegree); + Handle_Geom2d_Curve self = Handle_Geom2d_Curve::DownCast(getGeometry2dPtr()->handle()); + Geom2dConvert_ApproxCurve approx(self, tolerance, absShape, maxSegment, maxDegree); if (approx.IsDone()) { - return new BSplineCurvePy(new GeomBSplineCurve(approx.Curve())); + return new BSplineCurve2dPy(new Geom2dBSplineCurve(approx.Curve())); } else if (approx.HasResult()) { std::stringstream str; @@ -651,7 +676,7 @@ PyObject* Curve2dPy::approximateBSpline(PyObject *args) return 0; } } -#endif + Py::String Curve2dPy::getContinuity(void) const { GeomAbs_Shape c = Handle_Geom2d_Curve::DownCast @@ -719,7 +744,7 @@ int Curve2dPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) { return 0; } -#if 0 + PyObject* Curve2dPy::intersectCC(PyObject *args) { Handle_Geom2d_Curve curve1 = Handle_Geom2d_Curve::DownCast(getGeometry2dPtr()->handle()); @@ -727,7 +752,7 @@ PyObject* Curve2dPy::intersectCC(PyObject *args) if (!curve1.IsNull()) { PyObject *p; double prec = Precision::Confusion(); - if (!PyArg_ParseTuple(args, "O!|d", &(Part::GeometrySurfacePy::Type), &p, &prec)) + if (!PyArg_ParseTuple(args, "O!|d", &(Part::Curve2dPy::Type), &p, &prec)) return 0; Handle_Geom2d_Curve curve2 = Handle_Geom2d_Curve::DownCast(static_cast(p)->getGeometry2dPtr()->handle()); Geom2dAPI_ExtremaCurveCurve intersector(curve1, curve2, @@ -746,7 +771,13 @@ PyObject* Curve2dPy::intersectCC(PyObject *args) continue; gp_Pnt2d p1, p2; intersector.Points(i, p1, p2); - points.append(Py::asObject(new Base::Vector2dPy(p1.X(), p1.Y()))); + + Py::Module module("__FreeCADBase__"); + Py::Callable method(module.getAttr("Vector2d")); + Py::Tuple arg(2); + arg.setItem(0, Py::Float(p1.X())); + arg.setItem(1, Py::Float(p1.Y())); + points.append(method.apply(arg)); } return Py::new_reference_to(points); @@ -761,27 +792,3 @@ PyObject* Curve2dPy::intersectCC(PyObject *args) PyErr_SetString(PyExc_Exception, "Geometry is not a curve"); return 0; } - -// General intersection function - -PyObject* Curve2dPy::intersect(PyObject *args) -{ - Handle_Geom_Curve curve = Handle_Geom_Curve::DownCast(getGeometry2dPtr()->handle()); - try { - if (!curve.IsNull()) { - PyObject *p; - double prec = Precision::Confusion(); - if (PyArg_ParseTuple(args, "O!|d", &(Part::GeometryCurvePy::Type), &p, &prec)) - return intersectCC(args); - } - } - catch (Standard_Failure) { - Handle_Standard_Failure e = Standard_Failure::Caught(); - PyErr_SetString(PyExc_Exception, e->GetMessageString()); - return 0; - } - - PyErr_SetString(PyExc_Exception, "Geometry is not a curve"); - return 0; -} -#endif