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