From ff4d613acb7db2524e5dc0b46572245eaf613e1d Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 26 Jul 2017 22:59:39 +0200 Subject: [PATCH] + add method trim() to Part.Curve --- src/Mod/Part/App/GeometryCurvePy.xml | 8 ++ src/Mod/Part/App/GeometryCurvePyImp.cpp | 25 +++++ src/Mod/Part/App/GeometrySurfacePyImp.cpp | 109 ++++++++++++++++++++++ 3 files changed, 142 insertions(+) diff --git a/src/Mod/Part/App/GeometryCurvePy.xml b/src/Mod/Part/App/GeometryCurvePy.xml index 6940e53d61..2689c5d765 100644 --- a/src/Mod/Part/App/GeometryCurvePy.xml +++ b/src/Mod/Part/App/GeometryCurvePy.xml @@ -148,6 +148,14 @@ of the nearest orthogonal projection of the point. + + + + Returns a trimmed curve defined in the given parameter range + trim([Float=First, Float=Last]) -> trimmed curve + + + diff --git a/src/Mod/Part/App/GeometryCurvePyImp.cpp b/src/Mod/Part/App/GeometryCurvePyImp.cpp index 0f6fd8a0df..21a728109d 100644 --- a/src/Mod/Part/App/GeometryCurvePyImp.cpp +++ b/src/Mod/Part/App/GeometryCurvePyImp.cpp @@ -74,6 +74,7 @@ namespace Part { extern const Py::Object makeGeometryCurvePy(const Handle(Geom_Curve)& c); +extern const Py::Object makeTrimmedCurvePy(const Handle(Geom_Curve)& c, double f,double l); } using namespace Part; @@ -633,6 +634,30 @@ PyObject* GeometryCurvePy::toNurbs(PyObject * args) return 0; } +PyObject* GeometryCurvePy::trim(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; + return Py::new_reference_to(makeTrimmedCurvePy(c,u,v)); + } + } + 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::approximateBSpline(PyObject *args) { double tolerance; diff --git a/src/Mod/Part/App/GeometrySurfacePyImp.cpp b/src/Mod/Part/App/GeometrySurfacePyImp.cpp index 0e009a8613..33260002e8 100644 --- a/src/Mod/Part/App/GeometrySurfacePyImp.cpp +++ b/src/Mod/Part/App/GeometrySurfacePyImp.cpp @@ -49,12 +49,17 @@ #include #include +#include #include #include #include +#include #include +#include #include +#include #include +#include #include #include @@ -110,6 +115,110 @@ const Py::Object makeGeometryCurvePy(const Handle(Geom_Curve)& c) throw Py::TypeError(err); } +const Py::Object makeTrimmedCurvePy(const Handle(Geom_Curve)& c, double f,double l) +{ + if (c->IsKind(STANDARD_TYPE(Geom_Circle))) { + Handle(Geom_Circle) circ = Handle(Geom_Circle)::DownCast(c); + GeomArcOfCircle* arc = new GeomArcOfCircle(); + Handle(Geom_TrimmedCurve) this_arc = Handle(Geom_TrimmedCurve)::DownCast + (arc->handle()); + Handle(Geom_Circle) this_circ = Handle(Geom_Circle)::DownCast + (this_arc->BasisCurve()); + this_circ->SetCirc(circ->Circ()); + this_arc->SetTrim(f, l); + return Py::Object(new ArcOfCirclePy(arc),true); + + } + else if (c->IsKind(STANDARD_TYPE(Geom_Ellipse))) { + Handle(Geom_Ellipse) ellp = Handle(Geom_Ellipse)::DownCast(c); + GeomArcOfEllipse* arc = new GeomArcOfEllipse(); + Handle(Geom_TrimmedCurve) this_arc = Handle(Geom_TrimmedCurve)::DownCast + (arc->handle()); + Handle(Geom_Ellipse) this_ellp = Handle(Geom_Ellipse)::DownCast + (this_arc->BasisCurve()); + this_ellp->SetElips(ellp->Elips()); + this_arc->SetTrim(f, l); + return Py::Object(new ArcOfEllipsePy(arc),true); + } + else if (c->IsKind(STANDARD_TYPE(Geom_Hyperbola))) { + Handle(Geom_Hyperbola) hypr = Handle(Geom_Hyperbola)::DownCast(c); + GeomArcOfHyperbola* arc = new GeomArcOfHyperbola(); + Handle(Geom_TrimmedCurve) this_arc = Handle(Geom_TrimmedCurve)::DownCast + (arc->handle()); + Handle(Geom_Hyperbola) this_hypr = Handle(Geom_Hyperbola)::DownCast + (this_arc->BasisCurve()); + this_hypr->SetHypr(hypr->Hypr()); + this_arc->SetTrim(f, l); + return Py::Object(new ArcOfHyperbolaPy(arc),true); + } + else if (c->IsKind(STANDARD_TYPE(Geom_Line))) { + Handle(Geom_Line) line = Handle(Geom_Line)::DownCast(c); + GeomLineSegment* segm = new GeomLineSegment(); + Handle(Geom_TrimmedCurve) this_segm = Handle(Geom_TrimmedCurve)::DownCast + (segm->handle()); + Handle(Geom_Line) this_line = Handle(Geom_Line)::DownCast + (this_segm->BasisCurve()); + this_line->SetLin(line->Lin()); + this_segm->SetTrim(f, l); + return Py::Object(new LineSegmentPy(segm),true); + } + else if (c->IsKind(STANDARD_TYPE(Geom_Parabola))) { + Handle(Geom_Parabola) para = Handle(Geom_Parabola)::DownCast(c); + GeomArcOfParabola* arc = new GeomArcOfParabola(); + Handle(Geom_TrimmedCurve) this_arc = Handle(Geom_TrimmedCurve)::DownCast + (arc->handle()); + Handle(Geom_Parabola) this_para = Handle(Geom_Parabola)::DownCast + (this_arc->BasisCurve()); + this_para->SetParab(para->Parab()); + this_arc->SetTrim(f, l); + return Py::Object(new ArcOfParabolaPy(arc),true); + } + else if (c->IsKind(STANDARD_TYPE(Geom_BezierCurve))) { + Handle(Geom_BezierCurve) bezier = Handle(Geom_BezierCurve)::DownCast(c->Copy()); + bezier->Segment(f, l); + return Py::asObject(new BezierCurvePy(new GeomBezierCurve(bezier))); + } + else if (c->IsKind(STANDARD_TYPE(Geom_BSplineCurve))) { + Handle(Geom_BSplineCurve) bspline = Handle(Geom_BSplineCurve)::DownCast(c->Copy()); + bspline->Segment(f, l); + return Py::asObject(new BSplineCurvePy(new GeomBSplineCurve(bspline))); + } + else if (c->IsKind(STANDARD_TYPE(Geom_OffsetCurve))) { + Handle(Geom_OffsetCurve) oc = Handle(Geom_OffsetCurve)::DownCast(c); + double v = oc->Offset(); + gp_Dir dir = oc->Direction(); + Py::Object off(makeTrimmedCurvePy(oc->BasisCurve(), f, l)); + + Py::Tuple args(3); + args.setItem(0, off); + args.setItem(1, Py::Float(v)); + + Py::Module baseModule("__FreeCADBase__"); + Py::Callable method(baseModule.getAttr("Vector")); + Py::Tuple coords(3); + coords.setItem(0, Py::Float(dir.X())); + coords.setItem(1, Py::Float(dir.Y())); + coords.setItem(2, Py::Float(dir.Z())); + args.setItem(2, method.apply(coords)); + + Py::Module partModule(PyImport_ImportModule("Part"), true); + Py::Callable call(partModule.getAttr("OffsetCurve")); + return call.apply(args); + } + else if (c->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) { + Handle(Geom_TrimmedCurve) trc = Handle(Geom_TrimmedCurve)::DownCast(c); + return makeTrimmedCurvePy(trc->BasisCurve(), f, l); + } + /*else if (c->IsKind(STANDARD_TYPE(Geom_BoundedCurve))) { + Handle(Geom_BoundedCurve) bc = Handle(Geom_BoundedCurve)::DownCast(c); + return Py::asObject(new GeometryCurvePy(new GeomBoundedCurve(bc))); + }*/ + + std::string err = "Unhandled curve type "; + err += c->DynamicType()->Name(); + throw Py::TypeError(err); +} + } // Part // ---------------------------------------