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