]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Boolean.cpp
Salome HOME
Issue #219 Corectly define selection type on edit
[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 boost::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Boolean::getShape(const std::string& theAttrName)
35 {
36   boost::shared_ptr<ModelAPI_AttributeReference> aObjRef = boost::dynamic_pointer_cast<
37       ModelAPI_AttributeReference>(data()->attribute(theAttrName));
38   if (aObjRef) {
39     boost::shared_ptr<ModelAPI_ResultBody> aConstr = boost::dynamic_pointer_cast<
40         ModelAPI_ResultBody>(aObjRef->value());
41     if (aConstr)
42       return aConstr->shape();
43   }
44   return boost::shared_ptr<GeomAPI_Shape>();
45 }
46
47
48 void FeaturesPlugin_Boolean::execute()
49 {
50   boost::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = boost::dynamic_pointer_cast<
51       ModelAPI_AttributeInteger>(data()->attribute(FeaturesPlugin_Boolean::TYPE_ID()));
52   if (!aTypeAttr)
53     return;
54   int aType = aTypeAttr->value();
55
56   boost::shared_ptr<GeomAPI_Shape> anObject = this->getShape(FeaturesPlugin_Boolean::OBJECT_ID());
57   if (!anObject)
58     return;
59
60   boost::shared_ptr<GeomAPI_Shape> aTool = this->getShape(FeaturesPlugin_Boolean::TOOL_ID());
61   if (!aTool)
62     return;
63
64   boost::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   //LoadNamingDS
87   LoadNamingDS(aFeature, aResultBody, anObject, aTool, aType);
88
89   setResult(aResultBody);
90 }
91
92 //============================================================================
93 void FeaturesPlugin_Boolean::LoadNamingDS(GeomAlgoAPI_Boolean* theFeature, 
94                                                 boost::shared_ptr<ModelAPI_ResultBody> theResultBody, 
95                                                 boost::shared_ptr<GeomAPI_Shape> theObject,
96                                                 boost::shared_ptr<GeomAPI_Shape> theTool,
97                                                 int theType)
98 {  
99
100   //load result
101   theResultBody->storeModified(theObject, theFeature->shape()); 
102
103   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
104   theFeature->mapOfShapes(*aSubShapes);
105
106   // Put in DF modified faces
107   theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theObject, FACE, _MODIFY_TAG, *aSubShapes);
108   theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theTool,   FACE, _MODIFY_TAG, *aSubShapes);
109
110   //Put in DF deleted faces
111   theResultBody->loadDeletedShapes(theFeature->makeShape(), theObject, FACE, _DELETED_TAG);
112   theResultBody->loadDeletedShapes(theFeature->makeShape(), theTool,   FACE, _DELETED_TAG);  
113 }