1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModelAPI_Feature.cpp
4 // Created: 17 Jul 2014
5 // Author: Mikhail PONIKAROV
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>
16 void ModelAPI_Feature::setError(const std::string& theError,
20 std::string anError = isTranslate ? Config_Translator::translate(getKind(), theError)
22 data()->setError(anError, isSend);
25 const std::list<std::shared_ptr<ModelAPI_Result> >& ModelAPI_Feature::results()
30 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::firstResult() const
32 return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.begin());
35 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::lastResult()
37 return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.rbegin());
40 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult)
42 static Events_ID EVENT_UPD = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
43 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
45 if (firstResult() == theResult) {
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);
55 myResults.push_back(theResult);
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);
62 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult,
65 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
66 for (int anIndex = 0; anIndex < theIndex; anIndex++) {
69 if (aResIter == myResults.end()) { // append
70 myResults.push_back(theResult);
72 *aResIter = theResult;
74 theResult->setDisabled(theResult, false);
77 void ModelAPI_Feature::removeResult(const std::shared_ptr<ModelAPI_Result>& theResult)
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);
86 void ModelAPI_Feature::eraseResultFromList(const std::shared_ptr<ModelAPI_Result>& theResult)
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);
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);
107 void ModelAPI_Feature::removeResults(
108 const int theSinceIndex, const bool theForever, const bool theFlush)
110 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
111 for(int anIndex = 0; anIndex < theSinceIndex && aResIter != myResults.end(); anIndex++)
115 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aNextIter = aResIter;
116 while( aNextIter != myResults.end()) {
117 aGroup = (*aNextIter)->groupName();
118 // remove previously erased results: to enable later if needed only actual (of history change)
119 (*aNextIter)->setDisabled(*aNextIter, true); // just disable results
121 aNextIter = myResults.erase(aNextIter);
126 if (!aGroup.empty() && theFlush) {
127 // flush visualisation changes
128 static Events_Loop* aLoop = Events_Loop::loop();
129 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
130 aLoop->flush(aRedispEvent);
131 static Events_ID aDelEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
132 aLoop->flush(aDelEvent);
136 void ModelAPI_Feature::eraseResults(const bool theForever)
138 removeResults(0, theForever, true);
141 const std::string& ModelAPI_Feature::documentToAdd()
143 // empty to use the current document
144 static const std::string anEmpty;
148 void ModelAPI_Feature::erase()
150 // if this is the current feature, make the upper feature as current before removing
151 if (document().get() && document()->currentFeature(false).get() == this) {
152 document()->setCurrentFeatureUp();
155 static Events_Loop* aLoop = Events_Loop::loop();
156 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
157 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
159 while (!myResults.empty()) { // remove one by one with messages
160 std::shared_ptr<ModelAPI_Result> aRes = *(myResults.begin());
161 aRes->setDisabled(aRes, true); // to avoid activation of the Part result
162 if (!myResults.empty()) // disabling result may erase the list (on undo of Part, issue 665)
163 myResults.erase(myResults.begin());
165 ModelAPI_Object::erase();
168 ModelAPI_Feature::~ModelAPI_Feature()
173 FeaturePtr ModelAPI_Feature::feature(ObjectPtr theObject)
175 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
177 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
179 DocumentPtr aDoc = aResult->document();
180 return aDoc->feature(aResult);
186 bool ModelAPI_Feature::isMacro() const
191 bool ModelAPI_Feature::setDisabled(const bool theFlag)
193 if (myIsDisabled != theFlag) {
194 myIsDisabled = theFlag;
196 removeResults(0, false, false); // flush will be in setCurrentFeature
198 // enable all disabled previously results
199 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
200 for(; aResIter != myResults.end(); aResIter++) {
201 (*aResIter)->setDisabled(*aResIter, false);
203 // update selection for the case something was updated higher in the history
204 // while this feature was disabled
205 static Events_Loop* aLoop = Events_Loop::loop();
206 static Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
207 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
208 aECreator->sendUpdated(data()->owner(), kUpdatedSel, false);
215 bool ModelAPI_Feature::isDisabled()
220 bool ModelAPI_Feature::setStable(const bool theFlag)
222 if (myIsStable != theFlag) {
223 myIsStable = theFlag;
224 // send an event about the stability change (editing is started/finished)
225 static Events_Loop* aLoop = Events_Loop::loop();
226 static Events_ID EVENT_STAB = aLoop->eventByName(EVENT_STABILITY_CHANGED);
227 std::shared_ptr<Events_Message> aMessage(new Events_Message(EVENT_STAB, this));
228 aLoop->send(aMessage, false);
234 bool ModelAPI_Feature::isStable()
239 bool ModelAPI_Feature::customAction(const std::string& theActionId)
244 bool ModelAPI_Feature::isPreviewNeeded() const
249 void ModelAPI_Feature::init()
251 myIsDisabled = false;