Salome HOME
Copyright update 2020
[modules/shaper.git] / src / FeaturesAPI / FeaturesAPI_Extrusion.cpp
1 // Copyright (C) 2014-2020  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
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   setSizes(theSize, ModelHighAPI_Double());
178 }
179
180 //==================================================================================================
181 void FeaturesAPI_Extrusion::setPlanesAndOffsets(const ModelHighAPI_Selection& theToObject,
182                                                 const ModelHighAPI_Double& theToOffset,
183                                                 const ModelHighAPI_Selection& theFromObject,
184                                                 const ModelHighAPI_Double& theFromOffset)
185 {
186   fillAttribute(FeaturesPlugin_Extrusion::CREATION_METHOD_BY_PLANES(), mycreationMethod);
187   fillAttribute(theToObject, mytoObject);
188   fillAttribute(theToOffset, mytoOffset);
189   fillAttribute(theFromObject, myfromObject);
190   fillAttribute(theFromOffset, myfromOffset);
191
192   execIfBaseNotEmpty();
193 }
194
195 //==================================================================================================
196 void FeaturesAPI_Extrusion::dump(ModelHighAPI_Dumper& theDumper) const
197 {
198   FeaturePtr aBase = feature();
199   const std::string& aDocName = theDumper.name(aBase->document());
200
201   AttributeReferencePtr anAttrSketch = aBase->reference(FeaturesPlugin_Extrusion::SKETCH_ID());
202   AttributeSelectionListPtr anAttrObjects =
203     aBase->selectionList(FeaturesPlugin_Extrusion::BASE_OBJECTS_ID());
204   AttributeSelectionPtr anAttrDirection =
205     aBase->selection(FeaturesPlugin_Extrusion::DIRECTION_OBJECT_ID());
206
207   theDumper << aBase << " = model.addExtrusion(" << aDocName << ", ";
208   anAttrSketch->isInitialized() ? theDumper << "[]" : theDumper << anAttrObjects;
209   theDumper << ", " << anAttrDirection;
210
211   std::string aCreationMethod =
212     aBase->string(FeaturesPlugin_Extrusion::CREATION_METHOD())->value();
213
214   if(aCreationMethod == FeaturesPlugin_Extrusion::CREATION_METHOD_BY_SIZES()) {
215     AttributeDoublePtr anAttrToSize = aBase->real(FeaturesPlugin_Extrusion::TO_SIZE_ID());
216     AttributeDoublePtr anAttrFromSize = aBase->real(FeaturesPlugin_Extrusion::FROM_SIZE_ID());
217
218     theDumper << ", " << anAttrToSize << ", " << anAttrFromSize;
219   } else if(aCreationMethod == FeaturesPlugin_Extrusion::CREATION_METHOD_BY_PLANES()) {
220     AttributeSelectionPtr anAttrToObject =
221       aBase->selection(FeaturesPlugin_Extrusion::TO_OBJECT_ID());
222     AttributeDoublePtr anAttrToOffset = aBase->real(FeaturesPlugin_Extrusion::TO_OFFSET_ID());
223     AttributeSelectionPtr anAttrFromObject =
224       aBase->selection(FeaturesPlugin_Extrusion::FROM_OBJECT_ID());
225     AttributeDoublePtr anAttrFromOffset = aBase->real(FeaturesPlugin_Extrusion::FROM_OFFSET_ID());
226
227     theDumper << ", " << anAttrToObject << ", " << anAttrToOffset <<
228       ", " << anAttrFromObject << ", " << anAttrFromOffset;
229   }
230
231   theDumper << ")" << std::endl;
232
233   if(anAttrSketch->isInitialized()) {
234     theDumper << aBase << ".setNestedSketch(" << anAttrSketch << ")" << std::endl;
235   }
236 }
237
238 //==================================================================================================
239 void FeaturesAPI_Extrusion::execIfBaseNotEmpty()
240 {
241   if(mybaseObjects->size() > 0) {
242     execute();
243   }
244 }
245
246 //==================================================================================================
247 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
248                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
249                           const ModelHighAPI_Double& theSize)
250 {
251   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
252   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature, theBaseObjects, theSize));
253 }
254
255 //==================================================================================================
256 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
257                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
258                           const ModelHighAPI_Selection& theDirection,
259                           const ModelHighAPI_Double& theSize)
260 {
261   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
262   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature, theBaseObjects, theDirection, theSize));
263 }
264
265 //==================================================================================================
266 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
267                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
268                           const ModelHighAPI_Double& theToSize,
269                           const ModelHighAPI_Double& theFromSize)
270 {
271   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
272   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature, theBaseObjects, theToSize, 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& theDirection,
279                           const ModelHighAPI_Double& theToSize,
280                           const ModelHighAPI_Double& theFromSize)
281 {
282   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
283   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature,
284                                                 theBaseObjects,
285                                                 theDirection,
286                                                 theToSize,
287                                                 theFromSize));
288 }
289
290 //==================================================================================================
291 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
292                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
293                           const ModelHighAPI_Selection& theToObject,
294                           const ModelHighAPI_Double& theToOffset,
295                           const ModelHighAPI_Selection& theFromObject,
296                           const ModelHighAPI_Double& theFromOffset)
297 {
298   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
299   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature,
300                                                 theBaseObjects,
301                                                 theToObject,
302                                                 theToOffset,
303                                                 theFromObject,
304                                                 theFromOffset));
305 }
306
307 //==================================================================================================
308 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
309                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
310                           const ModelHighAPI_Selection& theDirection,
311                           const ModelHighAPI_Selection& theToObject,
312                           const ModelHighAPI_Double& theToOffset,
313                           const ModelHighAPI_Selection& theFromObject,
314                           const ModelHighAPI_Double& theFromOffset)
315 {
316   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
317   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature,
318                                                 theBaseObjects,
319                                                 theDirection,
320                                                 theToObject,
321                                                 theToOffset,
322                                                 theFromObject,
323                                                 theFromOffset));
324 }