Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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       if (aFacesNum == -1) {
77         aFace = aValueFace;
78       } else {
79         aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
80       }
81       GeomAlgoAPI_Extrusion aFeature(aFace, aSize);
82       if(!aFeature.isDone()) {
83         static const std::string aFeatureError = "Extrusion algorithm failed";  
84         setError(aFeatureError);
85         break;
86       }
87
88       // Check if shape is valid
89       if (aFeature.shape()->isNull()) {
90         static const std::string aShapeError = "Resulting shape is Null";     
91         setError(aShapeError);
92         break;
93       }
94       if(!aFeature.isValid()) {
95         std::string aFeatureError = "Warning: resulting shape is not valid";  
96         setError(aFeatureError);
97         break;
98       }  
99       //LoadNamingDS
100       LoadNamingDS(aFeature, aResultBody, aFace, aContext);
101
102       setResult(aResultBody, aResultIndex);
103       aResultIndex++;
104
105       if (aFacesNum == -1)
106         break;
107     }
108   }
109   // remove the rest results if there were produced in the previous pass
110   removeResults(aResultIndex);
111 }
112
113 //============================================================================
114 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Extrusion& theFeature, 
115   std::shared_ptr<ModelAPI_ResultBody> theResultBody, 
116   std::shared_ptr<GeomAPI_Shape> theBasis,
117   std::shared_ptr<GeomAPI_Shape> theContext)
118 {  
119
120
121   //load result
122   if(theBasis->isEqual(theContext))
123     theResultBody->store(theFeature.shape());
124   else
125     theResultBody->storeGenerated(theContext, theFeature.shape()); 
126
127   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
128   theFeature.mapOfShapes(*aSubShapes);
129
130   //Insert lateral face : Face from Edge
131   std::string aLatName = "LateralFace";
132   theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
133
134   //Insert bottom face
135   std::string aBotName = "BottomFace";
136   std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();  
137   if (!aBottomFace->isNull()) {
138         if (aSubShapes->isBound(aBottomFace)) {  
139                 aBottomFace = aSubShapes->find(aBottomFace);            
140     }     
141     theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
142   }
143
144
145
146   //Insert top face
147   std::string aTopName = "TopFace";
148   std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
149   if (!aTopFace->isNull()) {
150     if (aSubShapes->isBound(aTopFace)) {         
151       aTopFace = aSubShapes->find(aTopFace);    
152     }
153     theResultBody->generated(aTopFace, aTopName, _LAST_TAG);
154   }
155   
156 }
157
158 //============================================================================
159 void FeaturesPlugin_Extrusion::clearResult()
160 {
161   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
162   std::shared_ptr<GeomAPI_Shape> anEmptyShape(new GeomAPI_Shape);
163   aResultBody->store(anEmptyShape);
164   setResult(aResultBody);
165 }