]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Boolean.cpp
Salome HOME
Multiple objects and tools for Boolean operations.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Boolean.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        FeaturesPlugin_Boolean.cpp
4 // Created:     02 Sept 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "FeaturesPlugin_Boolean.h"
8
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_AttributeReference.h>
12 #include <ModelAPI_AttributeInteger.h>
13 #include <ModelAPI_ResultBody.h>
14 #include <ModelAPI_AttributeSelectionList.h>
15
16 #include <GeomAlgoAPI_Boolean.h>
17 #include <GeomAlgoAPI_MakeShapeList.h>
18 #include <GeomAlgoAPI_ShapeProps.h>
19
20 #define FACE 4
21 #define _MODIFY_TAG 1
22 #define _DELETED_TAG 2
23
24 //=================================================================================================
25 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean()
26 {
27 }
28
29 //=================================================================================================
30 void FeaturesPlugin_Boolean::initAttributes()
31 {
32   data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
33
34   AttributeSelectionListPtr aSelection = 
35     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
36     FeaturesPlugin_Boolean::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
37   // extrusion works with faces always
38   aSelection->setSelectionType("SOLID");
39
40   aSelection = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
41     FeaturesPlugin_Boolean::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
42   // extrusion works with faces always
43   aSelection->setSelectionType("SOLID");
44 }
45
46 //=================================================================================================
47 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Boolean::getShape(const std::string& theAttrName)
48 {
49   std::shared_ptr<ModelAPI_AttributeReference> aObjRef = std::dynamic_pointer_cast<
50       ModelAPI_AttributeReference>(data()->attribute(theAttrName));
51   if (aObjRef) {
52     std::shared_ptr<ModelAPI_ResultBody> aConstr = std::dynamic_pointer_cast<
53         ModelAPI_ResultBody>(aObjRef->value());
54     if (aConstr)
55       return aConstr->shape();
56   }
57   return std::shared_ptr<GeomAPI_Shape>();
58 }
59
60 //=================================================================================================
61 void FeaturesPlugin_Boolean::execute()
62 {
63   // Getting operation type.
64   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
65       ModelAPI_AttributeInteger>(data()->attribute(FeaturesPlugin_Boolean::TYPE_ID()));
66   if (!aTypeAttr)
67     return;
68   int aType = aTypeAttr->value();
69
70   ListOfShape anObjects, aTools;
71
72   // Getting objects.
73   AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
74   if (anObjectsSelList->size() == 0) {
75     return;
76   }
77   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
78     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
79     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
80     if(!anObject.get()) {
81       return;
82     }
83     anObjects.push_back(anObject);
84   }
85
86   // Getting tools.
87   AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
88   if (aToolsSelList->size() == 0) {
89     return;
90   }
91   for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
92     std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
93     std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
94     if(!aTool.get()) {
95       return;
96     }
97     aTools.push_back(aTool);
98   }
99
100   int aResultIndex = 0;
101   ListOfMakeShape aListOfMakeShape;
102   std::shared_ptr<GeomAPI_Shape> aResShape;
103   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aDataMapOfShapes;
104
105   switch(aType) {
106     case GeomAlgoAPI_Boolean::BOOL_CUT: {
107       // Cut each object with all tools
108       for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
109         std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
110         aResShape = anObject;
111         ListOfShape aSingleObjectList; aSingleObjectList.push_back(anObject);
112         for(ListOfShape::iterator aToolsIt = aTools.begin(); aToolsIt != aTools.end(); aToolsIt++) {
113           std::shared_ptr<GeomAPI_Shape> aTool = *aToolsIt;
114           GeomAlgoAPI_Boolean* aBoolAlgo = new GeomAlgoAPI_Boolean(aResShape, aTool, aType);
115           if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
116             static const std::string aBoolAlgoError = "Boolean feature: algorithm failed";
117             setError(aBoolAlgoError);
118             return;
119           }
120           aListOfMakeShape.push_back(aBoolAlgo->makeShape());
121           aResShape = aBoolAlgo->shape();
122           aBoolAlgo->mapOfShapes(aDataMapOfShapes);
123         }
124
125         if(GeomAlgoAPI_ShapeProps::volume(aResShape) > 10.e-5) {
126           std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
127           std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList = std::shared_ptr<GeomAlgoAPI_MakeShapeList>(
128             new GeomAlgoAPI_MakeShapeList(aListOfMakeShape));
129
130           LoadNamingDS(aMakeShapeList, aResultBody, aResShape, aDataMapOfShapes, aSingleObjectList, aTools);
131           setResult(aResultBody, aResultIndex);
132           aResultIndex++;
133         }
134       }
135       break;
136     }
137     case GeomAlgoAPI_Boolean::BOOL_FUSE: {
138       // Fuse all objects.
139       std::shared_ptr<GeomAPI_Shape> aTempRes = anObjects.front();
140       for(ListOfShape::iterator anObjectsIt = ++anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
141         std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
142         GeomAlgoAPI_Boolean* aBoolAlgo = new GeomAlgoAPI_Boolean(aTempRes, anObject, aType);
143         if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
144           static const std::string aBoolAlgoError = "Boolean feature: algorithm failed";
145           setError(aBoolAlgoError);
146           return;
147         }
148         aListOfMakeShape.push_back(aBoolAlgo->makeShape());
149         aTempRes = aBoolAlgo->shape();
150       }
151
152       // Fuse all tools with result of objects fuse.
153       for(ListOfShape::iterator aToolsIt = aTools.begin(); aToolsIt != aTools.end(); aToolsIt++) {
154         std::shared_ptr<GeomAPI_Shape> aTool = *aToolsIt;
155         GeomAlgoAPI_Boolean* aBoolAlgo = new GeomAlgoAPI_Boolean(aTempRes, aTool, aType);
156         if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
157           static const std::string aBoolAlgoError = "Boolean feature: algorithm failed";
158           setError(aBoolAlgoError);
159           return;
160         }
161         aListOfMakeShape.push_back(aBoolAlgo->makeShape());
162         aTempRes = aBoolAlgo->shape();
163         aBoolAlgo->mapOfShapes(aDataMapOfShapes);
164       }
165       aResShape = aTempRes;
166
167       std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
168       std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList = std::shared_ptr<GeomAlgoAPI_MakeShapeList>(
169         new GeomAlgoAPI_MakeShapeList(aListOfMakeShape));
170
171       LoadNamingDS(aMakeShapeList, aResultBody, aResShape, aDataMapOfShapes, anObjects, aTools);
172       setResult(aResultBody);
173       aResultIndex++;
174       break;
175     }
176     case GeomAlgoAPI_Boolean::BOOL_COMMON: {
177       // Search common between object and tool and fuse them.
178       for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
179         std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
180         std::shared_ptr<GeomAPI_Shape> aTempRes;
181         ListOfShape aSingleObjectList; aSingleObjectList.push_back(anObject);
182         for(ListOfShape::iterator aToolsIt = aTools.begin(); aToolsIt != aTools.end(); aToolsIt++) {
183           std::shared_ptr<GeomAPI_Shape> aTool = *aToolsIt;
184           GeomAlgoAPI_Boolean* aBoolAlgo1 = new GeomAlgoAPI_Boolean(anObject, aTool, aType);
185           if(!aBoolAlgo1->isDone() || aBoolAlgo1->shape()->isNull() || !aBoolAlgo1->isValid()) {
186             static const std::string aBoolAlgoError = "Boolean feature: algorithm failed";
187             setError(aBoolAlgoError);
188             return;
189           }
190           aListOfMakeShape.push_back(aBoolAlgo1->makeShape());
191
192           if(!aTempRes) {
193             aTempRes = aBoolAlgo1->shape();
194             aBoolAlgo1->mapOfShapes(aDataMapOfShapes);
195           } else {
196             // Fuse common result with previous common results.
197             GeomAlgoAPI_Boolean* aBoolAlgo2 = new GeomAlgoAPI_Boolean(aTempRes,
198                                                                       aBoolAlgo1->shape(),
199                                                                       GeomAlgoAPI_Boolean::BOOL_FUSE);
200             if(!aBoolAlgo1->isDone() || aBoolAlgo1->shape()->isNull() || !aBoolAlgo1->isValid()) {
201               static const std::string aBoolAlgoError = "Boolean feature: algorithm failed";
202               setError(aBoolAlgoError);
203               return;
204             }
205             aListOfMakeShape.push_back(aBoolAlgo2->makeShape());
206             aTempRes = aBoolAlgo2->shape();
207             aBoolAlgo2->mapOfShapes(aDataMapOfShapes);
208           }
209         }
210         aResShape = aTempRes;
211
212         if(GeomAlgoAPI_ShapeProps::volume(aResShape) > 10.e-5) {
213           std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
214           std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList = std::shared_ptr<GeomAlgoAPI_MakeShapeList>(
215             new GeomAlgoAPI_MakeShapeList(aListOfMakeShape));
216
217           LoadNamingDS(aMakeShapeList, aResultBody, aResShape, aDataMapOfShapes, aSingleObjectList, aTools);
218           setResult(aResultBody, aResultIndex);
219           aResultIndex++;
220         }
221       }
222       break;
223     }
224   }
225   // remove the rest results if there were produced in the previous pass
226   removeResults(aResultIndex);
227 }
228
229 //=================================================================================================
230 void FeaturesPlugin_Boolean::LoadNamingDS(std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList,
231                                           std::shared_ptr<ModelAPI_ResultBody> theResultBody,
232                                           std::shared_ptr<GeomAPI_Shape> theResult,
233                                           std::shared_ptr<GeomAPI_DataMapOfShapeShape> theDataMapOfShapes,
234                                           const ListOfShape& theObjects,
235                                           const ListOfShape& theTools)
236 {
237   //load result
238   theResultBody->storeModified(theObjects.front(), theResult);
239
240   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
241
242   std::string aModName = "Modified";
243   for(ListOfShape::const_iterator anIter = theObjects.begin(); anIter != theObjects.end(); anIter++) {
244     theResultBody->loadAndOrientModifiedShapes(theMakeShapeList.get(), *anIter, FACE, _MODIFY_TAG, aModName, *theDataMapOfShapes.get());
245     theResultBody->loadDeletedShapes(theMakeShapeList.get(), *anIter, FACE, _DELETED_TAG);
246   }
247
248   for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
249     theResultBody->loadAndOrientModifiedShapes(theMakeShapeList.get(), *anIter, FACE, _MODIFY_TAG, aModName, *theDataMapOfShapes.get());
250     theResultBody->loadDeletedShapes(theMakeShapeList.get(), *anIter, FACE, _DELETED_TAG);
251   }
252 }