Salome HOME
55bc605c16f4c9f2ecdf67f510bf30268cd022a8
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Extrusion.cpp
1 // File:        FeaturesPlugin_Extrusion.cpp
2 // Created:     30 May 2014
3 // Author:      Vitaly SMETANNIKOV
4
5 #include "FeaturesPlugin_Extrusion.h"
6 #include <ModelAPI_Session.h>
7 #include <ModelAPI_Document.h>
8 #include <ModelAPI_Data.h>
9 #include <ModelAPI_ResultConstruction.h>
10 #include <ModelAPI_ResultBody.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeSelection.h>
13 #include <ModelAPI_AttributeBoolean.h>
14 #include <Events_Error.h>
15 #include <GeomAlgoAPI_Extrusion.h>
16
17 using namespace std;
18 #define _LATERAL_TAG 1
19 #define _FIRST_TAG 2
20 #define _LAST_TAG 3
21 #define EDGE 6
22 #ifdef _DEBUG
23 #include <iostream>
24 #include <ostream>
25 #endif
26
27 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
28 {
29 }
30
31 void FeaturesPlugin_Extrusion::initAttributes()
32 {
33   data()->addAttribute(FeaturesPlugin_Extrusion::FACE_ID(), ModelAPI_AttributeSelection::type());
34   data()->addAttribute(FeaturesPlugin_Extrusion::SIZE_ID(), ModelAPI_AttributeDouble::type());
35   data()->addAttribute(FeaturesPlugin_Extrusion::REVERSE_ID(), ModelAPI_AttributeBoolean::type());
36 }
37
38 void FeaturesPlugin_Extrusion::execute()
39 {
40   std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = std::dynamic_pointer_cast<
41     ModelAPI_AttributeSelection>(data()->attribute(FeaturesPlugin_Extrusion::FACE_ID()));
42   if (!aFaceRef)
43     return;
44
45   std::shared_ptr<GeomAPI_Shape> aFace = 
46     std::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
47   if (!aFace)
48     return;
49
50   std::shared_ptr<GeomAPI_Shape> aContext;
51   ResultPtr aContextRes = aFaceRef->context();
52   if (aContextRes) {
53     if (aContextRes->groupName() == ModelAPI_ResultBody::group())
54       aContext = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContextRes)->shape();
55     else if (aContextRes->groupName() == ModelAPI_ResultConstruction::group())
56       aContext = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes)->shape();
57   }
58   if (!aContext) {
59     std::string aContextError = "The selection context is bad";
60     Events_Error::send(aContextError, this);
61     return;
62   }
63
64   double aSize = data()->real(FeaturesPlugin_Extrusion::SIZE_ID())->value();
65   if (data()->boolean(FeaturesPlugin_Extrusion::REVERSE_ID())->value())
66     aSize = -aSize;
67
68   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
69   GeomAlgoAPI_Extrusion aFeature(aFace, aSize);
70   if(!aFeature.isDone()) {
71     std::string aFeatureError = "Extrusion algorithm failed";  
72     Events_Error::send(aFeatureError, this);
73     return;
74   }
75
76   // Check if shape is valid
77   if (aFeature.shape()->isNull()) {
78     std::string aShapeError = "Resulting shape is Null";     
79     Events_Error::send(aShapeError, this);
80 #ifdef _DEBUG
81     std::cerr << aShapeError << std::endl;
82 #endif
83     return;
84   }
85   if(!aFeature.isValid()) {
86     std::string aFeatureError = "Warning: resulting shape is not valid";  
87     Events_Error::send(aFeatureError, this);
88     return;
89   }  
90   //LoadNamingDS
91   LoadNamingDS(aFeature, aResultBody, aFace, aContext);
92
93   setResult(aResultBody);
94 }
95
96 //============================================================================
97 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Extrusion& theFeature, 
98   std::shared_ptr<ModelAPI_ResultBody> theResultBody, 
99   std::shared_ptr<GeomAPI_Shape> theBasis,
100   std::shared_ptr<GeomAPI_Shape> theContext)
101 {  
102
103
104   //load result
105   if(theBasis->isEqual(theContext))
106     theResultBody->store(theFeature.shape());
107   else
108     theResultBody->storeGenerated(theContext, theFeature.shape()); 
109
110   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
111   theFeature.mapOfShapes(*aSubShapes);
112
113     //Insert lateral face : Face from Edge
114   theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, *aSubShapes);
115
116   //Insert bottom face
117   std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();  
118   if (!aBottomFace->isNull()) {
119         if (aSubShapes->isBound(aBottomFace)) {  
120                 aBottomFace = aSubShapes->find(aBottomFace);            
121     }    
122     theResultBody->generated(aBottomFace, _FIRST_TAG);
123   }
124
125
126
127   //Insert top face
128   std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
129   if (!aTopFace->isNull()) {
130     if (aSubShapes->isBound(aTopFace)) {         
131       aTopFace = aSubShapes->find(aTopFace);    
132     }
133     theResultBody->generated(aTopFace, _LAST_TAG);
134   }
135
136   
137 }