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