PointsPyImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2008 Juergen Riegel <juergen.riegel@web.de>             *
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 "Mod/Points/App/Points.h"
00027 #include <Base/Builder3D.h>
00028 #include <Base/VectorPy.h>
00029 #include <Base/GeometryPyCXX.h>
00030 
00031 // inclusion of the generated files (generated out of PointsPy.xml)
00032 #include "PointsPy.h"
00033 #include "PointsPy.cpp"
00034 
00035 using namespace Points;
00036 
00037 // returns a string which represents the object e.g. when printed in python
00038 std::string PointsPy::representation(void) const
00039 {
00040     return std::string("<PointKernel object>");
00041 }
00042 
00043 PyObject *PointsPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00044 {
00045     // create a new instance of PointsPy and the Twin object 
00046     return new PointsPy(new PointKernel);
00047 }
00048 
00049 // constructor method
00050 int PointsPy::PyInit(PyObject* args, PyObject* /*kwd*/)
00051 {
00052     PyObject *pcObj=0;
00053     if (!PyArg_ParseTuple(args, "|O", &pcObj))     // convert args: Python->C 
00054         return -1;                             // NULL triggers exception
00055 
00056     // if no mesh is given
00057     if (!pcObj) return 0;
00058     if (PyObject_TypeCheck(pcObj, &(PointsPy::Type))) {
00059         *getPointKernelPtr() = *(static_cast<PointsPy*>(pcObj)->getPointKernelPtr());
00060     }
00061     else if (PyList_Check(pcObj)) {
00062         addPoints(args);
00063     }
00064     else if (PyTuple_Check(pcObj)) {
00065         addPoints(args);
00066     }
00067     else if (PyString_Check(pcObj)) {
00068         getPointKernelPtr()->load(PyString_AsString(pcObj));
00069     }
00070 
00071     return 0;
00072 }
00073 
00074 PyObject* PointsPy::copy(PyObject *args)
00075 {
00076     if (!PyArg_ParseTuple(args, ""))
00077         return NULL;
00078 
00079     PointKernel* kernel = new PointKernel();
00080     // assign data
00081     *kernel = *getPointKernelPtr();
00082     return new PointsPy(kernel);
00083 }
00084 
00085 PyObject* PointsPy::read(PyObject * args)
00086 {
00087     const char* Name;
00088     if (!PyArg_ParseTuple(args, "s",&Name))
00089         return NULL;                         
00090 
00091     PY_TRY {
00092         getPointKernelPtr()->load(Name);
00093     } PY_CATCH;
00094     
00095     Py_Return; 
00096 }
00097 
00098 PyObject* PointsPy::write(PyObject * args)
00099 {
00100     const char* Name;
00101     if (!PyArg_ParseTuple(args, "s",&Name))
00102         return NULL;                         
00103 
00104     PY_TRY {
00105         getPointKernelPtr()->save(Name);
00106     } PY_CATCH;
00107     
00108     Py_Return; 
00109 }
00110 
00111 PyObject* PointsPy::writeInventor(PyObject * args)
00112 {
00113     if (!PyArg_ParseTuple(args, ""))
00114         return NULL;
00115 
00116     std::stringstream result;
00117     Base::InventorBuilder builder(result);
00118     builder.beginPoints();
00119     PointKernel* kernel = getPointKernelPtr();
00120     for (Points::PointKernel::const_iterator it = kernel->begin(); it != kernel->end(); ++it)
00121         builder.addPoint((float)it->x,(float)it->y,(float)it->z);
00122     builder.endPoints();
00123     builder.close();
00124 
00125     return Py::new_reference_to(Py::String(result.str()));
00126 }
00127 
00128 PyObject* PointsPy::addPoints(PyObject * args)
00129 {
00130     PyObject *obj;
00131     if (!PyArg_ParseTuple(args, "O!", &PyList_Type, &obj))
00132         return 0;
00133 
00134     Py::List list(obj);
00135     union PyType_Object pyType = {&(Base::VectorPy::Type)};
00136     Py::Type vType(pyType.o);
00137 
00138     try {
00139         for (Py::List::iterator it = list.begin(); it != list.end(); ++it) {
00140             if ((*it).isType(vType)) {
00141                 Py::Vector p(*it);
00142                 getPointKernelPtr()->push_back(p.toVector());
00143             }
00144             else {
00145                 Base::Vector3d pnt;
00146                 Py::Tuple tuple(*it);
00147                 pnt.x = (double)Py::Float(tuple[0]);
00148                 pnt.y = (double)Py::Float(tuple[1]);
00149                 pnt.z = (double)Py::Float(tuple[2]);
00150                 getPointKernelPtr()->push_back(pnt);
00151             }
00152         }
00153     }
00154     catch (const Py::Exception&) {
00155         PyErr_SetString(PyExc_Exception, "either expect\n"
00156             "-- [Vector,...] \n"
00157             "-- [(x,y,z),...]");
00158         return 0;
00159     }
00160 
00161     Py_Return;
00162 }
00163 
00164 Py::Int PointsPy::getCountPoints(void) const
00165 {
00166     return Py::Int((long)getPointKernelPtr()->size());
00167 }
00168 
00169 Py::List PointsPy::getPoints(void) const
00170 {
00171     Py::List PointList;
00172     const PointKernel* points = getPointKernelPtr();
00173     for (PointKernel::const_point_iterator it = points->begin(); it != points->end(); ++it) {
00174         PointList.append(Py::Object(new Base::VectorPy(*it)));
00175     }
00176     return PointList;
00177 }
00178 
00179 PyObject *PointsPy::getCustomAttributes(const char* /*attr*/) const
00180 {
00181     return 0;
00182 }
00183 
00184 int PointsPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00185 {
00186     return 0; 
00187 }
00188 
00189 

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