From d3e0f03650b769b9a253ea77b4264e287b8504e5 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 26 Nov 2016 19:28:06 +0100 Subject: [PATCH] add method to extract pcurve from an edge --- src/Mod/Part/App/Geom2d/Curve2dPyImp.cpp | 80 ++++++++++++++++--- .../Part/App/Geom2d/OffsetCurve2dPyImp.cpp | 37 +-------- src/Mod/Part/App/Geometry2d.cpp | 49 ++++++++++-- src/Mod/Part/App/Geometry2d.h | 9 ++- src/Mod/Part/App/TopoShapeFacePy.xml | 13 ++- src/Mod/Part/App/TopoShapeFacePyImp.cpp | 72 +++++++++++++---- 6 files changed, 190 insertions(+), 70 deletions(-) diff --git a/src/Mod/Part/App/Geom2d/Curve2dPyImp.cpp b/src/Mod/Part/App/Geom2d/Curve2dPyImp.cpp index b9c85ede9f82..05c4a2b86573 100644 --- a/src/Mod/Part/App/Geom2d/Curve2dPyImp.cpp +++ b/src/Mod/Part/App/Geom2d/Curve2dPyImp.cpp @@ -51,6 +51,8 @@ # include # include # include +# include +# include #endif #include @@ -113,22 +115,76 @@ extern Py::Object shape2pyshape(const TopoDS_Shape &shape); PyObject* Curve2dPy::toShape(PyObject *args) { + if (PyArg_ParseTuple(args, "")) { + try { + Handle_Geom2d_Curve curv = Handle_Geom2d_Curve::DownCast(getGeometry2dPtr()->handle()); + + BRepBuilderAPI_MakeEdge2d mkBuilder(curv); + TopoDS_Shape edge = mkBuilder.Shape(); + return Py::new_reference_to(shape2pyshape(edge)); + } + catch (Standard_Failure) { + Handle_Standard_Failure e = Standard_Failure::Caught(); + PyErr_SetString(PartExceptionOCCError, e->GetMessageString()); + return 0; + } + } + + PyErr_Clear(); + double u1, u2; + if (PyArg_ParseTuple(args, "dd", &u1, &u2)) { + try { + Handle_Geom2d_Curve curv = Handle_Geom2d_Curve::DownCast(getGeometry2dPtr()->handle()); + + BRepBuilderAPI_MakeEdge2d mkBuilder(curv, u1, u2); + TopoDS_Shape edge = mkBuilder.Shape(); + return Py::new_reference_to(shape2pyshape(edge)); + } + catch (Standard_Failure) { + Handle_Standard_Failure e = Standard_Failure::Caught(); + PyErr_SetString(PartExceptionOCCError, e->GetMessageString()); + return 0; + } + } + + PyErr_Clear(); PyObject* p; - if (!PyArg_ParseTuple(args, "O!", &(Part::GeometrySurfacePy::Type), &p)) - return 0; - try { - Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast( - static_cast(p)->getGeomSurfacePtr()->handle()); - TopoDS_Shape sh = getGeometry2dPtr()->toShape(surf); - return Py::new_reference_to(shape2pyshape(sh)); + if (PyArg_ParseTuple(args, "O!", &(Part::GeometrySurfacePy::Type), &p)) { + try { + Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast( + static_cast(p)->getGeomSurfacePtr()->handle()); + Handle_Geom2d_Curve curv = Handle_Geom2d_Curve::DownCast(getGeometry2dPtr()->handle()); + + BRepBuilderAPI_MakeEdge mkBuilder(curv, surf); + TopoDS_Shape edge = mkBuilder.Shape(); + return Py::new_reference_to(shape2pyshape(edge)); + } + catch (Standard_Failure) { + Handle_Standard_Failure e = Standard_Failure::Caught(); + PyErr_SetString(PartExceptionOCCError, e->GetMessageString()); + return 0; + } } - catch (Standard_Failure) { - Handle_Standard_Failure e = Standard_Failure::Caught(); - PyErr_SetString(PartExceptionOCCError, e->GetMessageString()); - return 0; + + PyErr_Clear(); + if (PyArg_ParseTuple(args, "O!dd", &(Part::GeometrySurfacePy::Type), &p, &u1, &u2)) { + try { + Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast( + static_cast(p)->getGeomSurfacePtr()->handle()); + Handle_Geom2d_Curve curv = Handle_Geom2d_Curve::DownCast(getGeometry2dPtr()->handle()); + + BRepBuilderAPI_MakeEdge mkBuilder(curv, surf, u1, u2); + TopoDS_Shape edge = mkBuilder.Shape(); + return Py::new_reference_to(shape2pyshape(edge)); + } + 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"); + PyErr_SetString(PyExc_TypeError, "empty parameter list, parameter range or surface expected"); return 0; } diff --git a/src/Mod/Part/App/Geom2d/OffsetCurve2dPyImp.cpp b/src/Mod/Part/App/Geom2d/OffsetCurve2dPyImp.cpp index 4eeca2b0ce92..a58c907ad5b8 100644 --- a/src/Mod/Part/App/Geom2d/OffsetCurve2dPyImp.cpp +++ b/src/Mod/Part/App/Geom2d/OffsetCurve2dPyImp.cpp @@ -95,39 +95,10 @@ Py::Object OffsetCurve2dPy::getBasisCurve(void) const Handle_Geom2d_Curve basis = curve->BasisCurve(); if (basis.IsNull()) return Py::None(); - if (basis->IsKind(STANDARD_TYPE (Geom2d_Parabola))) { - Geom2dParabola c(Handle_Geom2d_Parabola::DownCast(basis)); - return Py::asObject(c.getPyObject()); - } - if (basis->IsKind(STANDARD_TYPE (Geom2d_Hyperbola))) { - Geom2dHyperbola c(Handle_Geom2d_Hyperbola::DownCast(basis)); - return Py::asObject(c.getPyObject()); - } - if (basis->IsKind(STANDARD_TYPE (Geom2d_Ellipse))) { - Geom2dEllipse c(Handle_Geom2d_Ellipse::DownCast(basis)); - return Py::asObject(c.getPyObject()); - } - if (basis->IsKind(STANDARD_TYPE (Geom2d_Circle))) { - Geom2dCircle c(Handle_Geom2d_Circle::DownCast(basis)); - return Py::asObject(c.getPyObject()); - } - if (basis->IsKind(STANDARD_TYPE (Geom2d_Line))) { - Geom2dLine c(Handle_Geom2d_Line::DownCast(basis)); - return Py::asObject(c.getPyObject()); - } - if (basis->IsKind(STANDARD_TYPE (Geom2d_BSplineCurve))) { - Geom2dBSplineCurve c(Handle_Geom2d_BSplineCurve::DownCast(basis)); - return Py::asObject(c.getPyObject()); - } - if (basis->IsKind(STANDARD_TYPE (Geom2d_BezierCurve))) { - Geom2dBezierCurve c(Handle_Geom2d_BezierCurve::DownCast(basis)); - return Py::asObject(c.getPyObject()); - } - if (basis->IsKind(STANDARD_TYPE (Geom2d_TrimmedCurve))) { - Geom2dTrimmedCurve c(Handle_Geom2d_TrimmedCurve::DownCast(basis)); - return Py::asObject(c.getPyObject()); - } - throw Py::RuntimeError("Unknown curve type"); + std::unique_ptr geo2d = getCurve2dFromGeom2d(basis); + if (!geo2d) + throw Py::RuntimeError("Unknown curve type"); + return Py::asObject(geo2d->getPyObject()); } void OffsetCurve2dPy::setBasisCurve(Py::Object arg) diff --git a/src/Mod/Part/App/Geometry2d.cpp b/src/Mod/Part/App/Geometry2d.cpp index e142aafd30f3..aada2d3ca4f7 100644 --- a/src/Mod/Part/App/Geometry2d.cpp +++ b/src/Mod/Part/App/Geometry2d.cpp @@ -23,7 +23,7 @@ #include "PreCompiled.h" #ifndef _PreComp_ -# include +# include # include # include # include @@ -135,11 +135,13 @@ Geom2dPoint::~Geom2dPoint() { } -TopoDS_Shape Geom2dPoint::toShape(const Handle_Geom_Surface& hSurface) const +TopoDS_Shape Geom2dPoint::toShape() const { Handle_Geom2d_CartesianPoint c = Handle_Geom2d_CartesianPoint::DownCast(handle()); gp_Pnt2d xy = c->Pnt2d(); - gp_Pnt pnt = hSurface->Value(xy.X(), xy.Y()); + gp_Pnt pnt; + pnt.SetX(xy.X()); + pnt.SetY(xy.Y()); BRepBuilderAPI_MakeVertex mkBuilder(pnt); return mkBuilder.Shape(); } @@ -223,10 +225,10 @@ Geom2dCurve::~Geom2dCurve() { } -TopoDS_Shape Geom2dCurve::toShape(const Handle_Geom_Surface& hSurface) const +TopoDS_Shape Geom2dCurve::toShape() const { Handle_Geom2d_Curve c = Handle_Geom2d_Curve::DownCast(handle()); - BRepBuilderAPI_MakeEdge mkBuilder(c, hSurface); + BRepBuilderAPI_MakeEdge2d mkBuilder(c); return mkBuilder.Shape(); } @@ -2271,3 +2273,40 @@ PyObject *Geom2dTrimmedCurve::getPyObject(void) PyErr_SetString(PyExc_RuntimeError, "Unknown curve type"); return 0; } + +// ------------------------------------------------------------------ + +namespace Part { +std::unique_ptr getCurve2dFromGeom2d(Handle_Geom2d_Curve curve) +{ + std::unique_ptr geo2d; + if (curve.IsNull()) + return geo2d; + if (curve->IsKind(STANDARD_TYPE (Geom2d_Parabola))) { + geo2d.reset(new Geom2dParabola(Handle_Geom2d_Parabola::DownCast(curve))); + } + else if (curve->IsKind(STANDARD_TYPE (Geom2d_Hyperbola))) { + geo2d.reset(new Geom2dHyperbola(Handle_Geom2d_Hyperbola::DownCast(curve))); + } + else if (curve->IsKind(STANDARD_TYPE (Geom2d_Ellipse))) { + geo2d.reset(new Geom2dEllipse(Handle_Geom2d_Ellipse::DownCast(curve))); + } + else if (curve->IsKind(STANDARD_TYPE (Geom2d_Circle))) { + geo2d.reset(new Geom2dCircle(Handle_Geom2d_Circle::DownCast(curve))); + } + else if (curve->IsKind(STANDARD_TYPE (Geom2d_Line))) { + geo2d.reset(new Geom2dLine(Handle_Geom2d_Line::DownCast(curve))); + } + else if (curve->IsKind(STANDARD_TYPE (Geom2d_BSplineCurve))) { + geo2d.reset(new Geom2dBSplineCurve(Handle_Geom2d_BSplineCurve::DownCast(curve))); + } + else if (curve->IsKind(STANDARD_TYPE (Geom2d_BezierCurve))) { + geo2d.reset(new Geom2dBezierCurve(Handle_Geom2d_BezierCurve::DownCast(curve))); + } + else if (curve->IsKind(STANDARD_TYPE (Geom2d_TrimmedCurve))) { + geo2d.reset(new Geom2dTrimmedCurve(Handle_Geom2d_TrimmedCurve::DownCast(curve))); + } + + return geo2d; +} +} diff --git a/src/Mod/Part/App/Geometry2d.h b/src/Mod/Part/App/Geometry2d.h index 78bbcbe5c2f9..fb7c2d1d9fa5 100644 --- a/src/Mod/Part/App/Geometry2d.h +++ b/src/Mod/Part/App/Geometry2d.h @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -50,7 +51,7 @@ class PartExport Geometry2d : public Base::Persistence public: virtual ~Geometry2d(); - virtual TopoDS_Shape toShape(const Handle_Geom_Surface&) const = 0; + virtual TopoDS_Shape toShape() const = 0; virtual const Handle_Geom2d_Geometry& handle() const = 0; // Persistence implementer --------------------- virtual unsigned int getMemSize(void) const; @@ -76,7 +77,7 @@ class PartExport Geom2dPoint : public Geometry2d Geom2dPoint(const Base::Vector2d&); virtual ~Geom2dPoint(); virtual Geometry2d *clone(void) const; - virtual TopoDS_Shape toShape(const Handle_Geom_Surface&) const; + virtual TopoDS_Shape toShape() const; // Persistence implementer --------------------- virtual unsigned int getMemSize(void) const; @@ -101,7 +102,7 @@ class PartExport Geom2dCurve : public Geometry2d Geom2dCurve(); virtual ~Geom2dCurve(); - virtual TopoDS_Shape toShape(const Handle_Geom_Surface&) const; + virtual TopoDS_Shape toShape() const; bool tangent(double u, gp_Dir2d&) const; Base::Vector2d pointAtParameter(double u) const; Base::Vector2d firstDerivativeAtParameter(double u) const; @@ -563,6 +564,8 @@ class PartExport Geom2dTrimmedCurve : public Geom2dCurve Handle_Geom2d_TrimmedCurve myCurve; }; +std::unique_ptr getCurve2dFromGeom2d(Handle_Geom2d_Curve); + } #endif // PART_GEOMETRY2D_H diff --git a/src/Mod/Part/App/TopoShapeFacePy.xml b/src/Mod/Part/App/TopoShapeFacePy.xml index 734f136747f6..dcc30b2f9ea5 100644 --- a/src/Mod/Part/App/TopoShapeFacePy.xml +++ b/src/Mod/Part/App/TopoShapeFacePy.xml @@ -64,7 +64,18 @@ Validate the face. - + + + +curveonSurface(Edge) -> None or tuple +Returns the curve associated to the edge in the +parametric space of the face. Returns None if this +curve does not exist. If this curve exists then a tuple +of curve and and parameter range is returned. + + + + Set the tolerance for the face. diff --git a/src/Mod/Part/App/TopoShapeFacePyImp.cpp b/src/Mod/Part/App/TopoShapeFacePyImp.cpp index 11fa9d2be46c..1bc339db140f 100644 --- a/src/Mod/Part/App/TopoShapeFacePyImp.cpp +++ b/src/Mod/Part/App/TopoShapeFacePyImp.cpp @@ -42,9 +42,11 @@ # include # include # include +# include # include # include # include +# include # include # include # include @@ -66,27 +68,30 @@ #include #include #include +#include #include #include #include "TopoShape.h" -#include "TopoShapeSolidPy.h" -#include "TopoShapeWirePy.h" -#include "TopoShapeFacePy.h" -#include "TopoShapeFacePy.cpp" -#include "TopoShapeCompoundPy.h" - -#include "BezierSurfacePy.h" -#include "BSplineSurfacePy.h" -#include "PlanePy.h" -#include "CylinderPy.h" -#include "ConePy.h" -#include "SpherePy.h" -#include "OffsetSurfacePy.h" -#include "SurfaceOfRevolutionPy.h" -#include "SurfaceOfExtrusionPy.h" -#include "ToroidPy.h" +#include "Geometry2d.h" +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "OCCError.h" #include "Tools.h" #include "FaceMaker.h" @@ -631,6 +636,41 @@ PyObject* TopoShapeFacePy::validate(PyObject *args) } } +PyObject* TopoShapeFacePy::curveOnSurface(PyObject *args) +{ + PyObject* e; + if (!PyArg_ParseTuple(args, "O!", &(TopoShapeEdgePy::Type), &e)) + return 0; + + try { + TopoDS_Shape shape = static_cast(e)->getTopoShapePtr()->getShape(); + if (shape.IsNull()) { + PyErr_SetString(PyExc_RuntimeError, "invalid shape"); + return 0; + } + + TopoDS_Edge edge = TopoDS::Edge(shape); + const TopoDS_Face& face = TopoDS::Face(getTopoShapePtr()->getShape()); + + Standard_Real first, last; + Handle_Geom2d_Curve curve = BRep_Tool::CurveOnSurface(edge, face, first, last); + std::unique_ptr geo2d = getCurve2dFromGeom2d(curve); + if (!geo2d) + Py_Return; + + Py::Tuple tuple(3); + tuple.setItem(0, Py::asObject(geo2d->getPyObject())); + tuple.setItem(1, Py::Float(first)); + tuple.setItem(2, Py::Float(last)); + return Py::new_reference_to(tuple); + } + catch (Standard_Failure) { + Handle_Standard_Failure e = Standard_Failure::Caught(); + PyErr_SetString(PartExceptionOCCError, e->GetMessageString()); + return 0; + } +} + Py::Object TopoShapeFacePy::getSurface() const { const TopoDS_Face& f = TopoDS::Face(getTopoShapePtr()->getShape());