]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.cpp
Salome HOME
Issue #1305: Make different gray color for selectable and non-selectable items
[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_AttributeReference.h>
11 #include <ModelAPI_ResultBody.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <ModelAPI_Session.h>
14 #include <ModelAPI_Tools.h>
15 #include <ModelAPI_Validator.h>
16
17 #include <GeomAlgoAPI_CompoundBuilder.h>
18 #include <GeomAlgoAPI_MakeShapeList.h>
19 #include <GeomAlgoAPI_MakeSweep.h>
20 #include <GeomAlgoAPI_PaveFiller.h>
21 #include <GeomAlgoAPI_Prism.h>
22 #include <GeomAlgoAPI_Revolution.h>
23 #include <GeomAlgoAPI_ShapeTools.h>
24 #include <GeomAPI_ShapeExplorer.h>
25
26 #include <map>
27 #include <sstream>
28
29 //=================================================================================================
30 void FeaturesPlugin_CompositeBoolean::initAttributes()
31 {
32   data()->addAttribute(SKETCH_OBJECT_ID(), ModelAPI_AttributeReference::typeId());
33
34   // Boolean works with solids always.
35   data()->addAttribute(BOOLEAN_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
36   AttributeSelectionListPtr aSelection = data()->selectionList(BOOLEAN_OBJECTS_ID());
37   aSelection->setSelectionType("SOLID");
38
39   initMakeSolidsAttributes();
40
41   data()->addAttribute(SKETCH_SELECTION_ID(), ModelAPI_AttributeSelection::typeId());
42   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_SELECTION_ID());
43 }
44
45 //=================================================================================================
46 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::addFeature(std::string theID)
47 {
48   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
49   if (aNew) {
50     data()->reference(SKETCH_OBJECT_ID())->setValue(aNew);
51   }
52   // set as current also after it becomes sub to set correctly enabled for other sketch subs
53   document()->setCurrentFeature(aNew, false);
54   return aNew;
55 }
56
57 //=================================================================================================
58 int FeaturesPlugin_CompositeBoolean::numberOfSubs(bool forTree) const
59 {
60   ObjectPtr aObj = data()->reference(SKETCH_OBJECT_ID())->value();
61   return aObj.get()? 1 : 0;
62 }
63
64 //=================================================================================================
65 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::subFeature(const int theIndex, bool forTree)
66 {
67   if (theIndex == 0)
68     return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
69   return std::shared_ptr<ModelAPI_Feature>();
70 }
71
72 //=================================================================================================
73 int FeaturesPlugin_CompositeBoolean::subFeatureId(const int theIndex) const
74 {
75   if (theIndex == 0) {
76     FeaturePtr aFeature = 
77       std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
78     if (aFeature.get())
79       return aFeature->data()->featureId();
80   }
81   return -1;
82 }
83
84 //=================================================================================================
85 bool FeaturesPlugin_CompositeBoolean::isSub(ObjectPtr theObject) const
86 {
87   // check is this feature of result
88   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
89   if (!aFeature)
90     return false;
91  
92   ObjectPtr aSub = data()->reference(SKETCH_OBJECT_ID())->value();
93   return aSub == theObject;
94 }
95
96 //=================================================================================================
97 void FeaturesPlugin_CompositeBoolean::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
98 {
99 }
100
101 //=================================================================================================
102 void FeaturesPlugin_CompositeBoolean::erase()
103 {
104   if (data().get() && data()->isValid()) { // on abort of sketch of this composite it may be invalid
105     FeaturePtr aSketch =
106       std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
107     if (aSketch.get() && aSketch->data()->isValid()) {
108       document()->removeFeature(aSketch);
109     }
110   }
111   ModelAPI_CompositeFeature::erase();
112 }
113
114
115 //=================================================================================================
116 void FeaturesPlugin_CompositeBoolean::execute()
117 {
118   // Getting faces to create solids.
119   std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
120                                                      reference(SKETCH_OBJECT_ID())->value());
121   if(!aSketchFeature || aSketchFeature->results().empty()) {
122     return;
123   }
124   ResultPtr aSketchRes = aSketchFeature->results().front();
125   ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
126   if(!aConstruction.get()) {
127     return;
128   }
129   selection(SKETCH_SELECTION_ID())->setValue(aSketchRes, std::shared_ptr<GeomAPI_Shape>());
130   int aSketchFacesNum = aConstruction->facesNum();
131   if(aSketchFacesNum == 0) {
132     return;
133   }
134   ListOfShape aFacesList;
135   for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
136     std::shared_ptr<GeomAPI_Shape> aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
137     aFacesList.push_back(aFace);
138   }
139
140   // Searching faces with common edges.
141   ListOfShape aShells;
142   ListOfShape aFreeFaces;
143   std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
144   GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
145   aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
146
147   // Pass shells/faces to solids creation function.
148   ListOfShape aTools;
149   ListOfMakeShape aSolidsAlgos;
150   makeSolids(aShells, aTools, aSolidsAlgos);
151   if(aTools.empty()) {
152     return;
153   }
154
155   // Getting objects for boolean operation.
156   ListOfShape anObjects;
157   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
158   AttributeSelectionListPtr anObjectsSelList = selectionList(BOOLEAN_OBJECTS_ID());
159   if(anObjectsSelList->size() == 0) {
160     return;
161   }
162   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
163     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
164     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
165     if(!anObject.get()) {
166       return;
167     }
168     ResultPtr aContext = anObjectAttr->context();
169     ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
170     if(aResCompSolidPtr.get()) {
171       std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
172       std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
173       for(; anIt != aCompSolidsObjects.end(); anIt++) {
174         if(anIt->first->isEqual(aContextShape)) {
175           aCompSolidsObjects[anIt->first].push_back(anObject);
176           break;
177         }
178       }
179       if(anIt == aCompSolidsObjects.end()) {
180         aCompSolidsObjects[aContextShape].push_back(anObject);
181       }
182     } else {
183       anObjects.push_back(anObject);
184     }
185   }
186
187   // Cut from each object solids.
188   int aResultIndex = 0;
189
190   switch(myBooleanOperationType) {
191     case GeomAlgoAPI_Boolean::BOOL_CUT:
192     case GeomAlgoAPI_Boolean::BOOL_COMMON:{
193       // Cut each object with all tools
194       for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
195         std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
196         ListOfShape aListWithObject;
197         aListWithObject.push_back(anObject);
198         GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aTools, myBooleanOperationType);
199
200         // Checking that the algorithm worked properly.
201         if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
202           setError("Error: Boolean algorithm failed.");
203           return;
204         }
205
206         if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo.shape()) > 1.e-7) {
207           std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
208           loadNamingDS(aResultBody, aShells, aSolidsAlgos, anObject, aTools, aBoolAlgo.shape(),
209                        aBoolAlgo, *aBoolAlgo.mapOfSubShapes().get());
210           setResult(aResultBody, aResultIndex);
211           aResultIndex++;
212         }
213       }
214
215       // Compsolids handling
216       for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
217         anIt != aCompSolidsObjects.end(); anIt++) {
218         std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
219         ListOfShape& aUsedInOperationSolids = anIt->second;
220
221         // Collecting solids from compsolids which will not be modified in boolean operation.
222         ListOfShape aNotUsedSolids;
223         for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
224           std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
225           ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
226           for(; anIt != aUsedInOperationSolids.end(); anIt++) {
227             if(aSolidInCompSolid->isEqual(*anIt)) {
228               break;
229             }
230           }
231           if(anIt == aUsedInOperationSolids.end()) {
232             aNotUsedSolids.push_back(aSolidInCompSolid);
233           }
234         }
235
236         std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedInOperationSolids, aTools, myBooleanOperationType));
237
238         // Checking that the algorithm worked properly.
239         if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
240           setError("Error: Boolean algorithm failed.");
241           return;
242         }
243
244         GeomAlgoAPI_MakeShapeList aMakeShapeList;
245         aMakeShapeList.appendAlgo(aBoolAlgo);
246         GeomAPI_DataMapOfShapeShape aMapOfShapes;
247         aMapOfShapes.merge(aBoolAlgo->mapOfSubShapes());
248
249         // Add result to not used solids from compsolid.
250         ListOfShape aShapesToAdd = aNotUsedSolids;
251         aShapesToAdd.push_back(aBoolAlgo->shape());
252         std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
253         if(!aFillerAlgo->isDone()) {
254           std::string aFeatureError = "Error: PaveFiller algorithm failed.";
255           setError(aFeatureError);
256           return;
257         }
258
259         aMakeShapeList.appendAlgo(aFillerAlgo);
260         aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
261
262         if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-7) {
263           std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
264           loadNamingDS(aResultBody, aShells, aSolidsAlgos, aCompSolid, aTools, aFillerAlgo->shape(), aMakeShapeList, aMapOfShapes);
265           setResult(aResultBody, aResultIndex);
266           aResultIndex++;
267         }
268       }
269       break;
270     }
271     case GeomAlgoAPI_Boolean::BOOL_FUSE: {
272       // Collecting all solids which will be fused.
273       ListOfShape aSolidsToFuse;
274       aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
275       aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
276
277       // Collecting solids from compsolids which will not be modified in boolean operation.
278       ListOfShape aNotUsedSolids;
279       for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
280         anIt != aCompSolidsObjects.end(); anIt++) {
281         std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
282         ListOfShape& aUsedInOperationSolids = anIt->second;
283         aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedInOperationSolids.begin(), aUsedInOperationSolids.end());
284
285         // Collect solids from compsolid which will not be modified in boolean operation.
286         for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
287           std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
288           ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
289           for(; anIt != aUsedInOperationSolids.end(); anIt++) {
290             if(aSolidInCompSolid->isEqual(*anIt)) {
291               break;
292             }
293           }
294           if(anIt == aUsedInOperationSolids.end()) {
295             aNotUsedSolids.push_back(aSolidInCompSolid);
296           }
297         }
298       }
299
300       ListOfShape anOriginalSolids = aSolidsToFuse;
301       anOriginalSolids.insert(anOriginalSolids.end(), aNotUsedSolids.begin(), aNotUsedSolids.end());
302       GeomAlgoAPI_MakeShapeList aMakeShapeList;
303       GeomAPI_DataMapOfShapeShape aMapOfShapes;
304
305       // If we have compsolids then cut with not used solids all others.
306       if(!aNotUsedSolids.empty()) {
307         aSolidsToFuse.clear();
308         for(ListOfShape::iterator anIt = anOriginalSolids.begin(); anIt != anOriginalSolids.end(); anIt++) {
309           ListOfShape aOneObjectList;
310           aOneObjectList.push_back(*anIt);
311           std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aOneObjectList, aNotUsedSolids, GeomAlgoAPI_Boolean::BOOL_CUT));
312
313           if(GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-7) {
314             aSolidsToFuse.push_back(aCutAlgo->shape());
315             aMakeShapeList.appendAlgo(aCutAlgo);
316             aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
317           }
318         }
319       }
320
321       anObjects.clear();
322       anObjects.push_back(aSolidsToFuse.back());
323       aSolidsToFuse.pop_back();
324       aTools = aSolidsToFuse;
325
326       // Fuse all objects and all tools.
327       std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects, aTools, myBooleanOperationType));
328
329       // Checking that the algorithm worked properly.
330       if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
331         static const std::string aFeatureError = "Error: Boolean algorithm failed.";
332         setError(aFeatureError);
333         return;
334       }
335
336       std::shared_ptr<GeomAPI_Shape> aShape = aFuseAlgo->shape();
337       aMakeShapeList.appendAlgo(aFuseAlgo);
338       aMapOfShapes.merge(aFuseAlgo->mapOfSubShapes());
339
340       // Add result to not used solids from compsolid (if we have any).
341       if(!aNotUsedSolids.empty()) {
342         aNotUsedSolids.push_back(aShape);
343         std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(new GeomAlgoAPI_PaveFiller(aNotUsedSolids, true));
344         if(!aFillerAlgo->isDone()) {
345           std::string aFeatureError = "Error: PaveFiller algorithm failed.";
346           setError(aFeatureError);
347           return;
348         }
349         if(aFillerAlgo->shape()->isNull()) {
350           static const std::string aShapeError = "Error: Resulting shape is Null.";
351           setError(aShapeError);
352           return;
353         }
354         if(!aFillerAlgo->isValid()) {
355           std::string aFeatureError = "Error: Resulting shape is not valid.";
356           setError(aFeatureError);
357           return;
358         }
359
360         aShape = aFillerAlgo->shape();
361         aMakeShapeList.appendAlgo(aFillerAlgo);
362         aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
363       }
364
365       std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
366       loadNamingDS(aResultBody, aShells, aSolidsAlgos, anOriginalSolids.front(), anOriginalSolids, aShape, aMakeShapeList, aMapOfShapes);
367       setResult(aResultBody, aResultIndex);
368       aResultIndex++;
369       break;
370     }
371     default: {
372       setError("Error: Wrong type of boolean operation.");
373       return;
374     }
375   }
376
377   // Remove the rest results if there were produced in the previous pass.
378   removeResults(aResultIndex);
379 }
380
381 //=================================================================================================
382 void FeaturesPlugin_CompositeBoolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
383                                                    const ListOfShape& theShells,
384                                                    ListOfMakeShape& theSolidsAlgos,
385                                                    const std::shared_ptr<GeomAPI_Shape> theBaseShape,
386                                                    const ListOfShape& theTools,
387                                                    const std::shared_ptr<GeomAPI_Shape> theResultShape,
388                                                    GeomAlgoAPI_MakeShape& theMakeShape,
389                                                    GeomAPI_DataMapOfShapeShape& theMapOfShapes)
390 {
391   //load result
392   if(theBaseShape->isEqual(theResultShape)) {
393     theResultBody->store(theResultShape);
394   } else {
395     const int aGenTag = 1;
396     const int aModTag = 2;
397     const int aDelTag = 3;
398     const int aSubsolidsTag=4; /// sub solids will be placed at labels 6, 7, etc. if result is compound of solids
399     int aToTag = 5000; // may be many labels, starting from this index
400     int aFromTag = 10000; // may be many labels, starting from this index or last aToTag index
401     const std::string aGenName = "Generated";
402     const std::string aModName = "Modified";
403     const std::string aLatName = "LateralFace";
404     const std::string aFromName = "FromFace";
405     const std::string aToName = "ToFace";
406
407     theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
408
409     ListOfShape::const_iterator aShellsIter = theShells.begin();
410     ListOfMakeShape::const_iterator aSolidsAlgosIter = theSolidsAlgos.begin();
411     for(; aShellsIter != theShells.end() && aSolidsAlgosIter != theSolidsAlgos.end(); aShellsIter++, aSolidsAlgosIter++) {
412       //Insert lateral face : Face from Edge
413       std::shared_ptr<GeomAlgoAPI_MakeShape> aSolidAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_MakeShape>(*aSolidsAlgosIter);
414       if(aSolidAlgo.get()) {
415         std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = aSolidAlgo->mapOfSubShapes();
416         theResultBody->loadAndOrientGeneratedShapes(aSolidAlgo.get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
417                                                     aLatName, *aSubShapes.get());
418
419
420         std::shared_ptr<GeomAlgoAPI_MakeSweep> aSweepAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(aSolidAlgo);
421         if(aSweepAlgo.get()) {
422           //Insert to faces
423           int aToFaceIndex = 1;
424           const ListOfShape& aToFaces = aSweepAlgo->toShapes();
425           for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
426             std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
427             if(aSubShapes->isBound(aToFace)) {
428               aToFace = aSubShapes->find(aToFace);
429             }
430             std::ostringstream aStr;
431             aStr << aToName << "_" << aToFaceIndex++;
432             theResultBody->generated(aToFace, aStr.str(), aToTag++);
433           }
434
435           //Insert from faces
436           int aFromFaceIndex = 1;
437           const ListOfShape& aFromFaces = aSweepAlgo->fromShapes();
438           if (aFromTag < aToTag) aFromTag = aToTag;
439           for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
440             std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
441             if(aSubShapes->isBound(aFromFace)) {
442               aFromFace = aSubShapes->find(aFromFace);
443             }
444             std::ostringstream aStr;
445             aStr << aFromName << "_" << aFromFaceIndex++;
446             theResultBody->generated(aFromFace, aStr.str(), aFromTag++);
447           }
448         }
449       }
450     }
451
452     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
453                                                aModTag, aModName, theMapOfShapes);
454     theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDelTag);
455
456     for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
457       theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
458                                                  aModTag, aModName, theMapOfShapes);
459       theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDelTag);
460     }
461   }
462 }