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