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