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