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