Salome HOME
Make Sketch of Complex boolean as sub-object of the complex Boolean feature
[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_ResultBody.h>
12 #include <ModelAPI_ResultConstruction.h>
13
14 #include <GeomAlgoAPI_ShapeProps.h>
15
16 //=================================================================================================
17 void FeaturesPlugin_CompositeBoolean::initAttributes()
18 {
19   data()->addAttribute(SKETCH_OBJECT_ID(), ModelAPI_AttributeReference::typeId());
20
21   // Boolean works with solids always.
22   data()->addAttribute(BOOLEAN_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
23   AttributeSelectionListPtr aSelection = data()->selectionList(BOOLEAN_OBJECTS_ID());
24   aSelection->setSelectionType("SOLID");
25
26   initMakeSolidsAttributes();
27 }
28
29 //=================================================================================================
30 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::addFeature(std::string theID)
31 {
32   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
33   if (aNew) {
34     data()->reference(SKETCH_OBJECT_ID())->setValue(aNew);
35   }
36    // set as current also after it becomes sub to set correctly enabled for other sketch subs
37   document()->setCurrentFeature(aNew, false);
38   return aNew;
39 }
40
41 //=================================================================================================
42 int FeaturesPlugin_CompositeBoolean::numberOfSubs(bool forTree) const
43 {
44   ObjectPtr aObj = data()->reference(SKETCH_OBJECT_ID())->value();
45   return aObj.get()? 1 : 0;
46 }
47
48 //=================================================================================================
49 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeBoolean::subFeature(const int theIndex, bool forTree) const
50 {
51   if (theIndex == 0)
52     return std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_OBJECT_ID())->value());
53   return std::shared_ptr<ModelAPI_Feature>();
54 }
55
56 //=================================================================================================
57 int FeaturesPlugin_CompositeBoolean::subFeatureId(const int theIndex) const
58 {
59   std::shared_ptr<ModelAPI_Feature> aFeature = subFeature(theIndex);
60   if (aFeature.get())
61     return aFeature->data()->featureId();
62   return -1;
63 }
64
65 //=================================================================================================
66 bool FeaturesPlugin_CompositeBoolean::isSub(ObjectPtr theObject) const
67 {
68   // check is this feature of result
69   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
70   if (!aFeature)
71     return false;
72  
73   ObjectPtr aSub = data()->reference(SKETCH_OBJECT_ID())->value();
74   return aSub == theObject;
75 }
76
77 //=================================================================================================
78 void FeaturesPlugin_CompositeBoolean::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
79 {
80 }
81
82 //=================================================================================================
83 void FeaturesPlugin_CompositeBoolean::execute()
84 {
85   // Getting faces to create solids.
86   std::shared_ptr<ModelAPI_Feature> aSketchFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
87                                                      reference(SKETCH_OBJECT_ID())->value());
88   if(!aSketchFeature) {
89     return;
90   }
91   ResultPtr aSketchRes = aSketchFeature->results().front();
92   ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
93   if(!aConstruction.get()) {
94     return;
95   }
96   int aSketchFacesNum = aConstruction->facesNum();
97   if(aSketchFacesNum == 0) {
98     return; //TODO: set error message
99   }
100   ListOfShape aSketchFacesList;
101   for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
102     std::shared_ptr<GeomAPI_Shape> aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
103     aSketchFacesList.push_back(aFace);
104   }
105
106   // Pass faces to soldis creation function.
107   ListOfShape aBooleanTools = MakeSolids(aSketchFacesList);
108   if(aBooleanTools.empty()) {
109     return;
110   }
111
112   // Getting objects for boolean operation.
113   ListOfShape aBooleanObjects;
114   AttributeSelectionListPtr anObjectsSelList = selectionList(BOOLEAN_OBJECTS_ID());
115   if (anObjectsSelList->size() == 0) {
116     return;
117   }
118   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
119     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
120     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
121     if(!anObject.get()) {
122       return;
123     }
124     aBooleanObjects.push_back(anObject);
125   }
126
127   // Cut from each object solids.
128   int aResultIndex = 0;
129
130   switch(myBooleanOperationType) {
131     case GeomAlgoAPI_Boolean::BOOL_CUT:
132     case GeomAlgoAPI_Boolean::BOOL_COMMON:{
133       // Cut each object with all tools
134       for(ListOfShape::iterator anObjectsIt = aBooleanObjects.begin(); anObjectsIt != aBooleanObjects.end(); anObjectsIt++) {
135         std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
136         ListOfShape aListWithObject;
137         aListWithObject.push_back(anObject);
138         GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aBooleanTools, myBooleanOperationType);
139
140         // Checking that the algorithm worked properly.
141         if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) {
142           setError("Boolean algorithm failed");
143           return;
144         }
145
146         if(GeomAlgoAPI_ShapeProps::volume(aBoolAlgo.shape()) > 1.e-7) {
147           std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
148           LoadNamingDS(aResultBody, anObject, aBooleanTools, aBoolAlgo);
149           setResult(aResultBody, aResultIndex);
150           aResultIndex++;
151         }
152       }
153       break;
154     }
155     case GeomAlgoAPI_Boolean::BOOL_FUSE: {
156       // Fuse all objects and all tools.
157       GeomAlgoAPI_Boolean aBoolAlgo(aBooleanObjects, 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       std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
166       LoadNamingDS(aResultBody, aBooleanObjects.front(), aBooleanTools, aBoolAlgo);
167       setResult(aResultBody, aResultIndex);
168       aResultIndex++;
169       break;
170     }
171     default: {
172       setError("Error: wrong type of boolean operation");
173       return;
174     }
175   }
176
177   // Remove the rest results if there were produced in the previous pass.
178   removeResults(aResultIndex);
179 }
180
181 //=================================================================================================
182 void FeaturesPlugin_CompositeBoolean::LoadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
183                                                    const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
184                                                    const ListOfShape& theTools,
185                                                    const GeomAlgoAPI_Boolean& theAlgo)
186 {
187   //load result
188   if(theBaseShape->isEqual(theAlgo.shape())) {
189     theResultBody->store(theAlgo.shape());
190   } else {
191     theResultBody->storeModified(theBaseShape, theAlgo.shape());
192
193     GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
194
195     const int aModTag = 1;
196     const int aDeleteTag = 2;
197     const std::string aModName = "Modified";
198     theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE,
199                                                aModTag, aModName, *theAlgo.mapOfShapes().get());
200     theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE, aDeleteTag);
201
202     for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
203       theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE,
204                                                  aModTag, aModName, *theAlgo.mapOfShapes().get());
205       theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE, aDeleteTag);
206     }
207   }
208 }