]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Boolean.cpp
Salome HOME
c2a4ea46b02c6eeeb91437110c2438c28094272c
[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   GeomAlgoAPI_Boolean::OperationType aType = (GeomAlgoAPI_Boolean::OperationType)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     case GeomAlgoAPI_Boolean::BOOL_COMMON:{
108       // Cut each object with all tools
109       for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
110         std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
111         ListOfShape aListWithObject;
112         aListWithObject.push_back(anObject);
113         aResShape = anObject;
114         GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aTools, aType);
115
116         // Checking that the algorithm worked properly.
117         if(!aBoolAlgo.isDone()) {
118           static const std::string aFeatureError = "Boolean algorithm failed";
119           setError(aFeatureError);
120           return;
121         }
122         if(aBoolAlgo.shape()->isNull()) {
123           static const std::string aShapeError = "Resulting shape is Null";
124           setError(aShapeError);
125           return;
126         }
127         if(!aBoolAlgo.isValid()) {
128           std::string aFeatureError = "Warning: resulting shape is not valid";
129           setError(aFeatureError);
130           return;
131         }
132
133         if(GeomAlgoAPI_ShapeProps::volume(aBoolAlgo.shape()) > 1.e-7) {
134           std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
135           std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList = std::shared_ptr<GeomAlgoAPI_MakeShapeList>(
136             new GeomAlgoAPI_MakeShapeList(aListOfMakeShape));
137
138           LoadNamingDS(aResultBody, anObject, aTools, aBoolAlgo);
139           setResult(aResultBody, aResultIndex);
140           aResultIndex++;
141         }
142       }
143       break;
144     }
145     case GeomAlgoAPI_Boolean::BOOL_FUSE: {
146       // Fuse all objects and all tools.
147       GeomAlgoAPI_Boolean aBoolAlgo(anObjects, aTools, aType);
148
149       // Checking that the algorithm worked properly.
150       if(!aBoolAlgo.isDone()) {
151         static const std::string aFeatureError = "Boolean algorithm failed";
152         setError(aFeatureError);
153         return;
154       }
155       if(aBoolAlgo.shape()->isNull()) {
156         static const std::string aShapeError = "Resulting shape is Null";
157         setError(aShapeError);
158         return;
159       }
160       if(!aBoolAlgo.isValid()) {
161         std::string aFeatureError = "Warning: resulting shape is not valid";
162         setError(aFeatureError);
163         return;
164       }
165
166       std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
167       std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList = std::shared_ptr<GeomAlgoAPI_MakeShapeList>(
168         new GeomAlgoAPI_MakeShapeList(aListOfMakeShape));
169
170       LoadNamingDS(aResultBody, anObjects.front(), aTools, aBoolAlgo);
171       setResult(aResultBody, aResultIndex);
172       aResultIndex++;
173       break;
174     }
175     default: {
176       std::string anOperationError = "Error: wrong type of operation";
177       setError(anOperationError);
178       return;
179     }
180   }
181   // remove the rest results if there were produced in the previous pass
182   removeResults(aResultIndex);
183 }
184
185 //=================================================================================================
186 void FeaturesPlugin_Boolean::LoadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
187                                           const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
188                                           const ListOfShape& theTools,
189                                           const GeomAlgoAPI_Boolean& theAlgo)
190 {
191   //load result
192   if(theBaseShape->isEqual(theAlgo.shape())) {
193     theResultBody->store(theAlgo.shape());
194   } else {
195     theResultBody->storeModified(theBaseShape, theAlgo.shape());
196
197     GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
198
199     std::string aModName = "Modified";
200     theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), theBaseShape, FACE,
201                                                _MODIFY_TAG, aModName, *theAlgo.mapOfShapes().get());
202     theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), theBaseShape, FACE, _DELETED_TAG);
203
204     for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
205       theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), *anIter, FACE,
206                                                  _MODIFY_TAG, aModName, *theAlgo.mapOfShapes().get());
207       theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), *anIter, FACE, _DELETED_TAG);
208     }
209   }
210 }