]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Extrusion.cpp
Salome HOME
Multi-selection widget to be used in the extrusion feature.
[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_AttributeSelectionList.h>
16 #include <ModelAPI_AttributeBoolean.h>
17 #include <GeomAlgoAPI_Extrusion.h>
18
19 using namespace std;
20 #define _LATERAL_TAG 1
21 #define _FIRST_TAG 2
22 #define _LAST_TAG 3
23 #define EDGE 6
24
25 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
26 {
27 }
28
29 void FeaturesPlugin_Extrusion::initAttributes()
30 {
31   data()->addAttribute(FeaturesPlugin_Extrusion::LIST_ID(), ModelAPI_AttributeSelectionList::type());
32   data()->addAttribute(FeaturesPlugin_Extrusion::SIZE_ID(), ModelAPI_AttributeDouble::type());
33   data()->addAttribute(FeaturesPlugin_Extrusion::REVERSE_ID(), ModelAPI_AttributeBoolean::type());
34 }
35
36 void FeaturesPlugin_Extrusion::execute()
37 {
38   std::shared_ptr<ModelAPI_AttributeSelectionList> aFaceRefs = std::dynamic_pointer_cast<
39     ModelAPI_AttributeSelectionList>(data()->attribute(FeaturesPlugin_Extrusion::LIST_ID()));
40   if (aFaceRefs.get() == NULL || aFaceRefs->size() == 0) {
41     clearResult();
42     return;
43   }
44   std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(0);
45   if (!aFaceRef)
46     return;
47
48   std::shared_ptr<GeomAPI_Shape> aFace = 
49     std::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
50   if (!aFace)
51     return;
52
53   std::shared_ptr<GeomAPI_Shape> aContext;
54   ResultPtr aContextRes = aFaceRef->context();
55   if (aContextRes && aContextRes->groupName() == ModelAPI_ResultConstruction::group()) {
56     aContext = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes)->shape();
57   }
58   if (!aContext) {
59     static const std::string aContextError = "The selection context is bad";
60     setError(aContextError);
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     static const std::string aFeatureError = "Extrusion algorithm failed";  
72     setError(aFeatureError);
73     return;
74   }
75
76   // Check if shape is valid
77   if (aFeature.shape()->isNull()) {
78     static const std::string aShapeError = "Resulting shape is Null";     
79     setError(aShapeError);
80     return;
81   }
82   if(!aFeature.isValid()) {
83     std::string aFeatureError = "Warning: resulting shape is not valid";  
84     setError(aFeatureError);
85     return;
86   }  
87   //LoadNamingDS
88   LoadNamingDS(aFeature, aResultBody, aFace, aContext);
89
90   setResult(aResultBody);
91 }
92
93 //============================================================================
94 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Extrusion& theFeature, 
95   std::shared_ptr<ModelAPI_ResultBody> theResultBody, 
96   std::shared_ptr<GeomAPI_Shape> theBasis,
97   std::shared_ptr<GeomAPI_Shape> theContext)
98 {  
99
100
101   //load result
102   if(theBasis->isEqual(theContext))
103     theResultBody->store(theFeature.shape());
104   else
105     theResultBody->storeGenerated(theContext, theFeature.shape()); 
106
107   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
108   theFeature.mapOfShapes(*aSubShapes);
109
110   //Insert lateral face : Face from Edge
111   std::string aLatName = "LateralFace";
112   theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
113
114   //Insert bottom face
115   std::string aBotName = "BottomFace";
116   std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();  
117   if (!aBottomFace->isNull()) {
118         if (aSubShapes->isBound(aBottomFace)) {  
119                 aBottomFace = aSubShapes->find(aBottomFace);            
120     }     
121     theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
122   }
123
124
125
126   //Insert top face
127   std::string aTopName = "TopFace";
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, aTopName, _LAST_TAG);
134   }
135   
136 }
137
138 //============================================================================
139 void FeaturesPlugin_Extrusion::clearResult()
140 {
141   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
142   std::shared_ptr<GeomAPI_Shape> anEmptyShape(new GeomAPI_Shape);
143   aResultBody->store(anEmptyShape);
144   setResult(aResultBody);
145 }