Salome HOME
Fix for the issue #3033: ExtrusionCut error
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_CompositeBoolean.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_CompositeBoolean.h"
21
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_Tools.h>
24
25 #include <GeomAlgoAPI_Boolean.h>
26 #include <GeomAlgoAPI_CompoundBuilder.h>
27 #include <GeomAlgoAPI_MakeShapeList.h>
28 #include <GeomAlgoAPI_PaveFiller.h>
29 #include <GeomAlgoAPI_ShapeTools.h>
30
31 #include <GeomAPI_ShapeIterator.h>
32
33 #include <map>
34
35 //=================================================================================================
36 void FeaturesPlugin_CompositeBoolean::initBooleanAttributes()
37 {
38   myFeature->data()->addAttribute(OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
39 }
40
41 //=================================================================================================
42 void FeaturesPlugin_CompositeBoolean::executeCompositeBoolean()
43 {
44   // Make generation.
45   ListOfShape aGenBaseShapes;
46   ListOfMakeShape aGenMakeShapes;
47   if(!makeGeneration(aGenBaseShapes, aGenMakeShapes)) {
48     return;
49   }
50
51   // Getting tools.
52   ListOfShape aTools;
53   for(ListOfMakeShape::const_iterator
54       anIt = aGenMakeShapes.cbegin(); anIt != aGenMakeShapes.cend(); ++anIt) {
55     aTools.push_back((*anIt)->shape());
56   }
57
58   // Make boolean.
59   ListOfShape aBooleanObjects;
60   ListOfMakeShape aBooleanMakeShapes;
61   if(!makeBoolean(aTools, aBooleanObjects, aBooleanMakeShapes)) {
62     return;
63   }
64
65   if(myOperationType == BOOL_FUSE) {
66     aTools.splice(aTools.begin(), aBooleanObjects);
67     aBooleanObjects.splice(aBooleanObjects.begin(), aTools, aTools.begin());
68   }
69
70   // Store result.
71   int aResultIndex = 0;
72   std::vector<ResultBaseAlgo> aResultBaseAlgoList;
73   ListOfShape aResultShapesList;
74   ListOfShape::const_iterator aBoolObjIt = aBooleanObjects.cbegin();
75   ListOfMakeShape::const_iterator aBoolMSIt = aBooleanMakeShapes.cbegin();
76   for(; aBoolObjIt != aBooleanObjects.cend() && aBoolMSIt != aBooleanMakeShapes.cend();
77       ++aBoolObjIt, ++aBoolMSIt) {
78
79     ResultBodyPtr aResultBody = myFeature->document()->createBody(myFeature->data(), aResultIndex);
80
81     if((*aBoolObjIt)->isEqual((*aBoolMSIt)->shape())) {
82       aResultBody->store((*aBoolMSIt)->shape(), false);
83     }
84     else
85     {
86       aResultBody->storeModified(*aBoolObjIt, (*aBoolMSIt)->shape());
87
88       // Store generation history.
89       ListOfShape::const_iterator aGenBaseIt = aGenBaseShapes.cbegin();
90       ListOfMakeShape::const_iterator aGenMSIt = aGenMakeShapes.cbegin();
91       for(; aGenBaseIt != aGenBaseShapes.cend() && aGenMSIt != aGenMakeShapes.cend();
92           ++aGenBaseIt, ++aGenMSIt) {
93         std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMSList(new GeomAlgoAPI_MakeShapeList());
94         aMSList->appendAlgo(*aGenMSIt);
95         aMSList->appendAlgo(*aBoolMSIt);
96         storeGenerationHistory(aResultBody, *aGenBaseIt, aMSList);
97       }
98
99       storeModificationHistory(aResultBody, *aBoolObjIt, aTools, *aBoolMSIt);
100
101       ResultBaseAlgo aRBA;
102       aRBA.resultBody = aResultBody;
103       aRBA.baseShape = *aBoolObjIt;
104       aRBA.makeShape = *aBoolMSIt;
105       aResultBaseAlgoList.push_back(aRBA);
106       aResultShapesList.push_back((*aBoolMSIt)->shape());
107     }
108
109     myFeature->setResult(aResultBody, aResultIndex++);
110   }
111
112   // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
113   // result shape has been deleted, but in another it was modified or stayed.
114   GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
115   storeDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
116
117   myFeature->removeResults(aResultIndex);
118 }
119
120 //=================================================================================================
121 bool FeaturesPlugin_CompositeBoolean::makeBoolean(const ListOfShape& theTools,
122                                                   ListOfShape& theObjects,
123                                                   ListOfMakeShape& theMakeShapes)
124 {
125   // Getting objects.
126   ListOfShape anObjects, anEdgesAndFaces, aCompSolids;
127   std::map<GeomShapePtr, ListOfShape> aCompSolidsObjects;
128   AttributeSelectionListPtr anObjectsSelList = myFeature->selectionList(OBJECTS_ID());
129   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
130     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
131     GeomShapePtr anObject = anObjectAttr->value();
132     if(!anObject.get()) {
133       myFeature->setError("Error: Could not get object.");
134       return false;
135     }
136     ResultPtr aContext = anObjectAttr->context();
137     ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
138     if(aResCompSolidPtr.get()) {
139       GeomShapePtr aContextShape = aResCompSolidPtr->shape();
140       std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
141       for(; anIt != aCompSolidsObjects.end(); anIt++) {
142         if(anIt->first->isEqual(aContextShape)) {
143           aCompSolidsObjects[anIt->first].push_back(anObject);
144           break;
145         }
146       }
147       if(anIt == aCompSolidsObjects.end()) {
148         aCompSolidsObjects[aContextShape].push_back(anObject);
149         aCompSolids.push_back(aContextShape);
150       }
151     } else {
152       if(anObject->shapeType() == GeomAPI_Shape::EDGE ||
153          anObject->shapeType() == GeomAPI_Shape::FACE) {
154         anEdgesAndFaces.push_back(anObject);
155       } else {
156         anObjects.push_back(anObject);
157       }
158     }
159   }
160
161   switch(myOperationType) {
162     case BOOL_CUT: {
163       if((anObjects.empty() && aCompSolidsObjects.empty()) || theTools.empty()) {
164         myFeature->setError("Error: Not enough objects for boolean operation.");
165         return false;
166       }
167
168       // For solids cut each object with all tools.
169       for(ListOfShape::const_iterator
170           anIt = anObjects.cbegin(); anIt != anObjects.cend(); ++anIt) {
171         GeomShapePtr anObject = *anIt;
172         ListOfShape aListWithObject;
173         aListWithObject.push_back(anObject);
174         std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aListWithObject,
175                                                                 theTools,
176                                                                 GeomAlgoAPI_Tools::BOOL_CUT));
177
178         // Checking that the algorithm worked properly.
179         if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
180           myFeature->setError("Error: Boolean algorithm failed.");
181           return false;
182         }
183
184         if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo->shape()) > 1.e-27) {
185           theObjects.push_back(anObject);
186           theMakeShapes.push_back(aBoolAlgo);
187         }
188       }
189
190       // Compsolids handling
191       for(std::map<GeomShapePtr, ListOfShape>::const_iterator anIt = aCompSolidsObjects.cbegin();
192           anIt != aCompSolidsObjects.cend(); ++anIt) {
193         GeomShapePtr aCompSolid = anIt->first;
194         const ListOfShape& aUsedShapes = anIt->second;
195
196         // Collecting solids from compsolids which will not be modified in boolean operation.
197         ListOfShape aShapesToAdd;
198         for (GeomAPI_ShapeIterator aCompSolidIt(aCompSolid);
199              aCompSolidIt.more();
200              aCompSolidIt.next())
201         {
202           GeomShapePtr aSolidInCompSolid = aCompSolidIt.current();
203           ListOfShape::const_iterator aUsedShapesIt = aUsedShapes.cbegin();
204           for(; aUsedShapesIt != aUsedShapes.cend(); ++aUsedShapesIt) {
205             if(aSolidInCompSolid->isEqual(*aUsedShapesIt)) {
206               break;
207             }
208           }
209           if(aUsedShapesIt == aUsedShapes.end()) {
210             aShapesToAdd.push_back(aSolidInCompSolid);
211           }
212         }
213
214         std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedShapes,
215                                                                   theTools,
216                                                                   GeomAlgoAPI_Tools::BOOL_CUT));
217
218         // Checking that the algorithm worked properly.
219         if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
220           myFeature->setError("Error: Boolean algorithm failed.");
221           return false;
222         }
223
224         std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
225         aMakeShapeList->appendAlgo(aBoolAlgo);
226
227         // Add result to not used solids from compsolid.
228         GeomShapePtr aBoolRes = aBoolAlgo->shape();
229         if (!aShapesToAdd.empty()) {
230           aShapesToAdd.push_back(aBoolRes);
231           std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
232             new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
233           if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
234             myFeature->setError("Error: PaveFiller algorithm failed.");
235             return false;
236           }
237           aBoolRes = aFillerAlgo->shape();
238           aMakeShapeList->appendAlgo(aFillerAlgo);
239         }
240
241         if(GeomAlgoAPI_ShapeTools::volume(aBoolRes) > 1.e-27) {
242           theObjects.push_back(aCompSolid);
243           theMakeShapes.push_back(aMakeShapeList);
244         }
245       }
246       break;
247     }
248     case BOOL_FUSE: {
249       // Set objects.
250       theObjects.insert(theObjects.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
251       theObjects.insert(theObjects.end(), anObjects.begin(), anObjects.end());
252       theObjects.insert(theObjects.end(), aCompSolids.begin(), aCompSolids.end());
253
254       // Filter edges and faces in tools.
255       ListOfShape aTools;
256       for(ListOfShape::const_iterator anIt = theTools.cbegin(); anIt != theTools.cend(); ++anIt) {
257         if((*anIt)->shapeType() == GeomAPI_Shape::EDGE ||
258            (*anIt)->shapeType() == GeomAPI_Shape::FACE) {
259           anEdgesAndFaces.push_back(*anIt);
260         } else {
261           aTools.push_back(*anIt);
262         }
263       }
264
265       if((anObjects.size() + aTools.size() +
266           aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
267         myFeature->setError("Error: Not enough objects for boolean operation.");
268         return false;
269       }
270
271       // Collecting all solids which will be fused.
272       ListOfShape aSolidsToFuse;
273       aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
274       aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
275
276       // Collecting solids from compsolids which will not be
277       // modified in boolean operation and will be added to result.
278       ListOfShape aShapesToAdd;
279       for(std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
280           anIt != aCompSolidsObjects.end(); anIt++) {
281         GeomShapePtr aCompSolid = anIt->first;
282         ListOfShape& aUsedShapes = anIt->second;
283         aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedShapes.begin(), aUsedShapes.end());
284
285         // Collect solids from compsolid which will not be modified in boolean operation.
286         for (GeomAPI_ShapeIterator aCompSolidIt(aCompSolid);
287              aCompSolidIt.more();
288              aCompSolidIt.next())
289         {
290           GeomShapePtr aSolidInCompSolid = aCompSolidIt.current();
291           ListOfShape::iterator anIt = aUsedShapes.begin();
292           for(; anIt != aUsedShapes.end(); anIt++) {
293             if(aSolidInCompSolid->isEqual(*anIt)) {
294               break;
295             }
296           }
297           if(anIt == aUsedShapes.end()) {
298             aShapesToAdd.push_back(aSolidInCompSolid);
299           }
300         }
301       }
302
303       // Cut edges and faces(if we have any) with solids.
304       ListOfShape aCutTools;
305       aCutTools.insert(aCutTools.end(), anObjects.begin(), anObjects.end());
306       aCutTools.insert(aCutTools.end(), aCompSolids.begin(), aCompSolids.end());
307       aCutTools.insert(aCutTools.end(), aTools.begin(), aTools.end());
308
309       std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
310       if(!anEdgesAndFaces.empty() && !aCutTools.empty()) {
311         std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
312                                                               aCutTools,
313                                                               GeomAlgoAPI_Tools::BOOL_CUT));
314         if(aCutAlgo->isDone() && !aCutAlgo->shape()->isNull() && aCutAlgo->isValid()) {
315           anEdgesAndFaces.clear();
316           anEdgesAndFaces.push_back(aCutAlgo->shape());
317           aMakeShapeList->appendAlgo(aCutAlgo);
318         }
319       }
320
321       // If we have compsolids then cut with not used solids all others.
322       if(!aShapesToAdd.empty()) {
323         std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aSolidsToFuse,
324                                                               aShapesToAdd,
325                                                               GeomAlgoAPI_Tools::BOOL_CUT));
326         if(aCutAlgo->isDone() && GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
327           aSolidsToFuse.clear();
328           aSolidsToFuse.push_back(aCutAlgo->shape());
329           aMakeShapeList->appendAlgo(aCutAlgo);
330         }
331       }
332
333       // Fuse all objects and all tools.
334       GeomShapePtr aFusedShape;
335       if(aSolidsToFuse.size() == 1) {
336         aFusedShape = aSolidsToFuse.front();
337       } else if(aSolidsToFuse.size() > 1){
338         anObjects.clear();
339         anObjects.push_back(aSolidsToFuse.front());
340         aSolidsToFuse.pop_front();
341         aTools = aSolidsToFuse;
342
343         std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
344                                                           aTools,
345                                                           GeomAlgoAPI_Tools::BOOL_FUSE));
346
347         // Checking that the algorithm worked properly.
348         if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
349           myFeature->setError("Error: Boolean algorithm failed.");
350           return false;
351         }
352
353         aFusedShape = aFuseAlgo->shape();
354         aMakeShapeList->appendAlgo(aFuseAlgo);
355       }
356
357       // Combine result with not used solids from compsolid and edges and faces (if we have any).
358       aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
359       if(!aShapesToAdd.empty()) {
360         if(aFusedShape.get()) {
361           aShapesToAdd.push_back(aFusedShape);
362         }
363
364         std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
365           new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
366         if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
367           myFeature->setError("Error: PaveFiller algorithm failed.");
368           return false;
369         }
370
371         aMakeShapeList->appendAlgo(aFillerAlgo);
372       }
373
374       theMakeShapes.push_back(aMakeShapeList);
375       break;
376     }
377   }
378
379   return true;
380 }
381
382 //=================================================================================================
383 void FeaturesPlugin_CompositeBoolean::storeModificationHistory(ResultBodyPtr theResultBody,
384                                 const GeomShapePtr theObject,
385                                 const ListOfShape& theTools,
386                                 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
387 {
388   ListOfShape aTools = theTools;
389   aTools.push_back(theObject);
390
391   for(ListOfShape::const_iterator anIt = aTools.begin(); anIt != aTools.end(); anIt++) {
392     theResultBody->loadModifiedShapes(theMakeShape, *anIt,
393                                       (*anIt)->shapeType() == GeomAPI_Shape::EDGE ?
394                                                               GeomAPI_Shape::EDGE :
395                                                               GeomAPI_Shape::FACE);
396   }
397 }
398
399 //==================================================================================================
400 void FeaturesPlugin_CompositeBoolean::storeDeletedShapes(
401   std::vector<ResultBaseAlgo>& theResultBaseAlgoList,
402   const ListOfShape& theTools,
403   const GeomShapePtr theResultShapesCompound)
404 {
405   for (std::vector<ResultBaseAlgo>::iterator anIt = theResultBaseAlgoList.begin();
406     anIt != theResultBaseAlgoList.end();
407     ++anIt)
408   {
409     ResultBaseAlgo& aRCA = *anIt;
410     aRCA.resultBody->loadDeletedShapes(aRCA.makeShape,
411       aRCA.baseShape,
412       GeomAPI_Shape::FACE,
413       theResultShapesCompound);
414
415     for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
416     {
417       aRCA.resultBody->loadDeletedShapes(aRCA.makeShape,
418         *anIter,
419         GeomAPI_Shape::FACE,
420         theResultShapesCompound);
421     }
422   }
423 }