GeometrySurfacePyImp.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_MakeFace.hxx>
00027 # include <gp_Dir.hxx>
00028 # include <gp_Vec.hxx>
00029 # include <Geom_Geometry.hxx>
00030 # include <Geom_Surface.hxx>
00031 # include <GeomConvert_ApproxSurface.hxx>
00032 # include <GeomLProp_SLProps.hxx>
00033 # include <Precision.hxx>
00034 # include <Standard_Failure.hxx>
00035 # include <ShapeAnalysis_Surface.hxx>
00036 #endif
00037 
00038 #include <Base/GeometryPyCXX.h>
00039 #include <Base/VectorPy.h>
00040 
00041 #include "Geometry.h"
00042 #include "GeometrySurfacePy.h"
00043 #include "GeometrySurfacePy.cpp"
00044 #include "BSplineSurfacePy.h"
00045 
00046 #include "TopoShape.h"
00047 #include "TopoShapePy.h"
00048 #include "TopoShapeFacePy.h"
00049 
00050 using namespace Part;
00051 
00052 // returns a string which represents the object e.g. when printed in python
00053 std::string GeometrySurfacePy::representation(void) const
00054 {
00055     return "<Surface object>";
00056 }
00057 
00058 PyObject *GeometrySurfacePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00059 {
00060     // never create such objects with the constructor
00061     PyErr_SetString(PyExc_RuntimeError,
00062         "You cannot create an instance of the abstract class 'GeometrySurface'.");
00063     return 0;
00064 }
00065 
00066 // constructor method
00067 int GeometrySurfacePy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
00068 {
00069     return 0;
00070 }
00071 
00072 PyObject* GeometrySurfacePy::toShape(PyObject *args)
00073 {
00074     Handle_Geom_Geometry g = getGeometryPtr()->handle();
00075     Handle_Geom_Surface s = Handle_Geom_Surface::DownCast(g);
00076     try {
00077         if (!s.IsNull()) {
00078             double u1,u2,v1,v2;
00079             s->Bounds(u1,u2,v1,v2);
00080             if (!PyArg_ParseTuple(args, "|dddd", &u1,&u2,&v1,&v2))
00081                 return 0;
00082             BRepBuilderAPI_MakeFace mkBuilder(s, u1, u2, v1, v2);
00083             TopoDS_Shape sh = mkBuilder.Shape();
00084             return new TopoShapeFacePy(new TopoShape(sh));
00085         }
00086     }
00087     catch (Standard_Failure) {
00088         Handle_Standard_Failure e = Standard_Failure::Caught();
00089         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00090         return 0;
00091     }
00092 
00093     PyErr_SetString(PyExc_Exception, "Geometry is not a surface");
00094     return 0;
00095 }
00096 
00097 PyObject* GeometrySurfacePy::value(PyObject *args)
00098 {
00099     Handle_Geom_Geometry g = getGeometryPtr()->handle();
00100     Handle_Geom_Surface s = Handle_Geom_Surface::DownCast(g);
00101     try {
00102         if (!s.IsNull()) {
00103             double u,v;
00104             if (!PyArg_ParseTuple(args, "dd", &u,&v))
00105                 return 0;
00106             gp_Pnt p = s->Value(u,v);
00107             return new Base::VectorPy(Base::Vector3d(p.X(),p.Y(),p.Z()));
00108         }
00109     }
00110     catch (Standard_Failure) {
00111         Handle_Standard_Failure e = Standard_Failure::Caught();
00112         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00113         return 0;
00114     }
00115 
00116     PyErr_SetString(PyExc_Exception, "Geometry is not a surface");
00117     return 0;
00118 }
00119 
00120 PyObject* GeometrySurfacePy::tangent(PyObject *args)
00121 {
00122     Handle_Geom_Geometry g = getGeometryPtr()->handle();
00123     Handle_Geom_Surface s = Handle_Geom_Surface::DownCast(g);
00124     try {
00125         if (!s.IsNull()) {
00126             double u,v;
00127             if (!PyArg_ParseTuple(args, "dd", &u,&v))
00128                 return 0;
00129             gp_Dir dir;
00130             Py::Tuple tuple(2);
00131             GeomLProp_SLProps prop(s,u,v,1,Precision::Confusion());
00132             if (prop.IsTangentUDefined()) {
00133                 prop.TangentU(dir);
00134                 tuple.setItem(0, Py::Vector(Base::Vector3d(dir.X(),dir.Y(),dir.Z())));
00135             }
00136             if (prop.IsTangentVDefined()) {
00137                 prop.TangentV(dir);
00138                 tuple.setItem(1, Py::Vector(Base::Vector3d(dir.X(),dir.Y(),dir.Z())));
00139             }
00140 
00141             return Py::new_reference_to(tuple);
00142         }
00143     }
00144     catch (Standard_Failure) {
00145         Handle_Standard_Failure e = Standard_Failure::Caught();
00146         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00147         return 0;
00148     }
00149 
00150     PyErr_SetString(PyExc_Exception, "Geometry is not a surface");
00151     return 0;
00152 }
00153 
00154 PyObject* GeometrySurfacePy::parameter(PyObject *args)
00155 {
00156     Handle_Geom_Surface surf = Handle_Geom_Surface
00157         ::DownCast(getGeometryPtr()->handle());
00158     try {
00159         if (!surf.IsNull()) {
00160             PyObject *p;
00161             double prec = Precision::Confusion();
00162             if (!PyArg_ParseTuple(args, "O!|d", &(Base::VectorPy::Type), &p, &prec))
00163                 return 0;
00164             Base::Vector3d v = Py::Vector(p, false).toVector();
00165             gp_Pnt pnt(v.x,v.y,v.z);
00166             ShapeAnalysis_Surface as(surf);
00167             gp_Pnt2d uv = as.ValueOfUV(pnt, prec);
00168             Py::Tuple tuple(2);
00169             tuple.setItem(0, Py::Float(uv.X()));
00170             tuple.setItem(1, Py::Float(uv.Y()));
00171             return Py::new_reference_to(tuple);
00172         }
00173     }
00174     catch (Standard_Failure) {
00175         Handle_Standard_Failure e = Standard_Failure::Caught();
00176         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00177         return 0;
00178     }
00179 
00180     PyErr_SetString(PyExc_Exception, "Geometry is not a surface");
00181     return 0;
00182 }
00183 
00184 PyObject* GeometrySurfacePy::bounds(PyObject * args)
00185 {
00186     if (!PyArg_ParseTuple(args, ""))
00187         return 0;
00188 
00189     Handle_Geom_Surface surf = Handle_Geom_Surface
00190         ::DownCast(getGeometryPtr()->handle());
00191     Py::Tuple bound(4);
00192     Standard_Real u1,u2,v1,v2;
00193     surf->Bounds(u1,u2,v1,v2);
00194     bound.setItem(0,Py::Float(u1));
00195     bound.setItem(1,Py::Float(u2));
00196     bound.setItem(2,Py::Float(v1));
00197     bound.setItem(3,Py::Float(v2));
00198     return Py::new_reference_to(bound);
00199 }
00200 
00201 PyObject* GeometrySurfacePy::isUPeriodic(PyObject * args)
00202 {
00203     if (!PyArg_ParseTuple(args, ""))
00204         return 0;
00205 
00206     Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast
00207         (getGeometryPtr()->handle());
00208     Standard_Boolean val = surf->IsUPeriodic();
00209     if (val) {
00210         Py_INCREF(Py_True);
00211         return Py_True;
00212     }
00213     else {
00214         Py_INCREF(Py_False);
00215         return Py_False;
00216     }
00217 }
00218 
00219 PyObject* GeometrySurfacePy::isVPeriodic(PyObject * args)
00220 {
00221     if (!PyArg_ParseTuple(args, ""))
00222         return 0;
00223 
00224     Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast
00225         (getGeometryPtr()->handle());
00226     Standard_Boolean val = surf->IsVPeriodic();
00227     if (val) {
00228         Py_INCREF(Py_True);
00229         return Py_True;
00230     }
00231     else {
00232         Py_INCREF(Py_False);
00233         return Py_False;
00234     }
00235 }
00236 
00237 PyObject* GeometrySurfacePy::isUClosed(PyObject * args)
00238 {
00239     if (!PyArg_ParseTuple(args, ""))
00240         return 0;
00241 
00242     Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast
00243         (getGeometryPtr()->handle());
00244     Standard_Boolean val = surf->IsUClosed();
00245     if (val) {
00246         Py_INCREF(Py_True);
00247         return Py_True;
00248     }
00249     else {
00250         Py_INCREF(Py_False);
00251         return Py_False;
00252     }
00253 }
00254 
00255 PyObject* GeometrySurfacePy::isVClosed(PyObject * args)
00256 {
00257     if (!PyArg_ParseTuple(args, ""))
00258         return 0;
00259 
00260     Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast
00261         (getGeometryPtr()->handle());
00262     Standard_Boolean val = surf->IsVClosed();
00263     if (val) {
00264         Py_INCREF(Py_True);
00265         return Py_True;
00266     }
00267     else {
00268         Py_INCREF(Py_False);
00269         return Py_False;
00270     }
00271 }
00272 
00273 PyObject* GeometrySurfacePy::UPeriod(PyObject * args)
00274 {
00275     if (!PyArg_ParseTuple(args, ""))
00276         return 0;
00277 
00278     try {
00279         Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast
00280             (getGeometryPtr()->handle());
00281         Standard_Real val = surf->UPeriod();
00282         return PyFloat_FromDouble(val);
00283     }
00284     catch (Standard_Failure) {
00285         Handle_Standard_Failure e = Standard_Failure::Caught();
00286         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00287         return 0;
00288     }
00289 }
00290 
00291 PyObject* GeometrySurfacePy::VPeriod(PyObject * args)
00292 {
00293     if (!PyArg_ParseTuple(args, ""))
00294         return 0;
00295 
00296     try {
00297         Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast
00298             (getGeometryPtr()->handle());
00299         Standard_Real val = surf->VPeriod();
00300         return PyFloat_FromDouble(val);
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* GeometrySurfacePy::toBSpline(PyObject * args)
00310 {
00311     double tol3d;
00312     char *ucont, *vcont;
00313     int maxDegU,maxDegV,maxSegm,prec=0;
00314     if (!PyArg_ParseTuple(args, "dssiii|i",&tol3d,&ucont,&vcont,
00315                                            &maxDegU,&maxDegV,&maxSegm,&prec))
00316         return 0;
00317 
00318     std::string uc = ucont;
00319     GeomAbs_Shape absU, absV;
00320     if (uc == "C0")
00321         absU = GeomAbs_C0;
00322     else if (uc == "C1")
00323         absU = GeomAbs_C1;
00324     else if (uc == "C2")
00325         absU = GeomAbs_C2;
00326     else if (uc == "C3")
00327         absU = GeomAbs_C3;
00328     else if (uc == "CN")
00329         absU = GeomAbs_CN;
00330     else if (uc == "G1")
00331         absU = GeomAbs_G1;
00332     else
00333         absU = GeomAbs_G2;
00334 
00335     std::string vc = vcont;
00336     if (vc == "C0")
00337         absV = GeomAbs_C0;
00338     else if (vc == "C1")
00339         absV = GeomAbs_C1;
00340     else if (vc == "C2")
00341         absV = GeomAbs_C2;
00342     else if (vc == "C3")
00343         absV = GeomAbs_C3;
00344     else if (vc == "CN")
00345         absV = GeomAbs_CN;
00346     else if (vc == "G1")
00347         absV = GeomAbs_G1;
00348     else
00349         absV = GeomAbs_G2;
00350 
00351     try {
00352         Handle_Geom_Surface surf = Handle_Geom_Surface::DownCast
00353             (getGeometryPtr()->handle());
00354         GeomConvert_ApproxSurface cvt(surf, tol3d, absU, absV, maxDegU, maxDegV, maxSegm, prec);
00355         if (cvt.IsDone() && cvt.HasResult()) {
00356             return new BSplineSurfacePy(new GeomBSplineSurface(cvt.Surface()));
00357         }
00358         else {
00359             Standard_Failure::Raise("Cannot convert to B-Spline surface");
00360         }
00361     }
00362     catch (Standard_Failure) {
00363         Handle_Standard_Failure e = Standard_Failure::Caught();
00364         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00365     }
00366 
00367     return 0;
00368 }
00369 
00370 PyObject *GeometrySurfacePy::getCustomAttributes(const char* /*attr*/) const
00371 {
00372     return 0;
00373 }
00374 
00375 int GeometrySurfacePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00376 {
00377     return 0; 
00378 }

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