AppMeshPartPy.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2008 Jürgen 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 #ifndef _PreComp_
00026 # include <BRepBuilderAPI_MakePolygon.hxx>
00027 #endif
00028 
00029 #include <Base/PyObjectBase.h>
00030 #include <Base/Console.h>
00031 #include <Base/Vector3D.h>
00032 #include <Mod/Part/App/TopoShapePy.h>
00033 #include <Mod/Part/App/TopoShapeWirePy.h>
00034 #include <Mod/Mesh/App/Core/Algorithm.h>
00035 #include <Mod/Mesh/App/Core/MeshKernel.h>
00036 #include <Mod/Mesh/App/Mesh.h>
00037 #include <Mod/Mesh/App/MeshPy.h>
00038 #include "MeshAlgos.h"
00039 #include "Mesher.h"
00040 
00041 static PyObject *                        
00042 loftOnCurve(PyObject *self, PyObject *args)
00043 {
00044     Part::TopoShapePy   *pcObject;
00045     PyObject *pcTopoObj,*pcListObj;
00046     float x=0.0f,y=0.0f,z=1.0f,size = 0.1f;
00047 
00048     if (!PyArg_ParseTuple(args, "O!O(fff)f", &(Part::TopoShapePy::Type), &pcTopoObj,&pcListObj,&x,&y,&z,&size))     // convert args: Python->C 
00049 //  if (!PyArg_ParseTuple(args, "O!O!", &(App::TopoShapePy::Type), &pcTopoObj,&PyList_Type,&pcListObj,x,y,z,size))     // convert args: Python->C 
00050         return NULL;                             // NULL triggers exception 
00051 
00052     pcObject = (Part::TopoShapePy*)pcTopoObj;
00053     MeshCore::MeshKernel M;
00054 
00055     std::vector<Base::Vector3f> poly;
00056 
00057     if (!PyList_Check(pcListObj))
00058         Py_Error(PyExc_Exception,"List of Tuble of three or two floats needed as second parameter!");
00059   
00060     int nSize = PyList_Size(pcListObj);
00061     for (int i=0; i<nSize;++i)
00062     {
00063         PyObject* item = PyList_GetItem(pcListObj, i);
00064         if (!PyTuple_Check(item))
00065             Py_Error(PyExc_Exception,"List of Tuble of three or two floats needed as second parameter!");
00066         int nTSize = PyTuple_Size(item);
00067         if(nTSize != 2 && nTSize != 3)
00068             Py_Error(PyExc_Exception,"List of Tuble of three or two floats needed as second parameter!");
00069 
00070         Base::Vector3f vec(0,0,0);
00071 
00072         for(int l = 0; l < nTSize;l++)
00073         {
00074             PyObject* item2 = PyTuple_GetItem(item, l);
00075             if (!PyFloat_Check(item2))
00076                 Py_Error(PyExc_Exception,"List of Tuble of three or two floats needed as second parameter!");
00077             vec[l] = (float)PyFloat_AS_DOUBLE(item2);
00078         }
00079         poly.push_back(vec);
00080     }
00081     
00082     PY_TRY {
00083         TopoDS_Shape aShape = pcObject->getTopoShapePtr()->_Shape;
00084         // use the MeshAlgos 
00085         MeshPart::MeshAlgos::LoftOnCurve(M,aShape,poly,Base::Vector3f(x,y,z),size);
00086 
00087     } PY_CATCH;
00088 
00089     return new Mesh::MeshPy(new Mesh::MeshObject(M));
00090 }
00091 
00092 PyDoc_STRVAR(loft_doc,
00093 "Loft on curve.");
00094 
00095 static PyObject *
00096 wireFromSegment(PyObject *self, PyObject *args)
00097 {
00098     PyObject *o, *m;
00099     if (!PyArg_ParseTuple(args, "O!O!", &(Mesh::MeshPy::Type), &m,&PyList_Type,&o))
00100         return 0;
00101     Py::List list(o);
00102     Mesh::MeshObject* mesh = static_cast<Mesh::MeshPy*>(m)->getMeshObjectPtr();
00103     std::vector<unsigned long> segm;
00104     segm.reserve(list.size());
00105     for (unsigned int i=0; i<list.size(); i++) {
00106         segm.push_back((int)Py::Int(list[i]));
00107     }
00108 
00109     std::list<std::vector<Base::Vector3f> > bounds;
00110     MeshCore::MeshAlgorithm algo(mesh->getKernel());
00111     algo.GetFacetBorders(segm, bounds);
00112 
00113     Py::List wires;
00114     std::list<std::vector<Base::Vector3f> >::iterator bt;
00115 
00116     try {
00117         for (bt = bounds.begin(); bt != bounds.end(); ++bt) {
00118             BRepBuilderAPI_MakePolygon mkPoly;
00119             for (std::vector<Base::Vector3f>::reverse_iterator it = bt->rbegin(); it != bt->rend(); ++it) {
00120                 mkPoly.Add(gp_Pnt(it->x,it->y,it->z));
00121             }
00122             if (mkPoly.IsDone()) {
00123                 PyObject* wire = new Part::TopoShapeWirePy(new Part::TopoShape(mkPoly.Wire()));
00124                 wires.append(Py::Object(wire, true));
00125             }
00126         }
00127     }
00128     catch (Standard_Failure) {
00129         Handle_Standard_Failure e = Standard_Failure::Caught();
00130         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00131         return 0;
00132     }
00133 
00134     return Py::new_reference_to(wires);
00135 }
00136 
00137 static PyObject *
00138 meshFromShape(PyObject *self, PyObject *args)
00139 {
00140     PyObject *shape;
00141     float maxLength=1.0f/*0.5f*/;
00142     float maxArea=0/*1.0f*/;
00143     float localLen=0/*0.1f*/;
00144     float deflection=0/*0.01f*/;
00145     if (!PyArg_ParseTuple(args, "O!|ffff", &(Part::TopoShapePy::Type), &shape,
00146                                            &maxLength,&maxArea,&localLen,&deflection))
00147         return 0;
00148 
00149     try {
00150         MeshPart::Mesher mesher(static_cast<Part::TopoShapePy*>(shape)->getTopoShapePtr()->_Shape);
00151         mesher.setMaxLength(maxLength);
00152         mesher.setMaxArea(maxArea);
00153         mesher.setLocalLength(localLen);
00154         mesher.setDeflection(deflection);
00155         mesher.setRegular(true);
00156         return new Mesh::MeshPy(mesher.createMesh());
00157     }
00158     catch (const Base::Exception& e) {
00159         PyErr_SetString(PyExc_Exception, e.what());
00160         return 0;
00161     }
00162 }
00163 
00164 /* registration table  */
00165 struct PyMethodDef MeshPart_methods[] = {
00166     {"loftOnCurve",loftOnCurve, METH_VARARGS, loft_doc},
00167     {"wireFromSegment",wireFromSegment, METH_VARARGS,
00168      "Create wire(s) from boundary of segment"},
00169     {"meshFromShape",meshFromShape, METH_VARARGS,
00170      "Create mesh from shape"},
00171     {NULL, NULL}        /* end of table marker */
00172 };

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