Salome HOME
updated copyright message
[modules/shaper.git] / src / FeaturesAPI / FeaturesAPI_Revolution.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_Revolution.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_Revolution::FeaturesAPI_Revolution(const std::shared_ptr<ModelAPI_Feature>& theFeature)
29 : ModelHighAPI_Interface(theFeature)
30 {
31   initialize();
32 }
33
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)
41 {
42   if(initialize()) {
43     fillAttribute(theBaseObjects, mybaseObjects);
44     fillAttribute(theAxis, myaxis);
45     setAngles(theAngle, ModelHighAPI_Double());
46   }
47 }
48
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)
58 {
59   if(initialize()) {
60     if (!theSelectionType.empty())
61       mybaseObjects->setSelectionType(theSelectionType);
62     fillAttribute(theBaseObjects, mybaseObjects);
63     fillAttribute(theAxis, myaxis);
64     setAngles(theToAngle, theFromAngle);
65   }
66 }
67
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)
79 {
80   if(initialize()) {
81     if (!theSelectionType.empty())
82       mybaseObjects->setSelectionType(theSelectionType);
83     fillAttribute(theBaseObjects, mybaseObjects);
84     fillAttribute(theAxis, myaxis);
85     setPlanesAndOffsets(theToObject, theToOffset, theFromObject, theFromOffset);
86   }
87 }
88
89 //==================================================================================================
90 FeaturesAPI_Revolution::~FeaturesAPI_Revolution()
91 {
92
93 }
94
95 //==================================================================================================
96 void FeaturesAPI_Revolution::setNestedSketch(const ModelHighAPI_Reference& theSketch)
97 {
98   mysketch->setValue(theSketch.feature());
99
100   // To make Sketch feature execute and subfeatures execute.
101   feature()->document()->setCurrentFeature(feature(), false);
102
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);
108   }
109   mybaseObjects->clear();
110   mybaseObjects->append(theSketch.feature()->firstResult(), GeomShapePtr());
111
112   execIfBaseNotEmpty();
113 }
114
115 //==================================================================================================
116 void FeaturesAPI_Revolution::setBase(const std::list<ModelHighAPI_Selection>& theBaseObjects)
117 {
118   mysketch->setValue(ObjectPtr());
119   mybaseObjects->clear();
120   fillAttribute(theBaseObjects, mybaseObjects);
121
122   execIfBaseNotEmpty();
123 }
124
125 //==================================================================================================
126 void FeaturesAPI_Revolution::setAxis(const ModelHighAPI_Selection& theAxis)
127 {
128   fillAttribute(theAxis, myaxis);
129
130   execIfBaseNotEmpty();
131 }
132
133 //==================================================================================================
134 void FeaturesAPI_Revolution::setAngles(const ModelHighAPI_Double& theToAngle,
135                                        const ModelHighAPI_Double& theFromAngle)
136 {
137   fillAttribute(FeaturesPlugin_Revolution::CREATION_METHOD_BY_ANGLES(), mycreationMethod);
138   fillAttribute(theToAngle, mytoAngle);
139   fillAttribute(theFromAngle, myfromAngle);
140
141   execIfBaseNotEmpty();
142 }
143
144 //==================================================================================================
145 void FeaturesAPI_Revolution::setAngle(const ModelHighAPI_Double& theAngle)
146 {
147   setAngles(theAngle, ModelHighAPI_Double());
148 }
149
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)
155 {
156   fillAttribute("ByPlanesAndOffsets", mycreationMethod);
157   fillAttribute(theToObject, mytoObject);
158   fillAttribute(theToOffset, mytoOffset);
159   fillAttribute(theFromObject, myfromObject);
160   fillAttribute(theFromOffset, myfromOffset);
161
162   execIfBaseNotEmpty();
163 }
164
165 //==================================================================================================
166 void FeaturesAPI_Revolution::dump(ModelHighAPI_Dumper& theDumper) const
167 {
168   FeaturePtr aBase = feature();
169   const std::string& aDocName = theDumper.name(aBase->document());
170
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());
175
176   theDumper << aBase << " = model.addRevolution(" << aDocName << ", ";
177   anAttrSketch->isInitialized() ? theDumper << "[]" : theDumper << anAttrObjects;
178   theDumper << ", " << anAttrAxis;
179
180   std::string aCreationMethod =
181     aBase->string(FeaturesPlugin_Revolution::CREATION_METHOD())->value();
182
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());
186
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());
195
196     theDumper << ", " << anAttrToObject << ", " << anAttrToOffset <<
197       ", " << anAttrFromObject << ", " << anAttrFromOffset;
198   }
199
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();
206     if (!aFirstShape)
207       aFirstShape = aFirstSelection->context()->shape();
208     if (!aFirstShape || aFirstShape->shapeType() != GeomAPI_Shape::shapeTypeByStr(aListSelType))
209       theDumper << ", \"" << aListSelType << "\"";
210   }
211
212   theDumper << ")" << std::endl;
213
214   if(anAttrSketch->isInitialized()) {
215     theDumper << aBase << ".setNestedSketch(" << anAttrSketch << ")" << std::endl;
216   }
217 }
218
219 //==================================================================================================
220 void FeaturesAPI_Revolution::execIfBaseNotEmpty()
221 {
222   if(mybaseObjects->size() > 0) {
223     execute();
224   }
225 }
226
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)
232 {
233   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Revolution::ID());
234   return RevolutionPtr(new FeaturesAPI_Revolution(aFeature, theBaseObjects, theAxis, theAngle));
235 }
236
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)
244 {
245   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Revolution::ID());
246   return RevolutionPtr(new FeaturesAPI_Revolution(aFeature,
247                                                 theBaseObjects,
248                                                 theAxis,
249                                                 theToAngle,
250                                                 theFromAngle,
251                                                 theSelectionType));
252 }
253
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)
263 {
264   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Revolution::ID());
265   return RevolutionPtr(new FeaturesAPI_Revolution(aFeature,
266                                                 theBaseObjects,
267                                                 theAxis,
268                                                 theToObject,
269                                                 theToOffset,
270                                                 theFromObject,
271                                                 theFromOffset,
272                                                 theSelectionType));
273 }