Salome HOME
[Code coverage FeaturesAPI]: Improve coverage of Scale, Rotation, Revolution and...
[modules/shaper.git] / src / FeaturesAPI / FeaturesAPI_RevolutionBoolean.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_RevolutionBoolean.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_RevolutionBoolean::FeaturesAPI_RevolutionBoolean(
30   const std::shared_ptr<ModelAPI_Feature>& theFeature)
31 : ModelHighAPI_Interface(theFeature)
32 {
33 }
34
35 //==================================================================================================
36 FeaturesAPI_RevolutionBoolean::~FeaturesAPI_RevolutionBoolean()
37 {
38 }
39
40 //==================================================================================================
41 void FeaturesAPI_RevolutionBoolean::setNestedSketch(const ModelHighAPI_Reference& theSketch)
42 {
43   mysketch->setValue(theSketch.feature());
44
45   // To make Sketch feature execute and subfeatures execute.
46   feature()->document()->setCurrentFeature(feature(), false);
47
48   // to inform that the history is updated due to the sketch moved under the composite feature
49   if (theSketch.feature().get()) {
50     theSketch.feature()->document()->updateHistory(ModelAPI_Feature::group());
51     if (theSketch.feature()->firstResult().get())
52       theSketch.feature()->firstResult()->setDisplayed(false);
53   }
54   mybaseObjects->clear();
55   mybaseObjects->append(theSketch.feature()->firstResult(), GeomShapePtr());
56
57   execIfBaseNotEmpty();
58 }
59
60 //==================================================================================================
61 void FeaturesAPI_RevolutionBoolean::setBase(const std::list<ModelHighAPI_Selection>& theBaseObjects)
62 {
63   mysketch->setValue(ObjectPtr());
64   mybaseObjects->clear();
65   fillAttribute(theBaseObjects, mybaseObjects);
66
67   execIfBaseNotEmpty();
68 }
69
70 //==================================================================================================
71 void FeaturesAPI_RevolutionBoolean::setAxis(const ModelHighAPI_Selection& theAxis)
72 {
73   fillAttribute(theAxis, myaxis);
74
75   execIfBaseNotEmpty();
76 }
77
78 //==================================================================================================
79 void FeaturesAPI_RevolutionBoolean::setAngles(const ModelHighAPI_Double& theToAngle,
80                                               const ModelHighAPI_Double& theFromAngle)
81 {
82   fillAttribute(FeaturesPlugin_Revolution::CREATION_METHOD_BY_ANGLES(), mycreationMethod);
83   fillAttribute(theToAngle, mytoAngle);
84   fillAttribute(theFromAngle, myfromAngle);
85
86   execIfBaseNotEmpty();
87 }
88
89 //==================================================================================================
90 void FeaturesAPI_RevolutionBoolean::setAngle(const ModelHighAPI_Double& theAngle)
91 {
92   setAngles(theAngle, ModelHighAPI_Double());
93 }
94
95 //==================================================================================================
96 void FeaturesAPI_RevolutionBoolean::setPlanesAndOffsets(const ModelHighAPI_Selection& theToObject,
97                                                         const ModelHighAPI_Double& theToOffset,
98                                                       const ModelHighAPI_Selection& theFromObject,
99                                                         const ModelHighAPI_Double& theFromOffset)
100 {
101   fillAttribute(FeaturesPlugin_Revolution::CREATION_METHOD_BY_PLANES(), mycreationMethod);
102   fillAttribute(theToObject, mytoObject);
103   fillAttribute(theToOffset, mytoOffset);
104   fillAttribute(theFromObject, myfromObject);
105   fillAttribute(theFromOffset, myfromOffset);
106
107   execIfBaseNotEmpty();
108 }
109
110 //==================================================================================================
111 void FeaturesAPI_RevolutionBoolean::setBooleanObjects(
112   const std::list<ModelHighAPI_Selection>& theBooleanObjects)
113 {
114   fillAttribute(theBooleanObjects, mybooleanObjects);
115
116   execIfBaseNotEmpty();
117 }
118
119 //==================================================================================================
120 void FeaturesAPI_RevolutionBoolean::dump(ModelHighAPI_Dumper& theDumper) const
121 {
122   FeaturePtr aBase = feature();
123   const std::string& aDocName = theDumper.name(aBase->document());
124
125   AttributeReferencePtr anAttrSketch =
126     aBase->reference(FeaturesPlugin_Revolution::SKETCH_ID());
127   AttributeSelectionListPtr anAttrObjects =
128     aBase->selectionList(FeaturesPlugin_Revolution::BASE_OBJECTS_ID());
129   AttributeSelectionPtr anAttrAxis =
130     aBase->selection(FeaturesPlugin_Revolution::AXIS_OBJECT_ID());
131
132   theDumper << aBase << " = model.addRevolution";
133   if(aBase->getKind() == FeaturesPlugin_RevolutionCut::ID()) {
134     theDumper << "Cut";
135   } else if(aBase->getKind() == FeaturesPlugin_RevolutionFuse::ID()) {
136     theDumper << "Fuse";
137   }
138   theDumper << "(" << aDocName << ", ";
139   anAttrSketch->isInitialized() ? theDumper << "[]" : theDumper << anAttrObjects;
140   theDumper << ", " << anAttrAxis;
141
142   std::string aCreationMethod =
143     aBase->string(FeaturesPlugin_Revolution::CREATION_METHOD())->value();
144
145   if(aCreationMethod == FeaturesPlugin_Revolution::CREATION_METHOD_BY_ANGLES()) {
146     AttributeDoublePtr anAttrToAngle = aBase->real(FeaturesPlugin_Revolution::TO_ANGLE_ID());
147     AttributeDoublePtr anAttrFromAngle = aBase->real(FeaturesPlugin_Revolution::FROM_ANGLE_ID());
148
149     theDumper << ", " << anAttrToAngle << ", " << anAttrFromAngle;
150   } else if(aCreationMethod == FeaturesPlugin_Revolution::CREATION_METHOD_BY_PLANES()) {
151     AttributeSelectionPtr anAttrToObject =
152       aBase->selection(FeaturesPlugin_Revolution::TO_OBJECT_ID());
153     AttributeDoublePtr anAttrToOffset =
154       aBase->real(FeaturesPlugin_Revolution::TO_OFFSET_ID());
155     AttributeSelectionPtr anAttrFromObject =
156       aBase->selection(FeaturesPlugin_Revolution::FROM_OBJECT_ID());
157     AttributeDoublePtr anAttrFromOffset =
158       aBase->real(FeaturesPlugin_Revolution::FROM_OFFSET_ID());
159
160     theDumper << ", " << anAttrToObject << ", " << anAttrToOffset <<
161       ", " << anAttrFromObject << ", " << anAttrFromOffset;
162   }
163
164   AttributeSelectionListPtr anAttrBoolObjects =
165     aBase->selectionList(FeaturesPlugin_CompositeBoolean::OBJECTS_ID());
166   theDumper << ", " << anAttrBoolObjects << ")" << std::endl;
167
168   if(anAttrSketch->isInitialized()) {
169     theDumper << aBase << ".setNestedSketch(" << anAttrSketch << ")" << std::endl;
170   }
171 }
172
173 //==================================================================================================
174 void FeaturesAPI_RevolutionBoolean::execIfBaseNotEmpty()
175 {
176   if(mybaseObjects->size() > 0) {
177     execute();
178   }
179 }
180
181 //==================================================================================================
182 FeaturesAPI_RevolutionCut::FeaturesAPI_RevolutionCut(
183   const std::shared_ptr<ModelAPI_Feature>& theFeature)
184 : FeaturesAPI_RevolutionBoolean(theFeature)
185 {
186   initialize();
187 }
188
189 //==================================================================================================
190 FeaturesAPI_RevolutionCut::FeaturesAPI_RevolutionCut(
191   const std::shared_ptr<ModelAPI_Feature>& theFeature,
192   const std::list<ModelHighAPI_Selection>& theBaseObjects,
193   const ModelHighAPI_Selection& theAxis,
194   const ModelHighAPI_Double& theSize,
195   const std::list<ModelHighAPI_Selection>& theBooleanObjects)
196 : FeaturesAPI_RevolutionBoolean(theFeature)
197 {
198   if(initialize()) {
199     fillAttribute(theBaseObjects, mybaseObjects);
200     fillAttribute(theAxis, myaxis);
201     setAngle(theSize);
202     setBooleanObjects(theBooleanObjects);
203   }
204 }
205
206 //==================================================================================================
207 FeaturesAPI_RevolutionCut::FeaturesAPI_RevolutionCut(
208   const std::shared_ptr<ModelAPI_Feature>& theFeature,
209   const std::list<ModelHighAPI_Selection>& theBaseObjects,
210   const ModelHighAPI_Selection& theAxis,
211   const ModelHighAPI_Double& theToAngle,
212   const ModelHighAPI_Double& theFromAngle,
213   const std::list<ModelHighAPI_Selection>& theBooleanObjects)
214 : FeaturesAPI_RevolutionBoolean(theFeature)
215 {
216   if(initialize()) {
217     fillAttribute(theBaseObjects, mybaseObjects);
218     fillAttribute(theAxis, myaxis);
219     fillAttribute(FeaturesPlugin_Revolution::CREATION_METHOD_BY_ANGLES(), mycreationMethod);
220     fillAttribute(theToAngle, mytoAngle);
221     fillAttribute(theFromAngle, myfromAngle);
222     setBooleanObjects(theBooleanObjects);
223   }
224 }
225
226 //==================================================================================================
227 FeaturesAPI_RevolutionCut::FeaturesAPI_RevolutionCut(
228   const std::shared_ptr<ModelAPI_Feature>& theFeature,
229   const std::list<ModelHighAPI_Selection>& theBaseObjects,
230   const ModelHighAPI_Selection& theAxis,
231   const ModelHighAPI_Selection& theToObject,
232   const ModelHighAPI_Double& theToOffset,
233   const ModelHighAPI_Selection& theFromObject,
234   const ModelHighAPI_Double& theFromOffset,
235   const std::list<ModelHighAPI_Selection>& theBooleanObjects)
236 : FeaturesAPI_RevolutionBoolean(theFeature)
237 {
238   if(initialize()) {
239     fillAttribute(theBaseObjects, mybaseObjects);
240     fillAttribute(theAxis, myaxis);
241     fillAttribute(FeaturesPlugin_Revolution::CREATION_METHOD_BY_PLANES(), mycreationMethod);
242     fillAttribute(theToObject, mytoObject);
243     fillAttribute(theToOffset, mytoOffset);
244     fillAttribute(theFromObject, myfromObject);
245     fillAttribute(theFromOffset, myfromOffset);
246     setBooleanObjects(theBooleanObjects);
247   }
248 }
249
250 //==================================================================================================
251 RevolutionCutPtr addRevolutionCut(const std::shared_ptr<ModelAPI_Document>& thePart,
252                                   const std::list<ModelHighAPI_Selection>& theBaseObjects,
253                                   const ModelHighAPI_Selection& theAxis,
254                                   const ModelHighAPI_Double& theSize,
255                                   const std::list<ModelHighAPI_Selection>& theBooleanObjects)
256 {
257   std::shared_ptr<ModelAPI_Feature> aFeature =
258     thePart->addFeature(FeaturesPlugin_RevolutionCut::ID());
259   return RevolutionCutPtr(new FeaturesAPI_RevolutionCut(aFeature, theBaseObjects,
260                                                         theAxis, theSize, theBooleanObjects));
261 }
262
263 //==================================================================================================
264 RevolutionCutPtr addRevolutionCut(const std::shared_ptr<ModelAPI_Document>& thePart,
265                                   const std::list<ModelHighAPI_Selection>& theBaseObjects,
266                                   const ModelHighAPI_Selection& theAxis,
267                                   const ModelHighAPI_Double& theToAngle,
268                                   const ModelHighAPI_Double& theFromAngle,
269                                   const std::list<ModelHighAPI_Selection>& theBooleanObjects)
270 {
271   std::shared_ptr<ModelAPI_Feature> aFeature =
272     thePart->addFeature(FeaturesPlugin_RevolutionCut::ID());
273   return RevolutionCutPtr(new FeaturesAPI_RevolutionCut(aFeature,
274                                                       theBaseObjects,
275                                                       theAxis,
276                                                       theToAngle,
277                                                       theFromAngle,
278                                                       theBooleanObjects));
279 }
280
281 //==================================================================================================
282 RevolutionCutPtr addRevolutionCut(const std::shared_ptr<ModelAPI_Document>& thePart,
283                                   const std::list<ModelHighAPI_Selection>& theBaseObjects,
284                                   const ModelHighAPI_Selection& theAxis,
285                                   const ModelHighAPI_Selection& theToObject,
286                                   const ModelHighAPI_Double& theToOffset,
287                                   const ModelHighAPI_Selection& theFromObject,
288                                   const ModelHighAPI_Double& theFromOffset,
289                                   const std::list<ModelHighAPI_Selection>& theBooleanObjects)
290 {
291   std::shared_ptr<ModelAPI_Feature> aFeature =
292     thePart->addFeature(FeaturesPlugin_RevolutionCut::ID());
293   return RevolutionCutPtr(new FeaturesAPI_RevolutionCut(aFeature,
294                                                       theBaseObjects,
295                                                       theAxis,
296                                                       theToObject,
297                                                       theToOffset,
298                                                       theFromObject,
299                                                       theFromOffset,
300                                                       theBooleanObjects));
301 }
302
303
304 //==================================================================================================
305 FeaturesAPI_RevolutionFuse::FeaturesAPI_RevolutionFuse(
306   const std::shared_ptr<ModelAPI_Feature>& theFeature)
307 : FeaturesAPI_RevolutionBoolean(theFeature)
308 {
309   initialize();
310 }
311
312 //==================================================================================================
313 FeaturesAPI_RevolutionFuse::FeaturesAPI_RevolutionFuse(
314   const std::shared_ptr<ModelAPI_Feature>& theFeature,
315   const std::list<ModelHighAPI_Selection>& theBaseObjects,
316   const ModelHighAPI_Selection& theAxis,
317   const ModelHighAPI_Double& theSize,
318   const std::list<ModelHighAPI_Selection>& theBooleanObjects)
319 : FeaturesAPI_RevolutionBoolean(theFeature)
320 {
321   if(initialize()) {
322     fillAttribute(theBaseObjects, mybaseObjects);
323     fillAttribute(theAxis, myaxis);
324     fillAttribute(FeaturesPlugin_Revolution::CREATION_METHOD_BY_ANGLES(), mycreationMethod);
325     fillAttribute(theSize, mytoAngle);
326     fillAttribute(ModelHighAPI_Double(), myfromAngle);
327     setBooleanObjects(theBooleanObjects);
328   }
329 }
330
331 //==================================================================================================
332 FeaturesAPI_RevolutionFuse::FeaturesAPI_RevolutionFuse(
333   const std::shared_ptr<ModelAPI_Feature>& theFeature,
334   const std::list<ModelHighAPI_Selection>& theBaseObjects,
335   const ModelHighAPI_Selection& theAxis,
336   const ModelHighAPI_Double& theToAngle,
337   const ModelHighAPI_Double& theFromAngle,
338   const std::list<ModelHighAPI_Selection>& theBooleanObjects)
339 : FeaturesAPI_RevolutionBoolean(theFeature)
340 {
341   if(initialize()) {
342     fillAttribute(theBaseObjects, mybaseObjects);
343     fillAttribute(theAxis, myaxis);
344     fillAttribute(FeaturesPlugin_Revolution::CREATION_METHOD_BY_ANGLES(), mycreationMethod);
345     fillAttribute(theToAngle, mytoAngle);
346     fillAttribute(theFromAngle, myfromAngle);
347     setBooleanObjects(theBooleanObjects);
348   }
349 }
350
351 //==================================================================================================
352 FeaturesAPI_RevolutionFuse::FeaturesAPI_RevolutionFuse(
353   const std::shared_ptr<ModelAPI_Feature>& theFeature,
354   const std::list<ModelHighAPI_Selection>& theBaseObjects,
355   const ModelHighAPI_Selection& theAxis,
356   const ModelHighAPI_Selection& theToObject,
357   const ModelHighAPI_Double& theToOffset,
358   const ModelHighAPI_Selection& theFromObject,
359   const ModelHighAPI_Double& theFromOffset,
360   const std::list<ModelHighAPI_Selection>& theBooleanObjects)
361 : FeaturesAPI_RevolutionBoolean(theFeature)
362 {
363   if(initialize()) {
364     fillAttribute(theBaseObjects, mybaseObjects);
365     fillAttribute(theAxis, myaxis);
366     fillAttribute(FeaturesPlugin_Revolution::CREATION_METHOD_BY_PLANES(), mycreationMethod);
367     fillAttribute(theToObject, mytoObject);
368     fillAttribute(theToOffset, mytoOffset);
369     fillAttribute(theFromObject, myfromObject);
370     fillAttribute(theFromOffset, myfromOffset);
371     setBooleanObjects(theBooleanObjects);
372   }
373 }
374
375 //==================================================================================================
376 RevolutionFusePtr addRevolutionFuse(const std::shared_ptr<ModelAPI_Document>& thePart,
377                                     const std::list<ModelHighAPI_Selection>& theBaseObjects,
378                                     const ModelHighAPI_Selection& theAxis,
379                                     const ModelHighAPI_Double& theSize,
380                                     const std::list<ModelHighAPI_Selection>& theBooleanObjects)
381 {
382   std::shared_ptr<ModelAPI_Feature> aFeature =
383     thePart->addFeature(FeaturesPlugin_RevolutionFuse::ID());
384   return RevolutionFusePtr(new FeaturesAPI_RevolutionFuse(aFeature, theBaseObjects,
385                                                           theAxis, theSize, theBooleanObjects));
386 }
387
388 //==================================================================================================
389 RevolutionFusePtr addRevolutionFuse(const std::shared_ptr<ModelAPI_Document>& thePart,
390                                     const std::list<ModelHighAPI_Selection>& theBaseObjects,
391                                     const ModelHighAPI_Selection& theAxis,
392                                     const ModelHighAPI_Double& theToAngle,
393                                     const ModelHighAPI_Double& theFromAngle,
394                                     const std::list<ModelHighAPI_Selection>& theBooleanObjects)
395 {
396   std::shared_ptr<ModelAPI_Feature> aFeature =
397     thePart->addFeature(FeaturesPlugin_RevolutionFuse::ID());
398   return RevolutionFusePtr(new FeaturesAPI_RevolutionFuse(aFeature,
399                                                         theBaseObjects,
400                                                         theAxis,
401                                                         theToAngle,
402                                                         theFromAngle,
403                                                         theBooleanObjects));
404 }
405
406 //==================================================================================================
407 RevolutionFusePtr addRevolutionFuse(const std::shared_ptr<ModelAPI_Document>& thePart,
408                                     const std::list<ModelHighAPI_Selection>& theBaseObjects,
409                                     const ModelHighAPI_Selection& theAxis,
410                                     const ModelHighAPI_Selection& theToObject,
411                                     const ModelHighAPI_Double& theToOffset,
412                                     const ModelHighAPI_Selection& theFromObject,
413                                     const ModelHighAPI_Double& theFromOffset,
414                                     const std::list<ModelHighAPI_Selection>& theBooleanObjects)
415 {
416   std::shared_ptr<ModelAPI_Feature> aFeature =
417     thePart->addFeature(FeaturesPlugin_RevolutionFuse::ID());
418   return RevolutionFusePtr(new FeaturesAPI_RevolutionFuse(aFeature,
419                                                         theBaseObjects,
420                                                         theAxis,
421                                                         theToObject,
422                                                         theToOffset,
423                                                         theFromObject,
424                                                         theFromOffset,
425                                                         theBooleanObjects));
426 }