ArcOfCirclePyImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2011 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 <Geom_Circle.hxx>
00028 # include <GC_MakeArcOfCircle.hxx>
00029 # include <GC_MakeCircle.hxx>
00030 # include <Geom_TrimmedCurve.hxx>
00031 #endif
00032 
00033 #include "Mod/Part/App/Geometry.h"
00034 #include "ArcOfCirclePy.h"
00035 #include "ArcOfCirclePy.cpp"
00036 #include "CirclePy.h"
00037 
00038 #include <Base/GeometryPyCXX.h>
00039 #include <Base/VectorPy.h>
00040 
00041 using namespace Part;
00042 
00043 extern const char* gce_ErrorStatusText(gce_ErrorType et);
00044 
00045 // returns a string which represents the object e.g. when printed in python
00046 std::string ArcOfCirclePy::representation(void) const
00047 {
00048     Handle_Geom_TrimmedCurve trim = Handle_Geom_TrimmedCurve::DownCast
00049         (getGeomArcOfCirclePtr()->handle());
00050     Handle_Geom_Circle circle = Handle_Geom_Circle::DownCast(trim->BasisCurve());
00051 
00052     gp_Ax1 axis = circle->Axis();
00053     gp_Dir dir = axis.Direction();
00054     gp_Pnt loc = axis.Location();
00055     Standard_Real fRad = circle->Radius();
00056     Standard_Real u1 = trim->FirstParameter();
00057     Standard_Real u2 = trim->LastParameter();
00058 
00059     std::stringstream str;
00060     str << "ArcOfCircle (";
00061     str << "Radius : " << fRad << ", "; 
00062     str << "Position : (" << loc.X() << ", "<< loc.Y() << ", "<< loc.Z() << "), "; 
00063     str << "Direction : (" << dir.X() << ", "<< dir.Y() << ", "<< dir.Z() << "), "; 
00064     str << "Parameter : (" << u1 << ", " << u2 << ")"; 
00065     str << ")";
00066 
00067     return str.str();
00068 }
00069 
00070 PyObject *ArcOfCirclePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00071 {
00072     // create a new instance of ArcOfCirclePy and the Twin object 
00073     return new ArcOfCirclePy(new GeomArcOfCircle);
00074 }
00075 
00076 // constructor method
00077 int ArcOfCirclePy::PyInit(PyObject* args, PyObject* kwds)
00078 {
00079     PyObject* o;
00080     double u1, u2;
00081     int sense=1;
00082     if (PyArg_ParseTuple(args, "O!dd|i", &(Part::CirclePy::Type), &o, &u1, &u2, &sense)) {
00083         try {
00084             Handle_Geom_Circle circle = Handle_Geom_Circle::DownCast
00085                 (static_cast<CirclePy*>(o)->getGeomCirclePtr()->handle());
00086             GC_MakeArcOfCircle arc(circle->Circ(), u1, u2, sense);
00087             if (!arc.IsDone()) {
00088                 PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(arc.Status()));
00089                 return -1;
00090             }
00091 
00092             getGeomArcOfCirclePtr()->setHandle(arc.Value());
00093             return 0;
00094         }
00095         catch (Standard_Failure) {
00096             Handle_Standard_Failure e = Standard_Failure::Caught();
00097             PyErr_SetString(PyExc_Exception, e->GetMessageString());
00098             return -1;
00099         }
00100         catch (...) {
00101             PyErr_SetString(PyExc_Exception, "creation of arc failed");
00102             return -1;
00103         }
00104     }
00105 
00106     PyErr_Clear();
00107     PyObject *pV1, *pV2, *pV3;
00108     if (PyArg_ParseTuple(args, "O!O!O!", &(Base::VectorPy::Type), &pV1,
00109                                          &(Base::VectorPy::Type), &pV2,
00110                                          &(Base::VectorPy::Type), &pV3)) {
00111         Base::Vector3d v1 = static_cast<Base::VectorPy*>(pV1)->value();
00112         Base::Vector3d v2 = static_cast<Base::VectorPy*>(pV2)->value();
00113         Base::Vector3d v3 = static_cast<Base::VectorPy*>(pV3)->value();
00114 
00115         GC_MakeArcOfCircle arc(gp_Pnt(v1.x,v1.y,v1.z),
00116                                gp_Pnt(v2.x,v2.y,v2.z),
00117                                gp_Pnt(v3.x,v3.y,v3.z));
00118         if (!arc.IsDone()) {
00119             PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(arc.Status()));
00120             return -1;
00121         }
00122 
00123         getGeomArcOfCirclePtr()->setHandle(arc.Value());
00124         return 0;
00125     }
00126 
00127     // All checks failed
00128     PyErr_SetString(PyExc_TypeError,
00129         "ArcOfCircle constructor expects a circle curve and a parameter range or three points");
00130     return -1;
00131 }
00132 
00133 Py::Float ArcOfCirclePy::getRadius(void) const
00134 {
00135     return Py::Float(getGeomArcOfCirclePtr()->getRadius()); 
00136 }
00137 
00138 void  ArcOfCirclePy::setRadius(Py::Float arg)
00139 {
00140     getGeomArcOfCirclePtr()->setRadius((double)arg);
00141 }
00142 
00143 Py::Object ArcOfCirclePy::getCenter(void) const
00144 {
00145     return Py::Vector(getGeomArcOfCirclePtr()->getCenter());
00146 }
00147 
00148 void  ArcOfCirclePy::setCenter(Py::Object arg)
00149 {
00150     PyObject* p = arg.ptr();
00151     if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
00152         Base::Vector3d loc = static_cast<Base::VectorPy*>(p)->value();
00153         getGeomArcOfCirclePtr()->setCenter(loc);
00154     }
00155     else if (PyObject_TypeCheck(p, &PyTuple_Type)) {
00156         Base::Vector3d loc = Base::getVectorFromTuple<double>(p);
00157         getGeomArcOfCirclePtr()->setCenter(loc);
00158     }
00159     else {
00160         std::string error = std::string("type must be 'Vector', not ");
00161         error += p->ob_type->tp_name;
00162         throw Py::TypeError(error);
00163     }
00164 }
00165 
00166 Py::Object ArcOfCirclePy::getAxis(void) const
00167 {
00168     Handle_Geom_TrimmedCurve trim = Handle_Geom_TrimmedCurve::DownCast
00169         (getGeomArcOfCirclePtr()->handle());
00170     Handle_Geom_Circle circle = Handle_Geom_Circle::DownCast(trim->BasisCurve());
00171     gp_Ax1 axis = circle->Axis();
00172     gp_Dir dir = axis.Direction();
00173     return Py::Vector(Base::Vector3d(dir.X(), dir.Y(), dir.Z()));
00174 }
00175 
00176 void  ArcOfCirclePy::setAxis(Py::Object arg)
00177 {
00178     PyObject* p = arg.ptr();
00179     Base::Vector3d val;
00180     if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
00181         val = static_cast<Base::VectorPy*>(p)->value();
00182     }
00183     else if (PyTuple_Check(p)) {
00184         val = Base::getVectorFromTuple<double>(p);
00185     }
00186     else {
00187         std::string error = std::string("type must be 'Vector', not ");
00188         error += p->ob_type->tp_name;
00189         throw Py::TypeError(error);
00190     }
00191 
00192     Handle_Geom_TrimmedCurve trim = Handle_Geom_TrimmedCurve::DownCast
00193         (getGeomArcOfCirclePtr()->handle());
00194     Handle_Geom_Circle circle = Handle_Geom_Circle::DownCast(trim->BasisCurve());
00195     try {
00196         gp_Ax1 axis;
00197         axis.SetLocation(circle->Location());
00198         axis.SetDirection(gp_Dir(val.x, val.y, val.z));
00199         circle->SetAxis(axis);
00200     }
00201     catch (Standard_Failure) {
00202         throw Py::Exception("cannot set axis");
00203     }
00204 }
00205 
00206 Py::Object ArcOfCirclePy::getCircle(void) const
00207 {
00208     Handle_Geom_TrimmedCurve trim = Handle_Geom_TrimmedCurve::DownCast
00209         (getGeomArcOfCirclePtr()->handle());
00210     Handle_Geom_Circle circle = Handle_Geom_Circle::DownCast(trim->BasisCurve());
00211     return Py::Object(new CirclePy(new GeomCircle(circle)), true);
00212 }
00213 
00214 PyObject *ArcOfCirclePy::getCustomAttributes(const char* attr) const
00215 {
00216     return 0;
00217 }
00218 
00219 int ArcOfCirclePy::setCustomAttributes(const char* attr, PyObject *obj)
00220 {
00221     return 0; 
00222 }

Generated on Wed Nov 23 18:59:57 2011 for FreeCAD by  doxygen 1.6.1