Salome HOME
Update copyrights
[modules/shaper.git] / src / FeaturesAPI / FeaturesAPI_Revolution.cpp
1 // Copyright (C) 2014-2019  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 : ModelHighAPI_Interface(theFeature)
57 {
58   if(initialize()) {
59     fillAttribute(theBaseObjects, mybaseObjects);
60     fillAttribute(theAxis, myaxis);
61     setAngles(theToAngle, theFromAngle);
62   }
63 }
64
65 //==================================================================================================
66 FeaturesAPI_Revolution::FeaturesAPI_Revolution(
67   const std::shared_ptr<ModelAPI_Feature>& theFeature,
68   const std::list<ModelHighAPI_Selection>& theBaseObjects,
69   const ModelHighAPI_Selection& theAxis,
70   const ModelHighAPI_Selection& theToObject,
71   const ModelHighAPI_Double& theToOffset,
72   const ModelHighAPI_Selection& theFromObject,
73   const ModelHighAPI_Double& theFromOffset)
74 : ModelHighAPI_Interface(theFeature)
75 {
76   if(initialize()) {
77     fillAttribute(theBaseObjects, mybaseObjects);
78     fillAttribute(theAxis, myaxis);
79     setPlanesAndOffsets(theToObject, theToOffset, theFromObject, theFromOffset);
80   }
81 }
82
83 //==================================================================================================
84 FeaturesAPI_Revolution::~FeaturesAPI_Revolution()
85 {
86
87 }
88
89 //==================================================================================================
90 void FeaturesAPI_Revolution::setNestedSketch(const ModelHighAPI_Reference& theSketch)
91 {
92   mysketch->setValue(theSketch.feature());
93
94   // To make Sketch feature execute and subfeatures execute.
95   feature()->document()->setCurrentFeature(feature(), false);
96
97   // to inform that the history is updated due to the sketch moved under the composite feature
98   if (theSketch.feature().get()) {
99     theSketch.feature()->document()->updateHistory(ModelAPI_Feature::group());
100     if (theSketch.feature()->firstResult().get())
101       theSketch.feature()->firstResult()->setDisplayed(false);
102   }
103   mybaseObjects->clear();
104   mybaseObjects->append(theSketch.feature()->firstResult(), GeomShapePtr());
105
106   execIfBaseNotEmpty();
107 }
108
109 //==================================================================================================
110 void FeaturesAPI_Revolution::setBase(const std::list<ModelHighAPI_Selection>& theBaseObjects)
111 {
112   mysketch->setValue(ObjectPtr());
113   mybaseObjects->clear();
114   fillAttribute(theBaseObjects, mybaseObjects);
115
116   execIfBaseNotEmpty();
117 }
118
119 //==================================================================================================
120 void FeaturesAPI_Revolution::setAxis(const ModelHighAPI_Selection& theAxis)
121 {
122   fillAttribute(theAxis, myaxis);
123
124   execIfBaseNotEmpty();
125 }
126
127 //==================================================================================================
128 void FeaturesAPI_Revolution::setAngles(const ModelHighAPI_Double& theToAngle,
129                                        const ModelHighAPI_Double& theFromAngle)
130 {
131   fillAttribute(FeaturesPlugin_Revolution::CREATION_METHOD_BY_ANGLES(), mycreationMethod);
132   fillAttribute(theToAngle, mytoAngle);
133   fillAttribute(theFromAngle, myfromAngle);
134
135   execIfBaseNotEmpty();
136 }
137
138 //==================================================================================================
139 void FeaturesAPI_Revolution::setAngle(const ModelHighAPI_Double& theAngle)
140 {
141   setAngles(theAngle, ModelHighAPI_Double());
142 }
143
144 //==================================================================================================
145 void FeaturesAPI_Revolution::setPlanesAndOffsets(const ModelHighAPI_Selection& theToObject,
146                                                 const ModelHighAPI_Double& theToOffset,
147                                                 const ModelHighAPI_Selection& theFromObject,
148                                                 const ModelHighAPI_Double& theFromOffset)
149 {
150   fillAttribute("ByPlanesAndOffsets", mycreationMethod);
151   fillAttribute(theToObject, mytoObject);
152   fillAttribute(theToOffset, mytoOffset);
153   fillAttribute(theFromObject, myfromObject);
154   fillAttribute(theFromOffset, myfromOffset);
155
156   execIfBaseNotEmpty();
157 }
158
159 //==================================================================================================
160 void FeaturesAPI_Revolution::dump(ModelHighAPI_Dumper& theDumper) const
161 {
162   FeaturePtr aBase = feature();
163   const std::string& aDocName = theDumper.name(aBase->document());
164
165   AttributeReferencePtr anAttrSketch = aBase->reference(FeaturesPlugin_Revolution::SKETCH_ID());
166   AttributeSelectionListPtr anAttrObjects =
167     aBase->selectionList(FeaturesPlugin_Revolution::BASE_OBJECTS_ID());
168   AttributeSelectionPtr anAttrAxis = aBase->selection(FeaturesPlugin_Revolution::AXIS_OBJECT_ID());
169
170   theDumper << aBase << " = model.addRevolution(" << aDocName << ", ";
171   anAttrSketch->isInitialized() ? theDumper << "[]" : theDumper << anAttrObjects;
172   theDumper << ", " << anAttrAxis;
173
174   std::string aCreationMethod =
175     aBase->string(FeaturesPlugin_Revolution::CREATION_METHOD())->value();
176
177   if(aCreationMethod == FeaturesPlugin_Revolution::CREATION_METHOD_BY_ANGLES()) {
178     AttributeDoublePtr anAttrToAngle = aBase->real(FeaturesPlugin_Revolution::TO_ANGLE_ID());
179     AttributeDoublePtr anAttrFromAngle = aBase->real(FeaturesPlugin_Revolution::FROM_ANGLE_ID());
180
181     theDumper << ", " << anAttrToAngle << ", " << anAttrFromAngle;
182   } else if(aCreationMethod == FeaturesPlugin_Revolution::CREATION_METHOD_BY_PLANES()) {
183     AttributeSelectionPtr anAttrToObject =
184       aBase->selection(FeaturesPlugin_Revolution::TO_OBJECT_ID());
185     AttributeDoublePtr anAttrToOffset = aBase->real(FeaturesPlugin_Revolution::TO_OFFSET_ID());
186     AttributeSelectionPtr anAttrFromObject =
187       aBase->selection(FeaturesPlugin_Revolution::FROM_OBJECT_ID());
188     AttributeDoublePtr anAttrFromOffset = aBase->real(FeaturesPlugin_Revolution::FROM_OFFSET_ID());
189
190     theDumper << ", " << anAttrToObject << ", " << anAttrToOffset <<
191       ", " << anAttrFromObject << ", " << anAttrFromOffset;
192   }
193
194   theDumper << ")" << std::endl;
195
196   if(anAttrSketch->isInitialized()) {
197     theDumper << aBase << ".setNestedSketch(" << anAttrSketch << ")" << std::endl;
198   }
199 }
200
201 //==================================================================================================
202 void FeaturesAPI_Revolution::execIfBaseNotEmpty()
203 {
204   if(mybaseObjects->size() > 0) {
205     execute();
206   }
207 }
208
209 //==================================================================================================
210 RevolutionPtr addRevolution(const std::shared_ptr<ModelAPI_Document>& thePart,
211                             const std::list<ModelHighAPI_Selection>& theBaseObjects,
212                             const ModelHighAPI_Selection& theAxis,
213                             const ModelHighAPI_Double& theAngle)
214 {
215   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Revolution::ID());
216   return RevolutionPtr(new FeaturesAPI_Revolution(aFeature, theBaseObjects, theAxis, theAngle));
217 }
218
219 //==================================================================================================
220 RevolutionPtr addRevolution(const std::shared_ptr<ModelAPI_Document>& thePart,
221                             const std::list<ModelHighAPI_Selection>& theBaseObjects,
222                             const ModelHighAPI_Selection& theAxis,
223                             const ModelHighAPI_Double& theToAngle,
224                             const ModelHighAPI_Double& theFromAngle)
225 {
226   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Revolution::ID());
227   return RevolutionPtr(new FeaturesAPI_Revolution(aFeature,
228                                                 theBaseObjects,
229                                                 theAxis,
230                                                 theToAngle,
231                                                 theFromAngle));
232 }
233
234 //==================================================================================================
235 RevolutionPtr addRevolution(const std::shared_ptr<ModelAPI_Document>& thePart,
236                             const std::list<ModelHighAPI_Selection>& theBaseObjects,
237                             const ModelHighAPI_Selection& theAxis,
238                             const ModelHighAPI_Selection& theToObject,
239                             const ModelHighAPI_Double& theToOffset,
240                             const ModelHighAPI_Selection& theFromObject,
241                             const ModelHighAPI_Double& theFromOffset)
242 {
243   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Revolution::ID());
244   return RevolutionPtr(new FeaturesAPI_Revolution(aFeature,
245                                                 theBaseObjects,
246                                                 theAxis,
247                                                 theToObject,
248                                                 theToOffset,
249                                                 theFromObject,
250                                                 theFromOffset));
251 }