Salome HOME
ad23adbcb49f5a167edc8744d1a0b97967f2b188
[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   AttributeSelectionListPtr aFaceRefs = selectionList(FeaturesPlugin_Extrusion::LIST_ID());
39
40   // for each selected face generate a result
41   int anIndex = 0;
42   for(; anIndex < aFaceRefs->size(); anIndex++) {
43     std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
44     if (!aFaceRef.get())
45       continue;
46     std::shared_ptr<GeomAPI_Shape> aFace = aFaceRef->value();
47     if (!aFace.get())
48       continue;
49     ResultPtr aContextRes = aFaceRef->context();
50     std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
51     if (!aContext.get()) {
52       static const std::string aContextError = "The selection context is bad";
53       setError(aContextError);
54       break;
55     }
56
57     double aSize = real(FeaturesPlugin_Extrusion::SIZE_ID())->value();
58     if (boolean(FeaturesPlugin_Extrusion::REVERSE_ID())->value())
59       aSize = -aSize;
60
61     ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
62     GeomAlgoAPI_Extrusion aFeature(aFace, aSize);
63     if(!aFeature.isDone()) {
64       static const std::string aFeatureError = "Extrusion algorithm failed";  
65       setError(aFeatureError);
66       break;
67     }
68
69     // Check if shape is valid
70     if (aFeature.shape()->isNull()) {
71       static const std::string aShapeError = "Resulting shape is Null";     
72       setError(aShapeError);
73       break;
74     }
75     if(!aFeature.isValid()) {
76       std::string aFeatureError = "Warning: resulting shape is not valid";  
77       setError(aFeatureError);
78       break;
79     }  
80     //LoadNamingDS
81     LoadNamingDS(aFeature, aResultBody, aFace, aContext);
82
83     setResult(aResultBody, anIndex);
84   }
85   // remove the rest results if there were produced in the previous pass
86   removeResults(anIndex);
87 }
88
89 //============================================================================
90 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Extrusion& theFeature, 
91   std::shared_ptr<ModelAPI_ResultBody> theResultBody, 
92   std::shared_ptr<GeomAPI_Shape> theBasis,
93   std::shared_ptr<GeomAPI_Shape> theContext)
94 {  
95
96
97   //load result
98   if(theBasis->isEqual(theContext))
99     theResultBody->store(theFeature.shape());
100   else
101     theResultBody->storeGenerated(theContext, theFeature.shape()); 
102
103   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
104   theFeature.mapOfShapes(*aSubShapes);
105
106   //Insert lateral face : Face from Edge
107   std::string aLatName = "LateralFace";
108   theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
109
110   //Insert bottom face
111   std::string aBotName = "BottomFace";
112   std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();  
113   if (!aBottomFace->isNull()) {
114         if (aSubShapes->isBound(aBottomFace)) {  
115                 aBottomFace = aSubShapes->find(aBottomFace);            
116     }     
117     theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
118   }
119
120
121
122   //Insert top face
123   std::string aTopName = "TopFace";
124   std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
125   if (!aTopFace->isNull()) {
126     if (aSubShapes->isBound(aTopFace)) {         
127       aTopFace = aSubShapes->find(aTopFace);    
128     }
129     theResultBody->generated(aTopFace, aTopName, _LAST_TAG);
130   }
131   
132 }
133
134 //============================================================================
135 void FeaturesPlugin_Extrusion::clearResult()
136 {
137   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
138   std::shared_ptr<GeomAPI_Shape> anEmptyShape(new GeomAPI_Shape);
139   aResultBody->store(anEmptyShape);
140   setResult(aResultBody);
141 }