]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Boolean.cpp
Salome HOME
multi selector controls for boolean feature
[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 #include <GeomAlgoAPI_Boolean.h>
16 using namespace std;
17
18 //#define DEBUG_ONE_OBJECT
19
20 #define FACE 4
21 #define _MODIFY_TAG 1
22 #define _DELETED_TAG 2
23 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean()
24 {
25 }
26
27 void FeaturesPlugin_Boolean::initAttributes()
28 {
29   data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
30
31 #ifdef DEBUG_ONE_OBJECT
32   data()->addAttribute(FeaturesPlugin_Boolean::OBJECT_ID(), ModelAPI_AttributeReference::typeId());
33 #else
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 #endif
40
41 #ifdef DEBUG_ONE_OBJECT
42   data()->addAttribute(FeaturesPlugin_Boolean::TOOL_ID(), ModelAPI_AttributeReference::typeId());
43 #else
44   aSelection = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
45     FeaturesPlugin_Boolean::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
46   // extrusion works with faces always
47   aSelection->setSelectionType("SOLID");
48 #endif
49 }
50
51 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Boolean::getShape(const std::string& theAttrName)
52 {
53   std::shared_ptr<ModelAPI_AttributeReference> aObjRef = std::dynamic_pointer_cast<
54       ModelAPI_AttributeReference>(data()->attribute(theAttrName));
55   if (aObjRef) {
56     std::shared_ptr<ModelAPI_ResultBody> aConstr = std::dynamic_pointer_cast<
57         ModelAPI_ResultBody>(aObjRef->value());
58     if (aConstr)
59       return aConstr->shape();
60   }
61   return std::shared_ptr<GeomAPI_Shape>();
62 }
63
64
65 void FeaturesPlugin_Boolean::execute()
66 {
67   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
68       ModelAPI_AttributeInteger>(data()->attribute(FeaturesPlugin_Boolean::TYPE_ID()));
69   if (!aTypeAttr)
70     return;
71   int aType = aTypeAttr->value();
72 #ifdef DEBUG_ONE_OBJECT
73   std::shared_ptr<GeomAPI_Shape> anObject = this->getShape(FeaturesPlugin_Boolean::OBJECT_ID());
74 #else
75   std::shared_ptr<GeomAPI_Shape> anObject;
76   {
77     AttributeSelectionListPtr anObjects = selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
78     if (anObjects->size() == 0)
79       return;
80
81     // Getting bounding planes.
82     std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = anObjects->value(0);
83     if (!anObjRef.get())
84       return;
85     anObject = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
86   }
87 #endif
88   if (!anObject)
89     return;
90
91 #ifdef DEBUG_ONE_OBJECT
92   std::shared_ptr<GeomAPI_Shape> aTool = this->getShape(FeaturesPlugin_Boolean::TOOL_ID());
93 #else
94   std::shared_ptr<GeomAPI_Shape> aTool;
95   {
96     AttributeSelectionListPtr anObjects = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
97     if (anObjects->size() == 0)
98       return;
99
100     // Getting bounding planes.
101     std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = anObjects->value(0);
102     if (!anObjRef.get())
103       return;
104     aTool = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
105   }
106 #endif
107   if (!aTool)
108     return;
109
110   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
111
112   GeomAlgoAPI_Boolean* aFeature = new GeomAlgoAPI_Boolean(anObject, aTool, aType);
113   if(aFeature && !aFeature->isDone()) {
114     static const std::string aFeatureError = "Boolean feature: algorithm failed";  
115     setError(aFeatureError);
116     return;
117   }
118    // Check if shape is valid
119   if (aFeature->shape()->isNull()) {
120     static const std::string aShapeError = "Boolean feature: resulting shape is Null";     
121     setError(aShapeError);
122     return;
123   }
124   if(!aFeature->isValid()) {
125     static const std::string aFeatureError = "Boolean feature: resulting shape is not valid";  
126     setError(aFeatureError);
127     return;
128   }  
129   // if result of Boolean operation is same as was before it means that Boolean operation has no sence
130   // and naming provides no result, so, generate an error in this case
131   if (anObject->isEqual(aFeature->shape())) {
132     static const std::string aFeatureError = "Boolean feature: operation was not performed";  
133     setError(aFeatureError);
134     return;
135   }
136   //LoadNamingDS
137   LoadNamingDS(aFeature, aResultBody, anObject, aTool, aType);
138
139   setResult(aResultBody);
140 }
141
142 //============================================================================
143 void FeaturesPlugin_Boolean::LoadNamingDS(GeomAlgoAPI_Boolean* theFeature, 
144                                                 std::shared_ptr<ModelAPI_ResultBody> theResultBody, 
145                                                 std::shared_ptr<GeomAPI_Shape> theObject,
146                                                 std::shared_ptr<GeomAPI_Shape> theTool,
147                                                 int theType)
148 {  
149
150   //load result
151   theResultBody->storeModified(theObject, theFeature->shape()); 
152
153   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
154   theFeature->mapOfShapes(*aSubShapes);
155
156   // Put in DF modified faces
157   std::string aModName = "Modified";
158   theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theObject, FACE, _MODIFY_TAG, aModName, *aSubShapes);
159   theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theTool,   FACE, _MODIFY_TAG, aModName, *aSubShapes);
160
161   //Put in DF deleted faces
162   theResultBody->loadDeletedShapes(theFeature->makeShape(), theObject, FACE, _DELETED_TAG);
163   theResultBody->loadDeletedShapes(theFeature->makeShape(), theTool,   FACE, _DELETED_TAG);  
164 }