TopoShapeWirePyImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Jürgen Riegel          (juergen.riegel@web.de) 2008     *
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 <Approx_Curve3d.hxx>
00027 # include <ShapeAlgo_AlgoContainer.hxx>
00028 # include <BRepAdaptor_CompCurve.hxx>
00029 # include <BRepBuilderAPI_MakeWire.hxx>
00030 # include <BRepOffsetAPI_MakeOffset.hxx>
00031 # include <TopoDS.hxx>
00032 # include <TopoDS_Wire.hxx>
00033 # include <gp_Ax1.hxx>
00034 #endif
00035 
00036 #include <BRepGProp.hxx>
00037 #include <GProp_GProps.hxx>
00038 
00039 #include <Base/VectorPy.h>
00040 #include <Base/GeometryPyCXX.h>
00041 
00042 #include "BSplineCurvePy.h"
00043 #include "TopoShape.h"
00044 #include "TopoShapeShellPy.h"
00045 #include "TopoShapeWirePy.h"
00046 #include "TopoShapeWirePy.cpp"
00047 
00048 using namespace Part;
00049 
00050 // returns a string which represents the object e.g. when printed in python
00051 std::string TopoShapeWirePy::representation(void) const
00052 {
00053     std::stringstream str;
00054     str << "<Wire object at " << getTopoShapePtr() << ">";
00055 
00056     return str.str();
00057 }
00058 
00059 PyObject *TopoShapeWirePy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00060 {
00061     // create a new instance of TopoShapeWirePy and the Twin object 
00062     return new TopoShapeWirePy(new TopoShape);
00063 }
00064 
00065 // constructor method
00066 int TopoShapeWirePy::PyInit(PyObject* args, PyObject* /*kwd*/)
00067 {
00068     PyObject *pcObj;
00069     if (PyArg_ParseTuple(args, "O!", &(Part::TopoShapePy::Type), &pcObj)) {
00070         BRepBuilderAPI_MakeWire mkWire;
00071         const TopoDS_Shape& sh = static_cast<Part::TopoShapePy*>(pcObj)->getTopoShapePtr()->_Shape;
00072         if (sh.IsNull()) {
00073             PyErr_SetString(PyExc_TypeError, "given shape is invalid");
00074             return -1;
00075         }
00076         if (sh.ShapeType() == TopAbs_EDGE)
00077             mkWire.Add(TopoDS::Edge(sh));
00078         else if (sh.ShapeType() == TopAbs_WIRE)
00079             mkWire.Add(TopoDS::Wire(sh));
00080         else {
00081             PyErr_SetString(PyExc_TypeError, "shape is neither edge nor wire");
00082             return -1;
00083         }
00084 
00085         try {
00086             getTopoShapePtr()->_Shape = mkWire.Wire();
00087             return 0;
00088         }
00089         catch (Standard_Failure) {
00090             Handle_Standard_Failure e = Standard_Failure::Caught();
00091             PyErr_SetString(PyExc_Exception, e->GetMessageString());
00092             return -1;
00093         }
00094     }
00095 
00096     PyErr_Clear();
00097     if (PyArg_ParseTuple(args, "O!", &(PyList_Type), &pcObj)) {
00098         BRepBuilderAPI_MakeWire mkWire;
00099         Py::List list(pcObj);
00100         for (Py::List::iterator it = list.begin(); it != list.end(); ++it) {
00101             PyObject* item = (*it).ptr();
00102             if (PyObject_TypeCheck(item, &(Part::TopoShapePy::Type))) {
00103                 const TopoDS_Shape& sh = static_cast<Part::TopoShapePy*>(item)->getTopoShapePtr()->_Shape;
00104                 if (sh.IsNull()) {
00105                     PyErr_SetString(PyExc_TypeError, "given shape is invalid");
00106                     return -1;
00107                 }
00108                 if (sh.ShapeType() == TopAbs_EDGE)
00109                     mkWire.Add(TopoDS::Edge(sh));
00110                 else if (sh.ShapeType() == TopAbs_WIRE)
00111                     mkWire.Add(TopoDS::Wire(sh));
00112                 else {
00113                     PyErr_SetString(PyExc_TypeError, "shape is neither edge nor wire");
00114                     return -1;
00115                 }
00116             }
00117             else {
00118                 PyErr_SetString(PyExc_TypeError, "item is not a shape");
00119                 return -1;
00120             }
00121         }
00122 
00123         try {
00124             getTopoShapePtr()->_Shape = mkWire.Wire();
00125             return 0;
00126         }
00127         catch (Standard_Failure) {
00128             Handle_Standard_Failure e = Standard_Failure::Caught();
00129             PyErr_SetString(PyExc_Exception, e->GetMessageString());
00130             return -1;
00131         }
00132     }
00133 
00134     PyErr_SetString(PyExc_Exception, "edge or wire or list of edges and wires expected");
00135     return -1;
00136 }
00137 
00138 PyObject* TopoShapeWirePy::makeOffset(PyObject *args)
00139 {
00140     float dist;
00141     if (!PyArg_ParseTuple(args, "f",&dist))
00142         return 0;
00143     const TopoDS_Wire& w = TopoDS::Wire(getTopoShapePtr()->_Shape);
00144 
00145     BRepOffsetAPI_MakeOffset mkOffset(w);
00146     mkOffset.Perform(dist);
00147     
00148     return new TopoShapePy(new TopoShape(mkOffset.Shape()));
00149 }
00150 
00151 PyObject* TopoShapeWirePy::makePipe(PyObject *args)
00152 {
00153     PyObject *pShape;
00154     if (PyArg_ParseTuple(args, "O!", &(Part::TopoShapePy::Type), &pShape)) {
00155         try {
00156             TopoDS_Shape profile = static_cast<TopoShapePy*>(pShape)->getTopoShapePtr()->_Shape;
00157             TopoDS_Shape shape = this->getTopoShapePtr()->makePipe(profile);
00158             return new TopoShapePy(new TopoShape(shape));
00159         }
00160         catch (Standard_Failure) {
00161             Handle_Standard_Failure e = Standard_Failure::Caught();
00162             PyErr_SetString(PyExc_Exception, e->GetMessageString());
00163             return 0;
00164         }
00165     }
00166 
00167     return 0;
00168 }
00169 
00170 PyObject* TopoShapeWirePy::makePipeShell(PyObject *args)
00171 {
00172     PyObject *obj;
00173     int make_solid = 0;
00174     int is_Frenet = 0;
00175 
00176     if (PyArg_ParseTuple(args, "O!|ii", &(PyList_Type), &obj, &make_solid, &is_Frenet)) {
00177         try {
00178             TopTools_ListOfShape sections;
00179             Py::List list(obj);
00180             for (Py::List::iterator it = list.begin(); it != list.end(); ++it) {
00181                 if (PyObject_TypeCheck((*it).ptr(), &(Part::TopoShapePy::Type))) {
00182                     const TopoDS_Shape& shape = static_cast<TopoShapePy*>((*it).ptr())->getTopoShapePtr()->_Shape;
00183                     sections.Append(shape);
00184                 }
00185             }
00186             TopoDS_Shape shape = this->getTopoShapePtr()->makePipeShell(sections, make_solid, is_Frenet);
00187             return new TopoShapePy(new TopoShape(shape));
00188         }
00189         catch (Standard_Failure) {
00190             Handle_Standard_Failure e = Standard_Failure::Caught();
00191             PyErr_SetString(PyExc_Exception, e->GetMessageString());
00192             return NULL;
00193         }
00194     }
00195 
00196     return 0;
00197 }
00198 
00199 PyObject* TopoShapeWirePy::makeHomogenousWires(PyObject *args)
00200 {
00201     PyObject* wire;
00202     if (!PyArg_ParseTuple(args, "O!",&(Part::TopoShapeWirePy::Type),&wire))
00203         return 0;
00204     try {
00205         TopoDS_Wire o1, o2;
00206         const TopoDS_Wire& w1 = TopoDS::Wire(getTopoShapePtr()->_Shape);
00207         const TopoDS_Wire& w2 = TopoDS::Wire(static_cast<TopoShapePy*>(wire)->getTopoShapePtr()->_Shape);
00208         ShapeAlgo_AlgoContainer shapeAlgo;
00209         if (shapeAlgo.HomoWires(w1,w2,o1,o2,Standard_True)) {
00210             getTopoShapePtr()->_Shape = o1;
00211             return new TopoShapeWirePy(new TopoShape(o2));
00212         }
00213         else {
00214             Py_INCREF(wire);
00215             return wire;
00216         }
00217     }
00218     catch (Standard_Failure) {
00219         Handle_Standard_Failure e = Standard_Failure::Caught();
00220         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00221         return 0;
00222     }
00223 }
00224 
00225 PyObject* TopoShapeWirePy::approximate(PyObject *args)
00226 {
00227     double tol2d = gp::Resolution();
00228     double tol3d = 0.0001;
00229     int maxseg=10, maxdeg=3;
00230     if (!PyArg_ParseTuple(args, "ddii",&tol2d,&tol3d,&maxseg,&maxdeg))
00231         return 0;
00232     try {
00233         BRepAdaptor_CompCurve adapt(TopoDS::Wire(getTopoShapePtr()->_Shape));
00234         Handle_Adaptor3d_HCurve hcurve = adapt.Trim(adapt.FirstParameter(),
00235                                                     adapt.LastParameter(),
00236                                                     tol2d);
00237         Approx_Curve3d approx(hcurve, tol3d, GeomAbs_C0, maxseg, maxdeg);
00238         if (approx.IsDone()) {
00239             return new BSplineCurvePy(new GeomBSplineCurve(approx.Curve()));
00240         }
00241         else {
00242             PyErr_SetString(PyExc_Exception, "failed to approximate wire");
00243             return 0;
00244         }
00245     }
00246     catch (Standard_Failure) {
00247         PyErr_SetString(PyExc_Exception, "failed to approximate wire");
00248         return 0;
00249     }
00250 }
00251 
00252 Py::Object TopoShapeWirePy::getCenterOfMass(void) const
00253 {
00254     GProp_GProps props;
00255     BRepGProp::LinearProperties(getTopoShapePtr()->_Shape, props);
00256     gp_Pnt c = props.CentreOfMass();
00257     return Py::Vector(Base::Vector3d(c.X(),c.Y(),c.Z()));
00258 }
00259 
00260 PyObject *TopoShapeWirePy::getCustomAttributes(const char* /*attr*/) const
00261 {
00262     return 0;
00263 }
00264 
00265 int TopoShapeWirePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00266 {
00267     return 0; 
00268 }
00269 
00270 

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