Salome HOME
[Code coverage FeaturesAPI]: Improve coverage of Scale, Rotation, Revolution and...
[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   setAngles(theAngle, ModelHighAPI_Double());
143 }
144
145 //==================================================================================================
146 void FeaturesAPI_Revolution::setPlanesAndOffsets(const ModelHighAPI_Selection& theToObject,
147                                                 const ModelHighAPI_Double& theToOffset,
148                                                 const ModelHighAPI_Selection& theFromObject,
149                                                 const ModelHighAPI_Double& theFromOffset)
150 {
151   fillAttribute("ByPlanesAndOffsets", mycreationMethod);
152   fillAttribute(theToObject, mytoObject);
153   fillAttribute(theToOffset, mytoOffset);
154   fillAttribute(theFromObject, myfromObject);
155   fillAttribute(theFromOffset, myfromOffset);
156
157   execIfBaseNotEmpty();
158 }
159
160 //==================================================================================================
161 void FeaturesAPI_Revolution::dump(ModelHighAPI_Dumper& theDumper) const
162 {
163   FeaturePtr aBase = feature();
164   const std::string& aDocName = theDumper.name(aBase->document());
165
166   AttributeReferencePtr anAttrSketch = aBase->reference(FeaturesPlugin_Revolution::SKETCH_ID());
167   AttributeSelectionListPtr anAttrObjects =
168     aBase->selectionList(FeaturesPlugin_Revolution::BASE_OBJECTS_ID());
169   AttributeSelectionPtr anAttrAxis = aBase->selection(FeaturesPlugin_Revolution::AXIS_OBJECT_ID());
170
171   theDumper << aBase << " = model.addRevolution(" << aDocName << ", ";
172   anAttrSketch->isInitialized() ? theDumper << "[]" : theDumper << anAttrObjects;
173   theDumper << ", " << anAttrAxis;
174
175   std::string aCreationMethod =
176     aBase->string(FeaturesPlugin_Revolution::CREATION_METHOD())->value();
177
178   if(aCreationMethod == FeaturesPlugin_Revolution::CREATION_METHOD_BY_ANGLES()) {
179     AttributeDoublePtr anAttrToAngle = aBase->real(FeaturesPlugin_Revolution::TO_ANGLE_ID());
180     AttributeDoublePtr anAttrFromAngle = aBase->real(FeaturesPlugin_Revolution::FROM_ANGLE_ID());
181
182     theDumper << ", " << anAttrToAngle << ", " << anAttrFromAngle;
183   } else if(aCreationMethod == FeaturesPlugin_Revolution::CREATION_METHOD_BY_PLANES()) {
184     AttributeSelectionPtr anAttrToObject =
185       aBase->selection(FeaturesPlugin_Revolution::TO_OBJECT_ID());
186     AttributeDoublePtr anAttrToOffset = aBase->real(FeaturesPlugin_Revolution::TO_OFFSET_ID());
187     AttributeSelectionPtr anAttrFromObject =
188       aBase->selection(FeaturesPlugin_Revolution::FROM_OBJECT_ID());
189     AttributeDoublePtr anAttrFromOffset = aBase->real(FeaturesPlugin_Revolution::FROM_OFFSET_ID());
190
191     theDumper << ", " << anAttrToObject << ", " << anAttrToOffset <<
192       ", " << anAttrFromObject << ", " << anAttrFromOffset;
193   }
194
195   theDumper << ")" << std::endl;
196
197   if(anAttrSketch->isInitialized()) {
198     theDumper << aBase << ".setNestedSketch(" << anAttrSketch << ")" << std::endl;
199   }
200 }
201
202 //==================================================================================================
203 void FeaturesAPI_Revolution::execIfBaseNotEmpty()
204 {
205   if(mybaseObjects->size() > 0) {
206     execute();
207   }
208 }
209
210 //==================================================================================================
211 RevolutionPtr addRevolution(const std::shared_ptr<ModelAPI_Document>& thePart,
212                             const std::list<ModelHighAPI_Selection>& theBaseObjects,
213                             const ModelHighAPI_Selection& theAxis,
214                             const ModelHighAPI_Double& theAngle)
215 {
216   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Revolution::ID());
217   return RevolutionPtr(new FeaturesAPI_Revolution(aFeature, theBaseObjects, theAxis, theAngle));
218 }
219
220 //==================================================================================================
221 RevolutionPtr addRevolution(const std::shared_ptr<ModelAPI_Document>& thePart,
222                             const std::list<ModelHighAPI_Selection>& theBaseObjects,
223                             const ModelHighAPI_Selection& theAxis,
224                             const ModelHighAPI_Double& theToAngle,
225                             const ModelHighAPI_Double& theFromAngle)
226 {
227   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Revolution::ID());
228   return RevolutionPtr(new FeaturesAPI_Revolution(aFeature,
229                                                 theBaseObjects,
230                                                 theAxis,
231                                                 theToAngle,
232                                                 theFromAngle));
233 }
234
235 //==================================================================================================
236 RevolutionPtr addRevolution(const std::shared_ptr<ModelAPI_Document>& thePart,
237                             const std::list<ModelHighAPI_Selection>& theBaseObjects,
238                             const ModelHighAPI_Selection& theAxis,
239                             const ModelHighAPI_Selection& theToObject,
240                             const ModelHighAPI_Double& theToOffset,
241                             const ModelHighAPI_Selection& theFromObject,
242                             const ModelHighAPI_Double& theFromOffset)
243 {
244   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_Revolution::ID());
245   return RevolutionPtr(new FeaturesAPI_Revolution(aFeature,
246                                                 theBaseObjects,
247                                                 theAxis,
248                                                 theToObject,
249                                                 theToOffset,
250                                                 theFromObject,
251                                                 theFromOffset));
252 }