Salome HOME
Task #3015 3.1. To add a mode 'through all' for features ExtrusionCut and ExtrusionFuse
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_ThroughAll.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_ThroughAll.h"
21
22 #include <GeomAlgoAPI_Boolean.h>
23 #include <GeomAlgoAPI_ShapeTools.h>
24
25 #include <BRep_Builder.hxx>
26 #include <BOPAlgo_BOP.hxx>
27 #include <TopTools_ListOfShape.hxx>
28 #include <TopoDS_Compound.hxx>
29 #include <TopoDS_Iterator.hxx>
30 #include <TopExp_Explorer.hxx>
31
32 //=================================================================================================
33 GeomAlgoAPI_ThroughAll::GeomAlgoAPI_ThroughAll(std::shared_ptr<GeomAlgoAPI_Prism> thePrismAlgo,
34                                                const ListOfShape& theObjects)
35 : GeomAlgoAPI_Boolean(thePrismAlgo->shape(), theObjects, GeomAlgoAPI_Tools::BOOL_CUT)
36 {
37   removeEnds(thePrismAlgo);
38 }
39
40 //=================================================================================================
41 void GeomAlgoAPI_ThroughAll::removeEnds(std::shared_ptr<GeomAlgoAPI_Prism> thePrismAlgo)
42 {
43   GeomShapePtr aCuttedTool = shape(); // result of BOP Cut (thePrismAlgo->shape() by theObjects)
44
45   // Simplify the result
46   ListOfShape aPieces = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aCuttedTool);
47
48   // Get end shapes of Prism
49   const ListOfShape& fromShapes = thePrismAlgo->fromShapes();
50   const ListOfShape& toShapes = thePrismAlgo->toShapes();
51   ListOfShape endShapes (fromShapes);
52   endShapes.insert(endShapes.end(), toShapes.begin(), toShapes.end());
53
54   // Throw away end pieces of cutted tools (containing endShapes)
55   TopTools_ListOfShape listTools;
56   for (ListOfShape::const_iterator
57          anIt = aPieces.begin(); anIt != aPieces.end(); anIt++) {
58     TopoDS_Shape aPiece = (*anIt)->impl<TopoDS_Shape>();
59     bool endPiece = false;
60
61     for (ListOfShape::const_iterator aBaseIt = endShapes.begin();
62          aBaseIt != endShapes.end() && !endPiece; aBaseIt++) {
63       // Check, if the piece contains aBase (one of endShapes)
64       TopoDS_Shape aBase = (*aBaseIt)->impl<TopoDS_Shape>();
65       TopExp_Explorer anExp (aPiece, aBase.ShapeType());
66       for (; anExp.More() && !endPiece; anExp.Next()) {
67         if (anExp.Current().IsSame(aBase))
68           endPiece = true;
69       }
70     }
71
72     if (!endPiece) {
73       listTools.Append(aPiece);
74     }
75   }
76
77   BRep_Builder aBuilder;
78   TopoDS_Compound aCompound;
79   aBuilder.MakeCompound(aCompound);
80   for (TopTools_ListOfShape::Iterator anIt(listTools); anIt.More(); anIt.Next()) {
81     aBuilder.Add(aCompound, anIt.Value());
82   }
83
84   std::shared_ptr<GeomAPI_Shape> aShape (new GeomAPI_Shape());
85   aShape->setImpl(new TopoDS_Shape(aCompound));
86   this->setShape(aShape);
87   this->setDone(true);
88 }