Salome HOME
f0876e1f12201d16fa004018d7297f07b0b6918c
[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   if(aShells.empty()) {
130     aShells = aFreeFaces;
131   } else {
132     aShells.merge(aFreeFaces);
133   }
134
135   // Pass shells/faces to soldis creation function.
136   ListOfShape aBooleanTools;
137   std::list<std::shared_ptr<GeomAPI_Interface>> aSolidsAlgos;
138   makeSolids(aShells, aBooleanTools, aSolidsAlgos);
139   if(aBooleanTools.empty()) {
140     return;
141   }
142
143   // Getting objects for boolean operation.
144   ListOfShape aBooleanObjects;
145   AttributeSelectionListPtr anObjectsSelList = selectionList(BOOLEAN_OBJECTS_ID());
146   if (anObjectsSelList->size() == 0) {
147     return;
148   }
149   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
150     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
151     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
152     if(!anObject.get()) {
153       return;
154     }
155     aBooleanObjects.push_back(anObject);
156   }
157
158   // Cut from each object solids.
159   int aResultIndex = 0;
160
161   switch(myBooleanOperationType) {
162     case GeomAlgoAPI_Boolean::BOOL_CUT:
163     case GeomAlgoAPI_Boolean::BOOL_COMMON:{
164       // Cut each object with all tools
165       for(ListOfShape::iterator anObjectsIt = aBooleanObjects.begin(); anObjectsIt != aBooleanObjects.end(); anObjectsIt++) {
166         std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
167         ListOfShape aListWithObject;
168         aListWithObject.push_back(anObject);
169         GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aBooleanTools, myBooleanOperationType);
170
171         // Checking that the algorithm worked properly.
172         if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
173           setError("Boolean algorithm failed");
174           return;
175         }
176
177         if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo.shape()) > 1.e-7) {
178           std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
179           loadNamingDS(aResultBody, anObject, aShells, aSolidsAlgos, aBooleanTools, aBoolAlgo);
180           setResult(aResultBody, aResultIndex);
181           aResultIndex++;
182         }
183       }
184       break;
185     }
186     case GeomAlgoAPI_Boolean::BOOL_FUSE: {
187       // Fuse all objects and all tools.
188       GeomAlgoAPI_Boolean aBoolAlgo(aBooleanObjects, aBooleanTools, myBooleanOperationType);
189
190       // Checking that the algorithm worked properly.
191       if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
192         setError("Boolean algorithm failed");
193         return;
194       }
195
196       std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
197       loadNamingDS(aResultBody, aBooleanObjects.front(), aShells, aSolidsAlgos, aBooleanTools, aBoolAlgo);
198       setResult(aResultBody, aResultIndex);
199       aResultIndex++;
200       break;
201     }
202     default: {
203       setError("Error: wrong type of boolean operation");
204       return;
205     }
206   }
207
208   // Remove the rest results if there were produced in the previous pass.
209   removeResults(aResultIndex);
210 }
211
212 //=================================================================================================
213 void FeaturesPlugin_CompositeBoolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
214                                                    const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
215                                                    const ListOfShape& theShells,
216                                                    const std::list<std::shared_ptr<GeomAPI_Interface>>& theSolidsAlgos,
217                                                    const ListOfShape& theTools,
218                                                    const GeomAlgoAPI_Boolean& theAlgo)
219 {
220   //load result
221   if(theBaseShape->isEqual(theAlgo.shape())) {
222     theResultBody->store(theAlgo.shape());
223   } else {
224     const int aGenTag = 1;
225     const int aFromTag = 2;
226     const int aToTag = 3;
227     const int aModTag = 4;
228     const int aDelTag = 5;
229     const int aSubsolidsTag=6; /// sub solids will be placed at labels 6, 7, etc. if result is compound of solids
230     const std::string aGenName = "Generated";
231     const std::string aModName = "Modified";
232     const std::string aLatName = "LateralFace";
233     const std::string aFromName = "FromFace";
234     const std::string aToName = "ToFace";
235
236     theResultBody->storeModified(theBaseShape, theAlgo.shape(), aSubsolidsTag);
237
238     ListOfShape::const_iterator aShellsIter = theShells.begin();
239     std::list<std::shared_ptr<GeomAPI_Interface>>::const_iterator aSolidsAlgosIter = theSolidsAlgos.begin();
240     for(; aShellsIter != theShells.end() && aSolidsAlgosIter != theSolidsAlgos.end(); aShellsIter++, aSolidsAlgosIter++) {
241       ListOfShape aFromFaces;
242       ListOfShape aToFaces;
243       std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes;
244
245       //Insert lateral face : Face from Edge
246       if(std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(*aSolidsAlgosIter)) {
247         std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_Prism>(*aSolidsAlgosIter);
248         aSubShapes = aPrismAlgo->mapOfShapes();
249         theResultBody->loadAndOrientGeneratedShapes(aPrismAlgo->makeShape().get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
250                                                     aLatName, *aSubShapes.get());
251
252         aFromFaces = aPrismAlgo->fromFaces();
253         aToFaces = aPrismAlgo->toFaces();
254       } else if(std::dynamic_pointer_cast<GeomAlgoAPI_Revolution>(*aSolidsAlgosIter)) {
255         std::shared_ptr<GeomAlgoAPI_Revolution> aRevolAlgo = std::dynamic_pointer_cast<GeomAlgoAPI_Revolution>(*aSolidsAlgosIter);
256         aSubShapes = aRevolAlgo->mapOfShapes();
257         theResultBody->loadAndOrientGeneratedShapes(aRevolAlgo->makeShape().get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag,
258                                                     aLatName, *aSubShapes.get());
259         aFromFaces = aRevolAlgo->fromFaces();
260         aToFaces = aRevolAlgo->toFaces();
261       }
262
263       //Insert to faces
264       for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
265         std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
266         if(aSubShapes->isBound(aToFace)) {
267           aToFace = aSubShapes->find(aToFace);
268         }
269         theResultBody->generated(aToFace, aToName, aToTag);
270       }
271
272       //Insert from faces
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 }