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