]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Extrusion.cpp
Salome HOME
Improvement #615: Widgets position in Extrusion and Rotation features must be changed.
[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(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
47
48   data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
49   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
50
51   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
52   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
53 }
54
55 //=================================================================================================
56 void FeaturesPlugin_Extrusion::execute()
57 {
58   AttributeSelectionListPtr aFaceRefs = selectionList(LIST_ID());
59
60   // Getting sizes.
61   double aFromSize = real(FROM_SIZE_ID())->value();
62   double aToSize = real(TO_SIZE_ID())->value();
63
64   // Getting bounding planes.
65   std::shared_ptr<GeomAPI_Shape> aFromShape;
66   std::shared_ptr<GeomAPI_Shape> aToShape;
67
68   if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
69     std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FROM_OBJECT_ID());
70     if(anObjRef.get() != NULL) {
71       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
72       if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
73         aFromShape = anObjRef->context()->shape();
74       }
75     }
76     anObjRef = selection(TO_OBJECT_ID());
77     if(anObjRef.get() != NULL) {
78       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
79       if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
80         aToShape =  anObjRef->context()->shape();
81       }
82     }
83   }
84
85   // for each selected face generate a result
86   int anIndex = 0, aResultIndex = 0;
87   for(; anIndex < aFaceRefs->size(); anIndex++) {
88     std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
89     ResultPtr aContextRes = aFaceRef->context();
90     std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
91     if (!aContext.get()) {
92       static const std::string aContextError = "The selection context is bad";
93       setError(aContextError);
94       break;
95     }
96
97     std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
98     int aFacesNum = -1; // this mean that "aFace" is used
99     ResultConstructionPtr aConstruction =
100       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
101     if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
102       if (aConstruction.get()) {
103         aFacesNum = aConstruction->facesNum();
104       } else {
105         static const std::string aFaceError = "Can not find basis for extrusion";
106         setError(aFaceError);
107         break;
108       }
109     }
110
111     for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
112       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
113       std::shared_ptr<GeomAPI_Shape> aBaseShape;
114       if (aFacesNum == -1) {
115         aBaseShape = aValueFace;
116       } else {
117         aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
118       }
119
120       GeomAlgoAPI_Prism aFeature(aBaseShape, aToShape, aToSize, aFromShape, aFromSize);
121       if(!aFeature.isDone()) {
122         static const std::string aFeatureError = "Extrusion algorithm failed";
123         setError(aFeatureError);
124         break;
125       }
126
127       // Check if shape is valid
128       if(aFeature.shape()->isNull()) {
129         static const std::string aShapeError = "Resulting shape is Null";
130         setError(aShapeError);
131         break;
132       }
133       if(!aFeature.isValid()) {
134         std::string aFeatureError = "Warning: resulting shape is not valid";
135         setError(aFeatureError);
136         break;
137       }
138       //LoadNamingDS
139       LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
140
141       setResult(aResultBody, aResultIndex);
142       aResultIndex++;
143
144       if (aFacesNum == -1)
145         break;
146     }
147   }
148   // remove the rest results if there were produced in the previous pass
149   removeResults(aResultIndex);
150 }
151
152 //=================================================================================================
153 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Prism& theFeature,
154                                             std::shared_ptr<ModelAPI_ResultBody> theResultBody,
155                                             std::shared_ptr<GeomAPI_Shape> theBasis,
156                                             std::shared_ptr<GeomAPI_Shape> theContext)
157 {
158   //load result
159   if(theBasis->isEqual(theContext))
160     theResultBody->store(theFeature.shape());
161   else
162     theResultBody->storeGenerated(theBasis, theFeature.shape());
163
164   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theFeature.mapOfShapes();
165
166   //Insert lateral face : Face from Edge
167   std::string aLatName = "LateralFace";
168   theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape().get(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
169
170   //Insert bottom face
171   std::string aBotName = "BottomFace";
172   std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
173   if(!aBottomFace->isNull()) {
174     if(aSubShapes->isBound(aBottomFace)) {
175       aBottomFace = aSubShapes->find(aBottomFace);
176     }
177     theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
178   }
179
180   //Insert top face
181   std::string aTopName = "TopFace";
182   std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
183   if (!aTopFace->isNull()) {
184     if (aSubShapes->isBound(aTopFace)) {
185       aTopFace = aSubShapes->find(aTopFace);
186     }
187     theResultBody->generated(aTopFace, aTopName, _LAST_TAG);
188   }
189 }