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