]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Extrusion.cpp
Salome HOME
External edges for sketch: lines and circles
[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   boost::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = boost::dynamic_pointer_cast<
41     ModelAPI_AttributeSelection>(data()->attribute(FeaturesPlugin_Extrusion::FACE_ID()));
42   if (!aFaceRef)
43     return;
44
45   boost::shared_ptr<GeomAPI_Shape> aFace = 
46     boost::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
47   if (!aFace)
48     return;
49
50   boost::shared_ptr<GeomAPI_Shape> aContext;
51   ResultPtr aContextRes = aFaceRef->context();
52   if (aContextRes) {
53     if (aContextRes->groupName() == ModelAPI_ResultBody::group())
54       aContext = boost::dynamic_pointer_cast<ModelAPI_ResultBody>(aContextRes)->shape();
55     else if (aContextRes->groupName() == ModelAPI_ResultConstruction::group())
56       aContext = boost::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   eraseResults(); // to erase the previously stored naming structures
69   boost::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
70   GeomAlgoAPI_Extrusion aFeature(aFace, aSize);
71   if(!aFeature.isDone()) {
72     std::string aFeatureError = "Extrusion algorithm failed";  
73     Events_Error::send(aFeatureError, this);
74     return;
75   }
76
77   // Check if shape is valid
78   if (aFeature.shape()->isNull()) {
79     std::string aShapeError = "Resulting shape is Null";     
80     Events_Error::send(aShapeError, this);
81 #ifdef _DEBUG
82     std::cerr << aShapeError << std::endl;
83 #endif
84     return;
85   }
86   if(!aFeature.isValid()) {
87     std::string aFeatureError = "Warning: resulting shape is not valid";  
88     Events_Error::send(aFeatureError, this);
89     return;
90   }  
91   //LoadNamingDS
92   LoadNamingDS(aFeature, aResultBody, aFace, aContext);
93
94   setResult(aResultBody);
95 }
96
97 //============================================================================
98 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Extrusion& theFeature, 
99   boost::shared_ptr<ModelAPI_ResultBody> theResultBody, 
100   boost::shared_ptr<GeomAPI_Shape> theBasis,
101   boost::shared_ptr<GeomAPI_Shape> theContext)
102 {  
103
104
105   //load result
106   if(theBasis->isEqual(theContext))
107     theResultBody->store(theFeature.shape());
108   else
109     theResultBody->storeGenerated(theContext, theFeature.shape()); 
110
111   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
112   theFeature.mapOfShapes(*aSubShapes);
113
114     //Insert lateral face : Face from Edge
115   theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theFeature.shape(), EDGE,_LATERAL_TAG, *aSubShapes);
116
117   //Insert bottom face
118   boost::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();  
119   if (!aBottomFace->isNull()) {
120         if (aSubShapes->isBound(aBottomFace)) {  
121                 aBottomFace = aSubShapes->find(aBottomFace);            
122     }    
123     theResultBody->generated(aBottomFace, _FIRST_TAG);
124   }
125
126
127
128   //Insert top face
129   boost::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
130   if (!aTopFace->isNull()) {
131     if (aSubShapes->isBound(aTopFace)) {         
132       aTopFace = aSubShapes->find(aTopFace);    
133     }
134     theResultBody->generated(aTopFace, _LAST_TAG);
135   }
136
137   
138 }