Skip to content

Commit

Permalink
add method to extract pcurve from an edge
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Nov 26, 2016
1 parent 420761d commit d3e0f03
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 70 deletions.
80 changes: 68 additions & 12 deletions src/Mod/Part/App/Geom2d/Curve2dPyImp.cpp
Expand Up @@ -51,6 +51,8 @@
# include <ShapeConstruct_Curve.hxx>
# include <GeomAPI_IntCS.hxx>
# include <Geom2dAPI_ExtremaCurveCurve.hxx>
# include <BRepBuilderAPI_MakeEdge2d.hxx>
# include <BRepBuilderAPI_MakeEdge.hxx>
#endif

#include <Base/GeometryPyCXX.h>
Expand Down Expand Up @@ -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<GeometrySurfacePy*>(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<GeometrySurfacePy*>(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<GeometrySurfacePy*>(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;
}

Expand Down
37 changes: 4 additions & 33 deletions src/Mod/Part/App/Geom2d/OffsetCurve2dPyImp.cpp
Expand Up @@ -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<Geom2dCurve> geo2d = getCurve2dFromGeom2d(basis);
if (!geo2d)
throw Py::RuntimeError("Unknown curve type");
return Py::asObject(geo2d->getPyObject());
}

void OffsetCurve2dPy::setBasisCurve(Py::Object arg)
Expand Down
49 changes: 44 additions & 5 deletions src/Mod/Part/App/Geometry2d.cpp
Expand Up @@ -23,7 +23,7 @@

#include "PreCompiled.h"
#ifndef _PreComp_
# include <BRepBuilderAPI_MakeEdge.hxx>
# include <BRepBuilderAPI_MakeEdge2d.hxx>
# include <BRepBuilderAPI_MakeVertex.hxx>
# include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
# include <Geom2dAPI_Interpolate.hxx>
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -2271,3 +2273,40 @@ PyObject *Geom2dTrimmedCurve::getPyObject(void)
PyErr_SetString(PyExc_RuntimeError, "Unknown curve type");
return 0;
}

// ------------------------------------------------------------------

namespace Part {
std::unique_ptr<Geom2dCurve> getCurve2dFromGeom2d(Handle_Geom2d_Curve curve)
{
std::unique_ptr<Geom2dCurve> 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;
}
}
9 changes: 6 additions & 3 deletions src/Mod/Part/App/Geometry2d.h
Expand Up @@ -39,6 +39,7 @@
#include <gp_Ax22d.hxx>
#include <list>
#include <vector>
#include <memory>
#include <Base/Persistence.h>
#include <Base/Tools2D.h>

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -563,6 +564,8 @@ class PartExport Geom2dTrimmedCurve : public Geom2dCurve
Handle_Geom2d_TrimmedCurve myCurve;
};

std::unique_ptr<Geom2dCurve> getCurve2dFromGeom2d(Handle_Geom2d_Curve);

}

#endif // PART_GEOMETRY2D_H
13 changes: 12 additions & 1 deletion src/Mod/Part/App/TopoShapeFacePy.xml
Expand Up @@ -64,7 +64,18 @@
<UserDocu>Validate the face.</UserDocu>
</Documentation>
</Methode>
<Methode Name="setTolerance">
<Methode Name="curveOnSurface">
<Documentation>
<UserDocu>
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.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="setTolerance">
<Documentation>
<UserDocu>Set the tolerance for the face.</UserDocu>
</Documentation>
Expand Down
72 changes: 56 additions & 16 deletions src/Mod/Part/App/TopoShapeFacePyImp.cpp
Expand Up @@ -42,9 +42,11 @@
# include <Geom_SphericalSurface.hxx>
# include <Geom_ToroidalSurface.hxx>
# include <Geom_Surface.hxx>
# include <Geom2d_Curve.hxx>
# include <TopoDS.hxx>
# include <TopoDS_Face.hxx>
# include <TopoDS_Wire.hxx>
# include <TopoDS_Edge.hxx>
# include <gp_Pnt2d.hxx>
# include <gp_Pln.hxx>
# include <gp_Cylinder.hxx>
Expand All @@ -66,27 +68,30 @@
#include <BRepLProp_SurfaceTool.hxx>
#include <BRepGProp_Face.hxx>
#include <GeomLProp_SLProps.hxx>
#include <BRep_Tool.hxx>

#include <Base/VectorPy.h>
#include <Base/GeometryPyCXX.h>

#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 <Mod/Part/App/TopoShapeSolidPy.h>
#include <Mod/Part/App/TopoShapeEdgePy.h>
#include <Mod/Part/App/TopoShapeWirePy.h>
#include <Mod/Part/App/TopoShapeFacePy.h>
#include <Mod/Part/App/TopoShapeFacePy.cpp>
#include <Mod/Part/App/TopoShapeCompoundPy.h>

#include <Mod/Part/App/BezierSurfacePy.h>
#include <Mod/Part/App/BSplineSurfacePy.h>
#include <Mod/Part/App/PlanePy.h>
#include <Mod/Part/App/CylinderPy.h>
#include <Mod/Part/App/ConePy.h>
#include <Mod/Part/App/SpherePy.h>
#include <Mod/Part/App/OffsetSurfacePy.h>
#include <Mod/Part/App/SurfaceOfRevolutionPy.h>
#include <Mod/Part/App/SurfaceOfExtrusionPy.h>
#include <Mod/Part/App/ToroidPy.h>
#include "OCCError.h"
#include "Tools.h"
#include "FaceMaker.h"
Expand Down Expand Up @@ -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<TopoShapeEdgePy*>(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<Part::Geom2dCurve> 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());
Expand Down

0 comments on commit d3e0f03

Please sign in to comment.