Salome HOME
a503c7d64e9bd45b165fb259f2759d5b177439ed
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Boolean.cpp
1 // Copyright (C) 2014-2021  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 GeomAlgoAPI_Tools::BOPType 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 GeomAlgoAPI_Tools::BOPType 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 GeomAlgoAPI_Tools::BOPType 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 GeomAlgoAPI_Tools::BOPType 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 GeomAlgoAPI_Tools::BOOL_CUT: {
89       aBuilder->SetOperation(BOPAlgo_CUT);
90       break;
91     }
92     case GeomAlgoAPI_Tools::BOOL_FUSE: {
93       aBuilder->SetOperation(BOPAlgo_FUSE);
94       break;
95     }
96     case GeomAlgoAPI_Tools::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   // Set parallel processing mode (default is false)
110   Standard_Boolean bRunParallel = Standard_True;
111   aBuilder->SetRunParallel(bRunParallel);
112
113   // Set fuzzy value to eliminate thin results
114   static const Standard_Real aFuzzy = 1.e-5;
115   aBuilder->SetFuzzyValue(aFuzzy);
116
117   // Building and getting result.
118   aBuilder->Perform();
119   if (aBuilder->HasErrors())
120     return;
121   TopoDS_Shape aResult = aBuilder->Shape();
122
123   if(aResult.ShapeType() == TopAbs_COMPOUND) {
124     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
125   }
126   if(aResult.ShapeType() == TopAbs_COMPOUND) {
127     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
128     aGeomShape->setImpl(new TopoDS_Shape(aResult));
129     ListOfShape aResults;
130     aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
131                                                        GeomAPI_Shape::COMPSOLID,
132                                                        aResults);
133     aResult = aGeomShape->impl<TopoDS_Shape>();
134   }
135
136   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
137   aShape->setImpl(new TopoDS_Shape(aResult));
138   this->setShape(aShape);
139   this->setDone(true);
140 }
141
142 static bool isHistoryType(TopAbs_ShapeEnum theType) {
143   return theType == TopAbs_VERTEX || theType == TopAbs_EDGE ||
144          theType == TopAbs_FACE || theType == TopAbs_SOLID;
145 }
146
147 /// searches the corresponding result for theOld
148 static void searchResult(const TopoDS_Shape& theOld, const TopoDS_Shape& theResult,
149   BOPAlgo_BOP* theBuilder, TopTools_MapOfShape& theNews)
150 {
151   if (theResult.ShapeType() == theOld.ShapeType()) { // check some sub-shape produces a sub-result
152     if (theOld.IsSame(theResult)) {
153       theNews.Add(theResult);
154       return;
155     }
156     // searching for new result by sub-shapes of aSubType type
157     TopAbs_ShapeEnum aSubType = TopAbs_ShapeEnum(int(theOld.ShapeType()) + 1);
158     while(aSubType < TopAbs_VERTEX && !isHistoryType(aSubType))
159       aSubType = TopAbs_ShapeEnum(int(aSubType) + 1);
160     if (aSubType == TopAbs_SHAPE)
161       return;
162     TopTools_MapOfShape aResSubs;
163     for(TopExp_Explorer aResExp(theResult, aSubType); aResExp.More(); aResExp.Next())
164       aResSubs.Add(aResExp.Current());
165     for(TopExp_Explorer anExp(theOld, aSubType); anExp.More(); anExp.Next()) {
166       const TopTools_ListOfShape& aNewSubs = theBuilder->Modified(anExp.Current());
167       // searching for this new sub in theResult
168       for(TopTools_ListIteratorOfListOfShape aNewSub(aNewSubs); aNewSub.More(); aNewSub.Next()) {
169         if (aResSubs.Contains(aNewSub.Value())) {
170           theNews.Add(theResult);
171           return;
172         }
173       }
174     }
175   } else if (theResult.ShapeType() < theOld.ShapeType()) { // recursive search among sub-shapes
176     for(TopoDS_Iterator aSubResults(theResult); aSubResults.More(); aSubResults.Next()) {
177       searchResult(theOld, aSubResults.Value(), theBuilder, theNews);
178     }
179   }
180 }
181
182 // check the shape is on the higher level of compound or compsolid
183 bool isInComp(const TopoDS_Shape& theComp, const TopoDS_Shape& theShape) {
184   if (theComp.ShapeType() == TopAbs_COMPOUND || theComp.ShapeType() == TopAbs_COMPSOLID) {
185     for(TopoDS_Iterator anIter(theComp); anIter.More(); anIter.Next()) {
186       if (isInComp(anIter.Value(), theShape))
187         return true;
188     }
189   } else return theShape.IsSame(theComp);
190   return false;
191 }
192
193 //=================================================================================================
194 /// make arguments of Fuse produce result shapes with "modified" evolution
195 void GeomAlgoAPI_Boolean::modified(const GeomShapePtr theOldShape, ListOfShape& theNewShapes)
196 {
197   BOPAlgo_BOP* aBuilder = this->implPtr<BOPAlgo_BOP>();
198   if (aBuilder->Operation() == BOPAlgo_FUSE) { // only for fuse and when old is and argument
199     TopoDS_Shape anOld = theOldShape->impl<TopoDS_Shape>();
200     bool isOldComp = anOld.ShapeType() == TopAbs_COMPOUND || anOld.ShapeType() == TopAbs_COMPSOLID;
201     bool aFound = false;
202     TopTools_ListIteratorOfListOfShape anIter(aBuilder->Arguments());
203     for(; !aFound && anIter.More(); anIter.Next())
204       aFound = anOld.IsSame(anIter.Value()) || (!isOldComp && isInComp(anIter.Value(), anOld));
205     for(anIter.Initialize(aBuilder->Tools()); !aFound && anIter.More(); anIter.Next())
206       aFound = anOld.IsSame(anIter.Value()) || (!isOldComp && isInComp(anIter.Value(), anOld));
207     if (aFound) {
208       TopoDS_Shape aResult = aBuilder->Shape();
209       TopTools_MapOfShape aNewsMap;
210       searchResult(anOld, aResult, aBuilder, aNewsMap);
211       if (!aNewsMap.IsEmpty()) {
212         for(TopTools_MapIteratorOfMapOfShape aNewsIter(aNewsMap);
213             aNewsIter.More(); aNewsIter.Next())
214         {
215           GeomShapePtr aShape(new GeomAPI_Shape);
216           aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aNewsIter.Value()));
217           theNewShapes.push_back(aShape);
218         }
219         return;
220       }
221     }
222   }
223   GeomAlgoAPI_MakeShape::modified(theOldShape, theNewShapes); // default behavior
224 }