Salome HOME
Tests update
[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
9 #include <ModelAPI_Session.h>
10 #include <ModelAPI_Validator.h>
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_ResultConstruction.h>
14 #include <ModelAPI_ResultBody.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeSelection.h>
17 #include <ModelAPI_AttributeSelectionList.h>
18 #include <ModelAPI_AttributeBoolean.h>
19 #include <ModelAPI_AttributeString.h>
20 #include <ModelAPI_AttributeReference.h>
21
22 #include <GeomAlgoAPI_Prism.h>
23
24 #define _LATERAL_TAG 1
25 #define _FIRST_TAG 2
26 #define _LAST_TAG 3
27 #define EDGE 6
28
29 //=================================================================================================
30 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
31 {
32 }
33
34 //=================================================================================================
35 void FeaturesPlugin_Extrusion::initAttributes()
36 {
37   AttributeSelectionListPtr aSelection = 
38     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
39     LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
40   // extrusion works with faces always
41   aSelection->setSelectionType("FACE");
42
43   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
44
45   data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
46   data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
47
48   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
49   data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
50
51   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
52   data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
53
54   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
55   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
56 }
57
58 //=================================================================================================
59 void FeaturesPlugin_Extrusion::execute()
60 {
61   AttributeSelectionListPtr aFaceRefs = selectionList(LIST_ID());
62
63   // Getting sizes.
64   double aToSize = 0.0;
65   double aFromSize = 0.0;
66
67   if(string(CREATION_METHOD())->value() == "BySizes") {
68     aToSize = real(TO_SIZE_ID())->value();
69     aFromSize =  real(FROM_SIZE_ID())->value();
70   } else {
71     aToSize = real(TO_OFFSET_ID())->value();
72     aFromSize =  real(FROM_OFFSET_ID())->value();
73   }
74
75   // Getting bounding planes.
76   std::shared_ptr<GeomAPI_Shape> aToShape;
77   std::shared_ptr<GeomAPI_Shape> aFromShape;
78
79   if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
80     std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(TO_OBJECT_ID());
81     if(anObjRef.get() != NULL) {
82       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
83       if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
84         aToShape =  anObjRef->context()->shape();
85       }
86     }
87     anObjRef = selection(FROM_OBJECT_ID());
88     if(anObjRef.get() != NULL) {
89       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
90       if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
91         aFromShape = anObjRef->context()->shape();
92       }
93     }
94   }
95
96   // for each selected face generate a result
97   int anIndex = 0, aResultIndex = 0;
98   for(; anIndex < aFaceRefs->size(); anIndex++) {
99     std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
100     ResultPtr aContextRes = aFaceRef->context();
101     std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
102     if (!aContext.get()) {
103       static const std::string aContextError = "The selection context is bad";
104       setError(aContextError);
105       break;
106     }
107
108     std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
109     int aFacesNum = -1; // this mean that "aFace" is used
110     ResultConstructionPtr aConstruction =
111       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
112     if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
113       if (aConstruction.get()) {
114         aFacesNum = aConstruction->facesNum();
115       } else {
116         static const std::string aFaceError = "Can not find basis for extrusion";
117         setError(aFaceError);
118         break;
119       }
120     }
121
122     for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
123       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
124       std::shared_ptr<GeomAPI_Shape> aBaseShape;
125       if (aFacesNum == -1) {
126         aBaseShape = aValueFace;
127       } else {
128         aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
129       }
130
131       GeomAlgoAPI_Prism aFeature(aBaseShape, aToShape, aToSize, aFromShape, aFromSize);
132       if(!aFeature.isDone()) {
133         static const std::string aFeatureError = "Extrusion algorithm failed";
134         setError(aFeatureError);
135         break;
136       }
137
138       // Check if shape is valid
139       if(!aFeature.shape().get() || aFeature.shape()->isNull()) {
140         static const std::string aShapeError = "Resulting shape is Null";
141         setError(aShapeError);
142         break;
143       }
144       if(!aFeature.isValid()) {
145         std::string aFeatureError = "Warning: resulting shape is not valid";
146         setError(aFeatureError);
147         break;
148       }
149       //LoadNamingDS
150       LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
151
152       setResult(aResultBody, aResultIndex);
153       aResultIndex++;
154
155       if (aFacesNum == -1)
156         break;
157     }
158   }
159   // remove the rest results if there were produced in the previous pass
160   removeResults(aResultIndex);
161 }
162
163 //=================================================================================================
164 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Prism& theFeature,
165                                             std::shared_ptr<ModelAPI_ResultBody> theResultBody,
166                                             std::shared_ptr<GeomAPI_Shape> theBasis,
167                                             std::shared_ptr<GeomAPI_Shape> theContext)
168 {
169   //load result
170   if(theBasis->isEqual(theContext))
171     theResultBody->store(theFeature.shape());
172   else
173     theResultBody->storeGenerated(theBasis, theFeature.shape());
174
175   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theFeature.mapOfShapes();
176
177   //Insert lateral face : Face from Edge
178   std::string aLatName = "LateralFace";
179   theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape().get(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
180
181   //Insert bottom face
182   std::string aBotName = "BottomFace";
183   std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
184   if(!aBottomFace->isNull()) {
185     if(aSubShapes->isBound(aBottomFace)) {
186       aBottomFace = aSubShapes->find(aBottomFace);
187     }
188     theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
189   }
190
191   //Insert top face
192   std::string aTopName = "TopFace";
193   std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
194   if (!aTopFace->isNull()) {
195     if (aSubShapes->isBound(aTopFace)) {
196       aTopFace = aSubShapes->find(aTopFace);
197     }
198     theResultBody->generated(aTopFace, aTopName, _LAST_TAG);
199   }
200 }