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_Revolution.h"
22 #include <ModelHighAPI_Double.h>
23 #include <ModelHighAPI_Dumper.h>
24 #include <ModelHighAPI_Reference.h>
25 #include <ModelHighAPI_Tools.h>
27 //==================================================================================================
28 FeaturesAPI_Revolution::FeaturesAPI_Revolution(const std::shared_ptr<ModelAPI_Feature>& theFeature)
29 : ModelHighAPI_Interface(theFeature)
34 //==================================================================================================
35 FeaturesAPI_Revolution::FeaturesAPI_Revolution(
36 const std::shared_ptr<ModelAPI_Feature>& theFeature,
37 const std::list<ModelHighAPI_Selection>& theBaseObjects,
38 const ModelHighAPI_Selection& theAxis,
39 const ModelHighAPI_Double& theAngle)
40 : ModelHighAPI_Interface(theFeature)
43 fillAttribute(theBaseObjects, mybaseObjects);
44 fillAttribute(theAxis, myaxis);
45 setAngles(theAngle, ModelHighAPI_Double());
49 //==================================================================================================
50 FeaturesAPI_Revolution::FeaturesAPI_Revolution(
51 const std::shared_ptr<ModelAPI_Feature>& theFeature,
52 const std::list<ModelHighAPI_Selection>& theBaseObjects,
53 const ModelHighAPI_Selection& theAxis,
54 const ModelHighAPI_Double& theToAngle,
55 const ModelHighAPI_Double& theFromAngle,
56 const std::string& theSelectionType)
57 : ModelHighAPI_Interface(theFeature)
60 if (!theSelectionType.empty())
61 mybaseObjects->setSelectionType(theSelectionType);
62 fillAttribute(theBaseObjects, mybaseObjects);
63 fillAttribute(theAxis, myaxis);
64 setAngles(theToAngle, theFromAngle);
68 //==================================================================================================
69 FeaturesAPI_Revolution::FeaturesAPI_Revolution(
70 const std::shared_ptr<ModelAPI_Feature>& theFeature,
71 const std::list<ModelHighAPI_Selection>& theBaseObjects,
72 const ModelHighAPI_Selection& theAxis,
73 const ModelHighAPI_Selection& theToObject,
74 const ModelHighAPI_Double& theToOffset,
75 const ModelHighAPI_Selection& theFromObject,
76 const ModelHighAPI_Double& theFromOffset,
77 const std::string& theSelectionType)
78 : ModelHighAPI_Interface(theFeature)
81 if (!theSelectionType.empty())
82 mybaseObjects->setSelectionType(theSelectionType);
83 fillAttribute(theBaseObjects, mybaseObjects);
84 fillAttribute(theAxis, myaxis);
85 setPlanesAndOffsets(theToObject, theToOffset, theFromObject, theFromOffset);
89 //==================================================================================================
90 FeaturesAPI_Revolution::~FeaturesAPI_Revolution()
95 //==================================================================================================
96 void FeaturesAPI_Revolution::setNestedSketch(const ModelHighAPI_Reference& theSketch)
98 mysketch->setValue(theSketch.feature());
100 // To make Sketch feature execute and subfeatures execute.
101 feature()->document()->setCurrentFeature(feature(), false);
103 // to inform that the history is updated due to the sketch moved under the composite feature
104 if (theSketch.feature().get()) {
105 theSketch.feature()->document()->updateHistory(ModelAPI_Feature::group());
106 if (theSketch.feature()->firstResult().get())
107 theSketch.feature()->firstResult()->setDisplayed(false);
109 mybaseObjects->clear();
110 mybaseObjects->append(theSketch.feature()->firstResult(), GeomShapePtr());
112 execIfBaseNotEmpty();
115 //==================================================================================================
116 void FeaturesAPI_Revolution::setBase(const std::list<ModelHighAPI_Selection>& theBaseObjects)
118 mysketch->setValue(ObjectPtr());
119 mybaseObjects->clear();
120 fillAttribute(theBaseObjects, mybaseObjects);
122 execIfBaseNotEmpty();
125 //==================================================================================================
126 void FeaturesAPI_Revolution::setAxis(const ModelHighAPI_Selection& theAxis)
128 fillAttribute(theAxis, myaxis);
130 execIfBaseNotEmpty();
133 //==================================================================================================
134 void FeaturesAPI_Revolution::setAngles(const ModelHighAPI_Double& theToAngle,
135 const ModelHighAPI_Double& theFromAngle)
137 fillAttribute(FeaturesPlugin_Revolution::CREATION_METHOD_BY_ANGLES(), mycreationMethod);
138 fillAttribute(theToAngle, mytoAngle);
139 fillAttribute(theFromAngle, myfromAngle);
141 execIfBaseNotEmpty();
144 //==================================================================================================
145 void FeaturesAPI_Revolution::setAngle(const ModelHighAPI_Double& theAngle)
147 setAngles(theAngle, ModelHighAPI_Double());
150 //==================================================================================================
151 void FeaturesAPI_Revolution::setPlanesAndOffsets(const ModelHighAPI_Selection& theToObject,
152 const ModelHighAPI_Double& theToOffset,
153 const ModelHighAPI_Selection& theFromObject,
154 const ModelHighAPI_Double& theFromOffset)
156 fillAttribute("ByPlanesAndOffsets", mycreationMethod);
157 fillAttribute(theToObject, mytoObject);
158 fillAttribute(theToOffset, mytoOffset);
159 fillAttribute(theFromObject, myfromObject);
160 fillAttribute(theFromOffset, myfromOffset);
162 execIfBaseNotEmpty();
165 //==================================================================================================
166 void FeaturesAPI_Revolution::dump(ModelHighAPI_Dumper& theDumper) const
168 FeaturePtr aBase = feature();
169 const std::string& aDocName = theDumper.name(aBase->document());
171 AttributeReferencePtr anAttrSketch = aBase->reference(FeaturesPlugin_Revolution::SKETCH_ID());
172 AttributeSelectionListPtr anAttrObjects =
173 aBase->selectionList(FeaturesPlugin_Revolution::BASE_OBJECTS_ID());
174 AttributeSelectionPtr anAttrAxis = aBase->selection(FeaturesPlugin_Revolution::AXIS_OBJECT_ID());
176 theDumper << aBase << " = model.addRevolution(" << aDocName << ", ";
177 anAttrSketch->isInitialized() ? theDumper << "[]" : theDumper << anAttrObjects;
178 theDumper << ", " << anAttrAxis;
180 std::string aCreationMethod =
181 aBase->string(FeaturesPlugin_Revolution::CREATION_METHOD())->value();
183 if(aCreationMethod == FeaturesPlugin_Revolution::CREATION_METHOD_BY_ANGLES()) {
184 AttributeDoublePtr anAttrToAngle = aBase->real(FeaturesPlugin_Revolution::TO_ANGLE_ID());
185 AttributeDoublePtr anAttrFromAngle = aBase->real(FeaturesPlugin_Revolution::FROM_ANGLE_ID());
187 theDumper << ", " << anAttrToAngle << ", " << anAttrFromAngle;
188 } else if(aCreationMethod == FeaturesPlugin_Revolution::CREATION_METHOD_BY_PLANES()) {
189 AttributeSelectionPtr anAttrToObject =
190 aBase->selection(FeaturesPlugin_Revolution::TO_OBJECT_ID());
191 AttributeDoublePtr anAttrToOffset = aBase->real(FeaturesPlugin_Revolution::TO_OFFSET_ID());
192 AttributeSelectionPtr anAttrFromObject =
193 aBase->selection(FeaturesPlugin_Revolution::FROM_OBJECT_ID());
194 AttributeDoublePtr anAttrFromOffset = aBase->real(FeaturesPlugin_Revolution::FROM_OFFSET_ID());
196 theDumper << ", " << anAttrToObject << ", " << anAttrToOffset <<
197 ", " << anAttrFromObject << ", " << anAttrFromOffset;
200 // write explicitly the type of selection if it does not correspond
201 // to the type of first selected shape
202 if (!anAttrSketch->isInitialized()) {
203 std::string aListSelType = anAttrObjects->selectionType();
204 AttributeSelectionPtr aFirstSelection = anAttrObjects->value(0);
205 GeomShapePtr aFirstShape = aFirstSelection->value();
207 aFirstShape = aFirstSelection->context()->shape();
208 if (!aFirstShape || aFirstShape->shapeType() != GeomAPI_Shape::shapeTypeByStr(aListSelType))
209 theDumper << ", \"" << aListSelType << "\"";
212 theDumper << ")" << std::endl;
214 if(anAttrSketch->isInitialized()) {
215 theDumper << aBase << ".setNestedSketch(" << anAttrSketch << ")" << std::endl;
219 //==================================================================================================
220 void FeaturesAPI_Revolution::execIfBaseNotEmpty()
222 if(mybaseObjects->size() > 0) {
227 //==================================================================================================
228 RevolutionPtr addRevolution(const std::shared_ptr<ModelAPI_Document>& thePart,
229 const std::list<ModelHighAPI_Selection>& theBaseObjects,
230 const ModelHighAPI_Selection& theAxis,
231 const ModelHighAPI_Double& theAngle)
233 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Revolution::ID());
234 return RevolutionPtr(new FeaturesAPI_Revolution(aFeature, theBaseObjects, theAxis, theAngle));
237 //==================================================================================================
238 RevolutionPtr addRevolution(const std::shared_ptr<ModelAPI_Document>& thePart,
239 const std::list<ModelHighAPI_Selection>& theBaseObjects,
240 const ModelHighAPI_Selection& theAxis,
241 const ModelHighAPI_Double& theToAngle,
242 const ModelHighAPI_Double& theFromAngle,
243 const std::string& theSelectionType)
245 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Revolution::ID());
246 return RevolutionPtr(new FeaturesAPI_Revolution(aFeature,
254 //==================================================================================================
255 RevolutionPtr addRevolution(const std::shared_ptr<ModelAPI_Document>& thePart,
256 const std::list<ModelHighAPI_Selection>& theBaseObjects,
257 const ModelHighAPI_Selection& theAxis,
258 const ModelHighAPI_Selection& theToObject,
259 const ModelHighAPI_Double& theToOffset,
260 const ModelHighAPI_Selection& theFromObject,
261 const ModelHighAPI_Double& theFromOffset,
262 const std::string& theSelectionType)
264 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Revolution::ID());
265 return RevolutionPtr(new FeaturesAPI_Revolution(aFeature,