]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Boolean.cpp
Salome HOME
d8b8198ed1219c4ec710785d678a2f5722c0c242
[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 #include <Events_Error.h>
14 using namespace std;
15 #ifdef _DEBUG
16 #include <iostream>
17 #include <ostream>
18 #endif
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::type());
30   data()->addAttribute(FeaturesPlugin_Boolean::OBJECT_ID(), ModelAPI_AttributeReference::type());
31   data()->addAttribute(FeaturesPlugin_Boolean::TOOL_ID(), ModelAPI_AttributeReference::type());
32 }
33
34 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Boolean::getShape(const std::string& theAttrName)
35 {
36   std::shared_ptr<ModelAPI_AttributeReference> aObjRef = std::dynamic_pointer_cast<
37       ModelAPI_AttributeReference>(data()->attribute(theAttrName));
38   if (aObjRef) {
39     std::shared_ptr<ModelAPI_ResultBody> aConstr = std::dynamic_pointer_cast<
40         ModelAPI_ResultBody>(aObjRef->value());
41     if (aConstr)
42       return aConstr->shape();
43   }
44   return std::shared_ptr<GeomAPI_Shape>();
45 }
46
47
48 void FeaturesPlugin_Boolean::execute()
49 {
50   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
51       ModelAPI_AttributeInteger>(data()->attribute(FeaturesPlugin_Boolean::TYPE_ID()));
52   if (!aTypeAttr)
53     return;
54   int aType = aTypeAttr->value();
55
56   std::shared_ptr<GeomAPI_Shape> anObject = this->getShape(FeaturesPlugin_Boolean::OBJECT_ID());
57   if (!anObject)
58     return;
59
60   std::shared_ptr<GeomAPI_Shape> aTool = this->getShape(FeaturesPlugin_Boolean::TOOL_ID());
61   if (!aTool)
62     return;
63
64   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
65
66   GeomAlgoAPI_Boolean* aFeature = new GeomAlgoAPI_Boolean(anObject, aTool, aType);
67   if(aFeature && !aFeature->isDone()) {
68     std::string aFeatureError = "Boolean feature: algorithm failed";  
69     Events_Error::send(aFeatureError, this);
70     return;
71   }
72    // Check if shape is valid
73   if (aFeature->shape()->isNull()) {
74     std::string aShapeError = "Boolean feature: resulting shape is Null";     
75     Events_Error::send(aShapeError, this);
76 #ifdef _DEBUG
77     std::cerr << aShapeError << std::endl;
78 #endif
79     return;
80   }
81   if(!aFeature->isValid()) {
82     std::string aFeatureError = "Boolean feature: resulting shape is not valid";  
83     Events_Error::send(aFeatureError, this);
84     return;
85   }  
86   // if result of Boolean operation is same as was before it means that Boolean operation has no sence
87   // and naming provides no result, so, generate an error in this case
88   if (anObject->isEqual(aFeature->shape())) {
89     std::string aFeatureError = "Boolean feature: operation was not performed";  
90     Events_Error::send(aFeatureError, this);
91     return;
92   }
93   //LoadNamingDS
94   LoadNamingDS(aFeature, aResultBody, anObject, aTool, aType);
95
96   setResult(aResultBody);
97 }
98
99 //============================================================================
100 void FeaturesPlugin_Boolean::LoadNamingDS(GeomAlgoAPI_Boolean* theFeature, 
101                                                 std::shared_ptr<ModelAPI_ResultBody> theResultBody, 
102                                                 std::shared_ptr<GeomAPI_Shape> theObject,
103                                                 std::shared_ptr<GeomAPI_Shape> theTool,
104                                                 int theType)
105 {  
106
107   //load result
108   theResultBody->storeModified(theObject, theFeature->shape()); 
109
110   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
111   theFeature->mapOfShapes(*aSubShapes);
112
113   // Put in DF modified faces
114   theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theObject, FACE, _MODIFY_TAG, *aSubShapes);
115   theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theTool,   FACE, _MODIFY_TAG, *aSubShapes);
116
117   //Put in DF deleted faces
118   theResultBody->loadDeletedShapes(theFeature->makeShape(), theObject, FACE, _DELETED_TAG);
119   theResultBody->loadDeletedShapes(theFeature->makeShape(), theTool,   FACE, _DELETED_TAG);  
120 }