TopoShapeEdgePyImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Jürgen Riegel          (juergen.riegel@web.de) 2008     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <BRep_Builder.hxx>
00027 # include <BRep_Tool.hxx>
00028 # include <BRepAdaptor_Curve.hxx>
00029 # include <BRepBuilderAPI_MakeEdge.hxx>
00030 # include <BRepLProp_CLProps.hxx>
00031 # include <BRepLProp_CurveTool.hxx>
00032 # include <GProp_GProps.hxx>
00033 # include <Geom_Circle.hxx>
00034 # include <Geom_Curve.hxx>
00035 # include <Geom_Ellipse.hxx>
00036 # include <Geom_Hyperbola.hxx>
00037 # include <Geom_Parabola.hxx>
00038 # include <Geom_Line.hxx>
00039 # include <Geom_TrimmedCurve.hxx>
00040 # include <Geom_BezierCurve.hxx>
00041 # include <Geom_BSplineCurve.hxx>
00042 # include <gp_Circ.hxx>
00043 # include <gp_Elips.hxx>
00044 # include <gp_Hypr.hxx>
00045 # include <gp_Parab.hxx>
00046 # include <gp_Lin.hxx>
00047 # include <TopoDS.hxx>
00048 # include <TopoDS_Shape.hxx>
00049 # include <TopoDS_Edge.hxx>
00050 # include <Standard_Failure.hxx>
00051 #endif
00052 
00053 #include <BRepGProp.hxx>
00054 #include <GProp_GProps.hxx>
00055 #include <GCPnts_AbscissaPoint.hxx>
00056 
00057 #include <Base/VectorPy.h>
00058 #include <Base/GeometryPyCXX.h>
00059 
00060 #include "TopoShape.h"
00061 #include "TopoShapeFacePy.h"
00062 #include "TopoShapeVertexPy.h"
00063 #include "TopoShapeEdgePy.h"
00064 #include "TopoShapeEdgePy.cpp"
00065 
00066 #include "Geometry.h"
00067 #include "GeometryPy.h"
00068 #include "LinePy.h"
00069 #include "CirclePy.h"
00070 #include "EllipsePy.h"
00071 #include "HyperbolaPy.h"
00072 #include "ParabolaPy.h"
00073 #include "BezierCurvePy.h"
00074 #include "BSplineCurvePy.h"
00075 
00076 using namespace Part;
00077 
00078 // returns a string which represents the object e.g. when printed in python
00079 std::string TopoShapeEdgePy::representation(void) const
00080 {
00081     std::stringstream str;
00082     str << "<Edge object at " << getTopoShapePtr() << ">";
00083 
00084     return str.str();
00085 }
00086 
00087 PyObject *TopoShapeEdgePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00088 {
00089     // create a new instance of TopoShapeEdgePy and the Twin object 
00090     return new TopoShapeEdgePy(new TopoShape);
00091 }
00092 
00093 // constructor method
00094 int TopoShapeEdgePy::PyInit(PyObject* args, PyObject* /*kwd*/)
00095 {
00096     PyObject *pcObj, *pcObj2;
00097     double first=DBL_MAX, last=DBL_MAX;
00098     if (PyArg_ParseTuple(args, "O!|dd", &(Part::GeometryPy::Type), &pcObj, &first, &last)) {
00099         Geometry* geom = static_cast<GeometryPy*>(pcObj)->getGeometryPtr();
00100         Handle_Geom_Curve curve = Handle_Geom_Curve::DownCast(geom->handle());
00101         if (curve.IsNull()) {
00102             PyErr_SetString(PyExc_Exception, "geometry is not a curve type");
00103             return -1;
00104         }
00105 
00106         if (first==DBL_MAX)
00107             first = curve->FirstParameter();
00108         if (last==DBL_MAX)
00109             last = curve->LastParameter();
00110 
00111         try {
00112             BRepBuilderAPI_MakeEdge mkEdge(curve, first, last);
00113             getTopoShapePtr()->_Shape = mkEdge.Edge();
00114             return 0;
00115         }
00116         catch (Standard_Failure) {
00117             Handle_Standard_Failure e = Standard_Failure::Caught();
00118             PyErr_SetString(PyExc_Exception, e->GetMessageString());
00119             return -1;
00120         }
00121     }
00122 
00123     PyErr_Clear();
00124     if (PyArg_ParseTuple(args, "O!", &(Part::TopoShapePy::Type), &pcObj)) {
00125         TopoShape* shape = static_cast<TopoShapePy*>(pcObj)->getTopoShapePtr();
00126         if (shape && shape->_Shape.ShapeType() == TopAbs_EDGE) {
00127             this->getTopoShapePtr()->_Shape = shape->_Shape;
00128             return 0;
00129         }
00130         else {
00131             PyErr_SetString(PyExc_TypeError, "Shape is not an edge");
00132             return -1;
00133         }
00134     }
00135 
00136     PyErr_Clear();
00137     if (PyArg_ParseTuple(args, "O!O!", &(Part::TopoShapeVertexPy::Type), &pcObj,
00138                                        &(Part::TopoShapeVertexPy::Type), &pcObj2)) {
00139         TopoShape* shape1 = static_cast<TopoShapePy*>(pcObj)->getTopoShapePtr();
00140         TopoShape* shape2 = static_cast<TopoShapePy*>(pcObj2)->getTopoShapePtr();
00141         const TopoDS_Vertex& v1 = TopoDS::Vertex(shape1->_Shape);
00142         const TopoDS_Vertex& v2 = TopoDS::Vertex(shape2->_Shape);
00143 
00144         try {
00145             BRepBuilderAPI_MakeEdge mkEdge(v1, v2);
00146             getTopoShapePtr()->_Shape = mkEdge.Edge();
00147             return 0;
00148         }
00149         catch (Standard_Failure) {
00150             Handle_Standard_Failure e = Standard_Failure::Caught();
00151             PyErr_SetString(PyExc_Exception, e->GetMessageString());
00152             return -1;
00153         }
00154     }
00155 
00156     PyErr_SetString(PyExc_Exception, "Curve or shape expected");
00157     return -1;
00158 }
00159 
00160 // ====== Methods  ======================================================================
00161 
00162 PyObject* TopoShapeEdgePy::valueAt(PyObject *args)
00163 {
00164     double u;
00165     if (!PyArg_ParseTuple(args, "d",&u))
00166         return 0;
00167 
00168     const TopoDS_Edge& e = TopoDS::Edge(getTopoShapePtr()->_Shape);
00169     BRepAdaptor_Curve adapt(e);
00170 
00171     // normalizing parameter space to length
00172     double first = BRepLProp_CurveTool::FirstParameter(adapt);
00173     double last = BRepLProp_CurveTool::LastParameter(adapt);
00174     if (!Precision::IsInfinite(first) && !Precision::IsInfinite(last)) {
00175         double length = GCPnts_AbscissaPoint::Length(adapt);
00176         double stretch = (last - first) / length;
00177         u = first + u*stretch;
00178     }
00179 
00180     // Check now the orientation of the edge to make
00181     // sure that we get the right wanted point!
00182     BRepLProp_CLProps prop(adapt,u,0,Precision::Confusion());
00183     const gp_Pnt& V = prop.Value();
00184     return new Base::VectorPy(new Base::Vector3d(V.X(),V.Y(),V.Z()));
00185 }
00186 
00187 PyObject* TopoShapeEdgePy::tangentAt(PyObject *args)
00188 {
00189     double u;
00190     if (!PyArg_ParseTuple(args, "d",&u))
00191         return 0;
00192 
00193     const TopoDS_Edge& e = TopoDS::Edge(getTopoShapePtr()->_Shape);
00194     BRepAdaptor_Curve adapt(e);
00195 
00196     // normalizing parameter space to length
00197     double first = BRepLProp_CurveTool::FirstParameter(adapt);
00198     double last = BRepLProp_CurveTool::LastParameter(adapt);
00199     if (!Precision::IsInfinite(first) && !Precision::IsInfinite(last)) {
00200         double length = GCPnts_AbscissaPoint::Length(adapt);
00201         double stretch = (last - first) / length;
00202         u = first + u*stretch;
00203     }
00204 
00205     BRepLProp_CLProps prop(adapt,u,1,Precision::Confusion());
00206     if (prop.IsTangentDefined()) {
00207         gp_Dir dir;
00208         prop.Tangent(dir);
00209         return new Base::VectorPy(new Base::Vector3d(dir.X(),dir.Y(),dir.Z()));
00210     }
00211     else {
00212         PyErr_SetString(PyExc_NotImplementedError, "Tangent not defined at this position!");
00213         return 0;
00214     }
00215 }
00216 
00217 PyObject* TopoShapeEdgePy::normalAt(PyObject *args)
00218 {
00219     double u;
00220     if (!PyArg_ParseTuple(args, "d",&u))
00221         return 0;
00222 
00223     const TopoDS_Edge& e = TopoDS::Edge(getTopoShapePtr()->_Shape);
00224     BRepAdaptor_Curve adapt(e);
00225 
00226     // normalizing parameter space to length
00227     double first = BRepLProp_CurveTool::FirstParameter(adapt);
00228     double last = BRepLProp_CurveTool::LastParameter(adapt);
00229     if (!Precision::IsInfinite(first) && !Precision::IsInfinite(last)) {
00230         double length = GCPnts_AbscissaPoint::Length(adapt);
00231         double stretch = (last - first) / length;
00232         u = first + u*stretch;
00233     }
00234 
00235     try {
00236         BRepLProp_CLProps prop(adapt,u,1,Precision::Confusion());
00237         gp_Dir V ;
00238         prop.Normal(V);
00239         return new Base::VectorPy(new Base::Vector3d(V.X(),V.Y(),V.Z()));
00240     }
00241     catch (Standard_Failure) {
00242         Handle_Standard_Failure e = Standard_Failure::Caught();
00243         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00244         return 0;
00245     }
00246 }
00247 
00248 PyObject* TopoShapeEdgePy::curvatureAt(PyObject *args)
00249 {
00250     double u;
00251     if (!PyArg_ParseTuple(args, "d",&u))
00252         return 0;
00253 
00254     const TopoDS_Edge& e = TopoDS::Edge(getTopoShapePtr()->_Shape);
00255     BRepAdaptor_Curve adapt(e);
00256 
00257     // normalizing parameter space to length
00258     double first = BRepLProp_CurveTool::FirstParameter(adapt);
00259     double last = BRepLProp_CurveTool::LastParameter(adapt);
00260     if (!Precision::IsInfinite(first) && !Precision::IsInfinite(last)) {
00261         double length = GCPnts_AbscissaPoint::Length(adapt);
00262         double stretch = (last - first) / length;
00263         u = first + u*stretch;
00264     }
00265 
00266     try {
00267         BRepLProp_CLProps prop(adapt,u,2,Precision::Confusion());
00268         double C = prop.Curvature();
00269         return Py::new_reference_to(Py::Float(C));
00270     }
00271     catch (Standard_Failure) {
00272         Handle_Standard_Failure e = Standard_Failure::Caught();
00273         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00274         return 0;
00275     }
00276 }
00277 
00278 PyObject* TopoShapeEdgePy::centerOfCurvatureAt(PyObject *args)
00279 {
00280     double u;
00281     if (!PyArg_ParseTuple(args, "d",&u))
00282         return 0;
00283 
00284     const TopoDS_Edge& e = TopoDS::Edge(getTopoShapePtr()->_Shape);
00285     BRepAdaptor_Curve adapt(e);
00286 
00287     // normalizing parameter space to length
00288     double first = BRepLProp_CurveTool::FirstParameter(adapt);
00289     double last = BRepLProp_CurveTool::LastParameter(adapt);
00290     if (!Precision::IsInfinite(first) && !Precision::IsInfinite(last)) {
00291         double length = GCPnts_AbscissaPoint::Length(adapt);
00292         double stretch = (last - first) / length;
00293         u = first + u*stretch;
00294     }
00295 
00296     try {
00297         BRepLProp_CLProps prop(adapt,u,2,Precision::Confusion());
00298         gp_Pnt V ;
00299         prop.CentreOfCurvature(V);
00300         return new Base::VectorPy(new Base::Vector3d(V.X(),V.Y(),V.Z()));
00301     }
00302     catch (Standard_Failure) {
00303         Handle_Standard_Failure e = Standard_Failure::Caught();
00304         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00305         return 0;
00306     }
00307 }
00308 
00309 PyObject* TopoShapeEdgePy::derivative1At(PyObject *args)
00310 {
00311     double u;
00312     if (!PyArg_ParseTuple(args, "d",&u))
00313         return 0;
00314 
00315     const TopoDS_Edge& e = TopoDS::Edge(getTopoShapePtr()->_Shape);
00316     BRepAdaptor_Curve adapt(e);
00317 
00318     // normalizing parameter space to length
00319     double first = BRepLProp_CurveTool::FirstParameter(adapt);
00320     double last = BRepLProp_CurveTool::LastParameter(adapt);
00321     if (!Precision::IsInfinite(first) && !Precision::IsInfinite(last)) {
00322         double length = GCPnts_AbscissaPoint::Length(adapt);
00323         double stretch = (last - first) / length;
00324         u = first + u*stretch;
00325     }
00326 
00327     try {
00328         BRepLProp_CLProps prop(adapt,u,1,Precision::Confusion());
00329         const gp_Vec& V = prop.D1();
00330         return new Base::VectorPy(new Base::Vector3d(V.X(),V.Y(),V.Z()));
00331     }
00332     catch (Standard_Failure) {
00333         Handle_Standard_Failure e = Standard_Failure::Caught();
00334         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00335         return 0;
00336     }
00337 }
00338 
00339 PyObject* TopoShapeEdgePy::derivative2At(PyObject *args)
00340 {
00341     double u;
00342     if (!PyArg_ParseTuple(args, "d",&u))
00343         return 0;
00344 
00345     const TopoDS_Edge& e = TopoDS::Edge(getTopoShapePtr()->_Shape);
00346     BRepAdaptor_Curve adapt(e);
00347 
00348     // normalizing parameter space to length
00349     double first = BRepLProp_CurveTool::FirstParameter(adapt);
00350     double last = BRepLProp_CurveTool::LastParameter(adapt);
00351     if (!Precision::IsInfinite(first) && !Precision::IsInfinite(last)) {
00352         double length = GCPnts_AbscissaPoint::Length(adapt);
00353         double stretch = (last - first) / length;
00354         u = first + u*stretch;
00355     }
00356 
00357     try {
00358         BRepLProp_CLProps prop(adapt,u,2,Precision::Confusion());
00359         const gp_Vec& V = prop.D2();
00360         return new Base::VectorPy(new Base::Vector3d(V.X(),V.Y(),V.Z()));
00361     }
00362     catch (Standard_Failure) {
00363         Handle_Standard_Failure e = Standard_Failure::Caught();
00364         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00365         return 0;
00366     }
00367 }
00368 
00369 PyObject* TopoShapeEdgePy::derivative3At(PyObject *args)
00370 {
00371     double u;
00372     if (!PyArg_ParseTuple(args, "d",&u))
00373         return 0;
00374 
00375     const TopoDS_Edge& e = TopoDS::Edge(getTopoShapePtr()->_Shape);
00376     BRepAdaptor_Curve adapt(e);
00377 
00378     // normalizing parameter space to length
00379     double first = BRepLProp_CurveTool::FirstParameter(adapt);
00380     double last = BRepLProp_CurveTool::LastParameter(adapt);
00381     if (!Precision::IsInfinite(first) && !Precision::IsInfinite(last)) {
00382         double length = GCPnts_AbscissaPoint::Length(adapt);
00383         double stretch = (last - first) / length;
00384         u = first + u*stretch;
00385     }
00386 
00387     try {
00388         BRepLProp_CLProps prop(adapt,u,3,Precision::Confusion());
00389         const gp_Vec& V = prop.D3();
00390         return new Base::VectorPy(new Base::Vector3d(V.X(),V.Y(),V.Z()));
00391     }
00392     catch (Standard_Failure) {
00393         Handle_Standard_Failure e = Standard_Failure::Caught();
00394         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00395         return 0;
00396     }
00397 }
00398 
00399 PyObject* TopoShapeEdgePy::setTolerance(PyObject *args)
00400 {
00401     double tol;
00402     if (!PyArg_ParseTuple(args, "d", &tol))
00403         return 0;
00404     BRep_Builder aBuilder;
00405     const TopoDS_Edge& e = TopoDS::Edge(getTopoShapePtr()->_Shape);
00406     aBuilder.UpdateEdge(e, tol);
00407     Py_Return;
00408 }
00409 
00410 // ====== Attributes ======================================================================
00411 
00412 Py::Float TopoShapeEdgePy::getLength(void) const
00413 {
00414     const TopoDS_Edge& e = TopoDS::Edge(getTopoShapePtr()->_Shape);
00415     BRepAdaptor_Curve adapt(e);
00416     return Py::Float(GCPnts_AbscissaPoint::Length(adapt));
00417 }
00418 
00419 Py::Object TopoShapeEdgePy::getCurve() const
00420 {
00421     const TopoDS_Edge& e = TopoDS::Edge(getTopoShapePtr()->_Shape);
00422     BRepAdaptor_Curve adapt(e);
00423     switch(adapt.GetType())
00424     {
00425     case GeomAbs_Line:
00426         {
00427             GeomLineSegment* line = new GeomLineSegment();
00428             Handle_Geom_TrimmedCurve this_curv = Handle_Geom_TrimmedCurve::DownCast
00429                 (line->handle());
00430             Handle_Geom_Line this_line = Handle_Geom_Line::DownCast
00431                 (this_curv->BasisCurve());
00432             this_line->SetLin(adapt.Line());
00433             this_curv->SetTrim(adapt.FirstParameter(), adapt.LastParameter());
00434             return Py::Object(new LinePy(line),true);
00435         }
00436     case GeomAbs_Circle:
00437         {
00438             GeomCircle* circle = new GeomCircle();
00439             Handle_Geom_Circle this_curv = Handle_Geom_Circle::DownCast
00440                 (circle->handle());
00441             this_curv->SetCirc(adapt.Circle());
00442             //Standard_Real dd = adapt.FirstParameter();
00443             //Standard_Real ee = adapt.LastParameter();
00444             return Py::Object(new CirclePy(circle),true);
00445         }
00446     case GeomAbs_Ellipse:
00447         {
00448             GeomEllipse* elips = new GeomEllipse();
00449             Handle_Geom_Ellipse this_curv = Handle_Geom_Ellipse::DownCast
00450                 (elips->handle());
00451             this_curv->SetElips(adapt.Ellipse());
00452             return Py::Object(new EllipsePy(elips),true);
00453         }
00454     case GeomAbs_Hyperbola:
00455         {
00456             GeomHyperbola* hypr = new GeomHyperbola();
00457             Handle_Geom_Hyperbola this_curv = Handle_Geom_Hyperbola::DownCast
00458                 (hypr->handle());
00459             this_curv->SetHypr(adapt.Hyperbola());
00460             return Py::Object(new HyperbolaPy(hypr),true);
00461         }
00462     case GeomAbs_Parabola:
00463         {
00464             GeomParabola* parab = new GeomParabola();
00465             Handle_Geom_Parabola this_curv = Handle_Geom_Parabola::DownCast
00466                 (parab->handle());
00467             this_curv->SetParab(adapt.Parabola());
00468             return Py::Object(new ParabolaPy(parab),true);
00469         }
00470     case GeomAbs_BezierCurve:
00471         {
00472             GeomBezierCurve* curve = new GeomBezierCurve(adapt.Bezier());
00473             return Py::Object(new BezierCurvePy(curve),true);
00474         }
00475     case GeomAbs_BSplineCurve:
00476         {
00477             GeomBSplineCurve* curve = new GeomBSplineCurve(adapt.BSpline());
00478             return Py::Object(new BSplineCurvePy(curve),true);
00479         }
00480     case GeomAbs_OtherCurve:
00481         break;
00482     }
00483 
00484     throw Py::TypeError("undefined curve type");
00485 }
00486 
00487 Py::Tuple TopoShapeEdgePy::getParameterRange(void) const
00488 {
00489     const TopoDS_Edge& e = TopoDS::Edge(getTopoShapePtr()->_Shape);
00490     BRepAdaptor_Curve adapt(e);
00491     double u = adapt.FirstParameter();
00492     double v = adapt.LastParameter();
00493 
00494     Py::Tuple t(2);
00495     t.setItem(0, Py::Float(u));
00496     t.setItem(1, Py::Float(v));
00497     return t;
00498 }
00499 
00500 Py::Object TopoShapeEdgePy::getCenterOfMass(void) const
00501 {
00502     GProp_GProps props;
00503     BRepGProp::LinearProperties(getTopoShapePtr()->_Shape, props);
00504     gp_Pnt c = props.CentreOfMass();
00505     return Py::Vector(Base::Vector3d(c.X(),c.Y(),c.Z()));
00506 }
00507 
00508 Py::Boolean TopoShapeEdgePy::getClosed(void) const
00509 {
00510     Standard_Boolean ok = BRep_Tool::IsClosed(getTopoShapePtr()->_Shape);
00511     return Py::Boolean(ok ? true : false);
00512 }
00513 
00514 Py::Boolean TopoShapeEdgePy::getDegenerated(void) const
00515 {
00516     Standard_Boolean ok = BRep_Tool::Degenerated(TopoDS::Edge(getTopoShapePtr()->_Shape));
00517     return Py::Boolean(ok ? true : false);
00518 }
00519 
00520 PyObject *TopoShapeEdgePy::getCustomAttributes(const char* /*attr*/) const
00521 {
00522     return 0;
00523 }
00524 
00525 int TopoShapeEdgePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00526 {
00527     return 0; 
00528 }

Generated on Wed Nov 23 19:00:46 2011 for FreeCAD by  doxygen 1.6.1