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