Salome HOME
1de6560c76c910eea4225c0879c3873caef657f6
[modules/shaper.git] / src / FeaturesAPI / FeaturesAPI_Extrusion.cpp
1 // Copyright (C) 2014-2023  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                                           const std::string& theSelectionType)
80 : ModelHighAPI_Interface(theFeature)
81 {
82   if(initialize()) {
83     if (!theSelectionType.empty())
84       mybaseObjects->setSelectionType(theSelectionType);
85     fillAttribute(theBaseObjects, mybaseObjects);
86     fillAttribute(theDirection, mydirection);
87     setSizes(theToSize, theFromSize);
88   }
89 }
90
91 //==================================================================================================
92 FeaturesAPI_Extrusion::FeaturesAPI_Extrusion(const std::shared_ptr<ModelAPI_Feature>& theFeature,
93                                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
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     setPlanesAndOffsets(theToObject, theToOffset, theFromObject, theFromOffset);
103   }
104 }
105
106 //==================================================================================================
107 FeaturesAPI_Extrusion::FeaturesAPI_Extrusion(const std::shared_ptr<ModelAPI_Feature>& theFeature,
108                                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
109                                           const ModelHighAPI_Selection& theDirection,
110                                           const ModelHighAPI_Selection& theToObject,
111                                           const ModelHighAPI_Double& theToOffset,
112                                           const ModelHighAPI_Selection& theFromObject,
113                                           const ModelHighAPI_Double& theFromOffset,
114                                           const std::string& theSelectionType)
115 : ModelHighAPI_Interface(theFeature)
116 {
117   if(initialize()) {
118     if (!theSelectionType.empty())
119       mybaseObjects->setSelectionType(theSelectionType);
120     fillAttribute(theBaseObjects, mybaseObjects);
121     fillAttribute(theDirection, mydirection);
122     setPlanesAndOffsets(theToObject, theToOffset, theFromObject, theFromOffset);
123   }
124 }
125
126 //==================================================================================================
127 FeaturesAPI_Extrusion::~FeaturesAPI_Extrusion()
128 {
129 }
130
131 //==================================================================================================
132 void FeaturesAPI_Extrusion::setNestedSketch(const ModelHighAPI_Reference& theSketch)
133 {
134   mysketch->setValue(theSketch.feature());
135
136   // To make Sketch feature execute and subfeatures execute.
137   feature()->document()->setCurrentFeature(feature(), false);
138
139   // to inform that the history is updated due to the sketch moved under the composite feature
140   if (theSketch.feature().get()) {
141     theSketch.feature()->document()->updateHistory(ModelAPI_Feature::group());
142     if (theSketch.feature()->firstResult().get())
143       theSketch.feature()->firstResult()->setDisplayed(false);
144   }
145   mybaseObjects->clear();
146   mybaseObjects->append(theSketch.feature()->firstResult(), GeomShapePtr());
147
148   execIfBaseNotEmpty();
149 }
150
151 //==================================================================================================
152 void FeaturesAPI_Extrusion::setBase(const std::list<ModelHighAPI_Selection>& theBaseObjects)
153 {
154   mysketch->setValue(ObjectPtr());
155   mybaseObjects->clear();
156   fillAttribute(theBaseObjects, mybaseObjects);
157
158   execIfBaseNotEmpty();
159 }
160
161 //==================================================================================================
162 void FeaturesAPI_Extrusion::setDirection(const ModelHighAPI_Selection& theDirection)
163 {
164   fillAttribute(theDirection, mydirection);
165
166   execIfBaseNotEmpty();
167 }
168
169 //==================================================================================================
170 void FeaturesAPI_Extrusion::setSizes(const ModelHighAPI_Double& theToSize,
171                                      const ModelHighAPI_Double& theFromSize)
172 {
173   fillAttribute(FeaturesPlugin_Extrusion::CREATION_METHOD_BY_SIZES(), mycreationMethod);
174   fillAttribute(theToSize, mytoSize);
175   fillAttribute(theFromSize, myfromSize);
176
177   execIfBaseNotEmpty();
178 }
179
180 //==================================================================================================
181 void FeaturesAPI_Extrusion::setSize(const ModelHighAPI_Double& theSize)
182 {
183   setSizes(theSize, ModelHighAPI_Double());
184 }
185
186 //==================================================================================================
187 void FeaturesAPI_Extrusion::setPlanesAndOffsets(const ModelHighAPI_Selection& theToObject,
188                                                 const ModelHighAPI_Double& theToOffset,
189                                                 const ModelHighAPI_Selection& theFromObject,
190                                                 const ModelHighAPI_Double& theFromOffset)
191 {
192   fillAttribute(FeaturesPlugin_Extrusion::CREATION_METHOD_BY_PLANES(), mycreationMethod);
193   fillAttribute(theToObject, mytoObject);
194   fillAttribute(theToOffset, mytoOffset);
195   fillAttribute(theFromObject, myfromObject);
196   fillAttribute(theFromOffset, myfromOffset);
197
198   execIfBaseNotEmpty();
199 }
200
201 //==================================================================================================
202 void FeaturesAPI_Extrusion::dump(ModelHighAPI_Dumper& theDumper) const
203 {
204   FeaturePtr aBase = feature();
205   const std::string& aDocName = theDumper.name(aBase->document());
206
207   AttributeReferencePtr anAttrSketch = aBase->reference(FeaturesPlugin_Extrusion::SKETCH_ID());
208   AttributeSelectionListPtr anAttrObjects =
209     aBase->selectionList(FeaturesPlugin_Extrusion::BASE_OBJECTS_ID());
210   AttributeSelectionPtr anAttrDirection =
211     aBase->selection(FeaturesPlugin_Extrusion::DIRECTION_OBJECT_ID());
212
213   theDumper << aBase << " = model.addExtrusion(" << aDocName << ", ";
214   anAttrSketch->isInitialized() ? theDumper << "[]" : theDumper << anAttrObjects;
215   theDumper << ", " << anAttrDirection;
216
217   std::string aCreationMethod =
218     aBase->string(FeaturesPlugin_Extrusion::CREATION_METHOD())->value();
219
220   if(aCreationMethod == FeaturesPlugin_Extrusion::CREATION_METHOD_BY_SIZES()) {
221     AttributeDoublePtr anAttrToSize = aBase->real(FeaturesPlugin_Extrusion::TO_SIZE_ID());
222     AttributeDoublePtr anAttrFromSize = aBase->real(FeaturesPlugin_Extrusion::FROM_SIZE_ID());
223
224     theDumper << ", " << anAttrToSize << ", " << anAttrFromSize;
225   } else if(aCreationMethod == FeaturesPlugin_Extrusion::CREATION_METHOD_BY_PLANES()) {
226     AttributeSelectionPtr anAttrToObject =
227       aBase->selection(FeaturesPlugin_Extrusion::TO_OBJECT_ID());
228     AttributeDoublePtr anAttrToOffset = aBase->real(FeaturesPlugin_Extrusion::TO_OFFSET_ID());
229     AttributeSelectionPtr anAttrFromObject =
230       aBase->selection(FeaturesPlugin_Extrusion::FROM_OBJECT_ID());
231     AttributeDoublePtr anAttrFromOffset = aBase->real(FeaturesPlugin_Extrusion::FROM_OFFSET_ID());
232
233     theDumper << ", " << anAttrToObject << ", " << anAttrToOffset <<
234       ", " << anAttrFromObject << ", " << anAttrFromOffset;
235   }
236
237   // write explicitly the type of selection if it does not correspond
238   // to the type of first selected shape
239   if (!anAttrSketch->isInitialized()) {
240     std::string aListSelType = anAttrObjects->selectionType();
241     AttributeSelectionPtr aFirstSelection = anAttrObjects->value(0);
242     GeomShapePtr aFirstShape = aFirstSelection->value();
243     if (!aFirstShape)
244       aFirstShape = aFirstSelection->context()->shape();
245     if (!aFirstShape || aFirstShape->shapeType() != GeomAPI_Shape::shapeTypeByStr(aListSelType))
246       theDumper << ", \"" << aListSelType << "\"";
247   }
248
249   theDumper << ")" << std::endl;
250
251   if(anAttrSketch->isInitialized()) {
252     theDumper << aBase << ".setNestedSketch(" << anAttrSketch << ")" << std::endl;
253   }
254 }
255
256 //==================================================================================================
257 void FeaturesAPI_Extrusion::execIfBaseNotEmpty()
258 {
259   if(mybaseObjects->size() > 0) {
260     execute();
261   }
262 }
263
264 //==================================================================================================
265 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
266                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
267                           const ModelHighAPI_Double& theSize)
268 {
269   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
270   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature, theBaseObjects, theSize));
271 }
272
273 //==================================================================================================
274 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
275                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
276                           const ModelHighAPI_Selection& theDirection,
277                           const ModelHighAPI_Double& theSize)
278 {
279   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
280   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature, theBaseObjects, theDirection, theSize));
281 }
282
283 //==================================================================================================
284 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
285                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
286                           const ModelHighAPI_Double& theToSize,
287                           const ModelHighAPI_Double& theFromSize)
288 {
289   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
290   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature, theBaseObjects, theToSize, theFromSize));
291 }
292
293 //==================================================================================================
294 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
295                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
296                           const ModelHighAPI_Selection& theDirection,
297                           const ModelHighAPI_Double& theToSize,
298                           const ModelHighAPI_Double& theFromSize,
299                           const std::string& theSelectionType)
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                                                 theToSize,
306                                                 theFromSize,
307                                                 theSelectionType));
308 }
309
310 //==================================================================================================
311 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
312                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
313                           const ModelHighAPI_Selection& theToObject,
314                           const ModelHighAPI_Double& theToOffset,
315                           const ModelHighAPI_Selection& theFromObject,
316                           const ModelHighAPI_Double& theFromOffset)
317 {
318   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
319   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature,
320                                                 theBaseObjects,
321                                                 theToObject,
322                                                 theToOffset,
323                                                 theFromObject,
324                                                 theFromOffset));
325 }
326
327 //==================================================================================================
328 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
329                           const std::list<ModelHighAPI_Selection>& theBaseObjects,
330                           const ModelHighAPI_Selection& theDirection,
331                           const ModelHighAPI_Selection& theToObject,
332                           const ModelHighAPI_Double& theToOffset,
333                           const ModelHighAPI_Selection& theFromObject,
334                           const ModelHighAPI_Double& theFromOffset,
335                           const std::string& theSelectionType)
336 {
337   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
338   return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature,
339                                                 theBaseObjects,
340                                                 theDirection,
341                                                 theToObject,
342                                                 theToOffset,
343                                                 theFromObject,
344                                                 theFromOffset,
345                                                 theSelectionType));
346 }