TopoShapeVertexPyImp.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 
00026 #ifndef _PreComp_
00027 # include <gp_Pnt.hxx>
00028 # include <gp_Ax1.hxx>
00029 # include <BRep_Tool.hxx>
00030 # include <TopoDS.hxx>
00031 # include <TopoDS_Vertex.hxx>
00032 # include <BRep_Builder.hxx>
00033 # include <BRepBuilderAPI_MakeVertex.hxx>
00034 #endif
00035 
00036 #include <Mod/Part/App/TopoShape.h>
00037 #include <Base/VectorPy.h>
00038 #include <Base/Vector3D.h>
00039 
00040 #include "TopoShapeEdgePy.h"
00041 #include "TopoShapeVertexPy.h"
00042 #include "TopoShapeVertexPy.cpp"
00043 
00044 using namespace Part;
00045 
00046 // returns a string which represents the object e.g. when printed in python
00047 std::string TopoShapeVertexPy::representation(void) const
00048 {
00049     std::stringstream str;
00050     str << "<Vertex object at " << getTopoShapePtr() << ">";
00051 
00052     return str.str();
00053 }
00054 
00055 PyObject *TopoShapeVertexPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00056 {
00057     // create a new instance of TopoShapeVertexPy and the Twin object 
00058     return new TopoShapeVertexPy(new TopoShape);
00059 }
00060 
00061 // constructor method
00062 int TopoShapeVertexPy::PyInit(PyObject* args, PyObject* /*kwd*/)
00063 {
00064     double x=0.0,y=0.0,z=0.0;
00065     PyObject *object;
00066     bool success = false;
00067     if (PyArg_ParseTuple(args, "|ddd", &x,&y,&z)) {
00068         // do nothing here
00069         success = true;
00070     }
00071     if (!success) {
00072         PyErr_Clear(); // set by PyArg_ParseTuple()
00073         if (PyArg_ParseTuple(args,"O!",&(Base::VectorPy::Type), &object)) {
00074             // Note: must be static_cast, not reinterpret_cast
00075             Base::Vector3d* ptr = static_cast<Base::VectorPy*>(object)->getVectorPtr();
00076             x = ptr->x;
00077             y = ptr->y;
00078             z = ptr->z;
00079             success = true;
00080         }
00081     }
00082     if (!success) {
00083         PyErr_Clear(); // set by PyArg_ParseTuple()
00084         if (PyArg_ParseTuple(args,"O!",&(PyTuple_Type), &object)) {
00085             try {
00086                 Py::Tuple tuple(object);
00087                 x = (float)Py::Float(tuple.getItem(0));
00088                 y = (float)Py::Float(tuple.getItem(1));
00089                 z = (float)Py::Float(tuple.getItem(2));
00090                 success = true;
00091             }
00092             catch (const Py::Exception&) {
00093                 return -1;
00094             }
00095         }
00096     }
00097     if (!success) {
00098         PyErr_Clear(); // set by PyArg_ParseTuple()
00099         if (PyArg_ParseTuple(args,"O!",&(Part::TopoShapePy::Type), &object)) {
00100             TopoShape* ptr = static_cast<TopoShapePy*>(object)->getTopoShapePtr();
00101             TopoDS_Shape shape = ptr->_Shape;
00102             if (!shape.IsNull() && shape.ShapeType() == TopAbs_VERTEX) {
00103                 TopoShapeVertexPy::PointerType vert = reinterpret_cast<TopoShapeVertexPy::PointerType>(_pcTwinPointer);
00104                 vert->_Shape = ptr->_Shape;
00105                 return 0;
00106             }
00107         }
00108     }
00109     if (!success) {
00110         PyErr_SetString(PyExc_TypeError, "Either three floats, tuple, vector or vertex expected");
00111         return -1;
00112     }
00113 
00114     TopoShapeVertexPy::PointerType ptr = reinterpret_cast<TopoShapeVertexPy::PointerType>(_pcTwinPointer);
00115     BRepBuilderAPI_MakeVertex aBuilder(gp_Pnt(x,y,z));
00116     TopoDS_Shape s = aBuilder.Vertex();
00117     ptr->_Shape = s;
00118 
00119     return 0;
00120 }
00121 
00122 PyObject* TopoShapeVertexPy::setTolerance(PyObject *args)
00123 {
00124     double tol;
00125     if (!PyArg_ParseTuple(args, "d", &tol))
00126         return 0;
00127     BRep_Builder aBuilder;
00128     const TopoDS_Vertex& v = TopoDS::Vertex(getTopoShapePtr()->_Shape);
00129     aBuilder.UpdateVertex(v, tol);
00130     Py_Return;
00131 }
00132 
00133 Py::Float TopoShapeVertexPy::getX(void) const
00134 {
00135     const TopoDS_Vertex& v = TopoDS::Vertex(getTopoShapePtr()->_Shape);
00136     return Py::Float(BRep_Tool::Pnt(v).X());
00137 }
00138 
00139 Py::Float TopoShapeVertexPy::getY(void) const
00140 {
00141     const TopoDS_Vertex& v = TopoDS::Vertex(getTopoShapePtr()->_Shape);
00142     return Py::Float(BRep_Tool::Pnt(v).Y());
00143 }
00144 
00145 Py::Float TopoShapeVertexPy::getZ(void) const
00146 {
00147     const TopoDS_Vertex& v = TopoDS::Vertex(getTopoShapePtr()->_Shape);
00148     return Py::Float(BRep_Tool::Pnt(v).Z());
00149 }
00150 
00151 Py::Object TopoShapeVertexPy::getPoint(void) const
00152 {
00153     const TopoDS_Vertex& v = TopoDS::Vertex(getTopoShapePtr()->_Shape);
00154     gp_Pnt p = BRep_Tool::Pnt(v);
00155     return Py::Object(new Base::VectorPy(new Base::Vector3d(p.X(),p.Y(),p.Z())));
00156 }
00157 
00158 PyObject *TopoShapeVertexPy::getCustomAttributes(const char* /*attr*/) const
00159 {
00160     return 0;
00161 }
00162 
00163 int TopoShapeVertexPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00164 {
00165     return 0; 
00166 }

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