HyperbolaPyImp.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_Hyperbola.hxx>
00027 # include <GC_MakeHyperbola.hxx>
00028 # include <gp_Hypr.hxx>
00029 #endif
00030 
00031 #include <Base/VectorPy.h>
00032 #include <Base/GeometryPyCXX.h>
00033 
00034 #include "Geometry.h"
00035 #include "HyperbolaPy.h"
00036 #include "HyperbolaPy.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 HyperbolaPy::representation(void) const
00044 {
00045     return "<Hyperbola object>";
00046 }
00047 
00048 PyObject *HyperbolaPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00049 {
00050     // create a new instance of HyperbolaPy and the Twin object 
00051     return new HyperbolaPy(new GeomHyperbola);
00052 }
00053 
00054 // constructor method
00055 int HyperbolaPy::PyInit(PyObject* args, PyObject* kwds)
00056 {
00057     char* keywords_n[] = {NULL};
00058     if (PyArg_ParseTupleAndKeywords(args, kwds, "", keywords_n)) {
00059         Handle_Geom_Hyperbola hypr = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
00060         hypr->SetMajorRadius(2.0);
00061         hypr->SetMinorRadius(1.0);
00062         return 0;
00063     }
00064 
00065     char* keywords_e[] = {"Hyperbola",NULL};
00066     PyErr_Clear();
00067     PyObject *pHypr;
00068     if (PyArg_ParseTupleAndKeywords(args, kwds, "O!",keywords_e, &(HyperbolaPy::Type), &pHypr)) {
00069         HyperbolaPy* pH = static_cast<HyperbolaPy*>(pHypr);
00070         Handle_Geom_Hyperbola Hypr1 = Handle_Geom_Hyperbola::DownCast
00071             (pH->getGeometryPtr()->handle());
00072         Handle_Geom_Hyperbola Hypr2 = Handle_Geom_Hyperbola::DownCast
00073             (this->getGeometryPtr()->handle());
00074         Hypr2->SetHypr(Hypr1->Hypr());
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_MakeHyperbola mh(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 (!mh.IsDone()) {
00092             PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(mh.Status()));
00093             return -1;
00094         }
00095 
00096         Handle_Geom_Hyperbola hyperb = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
00097         hyperb->SetHypr(mh.Value()->Hypr());
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_MakeHyperbola mh(gp_Ax2(gp_Pnt(c.x,c.y,c.z), gp_Dir(0.0,0.0,1.0)),
00110                           major, minor);
00111         if (!mh.IsDone()) {
00112             PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(mh.Status()));
00113             return -1;
00114         }
00115 
00116         Handle_Geom_Hyperbola hyperb = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
00117         hyperb->SetHypr(mh.Value()->Hypr());
00118         return 0;
00119     }
00120 
00121     PyErr_SetString(PyExc_TypeError, "Hyperbola constructor accepts:\n"
00122         "-- empty parameter list\n"
00123         "-- Hyperbola\n"
00124         "-- Point, double, double\n"
00125         "-- Point, Point, Point");
00126     return -1;
00127 }
00128 
00129 Py::Float HyperbolaPy::getEccentricity(void) const
00130 {
00131     Handle_Geom_Hyperbola curve = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
00132     return Py::Float(curve->Eccentricity()); 
00133 }
00134 
00135 Py::Float HyperbolaPy::getFocal(void) const
00136 {
00137     Handle_Geom_Hyperbola curve = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
00138     return Py::Float(curve->Focal()); 
00139 }
00140 
00141 Py::Object HyperbolaPy::getFocus1(void) const
00142 {
00143     Handle_Geom_Hyperbola c = Handle_Geom_Hyperbola::DownCast
00144         (getGeometryPtr()->handle());
00145     gp_Pnt loc = c->Focus1();
00146     return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
00147 }
00148 
00149 Py::Object HyperbolaPy::getFocus2(void) const
00150 {
00151     Handle_Geom_Hyperbola c = Handle_Geom_Hyperbola::DownCast
00152         (getGeometryPtr()->handle());
00153     gp_Pnt loc = c->Focus2();
00154     return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
00155 }
00156 
00157 Py::Float HyperbolaPy::getParameter(void) const
00158 {
00159     Handle_Geom_Hyperbola curve = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
00160     return Py::Float(curve->Parameter()); 
00161 }
00162 
00163 Py::Float HyperbolaPy::getMajorRadius(void) const
00164 {
00165     Handle_Geom_Hyperbola curve = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
00166     return Py::Float(curve->MajorRadius()); 
00167 }
00168 
00169 void HyperbolaPy::setMajorRadius(Py::Float arg)
00170 {
00171     Handle_Geom_Hyperbola curve = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
00172     curve->SetMajorRadius((double)arg); 
00173 }
00174 
00175 Py::Float HyperbolaPy::getMinorRadius(void) const
00176 {
00177     Handle_Geom_Hyperbola curve = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
00178     return Py::Float(curve->MinorRadius()); 
00179 }
00180 
00181 void HyperbolaPy::setMinorRadius(Py::Float arg)
00182 {
00183     Handle_Geom_Hyperbola curve = Handle_Geom_Hyperbola::DownCast(getGeometryPtr()->handle());
00184     curve->SetMinorRadius((double)arg); 
00185 }
00186 
00187 Py::Object HyperbolaPy::getLocation(void) const
00188 {
00189     Handle_Geom_Hyperbola c = Handle_Geom_Hyperbola::DownCast
00190         (getGeometryPtr()->handle());
00191     gp_Pnt loc = c->Location();
00192     return Py::Vector(Base::Vector3d(loc.X(), loc.Y(), loc.Z()));
00193 }
00194 
00195 void HyperbolaPy::setLocation(Py::Object arg)
00196 {
00197     PyObject* p = arg.ptr();
00198     if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
00199         Base::Vector3d loc = static_cast<Base::VectorPy*>(p)->value();
00200         Handle_Geom_Hyperbola c = Handle_Geom_Hyperbola::DownCast
00201             (getGeometryPtr()->handle());
00202         c->SetLocation(gp_Pnt(loc.x, loc.y, loc.z));
00203     }
00204     else if (PyTuple_Check(p)) {
00205         gp_Pnt loc;
00206         Py::Tuple tuple(arg);
00207         loc.SetX((double)Py::Float(tuple.getItem(0)));
00208         loc.SetY((double)Py::Float(tuple.getItem(1)));
00209         loc.SetZ((double)Py::Float(tuple.getItem(2)));
00210         Handle_Geom_Hyperbola c = Handle_Geom_Hyperbola::DownCast
00211             (getGeometryPtr()->handle());
00212         c->SetLocation(loc);
00213     }
00214     else {
00215         std::string error = std::string("type must be 'Vector', not ");
00216         error += p->ob_type->tp_name;
00217         throw Py::TypeError(error);
00218     }
00219 }
00220 
00221 Py::Object HyperbolaPy::getAxis(void) const
00222 {
00223     Handle_Geom_Hyperbola c = Handle_Geom_Hyperbola::DownCast
00224         (getGeometryPtr()->handle());
00225     gp_Dir dir = c->Axis().Direction();
00226     return Py::Vector(Base::Vector3d(dir.X(), dir.Y(), dir.Z()));
00227 }
00228 
00229 void HyperbolaPy::setAxis(Py::Object arg)
00230 {
00231     Standard_Real dir_x, dir_y, dir_z;
00232     PyObject *p = arg.ptr();
00233     if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
00234         Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
00235         dir_x = v.x;
00236         dir_y = v.y;
00237         dir_z = v.z;
00238     }
00239     else if (PyTuple_Check(p)) {
00240         Py::Tuple tuple(arg);
00241         dir_x = (double)Py::Float(tuple.getItem(0));
00242         dir_y = (double)Py::Float(tuple.getItem(1));
00243         dir_z = (double)Py::Float(tuple.getItem(2));
00244     }
00245     else {
00246         std::string error = std::string("type must be 'Vector' or tuple, not ");
00247         error += p->ob_type->tp_name;
00248         throw Py::TypeError(error);
00249     }
00250 
00251     try {
00252         Handle_Geom_Hyperbola this_curv = Handle_Geom_Hyperbola::DownCast
00253             (this->getGeometryPtr()->handle());
00254         gp_Ax1 axis;
00255         axis.SetLocation(this_curv->Location());
00256         axis.SetDirection(gp_Dir(dir_x, dir_y, dir_z));
00257         this_curv->SetAxis(axis);
00258     }
00259     catch (Standard_Failure) {
00260         throw Py::Exception("cannot set axis");
00261     }
00262 }
00263 
00264 PyObject *HyperbolaPy::getCustomAttributes(const char* /*attr*/) const
00265 {
00266     return 0;
00267 }
00268 
00269 int HyperbolaPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00270 {
00271     return 0; 
00272 }
00273 
00274 

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