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