Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / FeaturesAPI / FeaturesAPI_Extrusion.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "FeaturesAPI_Extrusion.h"
22
23 #include <ModelHighAPI_Double.h>
24 #include <ModelHighAPI_Dumper.h>
25 #include <ModelHighAPI_Reference.h>
26 #include <ModelHighAPI_Tools.h>
27
28 //==================================================================================================
29 FeaturesAPI_Extrusion::FeaturesAPI_Extrusion(const std::shared_ptr<ModelAPI_Feature>& theFeature)
30 : ModelHighAPI_Interface(theFeature)
31 {
32   initialize();
33 }
34
35 //==================================================================================================
36 FeaturesAPI_Extrusion::FeaturesAPI_Extrusion(const std::shared_ptr<ModelAPI_Feature>& theFeature,
37                                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
38                                           const ModelHighAPI_Double& theSize)
39 : ModelHighAPI_Interface(theFeature)
40 {
41   if(initialize()) {
42     fillAttribute(theBaseObjects, mybaseObjects);
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_Selection& theDirection,
51                                           const ModelHighAPI_Double& theSize)
52 : ModelHighAPI_Interface(theFeature)
53 {
54   if(initialize()) {
55     fillAttribute(theBaseObjects, mybaseObjects);
56     fillAttribute(theDirection, mydirection);
57     setSizes(theSize, ModelHighAPI_Double());
58   }
59 }
60
61 //==================================================================================================
62 FeaturesAPI_Extrusion::FeaturesAPI_Extrusion(const std::shared_ptr<ModelAPI_Feature>& theFeature,
63                                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
64                                           const ModelHighAPI_Double& theToSize,
65                                           const ModelHighAPI_Double& theFromSize)
66 : ModelHighAPI_Interface(theFeature)
67 {
68   if(initialize()) {
69     fillAttribute(theBaseObjects, mybaseObjects);
70     setSizes(theToSize, theFromSize);
71   }
72 }
73
74 //==================================================================================================
75 FeaturesAPI_Extrusion::FeaturesAPI_Extrusion(const std::shared_ptr<ModelAPI_Feature>& theFeature,
76                                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
77                                           const ModelHighAPI_Selection& theDirection,
78                                           const ModelHighAPI_Double& theToSize,
79                                           const ModelHighAPI_Double& theFromSize)
80 : ModelHighAPI_Interface(theFeature)
81 {
82   if(initialize()) {
83     fillAttribute(theBaseObjects, mybaseObjects);
84     fillAttribute(theDirection, mydirection);
85     setSizes(theToSize, theFromSize);
86   }
87 }
88
89 //==================================================================================================
90 FeaturesAPI_Extrusion::FeaturesAPI_Extrusion(const std::shared_ptr<ModelAPI_Feature>& theFeature,
91                                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
92                                           const ModelHighAPI_Selection& theToObject,
93                                           const ModelHighAPI_Double& theToOffset,
94                                           const ModelHighAPI_Selection& theFromObject,
95                                           const ModelHighAPI_Double& theFromOffset)
96 : ModelHighAPI_Interface(theFeature)
97 {
98   if(initialize()) {
99     fillAttribute(theBaseObjects, mybaseObjects);
100     setPlanesAndOffsets(theToObject, theToOffset, theFromObject, theFromOffset);
101   }
102 }
103
104 //==================================================================================================
105 FeaturesAPI_Extrusion::FeaturesAPI_Extrusion(const std::shared_ptr<ModelAPI_Feature>& theFeature,
106                                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
107                                           const ModelHighAPI_Selection& theDirection,
108                                           const ModelHighAPI_Selection& theToObject,
109                                           const ModelHighAPI_Double& theToOffset,
110                                           const ModelHighAPI_Selection& theFromObject,
111                                           const ModelHighAPI_Double& theFromOffset)
112 : ModelHighAPI_Interface(theFeature)
113 {
114   if(initialize()) {
115     fillAttribute(theBaseObjects, mybaseObjects);
116     fillAttribute(theDirection, mydirection);
117     setPlanesAndOffsets(theToObject, theToOffset, theFromObject, theFromOffset);
118   }
119 }
120
121 //==================================================================================================
122 FeaturesAPI_Extrusion::~FeaturesAPI_Extrusion()
123 {
124 }
125
126 //==================================================================================================
127 void FeaturesAPI_Extrusion::setNestedSketch(const ModelHighAPI_Reference& theSketch)
128 {
129   mysketch->setValue(theSketch.feature());
130
131   // To make Sketch feature execute and subfeatures execute.
132   feature()->document()->setCurrentFeature(feature(), false);
133
134   // to inform that the history is updated due to the sketch moved under the composite feature
135   if (theSketch.feature().get()) {
136     theSketch.feature()->document()->updateHistory(ModelAPI_Feature::group());
137     if (theSketch.feature()->firstResult().get())
138       theSketch.feature()->firstResult()->setDisplayed(false);
139   }
140   mybaseObjects->clear();
141   mybaseObjects->append(theSketch.feature()->firstResult(), GeomShapePtr());
142
143   execIfBaseNotEmpty();
144 }
145
146 //==================================================================================================
147 void FeaturesAPI_Extrusion::setBase(const std::list<ModelHighAPI_Selection>& theBaseObjects)
148 {
149   mysketch->setValue(ObjectPtr());
150   mybaseObjects->clear();
151   fillAttribute(theBaseObjects, mybaseObjects);
152
153   execIfBaseNotEmpty();
154 }
155
156 //==================================================================================================
157 void FeaturesAPI_Extrusion::setDirection(const ModelHighAPI_Selection& theDirection)
158 {
159   fillAttribute(theDirection, mydirection);
160
161   execIfBaseNotEmpty();
162 }
163
164 //==================================================================================================
165 void FeaturesAPI_Extrusion::setSizes(const ModelHighAPI_Double& theToSize,
166                                      const ModelHighAPI_Double& theFromSize)
167 {
168   fillAttribute(FeaturesPlugin_Extrusion::CREATION_METHOD_BY_SIZES(), mycreationMethod);
169   fillAttribute(theToSize, mytoSize);
170   fillAttribute(theFromSize, myfromSize);
171
172   execIfBaseNotEmpty();
173 }
174
175 //==================================================================================================
176 void FeaturesAPI_Extrusion::setSize(const ModelHighAPI_Double& theSize)
177 {
178   fillAttribute(FeaturesPlugin_Extrusion::CREATION_METHOD_BY_SIZES(), mycreationMethod);
179   fillAttribute(theSize, mytoSize);
180   fillAttribute(ModelHighAPI_Double(), myfromSize);
181
182   execIfBaseNotEmpty();
183 }
184
185 //==================================================================================================
186 void FeaturesAPI_Extrusion::setPlanesAndOffsets(const ModelHighAPI_Selection& theToObject,
187                                                 const ModelHighAPI_Double& theToOffset,
188                                                 const ModelHighAPI_Selection& theFromObject,
189                                                 const ModelHighAPI_Double& theFromOffset)
190 {
191   fillAttribute(FeaturesPlugin_Extrusion::CREATION_METHOD_BY_PLANES(), mycreationMethod);
192   fillAttribute(theToObject, mytoObject);
193   fillAttribute(theToOffset, mytoOffset);
194   fillAttribute(theFromObject, myfromObject);
195   fillAttribute(theFromOffset, myfromOffset);
196
197   execIfBaseNotEmpty();
198 }
199
200 //==================================================================================================
201 void FeaturesAPI_Extrusion::dump(ModelHighAPI_Dumper& theDumper) const
202 {
203   FeaturePtr aBase = feature();
204   const std::string& aDocName = theDumper.name(aBase->document());
205
206   AttributeReferencePtr anAttrSketch = aBase->reference(FeaturesPlugin_Extrusion::SKETCH_ID());
207   AttributeSelectionListPtr anAttrObjects =
208     aBase->selectionList(FeaturesPlugin_Extrusion::BASE_OBJECTS_ID());
209   AttributeSelectionPtr anAttrDirection =
210     aBase->selection(FeaturesPlugin_Extrusion::DIRECTION_OBJECT_ID());
211
212   theDumper << aBase << " = model.addExtrusion(" << aDocName << ", ";
213   anAttrSketch->isInitialized() ? theDumper << "[]" : theDumper << anAttrObjects;
214   theDumper << ", " << anAttrDirection;
215
216   std::string aCreationMethod =
217     aBase->string(FeaturesPlugin_Extrusion::CREATION_METHOD())->value();
218
219   if(aCreationMethod == FeaturesPlugin_Extrusion::CREATION_METHOD_BY_SIZES()) {
220     AttributeDoublePtr anAttrToSize = aBase->real(FeaturesPlugin_Extrusion::TO_SIZE_ID());
221     AttributeDoublePtr anAttrFromSize = aBase->real(FeaturesPlugin_Extrusion::FROM_SIZE_ID());
222
223     theDumper << ", " << anAttrToSize << ", " << anAttrFromSize;
224   } else if(aCreationMethod == FeaturesPlugin_Extrusion::CREATION_METHOD_BY_PLANES()) {
225     AttributeSelectionPtr anAttrToObject =
226       aBase->selection(FeaturesPlugin_Extrusion::TO_OBJECT_ID());
227     AttributeDoublePtr anAttrToOffset = aBase->real(FeaturesPlugin_Extrusion::TO_OFFSET_ID());
228     AttributeSelectionPtr anAttrFromObject =
229       aBase->selection(FeaturesPlugin_Extrusion::FROM_OBJECT_ID());
230     AttributeDoublePtr anAttrFromOffset = aBase->real(FeaturesPlugin_Extrusion::FROM_OFFSET_ID());
231
232     theDumper << ", " << anAttrToObject << ", " << anAttrToOffset <<
233       ", " << anAttrFromObject << ", " << anAttrFromOffset;
234   }
235
236   theDumper << ")" << std::endl;
237
238   if(anAttrSketch->isInitialized()) {
239     theDumper << aBase << ".setNestedSketch(" << anAttrSketch << ")" << std::endl;
240   }
241 }
242
243 //==================================================================================================
244 void FeaturesAPI_Extrusion::execIfBaseNotEmpty()
245 {
246   if(mybaseObjects->size() > 0) {
247     execute();
248   }
249 }
250
251 //==================================================================================================
252 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
253                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
254                           const ModelHighAPI_Double& theSize)
255 {
256   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
257   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature, theBaseObjects, theSize));
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& theSize)
265 {
266   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
267   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature, theBaseObjects, theDirection, theSize));
268 }
269
270 //==================================================================================================
271 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
272                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
273                           const ModelHighAPI_Double& theToSize,
274                           const ModelHighAPI_Double& theFromSize)
275 {
276   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
277   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature, theBaseObjects, theToSize, theFromSize));
278 }
279
280 //==================================================================================================
281 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
282                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
283                           const ModelHighAPI_Selection& theDirection,
284                           const ModelHighAPI_Double& theToSize,
285                           const ModelHighAPI_Double& theFromSize)
286 {
287   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
288   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature,
289                                                 theBaseObjects,
290                                                 theDirection,
291                                                 theToSize,
292                                                 theFromSize));
293 }
294
295 //==================================================================================================
296 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
297                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
298                           const ModelHighAPI_Selection& theToObject,
299                           const ModelHighAPI_Double& theToOffset,
300                           const ModelHighAPI_Selection& theFromObject,
301                           const ModelHighAPI_Double& theFromOffset)
302 {
303   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
304   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature,
305                                                 theBaseObjects,
306                                                 theToObject,
307                                                 theToOffset,
308                                                 theFromObject,
309                                                 theFromOffset));
310 }
311
312 //==================================================================================================
313 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
314                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
315                           const ModelHighAPI_Selection& theDirection,
316                           const ModelHighAPI_Selection& theToObject,
317                           const ModelHighAPI_Double& theToOffset,
318                           const ModelHighAPI_Selection& theFromObject,
319                           const ModelHighAPI_Double& theFromOffset)
320 {
321   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
322   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature,
323                                                 theBaseObjects,
324                                                 theDirection,
325                                                 theToObject,
326                                                 theToOffset,
327                                                 theFromObject,
328                                                 theFromOffset));
329 }