Salome HOME
Task #267: initial implementation of errors management
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Boolean.cpp
1 // File:        FeaturesPlugin_Boolean.cpp
2 // Created:     02 Sept 2014
3 // Author:      Vitaly SMETANNIKOV
4
5 #include "FeaturesPlugin_Boolean.h"
6
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_Document.h>
9 #include <ModelAPI_AttributeReference.h>
10 #include <ModelAPI_AttributeInteger.h>
11 #include <ModelAPI_ResultBody.h>
12 #include <GeomAlgoAPI_Boolean.h>
13 using namespace std;
14
15 #define FACE 4
16 #define _MODIFY_TAG 1
17 #define _DELETED_TAG 2
18 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean()
19 {
20 }
21
22 void FeaturesPlugin_Boolean::initAttributes()
23 {
24   data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::type());
25   data()->addAttribute(FeaturesPlugin_Boolean::OBJECT_ID(), ModelAPI_AttributeReference::type());
26   data()->addAttribute(FeaturesPlugin_Boolean::TOOL_ID(), ModelAPI_AttributeReference::type());
27 }
28
29 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Boolean::getShape(const std::string& theAttrName)
30 {
31   std::shared_ptr<ModelAPI_AttributeReference> aObjRef = std::dynamic_pointer_cast<
32       ModelAPI_AttributeReference>(data()->attribute(theAttrName));
33   if (aObjRef) {
34     std::shared_ptr<ModelAPI_ResultBody> aConstr = std::dynamic_pointer_cast<
35         ModelAPI_ResultBody>(aObjRef->value());
36     if (aConstr)
37       return aConstr->shape();
38   }
39   return std::shared_ptr<GeomAPI_Shape>();
40 }
41
42
43 void FeaturesPlugin_Boolean::execute()
44 {
45   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
46       ModelAPI_AttributeInteger>(data()->attribute(FeaturesPlugin_Boolean::TYPE_ID()));
47   if (!aTypeAttr)
48     return;
49   int aType = aTypeAttr->value();
50
51   std::shared_ptr<GeomAPI_Shape> anObject = this->getShape(FeaturesPlugin_Boolean::OBJECT_ID());
52   if (!anObject)
53     return;
54
55   std::shared_ptr<GeomAPI_Shape> aTool = this->getShape(FeaturesPlugin_Boolean::TOOL_ID());
56   if (!aTool)
57     return;
58
59   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
60
61   GeomAlgoAPI_Boolean* aFeature = new GeomAlgoAPI_Boolean(anObject, aTool, aType);
62   if(aFeature && !aFeature->isDone()) {
63     static const std::string aFeatureError = "Boolean feature: algorithm failed";  
64     setError(aFeatureError);
65     return;
66   }
67    // Check if shape is valid
68   if (aFeature->shape()->isNull()) {
69     static const std::string aShapeError = "Boolean feature: resulting shape is Null";     
70     setError(aShapeError);
71     return;
72   }
73   if(!aFeature->isValid()) {
74     static const std::string aFeatureError = "Boolean feature: resulting shape is not valid";  
75     setError(aFeatureError);
76     return;
77   }  
78   // if result of Boolean operation is same as was before it means that Boolean operation has no sence
79   // and naming provides no result, so, generate an error in this case
80   if (anObject->isEqual(aFeature->shape())) {
81     static const std::string aFeatureError = "Boolean feature: operation was not performed";  
82     setError(aFeatureError);
83     return;
84   }
85   //LoadNamingDS
86   LoadNamingDS(aFeature, aResultBody, anObject, aTool, aType);
87
88   setResult(aResultBody);
89 }
90
91 //============================================================================
92 void FeaturesPlugin_Boolean::LoadNamingDS(GeomAlgoAPI_Boolean* theFeature, 
93                                                 std::shared_ptr<ModelAPI_ResultBody> theResultBody, 
94                                                 std::shared_ptr<GeomAPI_Shape> theObject,
95                                                 std::shared_ptr<GeomAPI_Shape> theTool,
96                                                 int theType)
97 {  
98
99   //load result
100   theResultBody->storeModified(theObject, theFeature->shape()); 
101
102   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
103   theFeature->mapOfShapes(*aSubShapes);
104
105   // Put in DF modified faces
106   theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theObject, FACE, _MODIFY_TAG, *aSubShapes);
107   theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theTool,   FACE, _MODIFY_TAG, *aSubShapes);
108
109   //Put in DF deleted faces
110   theResultBody->loadDeletedShapes(theFeature->makeShape(), theObject, FACE, _DELETED_TAG);
111   theResultBody->loadDeletedShapes(theFeature->makeShape(), theTool,   FACE, _DELETED_TAG);  
112 }