diff --git a/src/Mod/Part/App/GeometryCurvePy.xml b/src/Mod/Part/App/GeometryCurvePy.xml index eaae3fb190..78090909c6 100644 --- a/src/Mod/Part/App/GeometryCurvePy.xml +++ b/src/Mod/Part/App/GeometryCurvePy.xml @@ -23,7 +23,7 @@ - Discretizes the curve using a given deflection and returns a list of points + Discretizes the curve using a given deflection or number of points and returns a list of points diff --git a/src/Mod/Part/App/GeometryCurvePyImp.cpp b/src/Mod/Part/App/GeometryCurvePyImp.cpp index 2c9deb4e69..01f91500a1 100644 --- a/src/Mod/Part/App/GeometryCurvePyImp.cpp +++ b/src/Mod/Part/App/GeometryCurvePyImp.cpp @@ -108,22 +108,33 @@ PyObject* GeometryCurvePy::toShape(PyObject *args) PyObject* GeometryCurvePy::discretize(PyObject *args) { - double d; - if (!PyArg_ParseTuple(args, "d", &d)) + PyObject* defl_or_num; + if (!PyArg_ParseTuple(args, "O", &defl_or_num)) return 0; - Handle_Geom_Geometry g = getGeometryPtr()->handle(); - Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(g); try { + Handle_Geom_Geometry g = getGeometryPtr()->handle(); + Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(g); if (!c.IsNull()) { - GeomAdaptor_Curve curve_adaptator(c); + GeomAdaptor_Curve adapt(c); GCPnts_UniformAbscissa discretizer; - discretizer.Initialize (curve_adaptator, d); + if (PyInt_Check(defl_or_num)) { + int num = PyInt_AsLong(defl_or_num); + discretizer.Initialize (adapt, num); + } + else if (PyFloat_Check(defl_or_num)) { + double defl = PyFloat_AsDouble(defl_or_num); + discretizer.Initialize (adapt, defl); + } + else { + PyErr_SetString(PyExc_TypeError, "Either int or float expected"); + return 0; + } if (discretizer.IsDone () && discretizer.NbPoints () > 0) { Py::List points; int nbPoints = discretizer.NbPoints (); for (int i=1; i<=nbPoints; i++) { - gp_Pnt p = curve_adaptator.Value (discretizer.Parameter (i)); + gp_Pnt p = adapt.Value (discretizer.Parameter (i)); points.append(Py::Vector(Base::Vector3d(p.X(),p.Y(),p.Z()))); } diff --git a/src/Mod/Part/App/TopoShapeEdgePy.xml b/src/Mod/Part/App/TopoShapeEdgePy.xml index 7e01e5a422..873b3fb726 100644 --- a/src/Mod/Part/App/TopoShapeEdgePy.xml +++ b/src/Mod/Part/App/TopoShapeEdgePy.xml @@ -59,6 +59,11 @@ Set the tolerance for the edge. + + + Discretizes the edge using a given deflection or number of points and returns a list of points + + Set or get the tolerance of the vertex diff --git a/src/Mod/Part/App/TopoShapeEdgePyImp.cpp b/src/Mod/Part/App/TopoShapeEdgePyImp.cpp index d33159b6fc..ec5bc09054 100644 --- a/src/Mod/Part/App/TopoShapeEdgePyImp.cpp +++ b/src/Mod/Part/App/TopoShapeEdgePyImp.cpp @@ -53,6 +53,7 @@ #include #include #include +#include #include #include @@ -396,6 +397,52 @@ PyObject* TopoShapeEdgePy::derivative3At(PyObject *args) } } +PyObject* TopoShapeEdgePy::discretize(PyObject *args) +{ + PyObject* defl_or_num; + if (!PyArg_ParseTuple(args, "O", &defl_or_num)) + return 0; + + try { + BRepAdaptor_Curve adapt(TopoDS::Edge(getTopoShapePtr()->_Shape)); + GCPnts_UniformAbscissa discretizer; + if (PyInt_Check(defl_or_num)) { + int num = PyInt_AsLong(defl_or_num); + discretizer.Initialize (adapt, num); + } + else if (PyFloat_Check(defl_or_num)) { + double defl = PyFloat_AsDouble(defl_or_num); + discretizer.Initialize (adapt, defl); + } + else { + PyErr_SetString(PyExc_TypeError, "Either int or float expected"); + return 0; + } + if (discretizer.IsDone () && discretizer.NbPoints () > 0) { + Py::List points; + int nbPoints = discretizer.NbPoints (); + for (int i=1; i<=nbPoints; i++) { + gp_Pnt p = adapt.Value (discretizer.Parameter (i)); + points.append(Py::Vector(Base::Vector3d(p.X(),p.Y(),p.Z()))); + } + + return Py::new_reference_to(points); + } + else { + PyErr_SetString(PyExc_Exception, "Descretization of curve failed"); + return 0; + } + } + 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; +} + PyObject* TopoShapeEdgePy::setTolerance(PyObject *args) { double tol; diff --git a/src/Mod/Part/App/TopoShapeWirePy.xml b/src/Mod/Part/App/TopoShapeWirePy.xml index d1558e9a36..057bfc9c99 100644 --- a/src/Mod/Part/App/TopoShapeWirePy.xml +++ b/src/Mod/Part/App/TopoShapeWirePy.xml @@ -45,13 +45,18 @@ - - Approximate B-Spline-curve from this wire - - - - Returns the center of mass of the current system. + Approximate B-Spline-curve from this wire + + + + + Discretizes the wire using a given deflection or number of points and returns a list of points + + + + + Returns the center of mass of the current system. If the gravitational field is uniform, it is the center of gravity. The coordinates returned for the center of mass are expressed in the absolute Cartesian coordinate system. diff --git a/src/Mod/Part/App/TopoShapeWirePyImp.cpp b/src/Mod/Part/App/TopoShapeWirePyImp.cpp index 895b17dff6..0011e21984 100644 --- a/src/Mod/Part/App/TopoShapeWirePyImp.cpp +++ b/src/Mod/Part/App/TopoShapeWirePyImp.cpp @@ -37,6 +37,7 @@ #include #include +#include #include #include @@ -320,6 +321,52 @@ PyObject* TopoShapeWirePy::approximate(PyObject *args) } } +PyObject* TopoShapeWirePy::discretize(PyObject *args) +{ + PyObject* defl_or_num; + if (!PyArg_ParseTuple(args, "O", &defl_or_num)) + return 0; + + try { + BRepAdaptor_CompCurve adapt(TopoDS::Wire(getTopoShapePtr()->_Shape)); + GCPnts_UniformAbscissa discretizer; + if (PyInt_Check(defl_or_num)) { + int num = PyInt_AsLong(defl_or_num); + discretizer.Initialize (adapt, num); + } + else if (PyFloat_Check(defl_or_num)) { + double defl = PyFloat_AsDouble(defl_or_num); + discretizer.Initialize (adapt, defl); + } + else { + PyErr_SetString(PyExc_TypeError, "Either int or float expected"); + return 0; + } + if (discretizer.IsDone () && discretizer.NbPoints () > 0) { + Py::List points; + int nbPoints = discretizer.NbPoints (); + for (int i=1; i<=nbPoints; i++) { + gp_Pnt p = adapt.Value (discretizer.Parameter (i)); + points.append(Py::Vector(Base::Vector3d(p.X(),p.Y(),p.Z()))); + } + + return Py::new_reference_to(points); + } + else { + PyErr_SetString(PyExc_Exception, "Descretization of wire failed"); + return 0; + } + } + 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; +} + Py::Object TopoShapeWirePy::getCenterOfMass(void) const { GProp_GProps props;