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