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