Salome HOME
04785ca6393c3eebcb6d2bc051700c26bdb9df41
[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_BodyBuilder.h>
12 #include <ModelAPI_ResultBody.h>
13 #include <ModelAPI_ResultConstruction.h>
14
15 #include <GeomAlgoAPI_CompoundBuilder.h>
16 #include <GeomAlgoAPI_Prism.h>
17 #include <GeomAlgoAPI_Revolution.h>
18 #include <GeomAlgoAPI_ShapeTools.h>
19
20 //=================================================================================================
21 void FeaturesPlugin_CompositeBoolean::initAttributes()
22 {
23   data()->addAttribute(SKETCH_OBJECT_ID(), ModelAPI_AttributeReference::typeId());
24
25   // Boolean works with solids always.
26   data()->addAttribute(BOOLEAN_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
27   AttributeSelectionListPtr aSelection = data()->selectionList(BOOLEAN_OBJECTS_ID());
28   aSelection->setSelectionType("SOLID");
29
30   initMakeSolidsAttributes();
31 }
32
33 //=================================================================================================
34 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::addFeature(std::string theID)
35 {
36   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
37   if (aNew) {
38     data()->reference(SKETCH_OBJECT_ID())->setValue(aNew);
39   }
40   // set as current also after it becomes sub to set correctly enabled for other sketch subs
41   document()->setCurrentFeature(aNew, false);
42   return aNew;
43 }
44
45 //=================================================================================================
46 int FeaturesPlugin_CompositeBoolean::numberOfSubs(bool forTree) const
47 {
48   ObjectPtr aObj = data()->reference(SKETCH_OBJECT_ID())->value();
49   return aObj.get()? 1 : 0;
50 }
51
52 //=================================================================================================
53 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::subFeature(const int theIndex, bool forTree)
54 {
55   if (theIndex == 0)
56     return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
57   return std::shared_ptr<ModelAPI_Feature>();
58 }
59
60 //=================================================================================================
61 int FeaturesPlugin_CompositeBoolean::subFeatureId(const int theIndex) const
62 {
63   if (theIndex == 0) {
64     FeaturePtr aFeature = 
65       std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
66     if (aFeature.get())
67       return aFeature->data()->featureId();
68   }
69   return -1;
70 }
71
72 //=================================================================================================
73 bool FeaturesPlugin_CompositeBoolean::isSub(ObjectPtr theObject) const
74 {
75   // check is this feature of result
76   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
77   if (!aFeature)
78     return false;
79  
80   ObjectPtr aSub = data()->reference(SKETCH_OBJECT_ID())->value();
81   return aSub == theObject;
82 }
83
84 //=================================================================================================
85 void FeaturesPlugin_CompositeBoolean::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
86 {
87 }
88
89 //=================================================================================================
90 void FeaturesPlugin_CompositeBoolean::erase()
91 {
92   if (data().get() && data()->isValid()) { // on abort of sketch of this composite it may be invalid
93     FeaturePtr aSketch =
94       std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
95     if (aSketch.get() && aSketch->data()->isValid()) {
96       document()->removeFeature(aSketch);
97     }
98   }
99   ModelAPI_CompositeFeature::erase();
100 }
101
102
103 //=================================================================================================
104 void FeaturesPlugin_CompositeBoolean::execute()
105 {
106   // Getting faces to create solids.
107   std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
108                                                      reference(SKETCH_OBJECT_ID())->value());
109   if(!aSketchFeature || aSketchFeature->results().empty()) {
110     return;
111   }
112   ResultPtr aSketchRes = aSketchFeature->results().front();
113   ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
114   if(!aConstruction.get()) {
115     return;
116   }
117   int aSketchFacesNum = aConstruction->facesNum();
118   if(aSketchFacesNum == 0) {
119     return;
120   }
121   ListOfShape aFacesList;
122   for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
123     std::shared_ptr<GeomAPI_Shape> aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
124     aFacesList.push_back(aFace);
125   }
126
127   // Searching faces with common edges.
128   ListOfShape aShells;
129   ListOfShape aFreeFaces;
130   std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
131   GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
132   aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
133
134   // Pass shells/faces to solids creation function.
135   ListOfShape aBooleanTools;
136   std::list<std::shared_ptr<GeomAPI_Interface>> aSolidsAlgos;
137   makeSolids(aShells, aBooleanTools, aSolidsAlgos);
138   if(aBooleanTools.empty()) {
139     return;
140   }
141
142   // Getting objects for boolean operation.
143   ListOfShape aBooleanObjects;
144   AttributeSelectionListPtr anObjectsSelList = selectionList(BOOLEAN_OBJECTS_ID());
145   if (anObjectsSelList->size() == 0) {
146     return;
147   }
148   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
149     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
150     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
151     if(!anObject.get()) {
152       return;
153     }
154     aBooleanObjects.push_back(anObject);
155   }
156
157   // Cut from each object solids.
158   int aResultIndex = 0;
159
160   switch(myBooleanOperationType) {
161     case GeomAlgoAPI_Boolean::BOOL_CUT:
162     case GeomAlgoAPI_Boolean::BOOL_COMMON:{
163       // Cut each object with all tools
164       for(ListOfShape::iterator anObjectsIt = aBooleanObjects.begin(); anObjectsIt != aBooleanObjects.end(); anObjectsIt++) {
165         std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
166         ListOfShape aListWithObject;
167         aListWithObject.push_back(anObject);
168         GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aBooleanTools, myBooleanOperationType);
169
170         // Checking that the algorithm worked properly.
171         if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
172           setError("Boolean algorithm failed");
173           return;
174         }
175
176         if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo.shape()) > 1.e-7) {
177           std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
178           loadNamingDS(aResultBody, anObject, aShells, aSolidsAlgos, aBooleanTools, aBoolAlgo);
179           setResult(aResultBody, aResultIndex);
180           aResultIndex++;
181         }
182       }
183       break;
184     }
185     case GeomAlgoAPI_Boolean::BOOL_FUSE: {
186       // Fuse all objects and all tools.
187       GeomAlgoAPI_Boolean aBoolAlgo(aBooleanObjects, aBooleanTools, myBooleanOperationType);
188
189       // Checking that the algorithm worked properly.
190       if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
191         setError("Boolean algorithm failed");
192         return;
193       }
194
195       std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
196       loadNamingDS(aResultBody, aBooleanObjects.front(), aShells, aSolidsAlgos, aBooleanTools, aBoolAlgo);
197       setResult(aResultBody, aResultIndex);
198       aResultIndex++;
199       break;
200     }
201     default: {
202       setError("Error: wrong type of boolean operation");
203       return;
204     }
205   }
206
207   // Remove the rest results if there were produced in the previous pass.
208   removeResults(aResultIndex);
209 }
210
211 //=================================================================================================
212 void FeaturesPlugin_CompositeBoolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
213                                                    const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
214                                                    const ListOfShape& theShells,
215                                                    const std::list<std::shared_ptr<GeomAPI_Interface>>& theSolidsAlgos,
216                                                    const ListOfShape& theTools,
217                                                    const GeomAlgoAPI_Boolean& theAlgo)
218 {
219   //load result
220   if(theBaseShape->isEqual(theAlgo.shape())) {
221     theResultBody->store(theAlgo.shape());
222   } else {
223     const int aGenTag = 1;
224     const int aModTag = 2;
225     const int aDelTag = 3;
226     const int aSubsolidsTag=4; /// sub solids will be placed at labels 6, 7, etc. if result is compound of solids
227     int aToTag = 5; // may be many labels, starting from this index
228     int aFromTag = 10000; // may be many labels, starting from this index or last aToTag index
229     const std::string aGenName = "Generated";
230     const std::string aModName = "Modified";
231     const std::string aLatName = "LateralFace";
232     const std::string aFromName = "FromFace";
233     const std::string aToName = "ToFace";
234
235     theResultBody->storeModified(theBaseShape, theAlgo.shape(), aSubsolidsTag);
236
237     ListOfShape::const_iterator aShellsIter = theShells.begin();
238     std::list<std::shared_ptr<GeomAPI_Interface>>::const_iterator aSolidsAlgosIter = theSolidsAlgos.begin();
239     for(; aShellsIter != theShells.end() && aSolidsAlgosIter != theSolidsAlgos.end(); aShellsIter++, aSolidsAlgosIter++) {
240       ListOfShape aFromFaces;
241       ListOfShape aToFaces;
242       std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes;
243
244       //Insert lateral face : Face from Edge
245       if(std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(*aSolidsAlgosIter)) {
246         std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(*aSolidsAlgosIter);
247         aSubShapes = aPrismAlgo->mapOfShapes();
248         theResultBody->loadAndOrientGeneratedShapes(aPrismAlgo->makeShape().get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
249                                                     aLatName, *aSubShapes.get());
250
251         aFromFaces = aPrismAlgo->fromFaces();
252         aToFaces = aPrismAlgo->toFaces();
253       } else if(std::dynamic_pointer_cast<GeomAlgoAPI_Revolution>(*aSolidsAlgosIter)) {
254         std::shared_ptr<GeomAlgoAPI_Revolution> aRevolAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_Revolution>(*aSolidsAlgosIter);
255         aSubShapes = aRevolAlgo->mapOfShapes();
256         theResultBody->loadAndOrientGeneratedShapes(aRevolAlgo->makeShape().get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
257                                                     aLatName, *aSubShapes.get());
258         aFromFaces = aRevolAlgo->fromFaces();
259         aToFaces = aRevolAlgo->toFaces();
260       }
261
262       //Insert to faces
263       for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
264         std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
265         if(aSubShapes->isBound(aToFace)) {
266           aToFace = aSubShapes->find(aToFace);
267         }
268         theResultBody->generated(aToFace, aToName, aToTag++);
269       }
270
271       //Insert from faces
272       if (aFromTag < aToTag) aFromTag = aToTag;
273       for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
274         std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
275         if(aSubShapes->isBound(aFromFace)) {
276           aFromFace = aSubShapes->find(aFromFace);
277         }
278         theResultBody->generated(aFromFace, aFromName, aFromTag++);
279       }
280     }
281
282     theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE,
283                                                aModTag, aModName, *theAlgo.mapOfShapes().get());
284     theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE, aDelTag);
285
286     for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
287       theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE,
288                                                  aModTag, aModName, *theAlgo.mapOfShapes().get());
289       theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE, aDelTag);
290     }
291   }
292 }