PlanePyImp.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_Ax1.hxx>
00027 # include <gp_Dir.hxx>
00028 # include <gp_Pnt.hxx>
00029 # include <gp_Lin.hxx>
00030 # include <gp_Pln.hxx>
00031 # include <Geom_Line.hxx>
00032 # include <Geom_Plane.hxx>
00033 # include <Geom_TrimmedCurve.hxx>
00034 # include <GC_MakePlane.hxx>
00035 # include <Standard_Failure.hxx>
00036 #endif
00037 
00038 #include <Base/VectorPy.h>
00039 #include <Base/GeometryPyCXX.h>
00040 
00041 #include "Geometry.h"
00042 #include "LinePy.h"
00043 #include "PlanePy.h"
00044 #include "PlanePy.cpp"
00045 
00046 using namespace Part;
00047 
00048 extern const char* gce_ErrorStatusText(gce_ErrorType et);
00049 
00050 // returns a string which represents the object e.g. when printed in python
00051 std::string PlanePy::representation(void) const
00052 {
00053     return "<Plane object>";
00054 }
00055 
00056 PyObject *PlanePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00057 {
00058     // create a new instance of PlanePy and the Twin object 
00059     return new PlanePy(new GeomPlane);
00060 }
00061 
00062 // constructor method
00063 int PlanePy::PyInit(PyObject* args, PyObject* kwds)
00064 {
00065     // plane and distance for offset
00066     PyObject *pPlane;
00067     double dist;
00068     static char* keywords_pd[] = {"Plane","Distance",NULL};
00069     if (PyArg_ParseTupleAndKeywords(args, kwds, "O!d", keywords_pd, &(PlanePy::Type), &pPlane, &dist)) {
00070         PlanePy* pcPlane = static_cast<PlanePy*>(pPlane);
00071         Handle_Geom_Plane plane = Handle_Geom_Plane::DownCast
00072             (pcPlane->getGeometryPtr()->handle());
00073         GC_MakePlane mc(plane->Pln(), dist);
00074         if (!mc.IsDone()) {
00075             PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(mc.Status()));
00076             return -1;
00077         }
00078 
00079         Handle_Geom_Plane plan = Handle_Geom_Plane::DownCast(getGeometryPtr()->handle());
00080         plan->SetPln(mc.Value()->Pln());
00081         return 0;
00082     }
00083 
00084     // plane from equation
00085     double a,b,c,d;
00086     static char* keywords_abcd[] = {"A","B","C","D",NULL};
00087     PyErr_Clear();
00088     if (PyArg_ParseTupleAndKeywords(args, kwds, "dddd", keywords_abcd,
00089                                         &a,&b,&c,&d)) {
00090         GC_MakePlane mc(a,b,c,d);
00091         if (!mc.IsDone()) {
00092             PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(mc.Status()));
00093             return -1;
00094         }
00095 
00096         Handle_Geom_Plane plane = Handle_Geom_Plane::DownCast(getGeometryPtr()->handle());
00097         plane->SetPln(mc.Value()->Pln());
00098         return 0;
00099     }
00100 
00101     PyObject *pV1, *pV2, *pV3;
00102     static char* keywords_ppp[] = {"Point1","Point2","Point3",NULL};
00103     PyErr_Clear();
00104     if (PyArg_ParseTupleAndKeywords(args, kwds, "O!O!O!", keywords_ppp,
00105                                          &(Base::VectorPy::Type), &pV1,
00106                                          &(Base::VectorPy::Type), &pV2,
00107                                          &(Base::VectorPy::Type), &pV3)) {
00108         Base::Vector3d v1 = static_cast<Base::VectorPy*>(pV1)->value();
00109         Base::Vector3d v2 = static_cast<Base::VectorPy*>(pV2)->value();
00110         Base::Vector3d v3 = static_cast<Base::VectorPy*>(pV3)->value();
00111         GC_MakePlane mc(gp_Pnt(v1.x,v1.y,v1.z),
00112                         gp_Pnt(v2.x,v2.y,v2.z),
00113                         gp_Pnt(v3.x,v3.y,v3.z));
00114         if (!mc.IsDone()) {
00115             PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(mc.Status()));
00116             return -1;
00117         }
00118 
00119         Handle_Geom_Plane plane = Handle_Geom_Plane::DownCast(getGeometryPtr()->handle());
00120         plane->SetPln(mc.Value()->Pln());
00121         return 0;
00122     }
00123 
00124     // location and normal
00125     static char* keywords_cnr[] = {"Location","Normal",NULL};
00126     PyErr_Clear();
00127     if (PyArg_ParseTupleAndKeywords(args, kwds, "O!O!", keywords_cnr,
00128                                         &(Base::VectorPy::Type), &pV1,
00129                                         &(Base::VectorPy::Type), &pV2)) {
00130         Base::Vector3d v1 = static_cast<Base::VectorPy*>(pV1)->value();
00131         Base::Vector3d v2 = static_cast<Base::VectorPy*>(pV2)->value();
00132         GC_MakePlane mc(gp_Pnt(v1.x,v1.y,v1.z),
00133                         gp_Dir(v2.x,v2.y,v2.z));
00134         if (!mc.IsDone()) {
00135             PyErr_SetString(PyExc_Exception, gce_ErrorStatusText(mc.Status()));
00136             return -1;
00137         }
00138 
00139         Handle_Geom_Plane plane = Handle_Geom_Plane::DownCast(getGeometryPtr()->handle());
00140         plane->SetPln(mc.Value()->Pln());
00141         return 0;
00142     }
00143 
00144     static char* keywords_p[] = {"Plane",NULL};
00145     PyErr_Clear();
00146     if (PyArg_ParseTupleAndKeywords(args, kwds, "O!", keywords_p, &(PlanePy::Type), &pPlane)) {
00147         PlanePy* pcPlane = static_cast<PlanePy*>(pPlane);
00148         Handle_Geom_Plane plane1 = Handle_Geom_Plane::DownCast
00149             (pcPlane->getGeometryPtr()->handle());
00150         Handle_Geom_Plane plane2 = Handle_Geom_Plane::DownCast
00151             (this->getGeometryPtr()->handle());
00152         plane2->SetPln(plane1->Pln());
00153         return 0;
00154     }
00155 
00156     static char* keywords_n[] = {NULL};
00157     PyErr_Clear();
00158     if (PyArg_ParseTupleAndKeywords(args, kwds, "", keywords_n)) {
00159         // do nothing
00160         return 0;
00161     }
00162 
00163     PyErr_SetString(PyExc_TypeError, "Plane constructor accepts:\n"
00164         "-- empty parameter list\n"
00165         "-- Plane\n"
00166         "-- Plane, Distance\n"
00167         "-- Location, Normal\n"
00168         "-- Point1, Point2, Point3\n"
00169         "-- A, B, C, D\n"
00170         "   (as equation: Ax + By + Cz + D = 0.0)");
00171     return -1;
00172 }
00173 
00174 Py::Object PlanePy::getPosition(void) const
00175 {
00176     Handle_Geom_Plane this_surf = Handle_Geom_Plane::DownCast
00177         (this->getGeomPlanePtr()->handle());
00178     gp_Pnt pnt = this_surf->Location();
00179     return Py::Vector(Base::Vector3d(pnt.X(), pnt.Y(), pnt.Z()));
00180 }
00181 
00182 void PlanePy::setPosition(Py::Object arg)
00183 {
00184     gp_Pnt loc;
00185     PyObject *p = arg.ptr();
00186     if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
00187         Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
00188         loc.SetX(v.x);
00189         loc.SetY(v.y);
00190         loc.SetZ(v.z);
00191     }
00192     else if (PyTuple_Check(p)) {
00193         Py::Tuple tuple(arg);
00194         loc.SetX((double)Py::Float(tuple.getItem(0)));
00195         loc.SetY((double)Py::Float(tuple.getItem(1)));
00196         loc.SetZ((double)Py::Float(tuple.getItem(2)));
00197     }
00198     else {
00199         std::string error = std::string("type must be 'Vector' or tuple, not ");
00200         error += p->ob_type->tp_name;
00201         throw Py::TypeError(error);
00202     }
00203 
00204     try {
00205         Handle_Geom_Plane this_surf = Handle_Geom_Plane::DownCast
00206             (this->getGeomPlanePtr()->handle());
00207         this_surf->SetLocation(loc);
00208     }
00209     catch (Standard_Failure) {
00210         Handle_Standard_Failure e = Standard_Failure::Caught();
00211         throw Py::Exception(e->GetMessageString());
00212     }
00213 }
00214 
00215 Py::Object PlanePy::getAxis(void) const
00216 {
00217     Handle_Geom_ElementarySurface s = Handle_Geom_ElementarySurface::DownCast
00218         (getGeometryPtr()->handle());
00219     gp_Dir dir = s->Axis().Direction();
00220     return Py::Vector(Base::Vector3d(dir.X(), dir.Y(), dir.Z()));
00221 }
00222 
00223 void PlanePy::setAxis(Py::Object arg)
00224 {
00225     Standard_Real dir_x, dir_y, dir_z;
00226     PyObject *p = arg.ptr();
00227     if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) {
00228         Base::Vector3d v = static_cast<Base::VectorPy*>(p)->value();
00229         dir_x = v.x;
00230         dir_y = v.y;
00231         dir_z = v.z;
00232     }
00233     else if (PyTuple_Check(p)) {
00234         Py::Tuple tuple(arg);
00235         dir_x = (double)Py::Float(tuple.getItem(0));
00236         dir_y = (double)Py::Float(tuple.getItem(1));
00237         dir_z = (double)Py::Float(tuple.getItem(2));
00238     }
00239     else {
00240         std::string error = std::string("type must be 'Vector' or tuple, not ");
00241         error += p->ob_type->tp_name;
00242         throw Py::TypeError(error);
00243     }
00244 
00245     try {
00246         Handle_Geom_ElementarySurface this_surf = Handle_Geom_ElementarySurface::DownCast
00247             (this->getGeometryPtr()->handle());
00248         gp_Ax1 axis;
00249         axis.SetLocation(this_surf->Location());
00250         axis.SetDirection(gp_Dir(dir_x, dir_y, dir_z));
00251         this_surf->SetAxis(axis);
00252     }
00253     catch (Standard_Failure) {
00254         throw Py::Exception("cannot set axis");
00255     }
00256 }
00257 
00258 PyObject* PlanePy::uIso(PyObject * args)
00259 {
00260     double u;
00261     if (!PyArg_ParseTuple(args, "d", &u))
00262         return 0;
00263 
00264     try {
00265         Handle_Geom_Plane plane = Handle_Geom_Plane::DownCast
00266             (getGeomPlanePtr()->handle());
00267         Handle_Geom_Line c = Handle_Geom_Line::DownCast(plane->UIso(u));
00268         GeomLineSegment* line = new GeomLineSegment();
00269         Handle_Geom_TrimmedCurve this_curv = Handle_Geom_TrimmedCurve::DownCast
00270             (line->handle());
00271         Handle_Geom_Line this_line = Handle_Geom_Line::DownCast
00272             (this_curv->BasisCurve());
00273         this_line->SetLin(c->Lin());
00274         return new LinePy(line);
00275     }
00276     catch (Standard_Failure) {
00277         Handle_Standard_Failure e = Standard_Failure::Caught();
00278         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00279         return 0;
00280     }
00281 }
00282 
00283 PyObject* PlanePy::vIso(PyObject * args)
00284 {
00285     double v;
00286     if (!PyArg_ParseTuple(args, "d", &v))
00287         return 0;
00288 
00289     try {
00290         Handle_Geom_Plane plane = Handle_Geom_Plane::DownCast
00291             (getGeomPlanePtr()->handle());
00292         Handle_Geom_Line c = Handle_Geom_Line::DownCast(plane->VIso(v));
00293         GeomLineSegment* line = new GeomLineSegment();
00294         Handle_Geom_TrimmedCurve this_curv = Handle_Geom_TrimmedCurve::DownCast
00295             (line->handle());
00296         Handle_Geom_Line this_line = Handle_Geom_Line::DownCast
00297             (this_curv->BasisCurve());
00298         this_line->SetLin(c->Lin());
00299         return new LinePy(line);
00300     }
00301     catch (Standard_Failure) {
00302         Handle_Standard_Failure e = Standard_Failure::Caught();
00303         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00304         return 0;
00305     }
00306 }
00307 
00308 PyObject *PlanePy::getCustomAttributes(const char* /*attr*/) const
00309 {
00310     return 0;
00311 }
00312 
00313 int PlanePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00314 {
00315     return 0; 
00316 }

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