FacetPyImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2007 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 "Mesh.h"
00027 #include "Facet.h"
00028 #include "FacetPy.h"
00029 #include "FacetPy.cpp"
00030 
00031 #include <Base/VectorPy.h>
00032 
00033 using namespace Mesh;
00034 
00035 // returns a string which represent the object e.g. when printed in python
00036 std::string FacetPy::representation(void) const
00037 {
00038     FacetPy::PointerType ptr = getFacetPtr();
00039     std::stringstream str;
00040     str << "Facet (";
00041     if (ptr->isBound()) {
00042         str << "(" << ptr->_aclPoints[0].x << ", " << ptr->_aclPoints[0].y << ", " << ptr->_aclPoints[0].z << ", Idx=" << ptr->PIndex[0] << "), ";
00043         str << "(" << ptr->_aclPoints[1].x << ", " << ptr->_aclPoints[1].y << ", " << ptr->_aclPoints[1].z << ", Idx=" << ptr->PIndex[1] << "), ";
00044         str << "(" << ptr->_aclPoints[2].x << ", " << ptr->_aclPoints[2].y << ", " << ptr->_aclPoints[2].z << ", Idx=" << ptr->PIndex[2] << "), ";
00045         str << "Idx=" << ptr->Index << ", (" << ptr->NIndex[0] << ", " << ptr->NIndex[1] << ", " << ptr->NIndex[2] << ")";
00046     }
00047     else {
00048         str << "(" << ptr->_aclPoints[0].x << ", " << ptr->_aclPoints[0].y << ", " << ptr->_aclPoints[0].z << "), ";
00049         str << "(" << ptr->_aclPoints[1].x << ", " << ptr->_aclPoints[1].y << ", " << ptr->_aclPoints[1].z << "), ";
00050         str << "(" << ptr->_aclPoints[2].x << ", " << ptr->_aclPoints[2].y << ", " << ptr->_aclPoints[2].z << ")";
00051     }
00052     str << ")";
00053  
00054     return str.str();
00055 }
00056 
00057 PyObject *FacetPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00058 {
00059     // create a new instance of FacetPy and the Twin object 
00060     return new FacetPy(new Facet);
00061 }
00062 
00063 // constructor method
00064 int FacetPy::PyInit(PyObject* args, PyObject*k)
00065 {
00066     return 0;
00067 }
00068 
00069 PyObject*  FacetPy::unbound(PyObject *args)
00070 {
00071     if (!PyArg_ParseTuple(args, ""))
00072         return NULL;
00073     getFacetPtr()->Index = ULONG_MAX;
00074     getFacetPtr()->Mesh = 0;
00075     Py_Return;
00076 }
00077 
00078 Py::Int FacetPy::getIndex(void) const
00079 {
00080     return Py::Int((long) getFacetPtr()->Index);
00081 }
00082 
00083 Py::Boolean FacetPy::getBound(void) const
00084 {
00085     return Py::Boolean(getFacetPtr()->Index != UINT_MAX);
00086 }
00087 
00088 Py::Object FacetPy::getNormal(void) const
00089 {
00090     Base::VectorPy* normal = new Base::VectorPy(getFacetPtr()->GetNormal());
00091     normal->setConst();
00092     return Py::Object(normal,true);
00093 }
00094 
00095 PyObject*  FacetPy::intersect(PyObject *args)
00096 {
00097     PyObject* object;
00098     if (!PyArg_ParseTuple(args, "O!", &FacetPy::Type, &object))
00099         return NULL;
00100     FacetPy  *face = static_cast<FacetPy*>(object);
00101     FacetPy::PointerType face_ptr = face->getFacetPtr();
00102     FacetPy::PointerType this_ptr = this->getFacetPtr();
00103     Base::Vector3f p0, p1;
00104     int ret = this_ptr->IntersectWithFacet(*face_ptr, p0, p1);
00105 
00106     try {
00107         Py::List sct;
00108 
00109         if (ret > 0) {
00110             Py::Tuple pt(3);
00111             pt.setItem(0, Py::Float(p0.x));
00112             pt.setItem(1, Py::Float(p0.y));
00113             pt.setItem(2, Py::Float(p0.z));
00114             sct.append(pt);
00115         }
00116         if (ret > 1) {
00117             Py::Tuple pt(3);
00118             pt.setItem(0, Py::Float(p1.x));
00119             pt.setItem(1, Py::Float(p1.y));
00120             pt.setItem(2, Py::Float(p1.z));
00121             sct.append(pt);
00122         }
00123 
00124         return Py::new_reference_to(sct);
00125     }
00126     catch (const Py::Exception&) {
00127         return 0;
00128     }
00129 }
00130 
00131 Py::List FacetPy::getPoints(void) const
00132 {
00133     FacetPy::PointerType face = this->getFacetPtr();
00134 
00135     Py::List pts;
00136     for (int i=0; i<3; i++) {
00137         Py::Tuple pt(3);
00138         pt.setItem(0, Py::Float(face->_aclPoints[i].x));
00139         pt.setItem(1, Py::Float(face->_aclPoints[i].y));
00140         pt.setItem(2, Py::Float(face->_aclPoints[i].z));
00141         pts.append(pt);
00142     }
00143 
00144     return pts;
00145 }
00146 
00147 PyObject *FacetPy::getCustomAttributes(const char* attr) const
00148 {
00149     return 0;
00150 }
00151 
00152 int FacetPy::setCustomAttributes(const char* attr, PyObject *obj)
00153 {
00154     return 0; 
00155 }
00156 
00157 

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