SpherePyImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2008 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 <gp_Circ.hxx>
00027 # include <gp_Sphere.hxx>
00028 # include <Geom_Circle.hxx>
00029 # include <Geom_SphericalSurface.hxx>
00030 # include <Standard_Failure.hxx>
00031 #endif
00032 
00033 #include <Base/GeometryPyCXX.h>
00034 #include <Base/VectorPy.h>
00035 
00036 #include "Geometry.h"
00037 #include "CirclePy.h"
00038 #include "SpherePy.h"
00039 #include "SpherePy.cpp"
00040 
00041 using namespace Part;
00042 
00043 // returns a string which represents the object e.g. when printed in python
00044 std::string SpherePy::representation(void) const
00045 {
00046     Handle_Geom_SphericalSurface sphere = Handle_Geom_SphericalSurface::DownCast
00047         (getGeomSpherePtr()->handle());
00048     gp_Ax1 axis = sphere->Axis();
00049     gp_Dir dir = axis.Direction();
00050     gp_Pnt loc = axis.Location();
00051     Standard_Real fRad = sphere->Radius();
00052 
00053     std::stringstream str;
00054     str << "Sphere (";
00055     str << "Radius : " << fRad << ", "; 
00056     str << "Center : (" << loc.X() << ", "<< loc.Y() << ", "<< loc.Z() << "), "; 
00057     str << "Direction : (" << dir.X() << ", "<< dir.Y() << ", "<< dir.Z() << ")"; 
00058     str << ")";
00059 
00060     return str.str();
00061 }
00062 
00063 PyObject *SpherePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00064 {
00065     // create a new instance of SpherePy and the Twin object 
00066     return new SpherePy(new GeomSphere);
00067 }
00068 
00069 // constructor method
00070 int SpherePy::PyInit(PyObject* args, PyObject* /*kwd*/)
00071 {
00072     if (PyArg_ParseTuple(args, "")) {
00073         Handle_Geom_SphericalSurface sphere = Handle_Geom_SphericalSurface::DownCast
00074             (getGeomSpherePtr()->handle());
00075         sphere->SetRadius(1.0);
00076         return 0;
00077     }
00078 
00079     return -1;
00080 }
00081 
00082 Py::Float SpherePy::getRadius(void) const
00083 {
00084     Handle_Geom_SphericalSurface sphere = Handle_Geom_SphericalSurface::DownCast
00085         (getGeomSpherePtr()->handle());
00086     return Py::Float(sphere->Radius()); 
00087 }
00088 
00089 void  SpherePy::setRadius(Py::Float arg)
00090 {
00091     Handle_Geom_SphericalSurface sphere = Handle_Geom_SphericalSurface::DownCast
00092         (getGeomSpherePtr()->handle());
00093     sphere->SetRadius((double)arg);
00094 }
00095 
00096 Py::Float SpherePy::getArea(void) const
00097 {
00098     Handle_Geom_SphericalSurface sphere = Handle_Geom_SphericalSurface::DownCast
00099         (getGeomSpherePtr()->handle());
00100     return Py::Float(sphere->Area()); 
00101 }
00102 
00103 Py::Float SpherePy::getVolume(void) const
00104 {
00105     Handle_Geom_SphericalSurface sphere = Handle_Geom_SphericalSurface::DownCast
00106         (getGeomSpherePtr()->handle());
00107     return Py::Float(sphere->Volume()); 
00108 }
00109 
00110 Py::Object SpherePy::getCenter(void) const
00111 {
00112     Handle_Geom_SphericalSurface sphere = Handle_Geom_SphericalSurface::DownCast
00113         (getGeomSpherePtr()->handle());
00114     gp_Pnt loc = sphere->Location();
00115     return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
00116 }
00117 
00118 void SpherePy::setCenter(Py::Object arg)
00119 {
00120     PyObject* p = arg.ptr();
00121     if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
00122         Base::Vector3d loc = static_cast<Base::VectorPy*>(p)->value();
00123         Handle_Geom_SphericalSurface sphere = Handle_Geom_SphericalSurface::DownCast
00124             (getGeomSpherePtr()->handle());
00125         sphere->SetLocation(gp_Pnt(loc.x, loc.y, loc.z));
00126     }
00127     else if (PyObject_TypeCheck(p, &PyTuple_Type)) {
00128         Base::Vector3d loc = Base::getVectorFromTuple<double>(p);
00129         Handle_Geom_SphericalSurface sphere = Handle_Geom_SphericalSurface::DownCast
00130             (getGeomSpherePtr()->handle());
00131         sphere->SetLocation(gp_Pnt(loc.x, loc.y, loc.z));
00132     }
00133     else {
00134         std::string error = std::string("type must be 'Vector', not ");
00135         error += p->ob_type->tp_name;
00136         throw Py::TypeError(error);
00137     }
00138 }
00139 
00140 Py::Object SpherePy::getAxis(void) const
00141 {
00142     Handle_Geom_ElementarySurface s = Handle_Geom_ElementarySurface::DownCast
00143         (getGeometryPtr()->handle());
00144     gp_Dir dir = s->Axis().Direction();
00145     return Py::Vector(Base::Vector3d(dir.X(), dir.Y(), dir.Z()));
00146 }
00147 
00148 void SpherePy::setAxis(Py::Object arg)
00149 {
00150     Standard_Real dir_x, dir_y, dir_z;
00151     PyObject *p = arg.ptr();
00152     if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
00153         Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
00154         dir_x = v.x;
00155         dir_y = v.y;
00156         dir_z = v.z;
00157     }
00158     else if (PyTuple_Check(p)) {
00159         Py::Tuple tuple(arg);
00160         dir_x = (double)Py::Float(tuple.getItem(0));
00161         dir_y = (double)Py::Float(tuple.getItem(1));
00162         dir_z = (double)Py::Float(tuple.getItem(2));
00163     }
00164     else {
00165         std::string error = std::string("type must be 'Vector' or tuple, not ");
00166         error += p->ob_type->tp_name;
00167         throw Py::TypeError(error);
00168     }
00169 
00170     try {
00171         Handle_Geom_ElementarySurface this_surf = Handle_Geom_ElementarySurface::DownCast
00172             (this->getGeometryPtr()->handle());
00173         gp_Ax1 axis;
00174         axis.SetLocation(this_surf->Location());
00175         axis.SetDirection(gp_Dir(dir_x, dir_y, dir_z));
00176         this_surf->SetAxis(axis);
00177     }
00178     catch (Standard_Failure) {
00179         throw Py::Exception("cannot set axis");
00180     }
00181 }
00182 
00183 PyObject *SpherePy::uIso(PyObject *args)
00184 {
00185     double v;
00186     if (!PyArg_ParseTuple(args, "d", &v))
00187         return 0;
00188 
00189     try {
00190         Handle_Geom_SphericalSurface sphere = Handle_Geom_SphericalSurface::DownCast
00191             (getGeomSpherePtr()->handle());
00192         Handle_Geom_Curve c = sphere->UIso(v);
00193         return new CirclePy(new GeomCircle(Handle_Geom_Circle::DownCast(c)));
00194     }
00195     catch (Standard_Failure) {
00196         Handle_Standard_Failure e = Standard_Failure::Caught();
00197         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00198         return 0;
00199     }
00200 }
00201 
00202 PyObject *SpherePy::vIso(PyObject *args)
00203 {
00204     double v;
00205     if (!PyArg_ParseTuple(args, "d", &v))
00206         return 0;
00207 
00208     try {
00209         Handle_Geom_SphericalSurface sphere = Handle_Geom_SphericalSurface::DownCast
00210             (getGeomSpherePtr()->handle());
00211         Handle_Geom_Curve c = sphere->VIso(v);
00212         return new CirclePy(new GeomCircle(Handle_Geom_Circle::DownCast(c)));
00213     }
00214     catch (Standard_Failure) {
00215         Handle_Standard_Failure e = Standard_Failure::Caught();
00216         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00217         return 0;
00218     }
00219 }
00220 
00221 PyObject *SpherePy::getCustomAttributes(const char* /*attr*/) const
00222 {
00223     return 0;
00224 }
00225 
00226 int SpherePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00227 {
00228     return 0; 
00229 }

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