PlacementPyImp.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 
00026 #include "Placement.h"
00027 #include "GeometryPyCXX.h"
00028 
00029 // inclusion of the generated files (generated out of PlacementPy.xml)
00030 #include "PlacementPy.h"
00031 #include "PlacementPy.cpp"
00032 #include "Matrix.h"
00033 #include "MatrixPy.h"
00034 #include "RotationPy.h"
00035 #include "VectorPy.h"
00036 
00037 using namespace Base;
00038 
00039 // returns a string which represents the object e.g. when printed in python
00040 std::string PlacementPy::representation(void) const
00041 {
00042     double A,B,C;
00043     PlacementPy::PointerType ptr = reinterpret_cast<PlacementPy::PointerType>(_pcTwinPointer);
00044     std::stringstream str;
00045     ptr->getRotation().getYawPitchRoll(A,B,C);
00046 
00047     str << "Placement [Pos=(";
00048     str << ptr->getPosition().x << ","<< ptr->getPosition().y << "," << ptr->getPosition().z;
00049     str << "), Yaw-Pitch-Roll=(" << A << "," << B << "," << C << ")]";
00050 
00051     return str.str();
00052 }
00053 
00054 PyObject *PlacementPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00055 {
00056     // create a new instance of PlacementPy and the Twin object 
00057     return new PlacementPy(new Placement);
00058 }
00059 
00060 // constructor method
00061 int PlacementPy::PyInit(PyObject* args, PyObject* /*kwd*/)
00062 {
00063     PyObject* o;
00064     if (PyArg_ParseTuple(args, "")) {
00065         return 0;
00066     }
00067 
00068     PyErr_Clear();
00069     if (PyArg_ParseTuple(args, "O!", &(Base::MatrixPy::Type), &o)) {
00070         Base::Matrix4D mat = static_cast<Base::MatrixPy*>(o)->value();
00071         getPlacementPtr()->fromMatrix(mat);
00072         return 0;
00073     }
00074 
00075     PyErr_Clear();
00076     if (PyArg_ParseTuple(args, "O!", &(Base::PlacementPy::Type), &o)) {
00077         Base::Placement *plm = static_cast<Base::PlacementPy*>(o)->getPlacementPtr();
00078         *(getPlacementPtr()) = *plm;
00079         return 0;
00080     }
00081 
00082     PyErr_Clear();
00083     PyObject* d;
00084     double angle;
00085     if (PyArg_ParseTuple(args, "O!O!d", &(Base::VectorPy::Type), &o,
00086                                         &(Base::VectorPy::Type), &d, &angle)) {
00087         // NOTE: The first parameter defines the translation, the second the rotation axis
00088         // and the last parameter defines the rotation angle in degree.
00089         Base::Rotation rot(static_cast<Base::VectorPy*>(d)->value(), angle/180.0*D_PI);
00090         *getPlacementPtr() = Base::Placement(static_cast<Base::VectorPy*>(o)->value(),rot);
00091         return 0;
00092     }
00093 
00094     PyErr_Clear();
00095     if (PyArg_ParseTuple(args, "O!O!", &(Base::VectorPy::Type), &o,
00096                                        &(Base::RotationPy::Type), &d)) {
00097         Base::Vector3d *pos = static_cast<Base::VectorPy*>(o)->getVectorPtr();
00098         getPlacementPtr()->setPosition(*pos);
00099         Base::Rotation *rot = static_cast<Base::RotationPy*>(d)->getRotationPtr();
00100         getPlacementPtr()->setRotation(*rot);
00101         return 0;
00102     }
00103 
00104     PyErr_Clear();
00105     PyObject* c;
00106     if (PyArg_ParseTuple(args, "O!O!O!", &(Base::VectorPy::Type), &o,
00107                                          &(Base::RotationPy::Type), &d,
00108                                          &(Base::VectorPy::Type), &c)) {
00109         Base::Vector3d *pos = static_cast<Base::VectorPy*>(o)->getVectorPtr();
00110         Base::Rotation *rot = static_cast<Base::RotationPy*>(d)->getRotationPtr();
00111         Base::Vector3d *cnt = static_cast<Base::VectorPy*>(c)->getVectorPtr();
00112         Base::Placement p(*pos,*rot,*cnt);
00113         getPlacementPtr()->operator = (p);
00114         return 0;
00115     }
00116 
00117     PyErr_SetString(PyExc_Exception, "empty parameter list, matrix or placement expected");
00118     return -1;
00119 }
00120 
00121 PyObject* PlacementPy::move(PyObject * args)
00122 {
00123     PyObject *vec;
00124     if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &vec))
00125         return NULL;
00126     getPlacementPtr()->move(static_cast<VectorPy*>(vec)->value());
00127     Py_Return;
00128 }
00129 
00130 PyObject* PlacementPy::multiply(PyObject * args)
00131 {
00132     PyObject *plm;
00133     if (!PyArg_ParseTuple(args, "O!", &(PlacementPy::Type), &plm))
00134         return NULL;
00135     Placement mult = (*getPlacementPtr()) * (*static_cast<PlacementPy*>(plm)->getPlacementPtr());
00136     return new PlacementPy(new Placement(mult));
00137 }
00138 
00139 PyObject* PlacementPy::multVec(PyObject * args)
00140 {
00141     PyObject *vec;
00142     if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &vec))
00143         return NULL;
00144     Base::Vector3d pnt(static_cast<VectorPy*>(vec)->value());
00145     getPlacementPtr()->multVec(pnt, pnt);
00146     return new VectorPy(new Vector3d(pnt));
00147 }
00148 
00149 PyObject* PlacementPy::copy(PyObject * args)
00150 {
00151     if (!PyArg_ParseTuple(args, ""))
00152         return NULL;
00153     return new PlacementPy(new Placement(*getPlacementPtr()));
00154 }
00155 
00156 PyObject* PlacementPy::toMatrix(PyObject * args)
00157 {
00158     if (!PyArg_ParseTuple(args, ""))
00159         return NULL;
00160     Base::Matrix4D mat = getPlacementPtr()->toMatrix();
00161     return new MatrixPy(new Matrix4D(mat));
00162 }
00163 
00164 PyObject* PlacementPy::inverse(PyObject * args)
00165 {
00166     if (!PyArg_ParseTuple(args, ""))
00167         return NULL;
00168     Base::Placement p = getPlacementPtr()->inverse();
00169     return new PlacementPy(new Placement(p));
00170 }
00171 
00172 Py::Object PlacementPy::getBase(void) const
00173 {
00174     return Py::Vector(getPlacementPtr()->getPosition());
00175 }
00176 
00177 void PlacementPy::setBase(Py::Object arg)
00178 {
00179     getPlacementPtr()->setPosition(Py::Vector(arg).toVector());
00180 }
00181 
00182 Py::Object PlacementPy::getRotation(void) const
00183 {
00184     return Py::Rotation(getPlacementPtr()->getRotation());
00185 }
00186 
00187 void PlacementPy::setRotation(Py::Object arg)
00188 {
00189     Py::Rotation rot;
00190     if (rot.accepts(arg.ptr())) {
00191         getPlacementPtr()->setRotation((Base::Rotation)Py::Rotation(arg));
00192         return;
00193     }
00194     Py::Tuple tuple;
00195     if (tuple.accepts(arg.ptr())) {
00196         tuple = arg;
00197         getPlacementPtr()->setRotation(Base::Rotation((double)Py::Float(tuple[0]),
00198                                                       (double)Py::Float(tuple[1]),
00199                                                       (double)Py::Float(tuple[2]),
00200                                                       (double)Py::Float(tuple[3])
00201                                                      ));
00202         return;
00203     }
00204 
00205     throw Py::TypeError("either Rotation or tuple of four floats expected");
00206 }
00207 
00208 PyObject *PlacementPy::getCustomAttributes(const char* /*attr*/) const
00209 {
00210     return 0;
00211 }
00212 
00213 int PlacementPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00214 {
00215     return 0; 
00216 }

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