Salome HOME
Issue #1711: Dump study/Load saved script - tuple index out of range
[modules/shaper.git] / src / FeaturesAPI / FeaturesAPI_Extrusion.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        FeaturesAPI_Extrusion.cpp
4 // Created:     09 June 2016
5 // Author:      Dmitry Bobylev
6
7 #include "FeaturesAPI_Extrusion.h"
8
9 #include <ModelHighAPI_Double.h>
10 #include <ModelHighAPI_Dumper.h>
11 #include <ModelHighAPI_Reference.h>
12 #include <ModelHighAPI_Tools.h>
13
14 //==================================================================================================
15 FeaturesAPI_Extrusion::FeaturesAPI_Extrusion(const std::shared_ptr<ModelAPI_Feature>& theFeature)
16 : ModelHighAPI_Interface(theFeature)
17 {
18   initialize();
19 }
20
21 //==================================================================================================
22 FeaturesAPI_Extrusion::FeaturesAPI_Extrusion(const std::shared_ptr<ModelAPI_Feature>& theFeature,
23                                              const std::list<ModelHighAPI_Selection>& theBaseObjects,
24                                              const ModelHighAPI_Double& theSize)
25 : ModelHighAPI_Interface(theFeature)
26 {
27   if(initialize()) {
28     fillAttribute(theBaseObjects, mybaseObjects);
29     setSizes(theSize, ModelHighAPI_Double());
30   }
31 }
32
33 //==================================================================================================
34 FeaturesAPI_Extrusion::FeaturesAPI_Extrusion(const std::shared_ptr<ModelAPI_Feature>& theFeature,
35                                              const std::list<ModelHighAPI_Selection>& theBaseObjects,
36                                              const ModelHighAPI_Selection& theDirection,
37                                              const ModelHighAPI_Double& theSize)
38 : ModelHighAPI_Interface(theFeature)
39 {
40   if(initialize()) {
41     fillAttribute(theBaseObjects, mybaseObjects);
42     fillAttribute(theDirection, mydirection);
43     setSizes(theSize, ModelHighAPI_Double());
44   }
45 }
46
47 //==================================================================================================
48 FeaturesAPI_Extrusion::FeaturesAPI_Extrusion(const std::shared_ptr<ModelAPI_Feature>& theFeature,
49                                              const std::list<ModelHighAPI_Selection>& theBaseObjects,
50                                              const ModelHighAPI_Double& theToSize,
51                                              const ModelHighAPI_Double& theFromSize)
52 : ModelHighAPI_Interface(theFeature)
53 {
54   if(initialize()) {
55     fillAttribute(theBaseObjects, mybaseObjects);
56     setSizes(theToSize, theFromSize);
57   }
58 }
59
60 //==================================================================================================
61 FeaturesAPI_Extrusion::FeaturesAPI_Extrusion(const std::shared_ptr<ModelAPI_Feature>& theFeature,
62                                              const std::list<ModelHighAPI_Selection>& theBaseObjects,
63                                              const ModelHighAPI_Selection& theDirection,
64                                              const ModelHighAPI_Double& theToSize,
65                                              const ModelHighAPI_Double& theFromSize)
66 : ModelHighAPI_Interface(theFeature)
67 {
68   if(initialize()) {
69     fillAttribute(theBaseObjects, mybaseObjects);
70     fillAttribute(theDirection, mydirection);
71     setSizes(theToSize, theFromSize);
72   }
73 }
74
75 //==================================================================================================
76 FeaturesAPI_Extrusion::FeaturesAPI_Extrusion(const std::shared_ptr<ModelAPI_Feature>& theFeature,
77                                              const std::list<ModelHighAPI_Selection>& theBaseObjects,
78                                              const ModelHighAPI_Selection& theToObject,
79                                              const ModelHighAPI_Double& theToOffset,
80                                              const ModelHighAPI_Selection& theFromObject,
81                                              const ModelHighAPI_Double& theFromOffset)
82 : ModelHighAPI_Interface(theFeature)
83 {
84   if(initialize()) {
85     fillAttribute(theBaseObjects, mybaseObjects);
86     setPlanesAndOffsets(theToObject, theToOffset, theFromObject, theFromOffset);
87   }
88 }
89
90 //==================================================================================================
91 FeaturesAPI_Extrusion::FeaturesAPI_Extrusion(const std::shared_ptr<ModelAPI_Feature>& theFeature,
92                                              const std::list<ModelHighAPI_Selection>& theBaseObjects,
93                                              const ModelHighAPI_Selection& theDirection,
94                                              const ModelHighAPI_Selection& theToObject,
95                                              const ModelHighAPI_Double& theToOffset,
96                                              const ModelHighAPI_Selection& theFromObject,
97                                              const ModelHighAPI_Double& theFromOffset)
98 : ModelHighAPI_Interface(theFeature)
99 {
100   if(initialize()) {
101     fillAttribute(theBaseObjects, mybaseObjects);
102     fillAttribute(theDirection, mydirection);
103     setPlanesAndOffsets(theToObject, theToOffset, theFromObject, theFromOffset);
104   }
105 }
106
107 //==================================================================================================
108 FeaturesAPI_Extrusion::~FeaturesAPI_Extrusion()
109 {
110 }
111
112 //==================================================================================================
113 void FeaturesAPI_Extrusion::setNestedSketch(const ModelHighAPI_Reference& theSketch)
114 {
115   mysketch->setValue(theSketch.feature());
116
117   // To make Sketch feature execute and subfeatures execute.
118   feature()->document()->setCurrentFeature(feature(), false);
119
120   // to inform that the history is updated due to the sketch moved under the composite feature
121   if (theSketch.feature().get()) {
122     theSketch.feature()->document()->updateHistory(ModelAPI_Feature::group());
123     if (theSketch.feature()->firstResult().get())
124       theSketch.feature()->firstResult()->setDisplayed(false);
125   }
126   mybaseObjects->clear();
127   mybaseObjects->append(theSketch.feature()->firstResult(), GeomShapePtr());
128
129   execIfBaseNotEmpty();
130 }
131
132 //==================================================================================================
133 void FeaturesAPI_Extrusion::setBase(const std::list<ModelHighAPI_Selection>& theBaseObjects)
134 {
135   mysketch->setValue(ObjectPtr());
136   mybaseObjects->clear();
137   fillAttribute(theBaseObjects, mybaseObjects);
138
139   execIfBaseNotEmpty();
140 }
141
142 //==================================================================================================
143 void FeaturesAPI_Extrusion::setDirection(const ModelHighAPI_Selection& theDirection)
144 {
145   fillAttribute(theDirection, mydirection);
146
147   execIfBaseNotEmpty();
148 }
149
150 //==================================================================================================
151 void FeaturesAPI_Extrusion::setSizes(const ModelHighAPI_Double& theToSize,
152                                      const ModelHighAPI_Double& theFromSize)
153 {
154   fillAttribute(FeaturesPlugin_Extrusion::CREATION_METHOD_BY_SIZES(), mycreationMethod);
155   fillAttribute(theToSize, mytoSize);
156   fillAttribute(theFromSize, myfromSize);
157
158   execIfBaseNotEmpty();
159 }
160
161 //==================================================================================================
162 void FeaturesAPI_Extrusion::setSize(const ModelHighAPI_Double& theSize)
163 {
164   fillAttribute(FeaturesPlugin_Extrusion::CREATION_METHOD_BY_SIZES(), mycreationMethod);
165   fillAttribute(theSize, mytoSize);
166   fillAttribute(ModelHighAPI_Double(), myfromSize);
167
168   execIfBaseNotEmpty();
169 }
170
171 //==================================================================================================
172 void FeaturesAPI_Extrusion::setPlanesAndOffsets(const ModelHighAPI_Selection& theToObject,
173                                                 const ModelHighAPI_Double& theToOffset,
174                                                 const ModelHighAPI_Selection& theFromObject,
175                                                 const ModelHighAPI_Double& theFromOffset)
176 {
177   fillAttribute(FeaturesPlugin_Extrusion::CREATION_METHOD_BY_PLANES(), mycreationMethod);
178   fillAttribute(theToObject, mytoObject);
179   fillAttribute(theToOffset, mytoOffset);
180   fillAttribute(theFromObject, myfromObject);
181   fillAttribute(theFromOffset, myfromOffset);
182
183   execIfBaseNotEmpty();
184 }
185
186 //==================================================================================================
187 void FeaturesAPI_Extrusion::dump(ModelHighAPI_Dumper& theDumper) const
188 {
189   FeaturePtr aBase = feature();
190   const std::string& aDocName = theDumper.name(aBase->document());
191
192   AttributeReferencePtr anAttrSketch = aBase->reference(FeaturesPlugin_Extrusion::SKETCH_ID());
193   AttributeSelectionListPtr anAttrObjects = aBase->selectionList(FeaturesPlugin_Extrusion::BASE_OBJECTS_ID());
194   AttributeSelectionPtr anAttrDirection = aBase->selection(FeaturesPlugin_Extrusion::DIRECTION_OBJECT_ID());
195
196   theDumper << aBase << " = model.addExtrusion(" << aDocName << ", ";
197   anAttrSketch->isInitialized() ? theDumper << "[]" : theDumper << anAttrObjects;
198   theDumper << ", " << anAttrDirection;
199
200   std::string aCreationMethod = aBase->string(FeaturesPlugin_Extrusion::CREATION_METHOD())->value();
201
202   if(aCreationMethod == FeaturesPlugin_Extrusion::CREATION_METHOD_BY_SIZES()) {
203     AttributeDoublePtr anAttrToSize = aBase->real(FeaturesPlugin_Extrusion::TO_SIZE_ID());
204     AttributeDoublePtr anAttrFromSize = aBase->real(FeaturesPlugin_Extrusion::FROM_SIZE_ID());
205
206     theDumper << ", " << anAttrToSize << ", " << anAttrFromSize;
207   } else if(aCreationMethod == FeaturesPlugin_Extrusion::CREATION_METHOD_BY_PLANES()) {
208     AttributeSelectionPtr anAttrToObject = aBase->selection(FeaturesPlugin_Extrusion::TO_OBJECT_ID());
209     AttributeDoublePtr anAttrToOffset = aBase->real(FeaturesPlugin_Extrusion::TO_OFFSET_ID());
210     AttributeSelectionPtr anAttrFromObject = aBase->selection(FeaturesPlugin_Extrusion::FROM_OBJECT_ID());
211     AttributeDoublePtr anAttrFromOffset = aBase->real(FeaturesPlugin_Extrusion::FROM_OFFSET_ID());
212
213     theDumper << ", " << anAttrToObject << ", " << anAttrToOffset << ", " << anAttrFromObject << ", " << anAttrFromOffset;
214   }
215
216   theDumper << ")" << std::endl;
217
218   if(anAttrSketch->isInitialized()) {
219     theDumper << aBase << ".setNestedSketch(" << anAttrSketch << ")" << std::endl;
220   }
221 }
222
223 //==================================================================================================
224 void FeaturesAPI_Extrusion::execIfBaseNotEmpty()
225 {
226   if(mybaseObjects->size() > 0) {
227     execute();
228   }
229 }
230
231 //==================================================================================================
232 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
233                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
234                           const ModelHighAPI_Double& theSize)
235 {
236   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
237   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature, theBaseObjects, theSize));
238 }
239
240 //==================================================================================================
241 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
242                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
243                           const ModelHighAPI_Selection& theDirection,
244                           const ModelHighAPI_Double& theSize)
245 {
246   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
247   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature, theBaseObjects, theDirection, theSize));
248 }
249
250 //==================================================================================================
251 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
252                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
253                           const ModelHighAPI_Double& theToSize,
254                           const ModelHighAPI_Double& theFromSize)
255 {
256   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
257   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature, theBaseObjects, theToSize, theFromSize));
258 }
259
260 //==================================================================================================
261 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
262                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
263                           const ModelHighAPI_Selection& theDirection,
264                           const ModelHighAPI_Double& theToSize,
265                           const ModelHighAPI_Double& theFromSize)
266 {
267   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
268   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature,
269                                                 theBaseObjects,
270                                                 theDirection,
271                                                 theToSize,
272                                                 theFromSize));
273 }
274
275 //==================================================================================================
276 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
277                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
278                           const ModelHighAPI_Selection& theToObject,
279                           const ModelHighAPI_Double& theToOffset,
280                           const ModelHighAPI_Selection& theFromObject,
281                           const ModelHighAPI_Double& theFromOffset)
282 {
283   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
284   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature,
285                                                 theBaseObjects,
286                                                 theToObject,
287                                                 theToOffset,
288                                                 theFromObject,
289                                                 theFromOffset));
290 }
291
292 //==================================================================================================
293 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
294                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
295                           const ModelHighAPI_Selection& theDirection,
296                           const ModelHighAPI_Selection& theToObject,
297                           const ModelHighAPI_Double& theToOffset,
298                           const ModelHighAPI_Selection& theFromObject,
299                           const ModelHighAPI_Double& theFromOffset)
300 {
301   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
302   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature,
303                                                 theBaseObjects,
304                                                 theDirection,
305                                                 theToObject,
306                                                 theToOffset,
307                                                 theFromObject,
308                                                 theFromOffset));
309 }