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