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::type()));
34   // extrusion works with faces always
35   aSelection->setSelectionType("FACE");
36   data()->addAttribute(FeaturesPlugin_Extrusion::SIZE_ID(), ModelAPI_AttributeDouble::type());
37   data()->addAttribute(FeaturesPlugin_Extrusion::REVERSE_ID(), ModelAPI_AttributeBoolean::type());
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;
46   for(; anIndex < aFaceRefs->size(); anIndex++) {
47     std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
48     if (!aFaceRef.get())
49       continue;
50     std::shared_ptr<GeomAPI_Shape> aFace = aFaceRef->value();
51     if (!aFace.get())
52       continue;
53     ResultPtr aContextRes = aFaceRef->context();
54     std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
55     if (!aContext.get()) {
56       static const std::string aContextError = "The selection context is bad";
57       setError(aContextError);
58       break;
59     }
60
61     double aSize = real(FeaturesPlugin_Extrusion::SIZE_ID())->value();
62     if (boolean(FeaturesPlugin_Extrusion::REVERSE_ID())->value())
63       aSize = -aSize;
64
65     ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
66     GeomAlgoAPI_Extrusion aFeature(aFace, aSize);
67     if(!aFeature.isDone()) {
68       static const std::string aFeatureError = "Extrusion algorithm failed";  
69       setError(aFeatureError);
70       break;
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       break;
78     }
79     if(!aFeature.isValid()) {
80       std::string aFeatureError = "Warning: resulting shape is not valid";  
81       setError(aFeatureError);
82       break;
83     }  
84     //LoadNamingDS
85     LoadNamingDS(aFeature, aResultBody, aFace, aContext);
86
87     setResult(aResultBody, anIndex);
88   }
89   // remove the rest results if there were produced in the previous pass
90   removeResults(anIndex);
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 }