1 // Copyright (C) 2014-2021 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "FeaturesAPI_Extrusion.h"
22 #include <ModelHighAPI_Double.h>
23 #include <ModelHighAPI_Dumper.h>
24 #include <ModelHighAPI_Reference.h>
25 #include <ModelHighAPI_Tools.h>
27 //==================================================================================================
28 FeaturesAPI_Extrusion::FeaturesAPI_Extrusion(const std::shared_ptr<ModelAPI_Feature>& theFeature)
29 : ModelHighAPI_Interface(theFeature)
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)
41 fillAttribute(theBaseObjects, mybaseObjects);
42 setSizes(theSize, ModelHighAPI_Double());
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)
54 fillAttribute(theBaseObjects, mybaseObjects);
55 fillAttribute(theDirection, mydirection);
56 setSizes(theSize, ModelHighAPI_Double());
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)
68 fillAttribute(theBaseObjects, mybaseObjects);
69 setSizes(theToSize, theFromSize);
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)
83 if (!theSelectionType.empty())
84 mybaseObjects->setSelectionType(theSelectionType);
85 fillAttribute(theBaseObjects, mybaseObjects);
86 fillAttribute(theDirection, mydirection);
87 setSizes(theToSize, theFromSize);
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)
101 fillAttribute(theBaseObjects, mybaseObjects);
102 setPlanesAndOffsets(theToObject, theToOffset, theFromObject, theFromOffset);
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)
118 if (!theSelectionType.empty())
119 mybaseObjects->setSelectionType(theSelectionType);
120 fillAttribute(theBaseObjects, mybaseObjects);
121 fillAttribute(theDirection, mydirection);
122 setPlanesAndOffsets(theToObject, theToOffset, theFromObject, theFromOffset);
126 //==================================================================================================
127 FeaturesAPI_Extrusion::~FeaturesAPI_Extrusion()
131 //==================================================================================================
132 void FeaturesAPI_Extrusion::setNestedSketch(const ModelHighAPI_Reference& theSketch)
134 mysketch->setValue(theSketch.feature());
136 // To make Sketch feature execute and subfeatures execute.
137 feature()->document()->setCurrentFeature(feature(), false);
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);
145 mybaseObjects->clear();
146 mybaseObjects->append(theSketch.feature()->firstResult(), GeomShapePtr());
148 execIfBaseNotEmpty();
151 //==================================================================================================
152 void FeaturesAPI_Extrusion::setBase(const std::list<ModelHighAPI_Selection>& theBaseObjects)
154 mysketch->setValue(ObjectPtr());
155 mybaseObjects->clear();
156 fillAttribute(theBaseObjects, mybaseObjects);
158 execIfBaseNotEmpty();
161 //==================================================================================================
162 void FeaturesAPI_Extrusion::setDirection(const ModelHighAPI_Selection& theDirection)
164 fillAttribute(theDirection, mydirection);
166 execIfBaseNotEmpty();
169 //==================================================================================================
170 void FeaturesAPI_Extrusion::setSizes(const ModelHighAPI_Double& theToSize,
171 const ModelHighAPI_Double& theFromSize)
173 fillAttribute(FeaturesPlugin_Extrusion::CREATION_METHOD_BY_SIZES(), mycreationMethod);
174 fillAttribute(theToSize, mytoSize);
175 fillAttribute(theFromSize, myfromSize);
177 execIfBaseNotEmpty();
180 //==================================================================================================
181 void FeaturesAPI_Extrusion::setSize(const ModelHighAPI_Double& theSize)
183 setSizes(theSize, ModelHighAPI_Double());
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)
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);
198 execIfBaseNotEmpty();
201 //==================================================================================================
202 void FeaturesAPI_Extrusion::dump(ModelHighAPI_Dumper& theDumper) const
204 FeaturePtr aBase = feature();
205 const std::string& aDocName = theDumper.name(aBase->document());
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());
213 theDumper << aBase << " = model.addExtrusion(" << aDocName << ", ";
214 anAttrSketch->isInitialized() ? theDumper << "[]" : theDumper << anAttrObjects;
215 theDumper << ", " << anAttrDirection;
217 std::string aCreationMethod =
218 aBase->string(FeaturesPlugin_Extrusion::CREATION_METHOD())->value();
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());
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());
233 theDumper << ", " << anAttrToObject << ", " << anAttrToOffset <<
234 ", " << anAttrFromObject << ", " << anAttrFromOffset;
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();
244 aFirstShape = aFirstSelection->context()->shape();
245 if (!aFirstShape || aFirstShape->shapeType() != GeomAPI_Shape::shapeTypeByStr(aListSelType))
246 theDumper << ", \"" << aListSelType << "\"";
249 theDumper << ")" << std::endl;
251 if(anAttrSketch->isInitialized()) {
252 theDumper << aBase << ".setNestedSketch(" << anAttrSketch << ")" << std::endl;
256 //==================================================================================================
257 void FeaturesAPI_Extrusion::execIfBaseNotEmpty()
259 if(mybaseObjects->size() > 0) {
264 //==================================================================================================
265 ExtrusionPtr addExtrusion(const std::shared_ptr<ModelAPI_Document>& thePart,
266 const std::list<ModelHighAPI_Selection>& theBaseObjects,
267 const ModelHighAPI_Double& theSize)
269 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
270 return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature, theBaseObjects, theSize));
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)
279 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
280 return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature, theBaseObjects, theDirection, theSize));
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)
289 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
290 return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature, theBaseObjects, theToSize, theFromSize));
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)
301 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
302 return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature,
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)
318 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
319 return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature,
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)
337 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Extrusion::ID());
338 return ExtrusionPtr(new FeaturesAPI_Extrusion(aFeature,