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

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