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