]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelAPI/ModelAPI_Feature.cpp
Salome HOME
Added MultiRotation feature (parametric API).
[modules/shaper.git] / src / ModelAPI / ModelAPI_Feature.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_Feature.cpp
4 // Created:     17 Jul 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "ModelAPI_Feature.h"
8 #include <ModelAPI_Events.h>
9 #include <ModelAPI_Result.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Session.h>
13 #include <Events_Loop.h>
14 #include <Config_Translator.h>
15
16 void ModelAPI_Feature::setError(const std::string& theError,
17                                 bool isSend,
18                                 bool isTranslate)
19 {
20   std::string anError = isTranslate ? Config_Translator::translate(getKind(), theError)
21                                     : theError;
22   data()->setError(anError, isSend);
23 }
24
25 const std::list<std::shared_ptr<ModelAPI_Result> >& ModelAPI_Feature::results()
26 {
27   return myResults;
28 }
29
30 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::firstResult() const
31 {
32   return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.begin());
33 }
34
35 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::lastResult()
36 {
37   return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.rbegin());
38 }
39
40 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult)
41 {
42   static Events_ID EVENT_UPD = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
43   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
44
45   if (firstResult() == theResult) {
46     // nothing to change
47   } else if (!myResults.empty()) {  // all except first become disabled
48     std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
49     *aResIter = theResult;
50     aECreator->sendUpdated(theResult, EVENT_UPD);
51     for(aResIter++; aResIter != myResults.end(); aResIter++) {
52       (*aResIter)->setDisabled((*aResIter), true);
53     }
54   } else {
55     myResults.push_back(theResult);
56   }
57   // in any case result becomes enabled
58   if (!isDisabled()) // disabled feature may be executed when it is added as not enabled (#2078)
59     theResult->setDisabled(theResult, false);
60 }
61
62 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult,
63                                  const int theIndex)
64 {
65   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
66   for (int anIndex = 0; anIndex < theIndex; anIndex++) {
67     aResIter++;
68   }
69   if (aResIter == myResults.end()) {  // append
70     myResults.push_back(theResult);
71   } else {  // update
72     *aResIter = theResult;
73   }
74   theResult->setDisabled(theResult, false);
75 }
76
77 void ModelAPI_Feature::removeResult(const std::shared_ptr<ModelAPI_Result>& theResult)
78 {
79   theResult->setDisabled(theResult, true);
80   // flush visualisation changes
81   static Events_Loop* aLoop = Events_Loop::loop();
82   static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
83   aLoop->flush(aRedispEvent);
84 }
85
86 void ModelAPI_Feature::eraseResultFromList(const std::shared_ptr<ModelAPI_Result>& theResult)
87 {
88   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
89   for(; aResIter != myResults.end(); aResIter++) {
90     ResultPtr aRes = *aResIter;
91     if (aRes == theResult) {
92       std::string aGroup = aRes->groupName();
93       aRes->setDisabled(aRes, true); // for complex results to disable all subs
94       aRes->data()->erase();
95       myResults.erase(aResIter);
96
97       static Events_Loop* aLoop = Events_Loop::loop();
98       static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
99       static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
100       aECreator->sendDeleted(document(), aGroup);
101       aECreator->sendUpdated(aRes, EVENT_DISP);
102       break;
103     }
104   }
105 }
106
107 void ModelAPI_Feature::removeResults(const int theSinceIndex, const bool theFlush)
108 {
109   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
110   for(int anIndex = 0; anIndex < theSinceIndex && aResIter != myResults.end(); anIndex++)
111     aResIter++;
112
113   std::string aGroup;
114   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aNextIter = aResIter;
115   while( aNextIter != myResults.end()) {
116     aGroup = (*aNextIter)->groupName();
117     // remove previously erased results: to enable later if needed only actual (of history change)
118     //if (theSinceIndex == 0 && (*aNextIter)->isDisabled()) {
119     //  aNextIter = myResults.erase(aNextIter);
120     //} else {
121       (*aNextIter)->setDisabled(*aNextIter, true); // just disable results
122       aNextIter++;
123     //}
124   }
125   if (!aGroup.empty() && theFlush) {
126     // flush visualisation changes
127     static Events_Loop* aLoop = Events_Loop::loop();
128     static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
129     aLoop->flush(aRedispEvent);
130     static Events_ID aDelEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
131     aLoop->flush(aDelEvent);
132   }
133 }
134
135 void ModelAPI_Feature::eraseResults()
136 {
137   removeResults(0);
138 }
139
140 const std::string& ModelAPI_Feature::documentToAdd()
141 {
142   // empty to use the current document
143   static const std::string anEmpty;
144   return anEmpty;
145 }
146
147 void ModelAPI_Feature::erase()
148 {
149   // if this is the current feature, make the upper feature as current before removing
150   if (document().get() && document()->currentFeature(false).get() == this) {
151     document()->setCurrentFeatureUp();
152   }
153
154   static Events_Loop* aLoop = Events_Loop::loop();
155   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
156   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
157
158   while (!myResults.empty()) {  // remove one by one with messages
159     std::shared_ptr<ModelAPI_Result> aRes = *(myResults.begin());
160     aRes->setDisabled(aRes, true); // to avoid activation of the Part result
161     if (!myResults.empty()) // disabling result may erase the list (on undo of Part, issue 665)
162       myResults.erase(myResults.begin());
163   }
164   ModelAPI_Object::erase();
165 }
166
167 ModelAPI_Feature::~ModelAPI_Feature()
168 {
169   erase();
170 }
171
172 FeaturePtr ModelAPI_Feature::feature(ObjectPtr theObject)
173 {
174   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
175   if (!aFeature) {
176     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
177     if (aResult) {
178       DocumentPtr aDoc = aResult->document();
179       return aDoc->feature(aResult);
180     }
181   }
182   return aFeature;
183 }
184
185 bool ModelAPI_Feature::isMacro() const
186 {
187   return false;
188 }
189
190 bool ModelAPI_Feature::setDisabled(const bool theFlag)
191 {
192   if (myIsDisabled != theFlag) {
193     myIsDisabled = theFlag;
194     if (myIsDisabled) {
195       removeResults(0, false); // flush will be in setCurrentFeature
196     } else {
197       // enable all disabled previously results
198       std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
199       for(; aResIter != myResults.end(); aResIter++) {
200         (*aResIter)->setDisabled(*aResIter, false);
201       }
202       // update selection for the case something was updated higher in the history
203       // while this feature was disabled
204       static Events_Loop* aLoop = Events_Loop::loop();
205       static Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
206       static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
207       aECreator->sendUpdated(data()->owner(), kUpdatedSel, false);
208     }
209     return true;
210   }
211   return false;
212 }
213
214 bool ModelAPI_Feature::isDisabled()
215 {
216   return myIsDisabled;
217 }
218
219 bool ModelAPI_Feature::setStable(const bool theFlag)
220 {
221   if (myIsStable != theFlag) {
222     myIsStable = theFlag;
223     // send an event about the stability change (editing is started/finished)
224     static Events_Loop* aLoop = Events_Loop::loop();
225     static Events_ID EVENT_STAB = aLoop->eventByName(EVENT_STABILITY_CHANGED);
226     std::shared_ptr<Events_Message> aMessage(new Events_Message(EVENT_STAB, this));
227     aLoop->send(aMessage, false);
228     return true;
229   }
230   return false;
231 }
232
233 bool ModelAPI_Feature::isStable()
234 {
235   return myIsStable;
236 }
237
238 bool ModelAPI_Feature::customAction(const std::string& theActionId)
239 {
240   return false;
241 }
242
243 bool ModelAPI_Feature::isPreviewNeeded() const
244 {
245   return true;
246 }
247
248 void ModelAPI_Feature::init()
249 {
250   myIsDisabled = false;
251   myIsStable = true;
252 }