From 27fd215e1c10022f7b120bea9294b1fe3b165511 Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 13 Feb 2017 10:38:09 +0100 Subject: [PATCH] add methods to convert a curve to a B-Spline or NURBS --- src/Mod/Part/App/Geometry.cpp | 17 ++++++++++++- src/Mod/Part/App/Geometry.h | 16 ++++++++++++- src/Mod/Part/App/GeometryCurvePy.xml | 10 +++++++- src/Mod/Part/App/GeometryCurvePyImp.cpp | 32 +++++++++++++++++++++---- 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/Mod/Part/App/Geometry.cpp b/src/Mod/Part/App/Geometry.cpp index 466b7fb48d..2316ca2555 100644 --- a/src/Mod/Part/App/Geometry.cpp +++ b/src/Mod/Part/App/Geometry.cpp @@ -95,7 +95,7 @@ # include # include # include - +# include #endif #include @@ -318,6 +318,21 @@ TopoDS_Shape GeomCurve::toShape() const return mkBuilder.Shape(); } +GeomBSplineCurve* GeomCurve::toBSpline(double first, double last) const +{ + ShapeConstruct_Curve scc; + Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(handle()); + Handle_Geom_BSplineCurve spline = scc.ConvertToBSpline(c, first, last, Precision::Confusion()); + if (spline.IsNull()) + throw Base::RuntimeError("Conversion to B-Spline failed"); + return new GeomBSplineCurve(spline); +} + +GeomBSplineCurve* GeomCurve::toNurbs(double first, double last) const +{ + return toBSpline(first, last); +} + bool GeomCurve::tangent(double u, gp_Dir& dir) const { Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(handle()); diff --git a/src/Mod/Part/App/Geometry.h b/src/Mod/Part/App/Geometry.h index aa0eeee884..dd059bee2f 100644 --- a/src/Mod/Part/App/Geometry.h +++ b/src/Mod/Part/App/Geometry.h @@ -97,7 +97,7 @@ public: virtual Geometry *clone(void) const; virtual TopoDS_Shape toShape() const; - // Persistence implementer --------------------- + // Persistence implementer --------------------- virtual unsigned int getMemSize(void) const; virtual void Save(Base::Writer &/*writer*/) const; virtual void Restore(Base::XMLReader &/*reader*/); @@ -113,6 +113,7 @@ private: Handle_Geom_CartesianPoint myPoint; }; +class GeomBSplineCurve; class PartExport GeomCurve : public Geometry { TYPESYSTEM_HEADER(); @@ -121,6 +122,19 @@ public: virtual ~GeomCurve(); TopoDS_Shape toShape() const; + /*! + * \brief toBSpline Converts the curve to a B-Spline + * \param This is the start parameter of the curve + * \param This is the end parameter of the curve + * \return a B-Spline curve + */ + GeomBSplineCurve* toBSpline(double first, double last) const; + /*! + The default implementation does the same as \ref toBSpline. + In sub-classes this can be reimplemented to create a real + NURBS curve and not just an approximation. + */ + virtual GeomBSplineCurve* toNurbs(double first, double last) const; bool tangent(double u, gp_Dir&) const; Base::Vector3d pointAtParameter(double u) const; Base::Vector3d firstDerivativeAtParameter(double u) const; diff --git a/src/Mod/Part/App/GeometryCurvePy.xml b/src/Mod/Part/App/GeometryCurvePy.xml index c2faf84d6f..6940e53d61 100644 --- a/src/Mod/Part/App/GeometryCurvePy.xml +++ b/src/Mod/Part/App/GeometryCurvePy.xml @@ -140,7 +140,15 @@ of the nearest orthogonal projection of the point. - + + + + Converts a curve of any type (only part from First to Last) + toNurbs([Float=First, Float=Last]) -> NURBS curve + + + + Approximates a curve of any type to a B-Spline curve diff --git a/src/Mod/Part/App/GeometryCurvePyImp.cpp b/src/Mod/Part/App/GeometryCurvePyImp.cpp index fca41133d7..a284e26856 100644 --- a/src/Mod/Part/App/GeometryCurvePyImp.cpp +++ b/src/Mod/Part/App/GeometryCurvePyImp.cpp @@ -587,11 +587,33 @@ PyObject* GeometryCurvePy::toBSpline(PyObject * args) v=c->LastParameter(); if (!PyArg_ParseTuple(args, "|dd", &u,&v)) return 0; - ShapeConstruct_Curve scc; - Handle_Geom_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)); + GeomBSplineCurve* spline = getGeomCurvePtr()->toBSpline(u, v); + return new BSplineCurvePy(spline); + } + } + catch (Standard_Failure) { + Handle_Standard_Failure e = Standard_Failure::Caught(); + PyErr_SetString(PartExceptionOCCError, e->GetMessageString()); + return 0; + } + + PyErr_SetString(PartExceptionOCCError, "Geometry is not a curve"); + return 0; +} + +PyObject* GeometryCurvePy::toNurbs(PyObject * args) +{ + Handle_Geom_Geometry g = getGeometryPtr()->handle(); + Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(g); + try { + if (!c.IsNull()) { + double u,v; + u=c->FirstParameter(); + v=c->LastParameter(); + if (!PyArg_ParseTuple(args, "|dd", &u,&v)) + return 0; + GeomBSplineCurve* spline = getGeomCurvePtr()->toNurbs(u, v); + return new BSplineCurvePy(spline); } } catch (Standard_Failure) {