]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Boolean.cpp
Salome HOME
bfb8655f400137167bffbb331412d2ac71297489
[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 #define _SUBSOLIDS_TAG 3 /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
24
25 //=================================================================================================
26 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean()
27 {
28 }
29
30 //=================================================================================================
31 void FeaturesPlugin_Boolean::initAttributes()
32 {
33   data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
34
35   AttributeSelectionListPtr aSelection = 
36     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
37     FeaturesPlugin_Boolean::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
38   // extrusion works with faces always
39   aSelection->setSelectionType("SOLID");
40
41   aSelection = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
42     FeaturesPlugin_Boolean::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
43   // extrusion works with faces always
44   aSelection->setSelectionType("SOLID");
45 }
46
47 //=================================================================================================
48 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Boolean::getShape(const std::string& theAttrName)
49 {
50   std::shared_ptr<ModelAPI_AttributeReference> aObjRef = std::dynamic_pointer_cast<
51       ModelAPI_AttributeReference>(data()->attribute(theAttrName));
52   if (aObjRef) {
53     std::shared_ptr<ModelAPI_ResultBody> aConstr = std::dynamic_pointer_cast<
54         ModelAPI_ResultBody>(aObjRef->value());
55     if (aConstr)
56       return aConstr->shape();
57   }
58   return std::shared_ptr<GeomAPI_Shape>();
59 }
60
61 //=================================================================================================
62 void FeaturesPlugin_Boolean::execute()
63 {
64   // Getting operation type.
65   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
66       ModelAPI_AttributeInteger>(data()->attribute(FeaturesPlugin_Boolean::TYPE_ID()));
67   if (!aTypeAttr)
68     return;
69   GeomAlgoAPI_Boolean::OperationType aType = (GeomAlgoAPI_Boolean::OperationType)aTypeAttr->value();
70
71   ListOfShape anObjects, aTools;
72
73   // Getting objects.
74   AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
75   if (anObjectsSelList->size() == 0) {
76     return;
77   }
78   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
79     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
80     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
81     if(!anObject.get()) {
82       return;
83     }
84     anObjects.push_back(anObject);
85   }
86
87   // Getting tools.
88   AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
89   if (aToolsSelList->size() == 0) {
90     return;
91   }
92   for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
93     std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
94     std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
95     if(!aTool.get()) {
96       return;
97     }
98     aTools.push_back(aTool);
99   }
100
101   int aResultIndex = 0;
102
103   switch(aType) {
104     case GeomAlgoAPI_Boolean::BOOL_CUT:
105     case GeomAlgoAPI_Boolean::BOOL_COMMON:{
106       // Cut each object with all tools
107       for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
108         std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
109         ListOfShape aListWithObject;
110         aListWithObject.push_back(anObject);
111         GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aTools, aType);
112
113         // Checking that the algorithm worked properly.
114         if(!aBoolAlgo.isDone()) {
115           static const std::string aFeatureError = "Boolean algorithm failed";
116           setError(aFeatureError);
117           return;
118         }
119         if(aBoolAlgo.shape()->isNull()) {
120           static const std::string aShapeError = "Resulting shape is Null";
121           setError(aShapeError);
122           return;
123         }
124         if(!aBoolAlgo.isValid()) {
125           std::string aFeatureError = "Warning: resulting shape is not valid";
126           setError(aFeatureError);
127           return;
128         }
129
130         if(GeomAlgoAPI_ShapeProps::volume(aBoolAlgo.shape()) > 1.e-7) {
131           std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
132           LoadNamingDS(aResultBody, anObject, aTools, aBoolAlgo);
133           setResult(aResultBody, aResultIndex);
134           aResultIndex++;
135         }
136       }
137       break;
138     }
139     case GeomAlgoAPI_Boolean::BOOL_FUSE: {
140       // Fuse all objects and all tools.
141       GeomAlgoAPI_Boolean aBoolAlgo(anObjects, aTools, aType);
142
143       // Checking that the algorithm worked properly.
144       if(!aBoolAlgo.isDone()) {
145         static const std::string aFeatureError = "Boolean algorithm failed";
146         setError(aFeatureError);
147         return;
148       }
149       if(aBoolAlgo.shape()->isNull()) {
150         static const std::string aShapeError = "Resulting shape is Null";
151         setError(aShapeError);
152         return;
153       }
154       if(!aBoolAlgo.isValid()) {
155         std::string aFeatureError = "Warning: resulting shape is not valid";
156         setError(aFeatureError);
157         return;
158       }
159
160       std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
161       LoadNamingDS(aResultBody, anObjects.front(), aTools, aBoolAlgo);
162       setResult(aResultBody, aResultIndex);
163       aResultIndex++;
164       break;
165     }
166     default: {
167       std::string anOperationError = "Error: wrong type of operation";
168       setError(anOperationError);
169       return;
170     }
171   }
172   // remove the rest results if there were produced in the previous pass
173   removeResults(aResultIndex);
174 }
175
176 //=================================================================================================
177 void FeaturesPlugin_Boolean::LoadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
178                                           const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
179                                           const ListOfShape& theTools,
180                                           const GeomAlgoAPI_Boolean& theAlgo)
181 {
182   //load result
183   if(theBaseShape->isEqual(theAlgo.shape())) {
184     theResultBody->store(theAlgo.shape());
185   } else {
186     theResultBody->storeModified(theBaseShape, theAlgo.shape(), _SUBSOLIDS_TAG);
187
188     GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
189
190     std::string aModName = "Modified";
191     theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), theBaseShape, FACE,
192                                                _MODIFY_TAG, aModName, *theAlgo.mapOfShapes().get());
193     theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), theBaseShape, FACE, _DELETED_TAG);
194
195     for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
196       theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), *anIter, FACE,
197                                                  _MODIFY_TAG, aModName, *theAlgo.mapOfShapes().get());
198       theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), *anIter, FACE, _DELETED_TAG);
199     }
200   }
201 }