TopoShapeShellPyImp.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 <gp_Ax1.hxx>
00027 # include <BRep_Builder.hxx>
00028 # include <BRepCheck_Analyzer.hxx>
00029 # include <TopoDS.hxx>
00030 # include <TopoDS_Shell.hxx>
00031 # include <ShapeUpgrade_ShellSewing.hxx>
00032 # include <ShapeAnalysis_Shell.hxx>
00033 #endif
00034 
00035 #include <BRepPrimAPI_MakeHalfSpace.hxx>
00036 
00037 #include <Base/VectorPy.h>
00038 #include <Base/GeometryPyCXX.h>
00039 
00040 #include "TopoShape.h"
00041 #include "TopoShapeCompoundPy.h"
00042 #include "TopoShapeCompSolidPy.h"
00043 #include "TopoShapeFacePy.h"
00044 #include "TopoShapeShellPy.h"
00045 #include "TopoShapeShellPy.cpp"
00046 #include "TopoShapeSolidPy.h"
00047 
00048 using namespace Part;
00049 
00050 // returns a string which represents the object e.g. when printed in python
00051 std::string TopoShapeShellPy::representation(void) const
00052 {
00053     // Note: As the return type is 'const char*' we cannot create a temporary
00054     // char array neither on the stack because the array would be freed when
00055     // leaving the scope nor on the heap because we would have a memory leak.
00056     // So we use a static array that is used by all instances of this class.
00057     // This, however, is not a problem as long as we only use this method in
00058     // _repr().
00059 
00060     std::stringstream str;
00061     str << "<Shell object at " << getTopoShapePtr() << ">";
00062 
00063     return str.str();
00064 }
00065 
00066 PyObject *TopoShapeShellPy::PyMake(struct _typeobject *, PyObject *, PyObject *)
00067 {
00068     // create a new instance of TopoShapeSolidPy and the Twin object 
00069     return new TopoShapeShellPy(new TopoShape);
00070 }
00071 
00072 // constructor method
00073 int TopoShapeShellPy::PyInit(PyObject* args, PyObject* /*kwd*/)
00074 {
00075     PyObject *obj;
00076     if (!PyArg_ParseTuple(args, "O!", &(PyList_Type), &obj))
00077         return -1;
00078 
00079     BRep_Builder builder;
00080     TopoDS_Shape shape;
00081     TopoDS_Shell shell;
00082     //BRepOffsetAPI_Sewing mkShell;
00083     builder.MakeShell(shell);
00084     
00085     try {
00086         Py::List list(obj);
00087         for (Py::List::iterator it = list.begin(); it != list.end(); ++it) {
00088             if (PyObject_TypeCheck((*it).ptr(), &(Part::TopoShapeFacePy::Type))) {
00089                 const TopoDS_Shape& sh = static_cast<TopoShapeFacePy*>((*it).ptr())->
00090                     getTopoShapePtr()->_Shape;
00091                 if (!sh.IsNull())
00092                     builder.Add(shell, sh);
00093             }
00094         }
00095 
00096         shape = shell;
00097         BRepCheck_Analyzer check(shell);
00098         if (!check.IsValid()) {
00099             ShapeUpgrade_ShellSewing sewShell;
00100             shape = sewShell.ApplySewing(shell);
00101         }
00102     }
00103     catch (Standard_Failure) {
00104         Handle_Standard_Failure e = Standard_Failure::Caught();
00105         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00106         return -1;
00107     }
00108 
00109     getTopoShapePtr()->_Shape = shape;
00110     return 0;
00111 }
00112 
00113 PyObject*  TopoShapeShellPy::add(PyObject *args)
00114 {
00115     PyObject *obj;
00116     if (!PyArg_ParseTuple(args, "O!", &(Part::TopoShapeFacePy::Type), &obj))
00117         return NULL;
00118 
00119     BRep_Builder builder;
00120     TopoDS_Shape& shell = getTopoShapePtr()->_Shape;
00121     
00122     try {
00123         const TopoDS_Shape& sh = static_cast<TopoShapeFacePy*>(obj)->
00124             getTopoShapePtr()->_Shape;
00125         if (!sh.IsNull()) {
00126             builder.Add(shell, sh);
00127             BRepCheck_Analyzer check(shell);
00128             if (!check.IsValid()) {
00129                 ShapeUpgrade_ShellSewing sewShell;
00130                 getTopoShapePtr()->_Shape = sewShell.ApplySewing(shell);
00131             }
00132         }
00133         else {
00134             Standard_Failure::Raise("cannot add empty shape");
00135         }
00136     }
00137     catch (Standard_Failure) {
00138         Handle_Standard_Failure e = Standard_Failure::Caught();
00139         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00140         return 0;
00141     }
00142 
00143     Py_Return;
00144 }
00145 
00146 PyObject*  TopoShapeShellPy::getFreeEdges(PyObject *args)
00147 {
00148     if (!PyArg_ParseTuple(args, ""))
00149         return NULL;
00150     ShapeAnalysis_Shell as;
00151     as.LoadShells(getTopoShapePtr()->_Shape);
00152 #if OCC_HEX_VERSION < 0x060500
00153     as.CheckOrientedShells(getTopoShapePtr()->_Shape, Standard_True);
00154 #else
00155     as.CheckOrientedShells(getTopoShapePtr()->_Shape, Standard_True, Standard_True);
00156 #endif
00157     TopoDS_Compound comp = as.FreeEdges();
00158     return new TopoShapeCompoundPy(new TopoShape(comp));
00159 }
00160 
00161 PyObject*  TopoShapeShellPy::getBadEdges(PyObject *args)
00162 {
00163     if (!PyArg_ParseTuple(args, ""))
00164         return NULL;
00165     ShapeAnalysis_Shell as;
00166     as.LoadShells(getTopoShapePtr()->_Shape);
00167 #if OCC_HEX_VERSION < 0x060500
00168     as.CheckOrientedShells(getTopoShapePtr()->_Shape, Standard_True);
00169 #else
00170     as.CheckOrientedShells(getTopoShapePtr()->_Shape, Standard_True, Standard_True);
00171 #endif
00172     TopoDS_Compound comp = as.BadEdges();
00173     return new TopoShapeCompoundPy(new TopoShape(comp));
00174 }
00175 
00176 PyObject* TopoShapeShellPy::makeHalfSpace(PyObject *args)
00177 {
00178     PyObject* pPnt;
00179     if (!PyArg_ParseTuple(args, "O!",&(Base::VectorPy::Type),&pPnt))
00180         return 0;
00181 
00182     try {
00183         Base::Vector3d pt = Py::Vector(pPnt,false).toVector();
00184         BRepPrimAPI_MakeHalfSpace mkHS(TopoDS::Face(this->getTopoShapePtr()->_Shape), gp_Pnt(pt.x,pt.y,pt.z));
00185         return new TopoShapeSolidPy(new TopoShape(mkHS.Solid()));
00186     }
00187     catch (Standard_Failure) {
00188         Handle_Standard_Failure e = Standard_Failure::Caught();
00189         PyErr_SetString(PyExc_Exception, e->GetMessageString());
00190         return 0;
00191     }
00192 }
00193 
00194 PyObject *TopoShapeShellPy::getCustomAttributes(const char* /*attr*/) const
00195 {
00196     return 0;
00197 }
00198 
00199 int TopoShapeShellPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00200 {
00201     return 0; 
00202 }

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