Salome HOME
Update copyrights
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanCut.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 "FeaturesPlugin_BooleanCut.h"
21
22 #include "FeaturesPlugin_Tools.h"
23
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_Tools.h>
27
28 #include <GeomAlgoAPI_Boolean.h>
29 #include <GeomAlgoAPI_CompoundBuilder.h>
30 #include <GeomAlgoAPI_MakeShapeList.h>
31 #include <GeomAlgoAPI_PaveFiller.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAlgoAPI_Tools.h>
34
35 #include <GeomAPI_ShapeExplorer.h>
36 #include <GeomAPI_ShapeIterator.h>
37
38 //==================================================================================================
39 FeaturesPlugin_BooleanCut::FeaturesPlugin_BooleanCut()
40 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_CUT)
41 {
42 }
43
44 //==================================================================================================
45 void FeaturesPlugin_BooleanCut::execute()
46 {
47   ListOfShape anObjects, aTools;
48   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
49   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompoundObjects;
50
51   // Getting objects.
52   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
53   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
54     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
55     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
56     if(!anObject.get()) {
57       return;
58     }
59     ResultPtr aContext = anObjectAttr->context();
60     ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
61     if (aResCompSolidPtr.get())
62     {
63       std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
64       GeomAPI_Shape::ShapeType aShapeType = aResCompSolidPtr->shape()->shapeType();
65       std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>& aMap =
66         aShapeType == GeomAPI_Shape::COMPSOLID ? aCompSolidsObjects : aCompoundObjects;
67
68         std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
69           anIt = aMap.begin();
70         for (; anIt != aMap.end(); anIt++) {
71           if (anIt->first->isEqual(aContextShape)) {
72             aMap[anIt->first].push_back(anObject);
73             break;
74           }
75         }
76         if (anIt == aMap.end()) {
77           aMap[aContextShape].push_back(anObject);
78         }
79
80     } else {
81       anObjects.push_back(anObject);
82     }
83   }
84
85   // Getting tools.
86   AttributeSelectionListPtr aToolsSelList = selectionList(TOOL_LIST_ID());
87   for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
88     AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
89     GeomShapePtr aTool = aToolAttr->value();
90     if(!aTool.get()) {
91       return;
92     }
93     aTools.push_back(aTool);
94   }
95
96   int aResultIndex = 0;
97
98   if((anObjects.empty() && aCompSolidsObjects.empty() && aCompoundObjects.empty())
99      || aTools.empty()) {
100     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
101     setError(aFeatureError);
102     return;
103   }
104
105   std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
106   ListOfShape aResultShapesList;
107   std::string anError;
108
109   // For solids cut each object with all tools.
110   for(ListOfShape::iterator anObjectsIt = anObjects.begin();
111       anObjectsIt != anObjects.end();
112       ++anObjectsIt) {
113     std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
114     std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
115     std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
116       new GeomAlgoAPI_Boolean(anObject,
117                               aTools,
118                               GeomAlgoAPI_Boolean::BOOL_CUT));
119     GeomShapePtr aResShape = aCutAlgo->shape();
120
121     // Checking that the algorithm worked properly.
122     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, getKind(), anError)) {
123       setError(anError);
124       return;
125     }
126
127     aMakeShapeList->appendAlgo(aCutAlgo);
128
129     GeomAPI_ShapeIterator aShapeIt(aResShape);
130     if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX)
131     {
132       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
133         document()->createBody(data(), aResultIndex);
134
135       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
136                                                anObject,
137                                                aTools,
138                                                aMakeShapeList,
139                                                aResShape);
140       setResult(aResultBody, aResultIndex);
141       aResultIndex++;
142
143       FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
144       aRBA.resultBody = aResultBody;
145       aRBA.baseShape = anObject;
146       aRBA.makeShape = aMakeShapeList;
147       aResultBaseAlgoList.push_back(aRBA);
148       aResultShapesList.push_back(aResShape);
149     }
150   }
151
152   // Compsolids handling
153   for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
154        anIt = aCompSolidsObjects.begin();
155        anIt != aCompSolidsObjects.end();
156        ++anIt)
157   {
158     std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
159     ListOfShape& aUsedInOperationSolids = anIt->second;
160
161     // Collecting solids from compsolids which will not be modified in boolean operation.
162     ListOfShape aNotUsedSolids;
163     for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
164         anExp.more();
165         anExp.next())
166     {
167       std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
168       ListOfShape::iterator aUsedIt = aUsedInOperationSolids.begin();
169       for (; aUsedIt != aUsedInOperationSolids.end(); aUsedIt++) {
170         if (aSolidInCompSolid->isEqual(*aUsedIt)) {
171           break;
172         }
173       }
174       if (aUsedIt == aUsedInOperationSolids.end()) {
175         aNotUsedSolids.push_back(aSolidInCompSolid);
176       }
177     }
178
179     std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
180     std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
181       new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
182                               aTools,
183                               GeomAlgoAPI_Boolean::BOOL_CUT));
184
185     // Checking that the algorithm worked properly.
186     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, getKind(), anError)) {
187       setError(anError);
188       return;
189     }
190
191     aMakeShapeList->appendAlgo(aCutAlgo);
192     GeomShapePtr aResultShape = aCutAlgo->shape();
193
194     // Add result to not used solids from compsolid.
195     if(!aNotUsedSolids.empty()) {
196       ListOfShape aShapesToAdd = aNotUsedSolids;
197       aShapesToAdd.push_back(aCutAlgo->shape());
198       std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
199         new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
200       if(!aFillerAlgo->isDone()) {
201         std::string aFeatureError = "Error: PaveFiller algorithm failed.";
202         setError(aFeatureError);
203         return;
204       }
205
206       aMakeShapeList->appendAlgo(aFillerAlgo);
207       aResultShape = aFillerAlgo->shape();
208     }
209
210     GeomAPI_ShapeIterator aShapeIt(aResultShape);
211     if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
212     {
213       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
214         document()->createBody(data(), aResultIndex);
215
216       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
217                                                aCompSolid,
218                                                aTools,
219                                                aMakeShapeList,
220                                                aResultShape);
221       setResult(aResultBody, aResultIndex);
222       aResultIndex++;
223
224       FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
225       aRBA.resultBody = aResultBody;
226       aRBA.baseShape = aCompSolid;
227       aRBA.makeShape = aMakeShapeList;
228       aResultBaseAlgoList.push_back(aRBA);
229       aResultShapesList.push_back(aResultShape);
230     }
231   }
232
233   // Compounds handling
234   for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
235        anIt = aCompoundObjects.begin();
236        anIt != aCompoundObjects.end();
237        ++anIt)
238   {
239     std::shared_ptr<GeomAPI_Shape> aCompound = anIt->first;
240     ListOfShape& aUsedInOperationShapes = anIt->second;
241
242     // Collecting shapes from compound which will not be modified in boolean operation.
243     ListOfShape aNotUsedShapes;
244     for (GeomAPI_ShapeIterator aCompIt(aCompound);
245          aCompIt.more();
246          aCompIt.next())
247     {
248       std::shared_ptr<GeomAPI_Shape> aShapeInCompound = aCompIt.current();
249       ListOfShape::iterator aUsedIt = aUsedInOperationShapes.begin();
250       for (; aUsedIt != aUsedInOperationShapes.end(); aUsedIt++) {
251         if (aShapeInCompound->isEqual(*aUsedIt)) {
252           break;
253         }
254       }
255       if (aUsedIt == aUsedInOperationShapes.end()) {
256         aNotUsedShapes.push_back(aShapeInCompound);
257       }
258     }
259
260     std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
261     std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
262       new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
263                               aTools,
264                               GeomAlgoAPI_Boolean::BOOL_CUT));
265
266     // Checking that the algorithm worked properly.
267     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, getKind(), anError)) {
268       setError(anError);
269       return;
270     }
271
272     aMakeShapeList->appendAlgo(aCutAlgo);
273     GeomShapePtr aResultShape = aCutAlgo->shape();
274
275     // Add result to not used shape from compound.
276     if (!aNotUsedShapes.empty()) {
277       ListOfShape aShapesForResult = aNotUsedShapes;
278       if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
279         for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
280           aShapesForResult.push_back(aResultIt.current());
281         }
282       } else {
283         aShapesForResult.push_back(aResultShape);
284       }
285
286       if (aShapesForResult.size() == 1) {
287         aResultShape = aShapesForResult.front();
288       } else {
289         aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
290       }
291     }
292
293     GeomAPI_ShapeIterator aShapeIt(aResultShape);
294     if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
295       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
296         document()->createBody(data(), aResultIndex);
297
298       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
299                                                aCompound,
300                                                aTools,
301                                                aMakeShapeList,
302                                                aResultShape);
303       setResult(aResultBody, aResultIndex);
304       aResultIndex++;
305
306       FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
307       aRBA.resultBody = aResultBody;
308       aRBA.baseShape = aCompound;
309       aRBA.makeShape = aMakeShapeList;
310       aResultBaseAlgoList.push_back(aRBA);
311       aResultShapesList.push_back(aResultShape);
312     }
313   }
314
315   // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
316   // result shape has been deleted, but in another it was modified or stayed.
317   GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
318   FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
319
320   // remove the rest results if there were produced in the previous pass
321   removeResults(aResultIndex);
322 }