Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Boolean.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_Boolean.h"
21
22 #include <GeomAlgoAPI_DFLoader.h>
23 #include <GeomAlgoAPI_ShapeTools.h>
24
25 #include <BOPAlgo_BOP.hxx>
26 #include <TopTools_ListOfShape.hxx>
27 #include <TopoDS_Iterator.hxx>
28 #include <TopExp_Explorer.hxx>
29
30 //=================================================================================================
31 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const GeomShapePtr theObject,
32                                          const GeomShapePtr theTool,
33                                          const OperationType theOperationType)
34 {
35   ListOfShape aListWithObject, aListWithTool;
36   aListWithObject.push_back(theObject);
37   aListWithTool.push_back(theTool);
38   build(aListWithObject, aListWithTool, theOperationType);
39 }
40
41 //=================================================================================================
42 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const GeomShapePtr theObject,
43                                          const ListOfShape& theTools,
44                                          const OperationType theOperationType)
45 {
46   ListOfShape aListWithObject;
47   aListWithObject.push_back(theObject);
48   build(aListWithObject, theTools, theOperationType);
49 }
50
51 //=================================================================================================
52 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const ListOfShape& theObjects,
53                                          const ListOfShape& theTools,
54                                          const OperationType theOperationType)
55 {
56   build(theObjects, theTools, theOperationType);
57 }
58
59
60 //=================================================================================================
61 void GeomAlgoAPI_Boolean::build(const ListOfShape& theObjects,
62                                 const ListOfShape& theTools,
63                                 const OperationType theOperationType)
64 {
65   if(theObjects.empty() || theTools.empty()) {
66     return;
67   }
68
69   // Getting objects.
70   TopTools_ListOfShape anObjects;
71   for(ListOfShape::const_iterator
72     anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
73   {
74     anObjects.Append((*anObjectsIt)->impl<TopoDS_Shape>());
75   }
76
77   // Getting tools.
78   TopTools_ListOfShape aTools;
79   for(ListOfShape::const_iterator
80     aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
81   {
82     aTools.Append((*aToolsIt)->impl<TopoDS_Shape>());
83   }
84
85   // Creating boolean operation.
86   BOPAlgo_BOP* aBuilder = new BOPAlgo_BOP();
87   switch (theOperationType) {
88     case BOOL_CUT: {
89       aBuilder->SetOperation(BOPAlgo_CUT);
90       break;
91     }
92     case BOOL_FUSE: {
93       aBuilder->SetOperation(BOPAlgo_FUSE);
94       break;
95     }
96     case BOOL_COMMON: {
97       aBuilder->SetOperation(BOPAlgo_COMMON);
98       break;
99     }
100     default: {
101       return;
102     }
103   }
104   this->setImpl(aBuilder);
105   this->setBuilderType(OCCT_BOPAlgo_Builder);
106   aBuilder->SetArguments(anObjects);
107   aBuilder->SetTools(aTools);
108
109   // Building and getting result.
110   aBuilder->Perform();
111   if (aBuilder->HasErrors())
112     return;
113   TopoDS_Shape aResult = aBuilder->Shape();
114
115   if(aResult.ShapeType() == TopAbs_COMPOUND) {
116     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
117   }
118   if(aResult.ShapeType() == TopAbs_COMPOUND) {
119     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
120     aGeomShape->setImpl(new TopoDS_Shape(aResult));
121     ListOfShape aResults;
122     aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
123                                                        GeomAPI_Shape::COMPSOLID,
124                                                        aResults);
125     aResult = aGeomShape->impl<TopoDS_Shape>();
126   }
127
128   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
129   aShape->setImpl(new TopoDS_Shape(aResult));
130   this->setShape(aShape);
131   this->setDone(true);
132 }
133
134 static bool isHistoryType(TopAbs_ShapeEnum theType) {
135   return theType == TopAbs_VERTEX || theType == TopAbs_EDGE ||
136          theType == TopAbs_FACE || theType == TopAbs_SOLID;
137 }
138
139 /// searches the corresponding result for theOld
140 static void searchResult(const TopoDS_Shape& theOld, const TopoDS_Shape& theResult,
141   BOPAlgo_BOP* theBuilder, TopTools_MapOfShape& theNews)
142 {
143   if (theResult.ShapeType() == theOld.ShapeType()) { // check some sub-shape produces a sub-result
144     if (theOld.IsSame(theResult)) {
145       theNews.Add(theResult);
146       return;
147     }
148     // searching for new result by sub-shapes of aSubType type
149     TopAbs_ShapeEnum aSubType = TopAbs_ShapeEnum(int(theOld.ShapeType()) + 1);
150     while(aSubType < TopAbs_VERTEX && !isHistoryType(aSubType))
151       aSubType = TopAbs_ShapeEnum(int(aSubType) + 1);
152     if (aSubType == TopAbs_SHAPE)
153       return;
154     TopTools_MapOfShape aResSubs;
155     for(TopExp_Explorer aResExp(theResult, aSubType); aResExp.More(); aResExp.Next())
156       aResSubs.Add(aResExp.Current());
157     for(TopExp_Explorer anExp(theOld, aSubType); anExp.More(); anExp.Next()) {
158       const TopTools_ListOfShape& aNewSubs = theBuilder->Modified(anExp.Current());
159       // searching for this new sub in theResult
160       for(TopTools_ListIteratorOfListOfShape aNewSub(aNewSubs); aNewSub.More(); aNewSub.Next()) {
161         if (aResSubs.Contains(aNewSub.Value())) {
162           theNews.Add(theResult);
163           return;
164         }
165       }
166     }
167   } else if (theResult.ShapeType() < theOld.ShapeType()) { // recursive search among sub-shapes
168     for(TopoDS_Iterator aSubResults(theResult); aSubResults.More(); aSubResults.Next()) {
169       searchResult(theOld, aSubResults.Value(), theBuilder, theNews);
170     }
171   }
172 }
173
174 // check the shape is on the higher level of compound or compsolid
175 bool isInComp(const TopoDS_Shape& theComp, const TopoDS_Shape& theShape) {
176   if (theComp.ShapeType() == TopAbs_COMPOUND || theComp.ShapeType() == TopAbs_COMPSOLID) {
177     for(TopoDS_Iterator anIter(theComp); anIter.More(); anIter.Next()) {
178       if (isInComp(anIter.Value(), theShape))
179         return true;
180     }
181   } else return theShape.IsSame(theComp);
182   return false;
183 }
184
185 //=================================================================================================
186 /// make arguments of Fuse produce result shapes with "modified" evolution
187 void GeomAlgoAPI_Boolean::modified(const GeomShapePtr theOldShape, ListOfShape& theNewShapes)
188 {
189   BOPAlgo_BOP* aBuilder = this->implPtr<BOPAlgo_BOP>();
190   if (aBuilder->Operation() == BOPAlgo_FUSE) { // only for fuse and when old is and argument
191     TopoDS_Shape anOld = theOldShape->impl<TopoDS_Shape>();
192     bool isOldComp = anOld.ShapeType() == TopAbs_COMPOUND || anOld.ShapeType() == TopAbs_COMPSOLID;
193     bool aFound = false;
194     TopTools_ListIteratorOfListOfShape anIter(aBuilder->Arguments());
195     for(; !aFound && anIter.More(); anIter.Next())
196       aFound = anOld.IsSame(anIter.Value()) || (!isOldComp && isInComp(anIter.Value(), anOld));
197     for(anIter.Initialize(aBuilder->Tools()); !aFound && anIter.More(); anIter.Next())
198       aFound = anOld.IsSame(anIter.Value()) || (!isOldComp && isInComp(anIter.Value(), anOld));
199     if (aFound) {
200       TopoDS_Shape aResult = aBuilder->Shape();
201       TopTools_MapOfShape aNewsMap;
202       searchResult(anOld, aResult, aBuilder, aNewsMap);
203       if (!aNewsMap.IsEmpty()) {
204         for(TopTools_MapIteratorOfMapOfShape aNewsIter(aNewsMap); aNewsIter.More(); aNewsIter.Next())
205         {
206           GeomShapePtr aShape(new GeomAPI_Shape);
207           aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewsIter.Value()));
208           theNewShapes.push_back(aShape);
209         }
210         return;
211       }
212     }
213   }
214   GeomAlgoAPI_MakeShape::modified(theOldShape, theNewShapes); // default behavior
215 }