Salome HOME
44ea7eddd4ea242e1cf79415ef004aa856416585
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_CompositeBoolean.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        FeaturesPlugin_CompositeBoolean.cpp
4 // Created:     11 June 2015
5 // Author:      Dmitry Bobylev
6
7 #include "FeaturesPlugin_CompositeBoolean.h"
8
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_Tools.h>
11
12 #include <GeomAlgoAPI_Boolean.h>
13 #include <GeomAlgoAPI_MakeShapeList.h>
14 #include <GeomAlgoAPI_PaveFiller.h>
15 #include <GeomAlgoAPI_ShapeTools.h>
16
17 #include <GeomAPI_ShapeExplorer.h>
18
19 #include <map>
20
21 //=================================================================================================
22 void FeaturesPlugin_CompositeBoolean::initBooleanAttributes()
23 {
24   myFeature->data()->addAttribute(OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
25 }
26
27 //=================================================================================================
28 void FeaturesPlugin_CompositeBoolean::executeCompositeBoolean()
29 {
30   // Make generation.
31   ListOfShape aGenBaseShapes;
32   ListOfMakeShape aGenMakeShapes;
33   if(!makeGeneration(aGenBaseShapes, aGenMakeShapes)) {
34     return;
35   }
36
37   // Getting tools.
38   ListOfShape aTools;
39   for(ListOfMakeShape::const_iterator 
40       anIt = aGenMakeShapes.cbegin(); anIt != aGenMakeShapes.cend(); ++anIt) {
41     aTools.push_back((*anIt)->shape());
42   }
43
44   // Make boolean.
45   ListOfShape aBooleanObjects;
46   ListOfMakeShape aBooleanMakeShapes;
47   if(!makeBoolean(aTools, aBooleanObjects, aBooleanMakeShapes)) {
48     return;
49   }
50
51   if(myOperationType == BOOL_FUSE) {
52     aTools.splice(aTools.begin(), aBooleanObjects);
53     aBooleanObjects.splice(aBooleanObjects.begin(), aTools, aTools.begin());
54   }
55
56   // Store result.
57   int aResultIndex = 0;
58   ListOfShape::const_iterator aBoolObjIt = aBooleanObjects.cbegin();
59   ListOfMakeShape::const_iterator aBoolMSIt = aBooleanMakeShapes.cbegin();
60   for(; aBoolObjIt != aBooleanObjects.cend() && aBoolMSIt != aBooleanMakeShapes.cend();
61       ++aBoolObjIt, ++aBoolMSIt) {
62
63     int aTag = 1;
64
65     ResultBodyPtr aResultBody = myFeature->document()->createBody(myFeature->data(), aResultIndex);
66     aResultBody->storeModified(*aBoolObjIt, (*aBoolMSIt)->shape(), aTag);
67
68     aTag += 5000;
69
70     // Store generation history.
71     ListOfShape::const_iterator aGenBaseIt = aGenBaseShapes.cbegin();
72     ListOfMakeShape::const_iterator aGenMSIt = aGenMakeShapes.cbegin();
73     for(; aGenBaseIt != aGenBaseShapes.cend() && aGenMSIt != aGenMakeShapes.cend();
74         ++aGenBaseIt, ++aGenMSIt) {
75       storeGenerationHistory(aResultBody, *aGenBaseIt, *aGenMSIt, aTag);
76     }
77
78     int aModTag = aTag;
79     storeModificationHistory(aResultBody, *aBoolObjIt, aTools, *aBoolMSIt, aModTag);
80
81     myFeature->setResult(aResultBody, aResultIndex++);
82   }
83
84   myFeature->removeResults(aResultIndex);
85 }
86
87 //=================================================================================================
88 bool FeaturesPlugin_CompositeBoolean::makeBoolean(const ListOfShape& theTools,
89                                                   ListOfShape& theObjects,
90                                                   ListOfMakeShape& theMakeShapes)
91 {
92   // Getting objects.
93   ListOfShape anObjects, anEdgesAndFaces, aCompSolids;
94   std::map<GeomShapePtr, ListOfShape> aCompSolidsObjects;
95   AttributeSelectionListPtr anObjectsSelList = myFeature->selectionList(OBJECTS_ID());
96   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
97     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
98     GeomShapePtr anObject = anObjectAttr->value();
99     if(!anObject.get()) {
100       myFeature->setError("Error: Could not get object.");
101       return false;
102     }
103     ResultPtr aContext = anObjectAttr->context();
104     ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
105     if(aResCompSolidPtr.get()) {
106       GeomShapePtr aContextShape = aResCompSolidPtr->shape();
107       std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
108       for(; anIt != aCompSolidsObjects.end(); anIt++) {
109         if(anIt->first->isEqual(aContextShape)) {
110           aCompSolidsObjects[anIt->first].push_back(anObject);
111           break;
112         }
113       }
114       if(anIt == aCompSolidsObjects.end()) {
115         aCompSolidsObjects[aContextShape].push_back(anObject);
116         aCompSolids.push_back(aContextShape);
117       }
118     } else {
119       if(anObject->shapeType() == GeomAPI_Shape::EDGE ||
120          anObject->shapeType() == GeomAPI_Shape::FACE) {
121         anEdgesAndFaces.push_back(anObject);
122       } else {
123         anObjects.push_back(anObject);
124       }
125     }
126   }
127
128   switch(myOperationType) {
129     case BOOL_CUT: {
130       if((anObjects.empty() && aCompSolidsObjects.empty()) || theTools.empty()) {
131         myFeature->setError("Error: Not enough objects for boolean operation.");
132         return false;
133       }
134
135       // For solids cut each object with all tools.
136       for(ListOfShape::const_iterator 
137           anIt = anObjects.cbegin(); anIt != anObjects.cend(); ++anIt) {
138         GeomShapePtr anObject = *anIt;
139         ListOfShape aListWithObject;
140         aListWithObject.push_back(anObject);
141         std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aListWithObject,
142                                                                 theTools,
143                                                                 GeomAlgoAPI_Boolean::BOOL_CUT));
144
145         // Checking that the algorithm worked properly.
146         if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
147           myFeature->setError("Error: Boolean algorithm failed.");
148           return false;
149         }
150
151         if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo->shape()) > 1.e-27) {
152           theObjects.push_back(anObject);
153           theMakeShapes.push_back(aBoolAlgo);
154         }
155       }
156
157       // Compsolids handling
158       for(std::map<GeomShapePtr, ListOfShape>::const_iterator anIt = aCompSolidsObjects.cbegin();
159           anIt != aCompSolidsObjects.cend(); ++anIt) {
160         GeomShapePtr aCompSolid = anIt->first;
161         const ListOfShape& aUsedShapes = anIt->second;
162
163         // Collecting solids from compsolids which will not be modified in boolean operation.
164         ListOfShape aShapesToAdd;
165         for(GeomAPI_ShapeExplorer 
166             anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
167           GeomShapePtr aSolidInCompSolid = anExp.current();
168           ListOfShape::const_iterator aUsedShapesIt = aUsedShapes.cbegin();
169           for(; aUsedShapesIt != aUsedShapes.cend(); ++aUsedShapesIt) {
170             if(aSolidInCompSolid->isEqual(*aUsedShapesIt)) {
171               break;
172             }
173           }
174           if(aUsedShapesIt == aUsedShapes.end()) {
175             aShapesToAdd.push_back(aSolidInCompSolid);
176           }
177         }
178
179         std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedShapes,
180                                                                   theTools,
181                                                                   GeomAlgoAPI_Boolean::BOOL_CUT));
182
183         // Checking that the algorithm worked properly.
184         if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
185           myFeature->setError("Error: Boolean algorithm failed.");
186           return false;
187         }
188
189         std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
190         aMakeShapeList->appendAlgo(aBoolAlgo);
191
192         // Add result to not used solids from compsolid.
193         aShapesToAdd.push_back(aBoolAlgo->shape());
194         std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
195           new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
196         if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
197           myFeature->setError("Error: PaveFiller algorithm failed.");
198           return false;
199         }
200
201         aMakeShapeList->appendAlgo(aFillerAlgo);
202
203         if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-27) {
204           theObjects.push_back(aCompSolid);
205           theMakeShapes.push_back(aMakeShapeList);
206         }
207       }
208       break;
209     }
210     case BOOL_FUSE: {
211       // Set objects.
212       theObjects.insert(theObjects.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
213       theObjects.insert(theObjects.end(), anObjects.begin(), anObjects.end());
214       theObjects.insert(theObjects.end(), aCompSolids.begin(), aCompSolids.end());
215
216       // Filter edges and faces in tools.
217       ListOfShape aTools;
218       for(ListOfShape::const_iterator anIt = theTools.cbegin(); anIt != theTools.cend(); ++anIt) {
219         if((*anIt)->shapeType() == GeomAPI_Shape::EDGE ||
220            (*anIt)->shapeType() == GeomAPI_Shape::FACE) {
221           anEdgesAndFaces.push_back(*anIt);
222         } else {
223           aTools.push_back(*anIt);
224         }
225       }
226
227       if((anObjects.size() + aTools.size() + 
228           aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
229         myFeature->setError("Error: Not enough objects for boolean operation.");
230         return false;
231       }
232
233       // Collecting all solids which will be fused.
234       ListOfShape aSolidsToFuse;
235       aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
236       aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
237
238       // Collecting solids from compsolids which will not be 
239       // modified in boolean operation and will be added to result.
240       ListOfShape aShapesToAdd;
241       for(std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
242           anIt != aCompSolidsObjects.end(); anIt++) {
243         GeomShapePtr aCompSolid = anIt->first;
244         ListOfShape& aUsedShapes = anIt->second;
245         aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedShapes.begin(), aUsedShapes.end());
246
247         // Collect solids from compsolid which will not be modified in boolean operation.
248         for(GeomAPI_ShapeExplorer 
249             anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
250           GeomShapePtr aSolidInCompSolid = anExp.current();
251           ListOfShape::iterator anIt = aUsedShapes.begin();
252           for(; anIt != aUsedShapes.end(); anIt++) {
253             if(aSolidInCompSolid->isEqual(*anIt)) {
254               break;
255             }
256           }
257           if(anIt == aUsedShapes.end()) {
258             aShapesToAdd.push_back(aSolidInCompSolid);
259           }
260         }
261       }
262
263       // Cut edges and faces(if we have any) with solids.
264       ListOfShape aCutTools;
265       aCutTools.insert(aCutTools.end(), anObjects.begin(), anObjects.end());
266       aCutTools.insert(aCutTools.end(), aCompSolids.begin(), aCompSolids.end());
267       aCutTools.insert(aCutTools.end(), aTools.begin(), aTools.end());
268
269       std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
270       if(!anEdgesAndFaces.empty() && !aCutTools.empty()) {
271         std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
272                                                               aCutTools,
273                                                               GeomAlgoAPI_Boolean::BOOL_CUT));
274         if(aCutAlgo->isDone() && !aCutAlgo->shape()->isNull() && aCutAlgo->isValid()) {
275           anEdgesAndFaces.clear();
276           anEdgesAndFaces.push_back(aCutAlgo->shape());
277           aMakeShapeList->appendAlgo(aCutAlgo);
278         }
279       }
280
281       // If we have compsolids then cut with not used solids all others.
282       if(!aShapesToAdd.empty()) {
283         std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aSolidsToFuse,
284                                                               aShapesToAdd,
285                                                               GeomAlgoAPI_Boolean::BOOL_CUT));
286         if(aCutAlgo->isDone() && GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
287           aSolidsToFuse.clear();
288           aSolidsToFuse.push_back(aCutAlgo->shape());
289           aMakeShapeList->appendAlgo(aCutAlgo);
290         }
291       }
292
293       // Fuse all objects and all tools.
294       GeomShapePtr aFusedShape;
295       if(aSolidsToFuse.size() == 1) {
296         aFusedShape = aSolidsToFuse.front();
297       } else if(aSolidsToFuse.size() > 1){
298         anObjects.clear();
299         anObjects.push_back(aSolidsToFuse.front());
300         aSolidsToFuse.pop_front();
301         aTools = aSolidsToFuse;
302
303         std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
304                                                           aTools,
305                                                           GeomAlgoAPI_Boolean::BOOL_FUSE));
306
307         // Checking that the algorithm worked properly.
308         if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
309           myFeature->setError("Error: Boolean algorithm failed.");
310           return false;
311         }
312
313         aFusedShape = aFuseAlgo->shape();
314         aMakeShapeList->appendAlgo(aFuseAlgo);
315       }
316
317       // Combine result with not used solids from compsolid and edges and faces (if we have any).
318       aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
319       if(!aShapesToAdd.empty()) {
320         if(aFusedShape.get()) {
321           aShapesToAdd.push_back(aFusedShape);
322         }
323
324         std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
325           new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
326         if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
327           myFeature->setError("Error: PaveFiller algorithm failed.");
328           return false;
329         }
330
331         aMakeShapeList->appendAlgo(aFillerAlgo);
332       }
333
334       theMakeShapes.push_back(aMakeShapeList);
335       break;
336     }
337   }
338
339   return true;
340 }
341
342 //=================================================================================================
343 void FeaturesPlugin_CompositeBoolean::storeModificationHistory(ResultBodyPtr theResultBody,
344                                 const GeomShapePtr theObject,
345                                 const ListOfShape& theTools,
346                                 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
347                                 int& theTag)
348 {
349   int aModTag = theTag;
350   int anEdgesAndFacesTag = ++aModTag;
351   int aDelTag = ++anEdgesAndFacesTag;
352   theTag = aDelTag;
353
354   const std::string aModName = "Modfied";
355
356   ListOfShape aTools = theTools;
357   aTools.push_back(theObject);
358
359   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMap = theMakeShape->mapOfSubShapes();
360
361   int aTag;
362   std::string aName;
363   for(ListOfShape::const_iterator anIt = aTools.begin(); anIt != aTools.end(); anIt++) {
364     if((*anIt)->shapeType() == GeomAPI_Shape::EDGE) {
365       aTag = anEdgesAndFacesTag;
366       aName = aModName + "_Edge";
367     }
368     else if((*anIt)->shapeType() == GeomAPI_Shape::FACE) {
369       aTag = anEdgesAndFacesTag;
370       aName = aModName + "_Face";
371     } else {
372       aTag = aModTag;
373       aName = aModName;
374     }
375     theResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), *anIt, 
376       (*anIt)->shapeType() == GeomAPI_Shape::EDGE ?
377       GeomAPI_Shape::EDGE : GeomAPI_Shape::FACE, aTag, aName, *aMap.get());
378     theResultBody->loadDeletedShapes(theMakeShape.get(), *anIt, GeomAPI_Shape::FACE, aDelTag);
379   }
380 }