Salome HOME
Removed unused attributes from fillet.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Boolean.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        FeaturesPlugin_Boolean.cpp
4 // Created:     02 Sept 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "FeaturesPlugin_Boolean.h"
8
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_AttributeReference.h>
12 #include <ModelAPI_AttributeInteger.h>
13 #include <ModelAPI_ResultBody.h>
14 #include <ModelAPI_AttributeSelectionList.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_Validator.h>
17 #include <ModelAPI_Tools.h>
18
19 #include <GeomAlgoAPI_Boolean.h>
20 #include <GeomAlgoAPI_MakeShapeList.h>
21 #include <GeomAlgoAPI_PaveFiller.h>
22 #include <GeomAlgoAPI_ShapeTools.h>
23 #include <GeomAPI_ShapeExplorer.h>
24
25 #include <algorithm>
26
27 //=================================================================================================
28 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean()
29 {
30 }
31
32 //=================================================================================================
33 void FeaturesPlugin_Boolean::initAttributes()
34 {
35   data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
36
37   AttributeSelectionListPtr aSelection = 
38     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
39     FeaturesPlugin_Boolean::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
40   // extrusion works with faces always
41   aSelection->setSelectionType("SOLID");
42
43   aSelection = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
44     FeaturesPlugin_Boolean::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
45   // extrusion works with faces always
46   aSelection->setSelectionType("SOLID");
47
48   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
49   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
50 }
51
52 //=================================================================================================
53 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Boolean::getShape(const std::string& theAttrName)
54 {
55   std::shared_ptr<ModelAPI_AttributeReference> aObjRef = std::dynamic_pointer_cast<
56       ModelAPI_AttributeReference>(data()->attribute(theAttrName));
57   if (aObjRef) {
58     std::shared_ptr<ModelAPI_ResultBody> aConstr = std::dynamic_pointer_cast<
59         ModelAPI_ResultBody>(aObjRef->value());
60     if (aConstr)
61       return aConstr->shape();
62   }
63   return std::shared_ptr<GeomAPI_Shape>();
64 }
65
66 //=================================================================================================
67 void FeaturesPlugin_Boolean::execute()
68 {
69   // Getting operation type.
70   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
71       ModelAPI_AttributeInteger>(data()->attribute(FeaturesPlugin_Boolean::TYPE_ID()));
72   if (!aTypeAttr)
73     return;
74   GeomAlgoAPI_Boolean::OperationType aType = (GeomAlgoAPI_Boolean::OperationType)aTypeAttr->value();
75
76   ListOfShape anObjects, aTools, anEdgesAndFaces;
77   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
78
79   // Getting objects.
80   AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
81   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
82     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
83     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
84     if(!anObject.get()) {
85       return;
86     }
87     ResultPtr aContext = anObjectAttr->context();
88     ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
89     if(aResCompSolidPtr.get()) {
90       std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
91       std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
92       for(; anIt != aCompSolidsObjects.end(); anIt++) {
93         if(anIt->first->isEqual(aContextShape)) {
94           aCompSolidsObjects[anIt->first].push_back(anObject);
95           break;
96         }
97       }
98       if(anIt == aCompSolidsObjects.end()) {
99         aCompSolidsObjects[aContextShape].push_back(anObject);
100       }
101     } else {
102       if(anObject->shapeType() == GeomAPI_Shape::EDGE ||
103          anObject->shapeType() == GeomAPI_Shape::FACE) {
104         anEdgesAndFaces.push_back(anObject);
105       } else {
106         anObjects.push_back(anObject);
107       }
108     }
109   }
110
111   // Getting tools.
112   AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
113   for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
114     AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
115     std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
116     if(!aTool.get()) {
117       return;
118     }
119     if(aTool->shapeType() == GeomAPI_Shape::EDGE ||
120        aTool->shapeType() == GeomAPI_Shape::FACE) {
121       anEdgesAndFaces.push_back(aTool);
122     } else {
123       aTools.push_back(aTool);
124     }
125   }
126
127   int aResultIndex = 0;
128
129   switch(aType) {
130     case GeomAlgoAPI_Boolean::BOOL_CUT:
131     case GeomAlgoAPI_Boolean::BOOL_COMMON:{
132       if((anObjects.empty() && aCompSolidsObjects.empty()) || aTools.empty()) {
133         std::string aFeatureError = "Not enough objects for boolean operation";
134         setError(aFeatureError);
135         return;
136       }
137
138       // For solids cut each object with all tools.
139       for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
140         std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
141         ListOfShape aListWithObject;
142         aListWithObject.push_back(anObject);
143         GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aTools, aType);
144
145         // Checking that the algorithm worked properly.
146         if(!aBoolAlgo.isDone()) {
147           static const std::string aFeatureError = "Boolean algorithm failed";
148           setError(aFeatureError);
149           return;
150         }
151         if(aBoolAlgo.shape()->isNull()) {
152           static const std::string aShapeError = "Resulting shape is Null";
153           setError(aShapeError);
154           return;
155         }
156         if(!aBoolAlgo.isValid()) {
157           std::string aFeatureError = "Warning: resulting shape is not valid";
158           setError(aFeatureError);
159           return;
160         }
161
162         if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo.shape()) > 1.e-7) {
163           std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
164           loadNamingDS(aResultBody, anObject, aTools, aBoolAlgo.shape(), aBoolAlgo, *aBoolAlgo.mapOfSubShapes().get());
165           setResult(aResultBody, aResultIndex);
166           aResultIndex++;
167         }
168       }
169
170       // Compsolids handling
171       for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
172         anIt != aCompSolidsObjects.end(); anIt++) {
173         std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
174         ListOfShape& aUsedInOperationSolids = anIt->second;
175
176         // Collecting solids from compsolids which will not be modified in boolean operation.
177         ListOfShape aNotUsedSolids;
178         for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
179           std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
180           ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
181           for(; anIt != aUsedInOperationSolids.end(); anIt++) {
182             if(aSolidInCompSolid->isEqual(*anIt)) {
183               break;
184             }
185           }
186           if(anIt == aUsedInOperationSolids.end()) {
187             aNotUsedSolids.push_back(aSolidInCompSolid);
188           }
189         }
190
191         std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedInOperationSolids, aTools, aType));
192
193         // Checking that the algorithm worked properly.
194         if(!aBoolAlgo->isDone()) {
195           static const std::string aFeatureError = "Boolean algorithm failed";
196           setError(aFeatureError);
197           return;
198         }
199         if(aBoolAlgo->shape()->isNull()) {
200           static const std::string aShapeError = "Resulting shape is Null";
201           setError(aShapeError);
202           return;
203         }
204         if(!aBoolAlgo->isValid()) {
205           std::string aFeatureError = "Warning: resulting shape is not valid";
206           setError(aFeatureError);
207           return;
208         }
209
210         GeomAlgoAPI_MakeShapeList aMakeShapeList;
211         aMakeShapeList.appendAlgo(aBoolAlgo);
212         GeomAPI_DataMapOfShapeShape aMapOfShapes;
213         aMapOfShapes.merge(aBoolAlgo->mapOfSubShapes());
214
215         // Add result to not used solids from compsolid.
216         ListOfShape aShapesToAdd = aNotUsedSolids;
217         aShapesToAdd.push_back(aBoolAlgo->shape());
218         std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
219         if(!aFillerAlgo->isDone()) {
220           std::string aFeatureError = "PaveFiller algorithm failed";
221           setError(aFeatureError);
222           return;
223         }
224
225         aMakeShapeList.appendAlgo(aFillerAlgo);
226         aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
227
228         if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-7) {
229           std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
230           loadNamingDS(aResultBody, aCompSolid, aTools, aFillerAlgo->shape(), aMakeShapeList, aMapOfShapes);
231           setResult(aResultBody, aResultIndex);
232           aResultIndex++;
233         }
234       }
235       break;
236     }
237     case GeomAlgoAPI_Boolean::BOOL_FUSE: {
238       if((anObjects.size() + aTools.size() + aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
239         std::string aFeatureError = "Not enough objects for boolean operation";
240         setError(aFeatureError);
241         return;
242       }
243
244       // Collecting all solids which will be fused.
245       ListOfShape aSolidsToFuse;
246       aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
247       aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
248
249       // Collecting solids from compsolids which will not be modified in boolean operation and will be added to result.
250       ListOfShape aShapesToAdd;
251       for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
252         anIt != aCompSolidsObjects.end(); anIt++) {
253         std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
254         ListOfShape& aUsedInOperationSolids = anIt->second;
255         aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedInOperationSolids.begin(), aUsedInOperationSolids.end());
256
257         // Collect solids from compsolid which will not be modified in boolean operation.
258         for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
259           std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
260           ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
261           for(; anIt != aUsedInOperationSolids.end(); anIt++) {
262             if(aSolidInCompSolid->isEqual(*anIt)) {
263               break;
264             }
265           }
266           if(anIt == aUsedInOperationSolids.end()) {
267             aShapesToAdd.push_back(aSolidInCompSolid);
268           }
269         }
270       }
271
272       ListOfShape anOriginalShapes = aSolidsToFuse;
273       anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
274
275       // Cut edges and faces(if we have any) with solids.
276       GeomAlgoAPI_MakeShapeList aMakeShapeList;
277       GeomAPI_DataMapOfShapeShape aMapOfShapes;
278       std::shared_ptr<GeomAPI_Shape> aCuttedEdgesAndFaces;
279       if(!anEdgesAndFaces.empty()) {
280         std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces, anOriginalShapes, GeomAlgoAPI_Boolean::BOOL_CUT));
281         if(aCutAlgo->isDone()) {
282           aCuttedEdgesAndFaces = aCutAlgo->shape();
283           aMakeShapeList.appendAlgo(aCutAlgo);
284           aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
285         }
286       }
287       anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
288
289       // If we have compsolids then cut with not used solids all others.
290       if(!aShapesToAdd.empty()) {
291         aSolidsToFuse.clear();
292         for(ListOfShape::iterator anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
293           ListOfShape aOneObjectList;
294           aOneObjectList.push_back(*anIt);
295           std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Boolean::BOOL_CUT));
296
297           if(GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-7) {
298             aSolidsToFuse.push_back(aCutAlgo->shape());
299             aMakeShapeList.appendAlgo(aCutAlgo);
300             aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
301           }
302         }
303       }
304
305       if(!aSolidsToFuse.empty()) {
306         anObjects.clear();
307         anObjects.push_back(aSolidsToFuse.back());
308         aSolidsToFuse.pop_back();
309         aTools = aSolidsToFuse;
310       }
311
312       // Fuse all objects and all tools.
313       std::shared_ptr<GeomAPI_Shape> aShape;
314       if(anObjects.size() == 1 && aTools.empty()) {
315         aShape = anObjects.front();
316       } else if(anObjects.empty() && aTools.size() == 1) {
317         aShape = aTools.front();
318       } else if((anObjects.size() + aTools.size()) > 1){
319         std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects, aTools, aType));
320
321         // Checking that the algorithm worked properly.
322         if(!aFuseAlgo->isDone()) {
323           static const std::string aFeatureError = "Boolean algorithm failed";
324           setError(aFeatureError);
325           return;
326         }
327         if(aFuseAlgo->shape()->isNull()) {
328           static const std::string aShapeError = "Resulting shape is Null";
329           setError(aShapeError);
330           return;
331         }
332         if(!aFuseAlgo->isValid()) {
333           std::string aFeatureError = "Warning: resulting shape is not valid";
334           setError(aFeatureError);
335           return;
336         }
337
338         aShape = aFuseAlgo->shape();
339         aMakeShapeList.appendAlgo(aFuseAlgo);
340         aMapOfShapes.merge(aFuseAlgo->mapOfSubShapes());
341       }
342
343       // Combine result with not used solids from compsolid and edges and faces (if we have any).
344       if(aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
345         aShapesToAdd.push_back(aCuttedEdgesAndFaces);
346       } else {
347         aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
348       }
349       if(!aShapesToAdd.empty()) {
350         if(aShape.get()) {
351           aShapesToAdd.push_back(aShape);
352         }
353         std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
354         if(!aFillerAlgo->isDone()) {
355           std::string aFeatureError = "PaveFiller algorithm failed";
356           setError(aFeatureError);
357           return;
358         }
359         if(aFillerAlgo->shape()->isNull()) {
360           static const std::string aShapeError = "Resulting shape is Null";
361           setError(aShapeError);
362           return;
363         }
364         if(!aFillerAlgo->isValid()) {
365           std::string aFeatureError = "Warning: resulting shape is not valid";
366           setError(aFeatureError);
367           return;
368         }
369
370         aShape = aFillerAlgo->shape();
371         aMakeShapeList.appendAlgo(aFillerAlgo);
372         aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
373       }
374
375       std::shared_ptr<GeomAPI_Shape> aBackShape = anOriginalShapes.back();
376       anOriginalShapes.pop_back();
377       std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
378       loadNamingDS(aResultBody, aBackShape, anOriginalShapes, aShape, aMakeShapeList, aMapOfShapes);
379       setResult(aResultBody, aResultIndex);
380       aResultIndex++;
381       break;
382     }
383     default: {
384       std::string anOperationError = "Error: wrong type of operation";
385       setError(anOperationError);
386       return;
387     }
388   }
389   // remove the rest results if there were produced in the previous pass
390   removeResults(aResultIndex);
391 }
392
393 //=================================================================================================
394 void FeaturesPlugin_Boolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
395                                           const std::shared_ptr<GeomAPI_Shape> theBaseShape,
396                                           const ListOfShape& theTools,
397                                           const std::shared_ptr<GeomAPI_Shape> theResultShape,
398                                           GeomAlgoAPI_MakeShape& theMakeShape,
399                                           GeomAPI_DataMapOfShapeShape& theMapOfShapes)
400 {
401   //load result
402   if(theBaseShape->isEqual(theResultShape)) {
403     theResultBody->store(theResultShape);
404   } else {
405     const int aModifyTag = 1;
406     const int aDeletedTag = 2;
407     const int aSubsolidsTag = 3; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
408     const int anEdgesAndFacesTag = 10000;
409
410     theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
411
412     const std::string aModName = "Modified";
413     const std::string aModEName = "Modified_Edge";
414     const std::string aModFName = "Modified_Face";
415
416     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
417                                                aModifyTag, aModName, theMapOfShapes);
418     theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDeletedTag);
419
420     int aTag;
421     std::string aName;
422     for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
423       if((*anIter)->shapeType() == GeomAPI_Shape::EDGE) {
424         aTag = anEdgesAndFacesTag;
425         aName = aModEName;
426       }
427       else if((*anIter)->shapeType() == GeomAPI_Shape::FACE) {
428         aTag = anEdgesAndFacesTag;
429         aName = aModFName;
430       } else {
431         aTag = aModifyTag;
432         aName = aModName;
433       }
434       theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, aName == aModEName ? GeomAPI_Shape::EDGE : GeomAPI_Shape::FACE,
435                                                  aTag, aName, theMapOfShapes);
436       theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
437     }
438   }
439 }