EllipsePyImp.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_Elips.hxx>
00027 # include <Geom_Ellipse.hxx>
00028 # include <GC_MakeEllipse.hxx>
00029 #endif
00030 
00031 #include <Base/GeometryPyCXX.h>
00032 #include <Base/VectorPy.h>
00033 
00034 #include "Geometry.h"
00035 #include "EllipsePy.h"
00036 #include "EllipsePy.cpp"
00037 
00038 using namespace Part;
00039 
00040 extern const char* gce_ErrorStatusText(gce_ErrorType et);
00041 
00042 // returns a string which represents the object e.g. when printed in python
00043 std::string EllipsePy::representation(void) const
00044 {
00045     return "<Ellipse object>";
00046 }
00047 
00048 PyObject *EllipsePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00049 {
00050     // create a new instance of EllipsePy and the Twin object 
00051     return new EllipsePy(new GeomEllipse);
00052 }
00053 
00054 // constructor method
00055 int EllipsePy::PyInit(PyObject* args, PyObject* kwds)
00056 {
00057     char* keywords_n[] = {NULL};
00058     if (PyArg_ParseTupleAndKeywords(args, kwds, "", keywords_n)) {
00059         Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
00060         ellipse->SetMajorRadius(2.0);
00061         ellipse->SetMinorRadius(1.0);
00062         return 0;
00063     }
00064 
00065     char* keywords_e[] = {"Ellipse",NULL};
00066     PyErr_Clear();
00067     PyObject *pElips;
00068     if (PyArg_ParseTupleAndKeywords(args, kwds, "O!",keywords_e, &(EllipsePy::Type), &pElips)) {
00069         EllipsePy* pEllipse = static_cast<EllipsePy*>(pElips);
00070         Handle_Geom_Ellipse Elips1 = Handle_Geom_Ellipse::DownCast
00071             (pEllipse->getGeomEllipsePtr()->handle());
00072         Handle_Geom_Ellipse Elips2 = Handle_Geom_Ellipse::DownCast
00073             (this->getGeomEllipsePtr()->handle());
00074         Elips2->SetElips(Elips1->Elips());
00075         return 0;
00076     }
00077 
00078     char* keywords_ssc[] = {"S1","S2","Center",NULL};
00079     PyErr_Clear();
00080     PyObject *pV1, *pV2, *pV3;
00081     if (PyArg_ParseTupleAndKeywords(args, kwds, "O!O!O!", keywords_ssc,
00082                                          &(Base::VectorPy::Type), &pV1,
00083                                          &(Base::VectorPy::Type), &pV2,
00084                                          &(Base::VectorPy::Type), &pV3)) {
00085         Base::Vector3d v1 = static_cast<Base::VectorPy*>(pV1)->value();
00086         Base::Vector3d v2 = static_cast<Base::VectorPy*>(pV2)->value();
00087         Base::Vector3d v3 = static_cast<Base::VectorPy*>(pV3)->value();
00088         GC_MakeEllipse me(gp_Pnt(v1.x,v1.y,v1.z),
00089                           gp_Pnt(v2.x,v2.y,v2.z),
00090                           gp_Pnt(v3.x,v3.y,v3.z));
00091         if (!me.IsDone()) {
00092             PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(me.Status()));
00093             return -1;
00094         }
00095 
00096         Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
00097         ellipse->SetElips(me.Value()->Elips());
00098         return 0;
00099     }
00100 
00101     char* keywords_cmm[] = {"Center","MajorRadius","MinorRadius",NULL};
00102     PyErr_Clear();
00103     PyObject *pV;
00104     double major, minor;
00105     if (PyArg_ParseTupleAndKeywords(args, kwds, "O!dd", keywords_cmm,
00106                                         &(Base::VectorPy::Type), &pV,
00107                                         &major, &minor)) {
00108         Base::Vector3d c = static_cast<Base::VectorPy*>(pV)->value();
00109         GC_MakeEllipse me(gp_Ax2(gp_Pnt(c.x,c.y,c.z), gp_Dir(0.0,0.0,1.0)),
00110                           major, minor);
00111         if (!me.IsDone()) {
00112             PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(me.Status()));
00113             return -1;
00114         }
00115 
00116         Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
00117         ellipse->SetElips(me.Value()->Elips());
00118         return 0;
00119     }
00120 
00121     PyErr_SetString(PyExc_TypeError, "Ellipse constructor accepts:\n"
00122         "-- empty parameter list\n"
00123         "-- Ellipse\n"
00124         "-- Point, double, double\n"
00125         "-- Point, Point, Point");
00126     return -1;
00127 }
00128 
00129 Py::Float EllipsePy::getMajorRadius(void) const
00130 {
00131     Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
00132     return Py::Float(ellipse->MajorRadius()); 
00133 }
00134 
00135 void EllipsePy::setMajorRadius(Py::Float arg)
00136 {
00137     Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
00138     ellipse->SetMajorRadius((double)arg);
00139 }
00140 
00141 Py::Float EllipsePy::getMinorRadius(void) const
00142 {
00143     Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
00144     return Py::Float(ellipse->MinorRadius()); 
00145 }
00146 
00147 void EllipsePy::setMinorRadius(Py::Float arg)
00148 {
00149     Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
00150     ellipse->SetMinorRadius((double)arg);
00151 }
00152 
00153 Py::Float EllipsePy::getEccentricity(void) const
00154 {
00155     Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
00156     return Py::Float(ellipse->Eccentricity()); 
00157 }
00158 
00159 Py::Float EllipsePy::getFocal(void) const
00160 {
00161     Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
00162     return Py::Float(ellipse->Focal()); 
00163 }
00164 
00165 Py::Object EllipsePy::getFocus1(void) const
00166 {
00167     Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
00168     gp_Pnt loc = ellipse->Focus1();
00169     return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
00170 }
00171 
00172 Py::Object EllipsePy::getFocus2(void) const
00173 {
00174     Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
00175     gp_Pnt loc = ellipse->Focus2();
00176     return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
00177 }
00178 
00179 Py::Object EllipsePy::getCenter(void) const
00180 {
00181     Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
00182     gp_Pnt loc = ellipse->Location();
00183     return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
00184 }
00185 
00186 void EllipsePy::setCenter(Py::Object arg)
00187 {
00188     PyObject* p = arg.ptr();
00189     if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
00190         Base::Vector3d loc = static_cast<Base::VectorPy*>(p)->value();
00191         Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
00192         ellipse->SetLocation(gp_Pnt(loc.x, loc.y, loc.z));
00193     }
00194     else if (PyTuple_Check(p)) {
00195         Py::Tuple tuple(arg);
00196         gp_Pnt loc;
00197         loc.SetX((double)Py::Float(tuple.getItem(0)));
00198         loc.SetY((double)Py::Float(tuple.getItem(1)));
00199         loc.SetZ((double)Py::Float(tuple.getItem(2)));
00200         Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
00201         ellipse->SetLocation(loc);
00202     }
00203     else {
00204         std::string error = std::string("type must be 'Vector', not ");
00205         error += p->ob_type->tp_name;
00206         throw Py::TypeError(error);
00207     }
00208 }
00209 
00210 Py::Object EllipsePy::getAxis(void) const
00211 {
00212     Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
00213     gp_Ax1 axis = ellipse->Axis();
00214     gp_Dir dir = axis.Direction();
00215     return Py::Vector(Base::Vector3d(dir.X(), dir.Y(), dir.Z()));
00216 }
00217 
00218 void EllipsePy::setAxis(Py::Object arg)
00219 {
00220     PyObject* p = arg.ptr();
00221     Base::Vector3d val;
00222     if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
00223         val = static_cast<Base::VectorPy*>(p)->value();
00224     }
00225     else if (PyTuple_Check(p)) {
00226         val = Base::getVectorFromTuple<double>(p);
00227     }
00228     else {
00229         std::string error = std::string("type must be 'Vector', not ");
00230         error += p->ob_type->tp_name;
00231         throw Py::TypeError(error);
00232     }
00233 
00234     Handle_Geom_Ellipse ellipse = Handle_Geom_Ellipse::DownCast(getGeomEllipsePtr()->handle());
00235     try {
00236         gp_Ax1 axis;
00237         axis.SetLocation(ellipse->Location());
00238         axis.SetDirection(gp_Dir(val.x, val.y, val.z));
00239         ellipse->SetAxis(axis);
00240     }
00241     catch (Standard_Failure) {
00242         throw Py::Exception("cannot set axis");
00243     }
00244 }
00245 
00246 PyObject *EllipsePy::getCustomAttributes(const char* /*attr*/) const
00247 {
00248     return 0;
00249 }
00250 
00251 int EllipsePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00252 {
00253     return 0; 
00254 }

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