Salome HOME
3d8c5bbc842d2fb14bebc33d78070df9bb22f284
[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   AttributeSelectionListPtr aSelection = 
32     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
33     FeaturesPlugin_Extrusion::LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
34   // extrusion works with faces always
35   aSelection->setSelectionType("FACE");
36   data()->addAttribute(FeaturesPlugin_Extrusion::SIZE_ID(), ModelAPI_AttributeDouble::typeId());
37   data()->addAttribute(FeaturesPlugin_Extrusion::REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
38 }
39
40 void FeaturesPlugin_Extrusion::execute()
41 {
42   AttributeSelectionListPtr aFaceRefs = selectionList(FeaturesPlugin_Extrusion::LIST_ID());
43
44   // for each selected face generate a result
45   int anIndex = 0, aResultIndex = 0;
46   for(; anIndex < aFaceRefs->size(); anIndex++) {
47     std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
48     ResultPtr aContextRes = aFaceRef->context();
49     std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
50     if (!aContext.get()) {
51       static const std::string aContextError = "The selection context is bad";
52       setError(aContextError);
53       break;
54     }
55     double aSize = real(FeaturesPlugin_Extrusion::SIZE_ID())->value();
56     if (boolean(FeaturesPlugin_Extrusion::REVERSE_ID())->value())
57       aSize = -aSize;
58
59     std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
60     int aFacesNum = -1; // this mean that "aFace" is used
61     ResultConstructionPtr aConstruction =
62       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
63     if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
64       if (aConstruction.get()) {
65         aFacesNum = aConstruction->facesNum();
66       } else {
67         static const std::string aFaceError = "Can not find basis for extrusion";
68         setError(aFaceError);
69         break;
70       }
71     }
72
73     for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
74       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
75       std::shared_ptr<GeomAPI_Shape> aFace = 
76         aFacesNum == -1 ? aValueFace : aConstruction->face(aFaceIndex);
77       GeomAlgoAPI_Extrusion aFeature(aFace, aSize);
78       if(!aFeature.isDone()) {
79         static const std::string aFeatureError = "Extrusion algorithm failed";  
80         setError(aFeatureError);
81         break;
82       }
83
84       // Check if shape is valid
85       if (aFeature.shape()->isNull()) {
86         static const std::string aShapeError = "Resulting shape is Null";     
87         setError(aShapeError);
88         break;
89       }
90       if(!aFeature.isValid()) {
91         std::string aFeatureError = "Warning: resulting shape is not valid";  
92         setError(aFeatureError);
93         break;
94       }  
95       //LoadNamingDS
96       LoadNamingDS(aFeature, aResultBody, aFace, aContext);
97
98       setResult(aResultBody, aResultIndex);
99       aResultIndex++;
100
101       if (aFacesNum == -1)
102         break;
103     }
104   }
105   // remove the rest results if there were produced in the previous pass
106   removeResults(aResultIndex);
107 }
108
109 //============================================================================
110 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Extrusion& theFeature, 
111   std::shared_ptr<ModelAPI_ResultBody> theResultBody, 
112   std::shared_ptr<GeomAPI_Shape> theBasis,
113   std::shared_ptr<GeomAPI_Shape> theContext)
114 {  
115
116
117   //load result
118   if(theBasis->isEqual(theContext))
119     theResultBody->store(theFeature.shape());
120   else
121     theResultBody->storeGenerated(theContext, theFeature.shape()); 
122
123   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
124   theFeature.mapOfShapes(*aSubShapes);
125
126   //Insert lateral face : Face from Edge
127   std::string aLatName = "LateralFace";
128   theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
129
130   //Insert bottom face
131   std::string aBotName = "BottomFace";
132   std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();  
133   if (!aBottomFace->isNull()) {
134         if (aSubShapes->isBound(aBottomFace)) {  
135                 aBottomFace = aSubShapes->find(aBottomFace);            
136     }     
137     theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
138   }
139
140
141
142   //Insert top face
143   std::string aTopName = "TopFace";
144   std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
145   if (!aTopFace->isNull()) {
146     if (aSubShapes->isBound(aTopFace)) {         
147       aTopFace = aSubShapes->find(aTopFace);    
148     }
149     theResultBody->generated(aTopFace, aTopName, _LAST_TAG);
150   }
151   
152 }
153
154 //============================================================================
155 void FeaturesPlugin_Extrusion::clearResult()
156 {
157   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
158   std::shared_ptr<GeomAPI_Shape> anEmptyShape(new GeomAPI_Shape);
159   aResultBody->store(anEmptyShape);
160   setResult(aResultBody);
161 }