GeometryCurvePyImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net>     *
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 <BRepBuilderAPI_MakeEdge.hxx>
00027 # include <gp_Dir.hxx>
00028 # include <gp_Vec.hxx>
00029 # include <GCPnts_UniformAbscissa.hxx>
00030 # include <Geom_Geometry.hxx>
00031 # include <Geom_Curve.hxx>
00032 # include <Geom_Surface.hxx>
00033 # include <GeomAdaptor_Curve.hxx>
00034 # include <GeomFill.hxx>
00035 # include <GeomLProp_CLProps.hxx>
00036 # include <Handle_Geom_RectangularTrimmedSurface.hxx>
00037 # include <Handle_Geom_BSplineSurface.hxx>
00038 # include <Precision.hxx>
00039 # include <GeomAPI_ProjectPointOnCurve.hxx>
00040 # include <Standard_Failure.hxx>
00041 #endif
00042 
00043 #include <Base/GeometryPyCXX.h>
00044 #include <Base/VectorPy.h>
00045 
00046 #include "Geometry.h"
00047 #include "GeometryCurvePy.h"
00048 #include "GeometryCurvePy.cpp"
00049 #include "RectangularTrimmedSurfacePy.h"
00050 #include "BSplineSurfacePy.h"
00051 
00052 #include "TopoShape.h"
00053 #include "TopoShapePy.h"
00054 #include "TopoShapeEdgePy.h"
00055 
00056 using namespace Part;
00057 
00058 // returns a string which represents the object e.g. when printed in python
00059 std::string GeometryCurvePy::representation(void) const
00060 {
00061     return "<Curve object>";
00062 }
00063 
00064 PyObject *GeometryCurvePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00065 {
00066     // never create such objects with the constructor
00067     PyErr_SetString(PyExc_RuntimeError,
00068         "You cannot create an instance of the abstract class 'GeometryCurve'.");
00069     return 0;
00070 }
00071 
00072 // constructor method
00073 int GeometryCurvePy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
00074 {
00075     return 0;
00076 }
00077 
00078 PyObject* GeometryCurvePy::toShape(PyObject *args)
00079 {
00080     Handle_Geom_Geometry g = getGeometryPtr()->handle();
00081     Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(g);
00082     try {
00083         if (!c.IsNull()) {
00084             double u,v;
00085             u=c->FirstParameter();
00086             v=c->LastParameter();
00087             if (!PyArg_ParseTuple(args, "|dd", &u,&v))
00088                 return 0;
00089             BRepBuilderAPI_MakeEdge mkBuilder(c, u, v);
00090             TopoDS_Shape sh = mkBuilder.Shape();
00091             return new TopoShapeEdgePy(new TopoShape(sh));
00092         }
00093     }
00094     catch (Standard_Failure) {
00095         Handle_Standard_Failure e = Standard_Failure::Caught();
00096         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00097         return 0;
00098     }
00099 
00100     PyErr_SetString(PyExc_Exception, "Geometry is not a curve");
00101     return 0;
00102 }
00103 
00104 PyObject* GeometryCurvePy::discretize(PyObject *args)
00105 {
00106     double d;
00107     if (!PyArg_ParseTuple(args, "d", &d))
00108         return 0;
00109 
00110     Handle_Geom_Geometry g = getGeometryPtr()->handle();
00111     Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(g);
00112     try {
00113         if (!c.IsNull()) {
00114             GeomAdaptor_Curve curve_adaptator(c);
00115             GCPnts_UniformAbscissa discretizer;
00116             discretizer.Initialize (curve_adaptator, d);
00117             if (discretizer.IsDone () && discretizer.NbPoints () > 0) {
00118                 Py::List points;
00119                 int nbPoints = discretizer.NbPoints ();
00120                 for (int i=1; i<=nbPoints; i++) {
00121                     gp_Pnt p = curve_adaptator.Value (discretizer.Parameter (i));
00122                     points.append(Py::Vector(Base::Vector3d(p.X(),p.Y(),p.Z())));
00123                 }
00124 
00125                 return Py::new_reference_to(points);
00126             }
00127             else {
00128                 PyErr_SetString(PyExc_Exception, "Descretization of curve failed");
00129                 return 0;
00130             }
00131         }
00132     }
00133     catch (Standard_Failure) {
00134         Handle_Standard_Failure e = Standard_Failure::Caught();
00135         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00136         return 0;
00137     }
00138 
00139     PyErr_SetString(PyExc_Exception, "Geometry is not a curve");
00140     return 0;
00141 }
00142 
00143 PyObject* GeometryCurvePy::value(PyObject *args)
00144 {
00145     Handle_Geom_Geometry g = getGeometryPtr()->handle();
00146     Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(g);
00147     try {
00148         if (!c.IsNull()) {
00149             double u;
00150             if (!PyArg_ParseTuple(args, "d", &u))
00151                 return 0;
00152             gp_Pnt p = c->Value(u);
00153             return new Base::VectorPy(Base::Vector3d(p.X(),p.Y(),p.Z()));
00154         }
00155     }
00156     catch (Standard_Failure) {
00157         Handle_Standard_Failure e = Standard_Failure::Caught();
00158         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00159         return 0;
00160     }
00161 
00162     PyErr_SetString(PyExc_Exception, "Geometry is not a curve");
00163     return 0;
00164 }
00165 
00166 PyObject* GeometryCurvePy::tangent(PyObject *args)
00167 {
00168     Handle_Geom_Geometry g = getGeometryPtr()->handle();
00169     Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(g);
00170     try {
00171         if (!c.IsNull()) {
00172             double u;
00173             if (!PyArg_ParseTuple(args, "d", &u))
00174                 return 0;
00175             gp_Dir dir;
00176             Py::Tuple tuple(1);
00177             GeomLProp_CLProps prop(c,u,1,Precision::Confusion());
00178             if (prop.IsTangentDefined()) {
00179                 prop.Tangent(dir);
00180                 tuple.setItem(0, Py::Vector(Base::Vector3d(dir.X(),dir.Y(),dir.Z())));
00181             }
00182 
00183             return Py::new_reference_to(tuple);
00184         }
00185     }
00186     catch (Standard_Failure) {
00187         Handle_Standard_Failure e = Standard_Failure::Caught();
00188         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00189         return 0;
00190     }
00191 
00192     PyErr_SetString(PyExc_Exception, "Geometry is not a curve");
00193     return 0;
00194 }
00195 
00196 PyObject* GeometryCurvePy::parameter(PyObject *args)
00197 {
00198     Handle_Geom_Geometry g = getGeometryPtr()->handle();
00199     Handle_Geom_Curve c = Handle_Geom_Curve::DownCast(g);
00200     try {
00201         if (!c.IsNull()) {
00202             PyObject *p;
00203             if (!PyArg_ParseTuple(args, "O!", &(Base::VectorPy::Type), &p))
00204                 return 0;
00205             Base::Vector3d v = Py::Vector(p, false).toVector();
00206             gp_Pnt pnt(v.x,v.y,v.z);
00207             GeomAPI_ProjectPointOnCurve ppc(pnt, c);
00208             double val = ppc.LowerDistanceParameter();
00209             return Py::new_reference_to(Py::Float(val));
00210         }
00211     }
00212     catch (Standard_Failure) {
00213         Handle_Standard_Failure e = Standard_Failure::Caught();
00214         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00215         return 0;
00216     }
00217 
00218     PyErr_SetString(PyExc_Exception, "Geometry is not a curve");
00219     return 0;
00220 }
00221 
00222 PyObject* GeometryCurvePy::makeRuledSurface(PyObject *args)
00223 {
00224     PyObject* curve;
00225     if (!PyArg_ParseTuple(args, "O!", &(Part::GeometryCurvePy::Type), &curve))
00226         return 0;
00227 
00228     try {
00229         Handle_Geom_Curve aCrv1 = Handle_Geom_Curve::DownCast(getGeometryPtr()->handle());
00230         GeometryCurvePy* c = static_cast<GeometryCurvePy*>(curve);
00231         Handle_Geom_Curve aCrv2 = Handle_Geom_Curve::DownCast(c->getGeometryPtr()->handle());
00232         Handle_Geom_Surface aSurf = GeomFill::Surface (aCrv1, aCrv2);
00233         if (aSurf.IsNull()) {
00234             PyErr_SetString(PyExc_Exception, "Failed to create ruled surface");
00235             return 0;
00236         }
00237         // check the result surface type
00238         if (aSurf->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
00239             Handle_Geom_RectangularTrimmedSurface aTSurf = 
00240                 Handle_Geom_RectangularTrimmedSurface::DownCast(aSurf);
00241             return new RectangularTrimmedSurfacePy(new GeomTrimmedSurface(aTSurf));
00242         }
00243         else if (aSurf->IsKind(STANDARD_TYPE(Geom_BSplineSurface))) {
00244             Handle_Geom_BSplineSurface aBSurf = 
00245                 Handle_Geom_BSplineSurface::DownCast(aSurf);
00246             return new BSplineSurfacePy(new GeomBSplineSurface(aBSurf));
00247         }
00248         else {
00249             PyErr_Format(PyExc_NotImplementedError, "Ruled surface is of type '%s'",
00250                 aSurf->DynamicType()->Name());
00251             return 0;
00252         }
00253     }
00254     catch (Standard_Failure) {
00255         Handle_Standard_Failure e = Standard_Failure::Caught();
00256         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00257         return 0;
00258     }
00259 
00260     PyErr_SetString(PyExc_Exception, "Geometry is not a curve");
00261     return 0;
00262 }
00263 
00264 Py::Float GeometryCurvePy::getFirstParameter(void) const
00265 {
00266     return Py::Float(Handle_Geom_Curve::DownCast
00267         (getGeometryPtr()->handle())->FirstParameter());
00268 }
00269 
00270 Py::Float GeometryCurvePy::getLastParameter(void) const
00271 {
00272     return Py::Float(Handle_Geom_Curve::DownCast
00273         (getGeometryPtr()->handle())->LastParameter());
00274 }
00275 
00276 PyObject *GeometryCurvePy::getCustomAttributes(const char* /*attr*/) const
00277 {
00278     return 0;
00279 }
00280 
00281 int GeometryCurvePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00282 {
00283     return 0; 
00284 }

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