Salome HOME
refs #98 - Default color for constraints
[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
14 using namespace std;
15
16 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean()
17 {
18 }
19
20 void FeaturesPlugin_Boolean::initAttributes()
21 {
22   data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::type());
23   data()->addAttribute(FeaturesPlugin_Boolean::OBJECT_ID(), ModelAPI_AttributeReference::type());
24   data()->addAttribute(FeaturesPlugin_Boolean::TOOL_ID(), ModelAPI_AttributeReference::type());
25 }
26
27 boost::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Boolean::getShape(const std::string& theAttrName)
28 {
29   boost::shared_ptr<ModelAPI_AttributeReference> aObjRef = boost::dynamic_pointer_cast<
30       ModelAPI_AttributeReference>(data()->attribute(theAttrName));
31   if (aObjRef) {
32     boost::shared_ptr<ModelAPI_ResultBody> aConstr = boost::dynamic_pointer_cast<
33         ModelAPI_ResultBody>(aObjRef->value());
34     if (aConstr)
35       return aConstr->shape();
36   }
37   return boost::shared_ptr<GeomAPI_Shape>();
38 }
39
40
41 void FeaturesPlugin_Boolean::execute()
42 {
43   boost::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = boost::dynamic_pointer_cast<
44       ModelAPI_AttributeInteger>(data()->attribute(FeaturesPlugin_Boolean::TYPE_ID()));
45   if (!aTypeAttr)
46     return;
47   int aType = aTypeAttr->value();
48
49   boost::shared_ptr<GeomAPI_Shape> aObject = this->getShape(FeaturesPlugin_Boolean::OBJECT_ID());
50   if (!aObject)
51     return;
52
53   boost::shared_ptr<GeomAPI_Shape> aTool = this->getShape(FeaturesPlugin_Boolean::TOOL_ID());
54   if (!aTool)
55     return;
56
57   boost::shared_ptr<ModelAPI_ResultBody> aResult = document()->createBody(data());
58   switch (aType) {
59   case BOOL_CUT:
60     aResult->store(GeomAlgoAPI_Boolean::makeCut(aObject, aTool));
61     break;
62   case BOOL_FUSE:
63     aResult->store(GeomAlgoAPI_Boolean::makeFuse(aObject, aTool));
64     break;
65   case BOOL_COMMON:
66     aResult->store(GeomAlgoAPI_Boolean::makeCommon(aObject, aTool));
67     break;
68   }
69   setResult(aResult);
70 }