CrossSection.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2010 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 #ifndef _PreComp_
00026 # include <BRepAlgoAPI_Section.hxx>
00027 # include <BRepBuilderAPI_MakeWire.hxx>
00028 # include <gp_Pln.hxx>
00029 # include <TopExp_Explorer.hxx>
00030 # include <TopoDS.hxx>
00031 # include <TopoDS_Edge.hxx>
00032 # include <TopoDS_Wire.hxx>
00033 #endif
00034 
00035 #include "CrossSection.h"
00036 
00037 using namespace Part;
00038 
00039 
00040 CrossSection::CrossSection(double a, double b, double c, const TopoDS_Shape& s)
00041   : a(a), b(b), c(c), s(s)
00042 {
00043 }
00044 
00045 std::list<TopoDS_Wire> CrossSection::section(double d) const
00046 {
00047     std::list<TopoDS_Wire> wires;
00048     BRepAlgoAPI_Section cs(s, gp_Pln(a,b,c,-d));
00049     if (cs.IsDone()) {
00050         std::list<TopoDS_Edge> edges;
00051         TopExp_Explorer xp;
00052         for (xp.Init(cs.Shape(), TopAbs_EDGE); xp.More(); xp.Next())
00053             edges.push_back(TopoDS::Edge(xp.Current()));
00054         connectEdges(edges, wires);
00055     }
00056 
00057     return wires;
00058 }
00059 
00060 void CrossSection::connectEdges (const std::list<TopoDS_Edge>& edges, std::list<TopoDS_Wire>& wires) const
00061 {
00062     std::list<TopoDS_Edge> edge_list = edges;
00063     while (edge_list.size() > 0) {
00064         BRepBuilderAPI_MakeWire mkWire;
00065         // add and erase first edge
00066         mkWire.Add(edge_list.front());
00067         edge_list.erase(edge_list.begin());
00068 
00069         TopoDS_Wire new_wire = mkWire.Wire();  // current new wire
00070 
00071         // try to connect each edge to the wire, the wire is complete if no more egdes are connectible
00072         bool found = false;
00073         do {
00074             found = false;
00075             for (std::list<TopoDS_Edge>::iterator pE = edge_list.begin(); pE != edge_list.end();++pE) {
00076                 mkWire.Add(*pE);
00077                 if (mkWire.Error() != BRepBuilderAPI_DisconnectedWire) {
00078                     // edge added ==> remove it from list
00079                     found = true;
00080                     edge_list.erase(pE);
00081                     new_wire = mkWire.Wire();
00082                     break;
00083                 }
00084             }
00085         }
00086         while (found);
00087         wires.push_back(new_wire);
00088     }
00089 }

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