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