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 theResult->setDisabled(theResult, false);
61 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult,
64 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
65 for (int anIndex = 0; anIndex < theIndex; anIndex++) {
68 if (aResIter == myResults.end()) { // append
69 myResults.push_back(theResult);
71 *aResIter = theResult;
73 theResult->setDisabled(theResult, false);
76 void ModelAPI_Feature::removeResult(const std::shared_ptr<ModelAPI_Result>& theResult)
78 theResult->setDisabled(theResult, true);
79 // flush visualisation changes
80 static Events_Loop* aLoop = Events_Loop::loop();
81 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
82 aLoop->flush(aRedispEvent);
85 void ModelAPI_Feature::eraseResultFromList(const std::shared_ptr<ModelAPI_Result>& theResult)
87 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
88 for(; aResIter != myResults.end(); aResIter++) {
89 ResultPtr aRes = *aResIter;
90 if (aRes == theResult) {
91 std::string aGroup = aRes->groupName();
92 aRes->setDisabled(aRes, true); // for complex results to disable all subs
93 aRes->data()->erase();
94 myResults.erase(aResIter);
96 static Events_Loop* aLoop = Events_Loop::loop();
97 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
98 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
99 aECreator->sendDeleted(document(), aGroup);
100 aECreator->sendUpdated(aRes, EVENT_DISP);
106 void ModelAPI_Feature::removeResults(const int theSinceIndex, const bool theFlush)
108 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
109 for(int anIndex = 0; anIndex < theSinceIndex && aResIter != myResults.end(); anIndex++)
113 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aNextIter = aResIter;
114 while( aNextIter != myResults.end()) {
115 aGroup = (*aNextIter)->groupName();
116 // remove previously erased results: to enable later if needed only actual (of history change)
117 //if (theSinceIndex == 0 && (*aNextIter)->isDisabled()) {
118 // aNextIter = myResults.erase(aNextIter);
120 (*aNextIter)->setDisabled(*aNextIter, true); // just disable results
124 if (!aGroup.empty() && theFlush) {
125 // flush visualisation changes
126 static Events_Loop* aLoop = Events_Loop::loop();
127 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
128 aLoop->flush(aRedispEvent);
129 static Events_ID aDelEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
130 aLoop->flush(aDelEvent);
134 void ModelAPI_Feature::eraseResults()
139 const std::string& ModelAPI_Feature::documentToAdd()
141 // empty to use the current document
142 static const std::string anEmpty;
146 void ModelAPI_Feature::erase()
148 // if this is the current feature, make the upper feature as current before removing
149 if (document().get() && document()->currentFeature(false).get() == this) {
150 document()->setCurrentFeatureUp();
153 static Events_Loop* aLoop = Events_Loop::loop();
154 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
155 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
157 while (!myResults.empty()) { // remove one by one with messages
158 std::shared_ptr<ModelAPI_Result> aRes = *(myResults.begin());
159 aRes->setDisabled(aRes, true); // to avoid activation of the Part result
160 if (!myResults.empty()) // disabling result may erase the list (on undo of Part, issue 665)
161 myResults.erase(myResults.begin());
163 ModelAPI_Object::erase();
166 ModelAPI_Feature::~ModelAPI_Feature()
171 FeaturePtr ModelAPI_Feature::feature(ObjectPtr theObject)
173 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
175 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
177 DocumentPtr aDoc = aResult->document();
178 return aDoc->feature(aResult);
184 bool ModelAPI_Feature::isMacro() const
189 bool ModelAPI_Feature::setDisabled(const bool theFlag)
191 if (myIsDisabled != theFlag) {
192 myIsDisabled = theFlag;
194 removeResults(0, false); // flush will be in setCurrentFeature
196 // enable all disabled previously results
197 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
198 for(; aResIter != myResults.end(); aResIter++) {
199 (*aResIter)->setDisabled(*aResIter, false);
201 // update selection for the case something was updated higher in the history
202 // while this feature was disabled
203 static Events_Loop* aLoop = Events_Loop::loop();
204 static Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
205 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
206 aECreator->sendUpdated(data()->owner(), kUpdatedSel, false);
213 bool ModelAPI_Feature::isDisabled()
218 bool ModelAPI_Feature::setStable(const bool theFlag)
220 if (myIsStable != theFlag) {
221 myIsStable = theFlag;
222 // send an event about the stability change (editing is started/finished)
223 static Events_Loop* aLoop = Events_Loop::loop();
224 static Events_ID EVENT_STAB = aLoop->eventByName(EVENT_STABILITY_CHANGED);
225 std::shared_ptr<Events_Message> aMessage(new Events_Message(EVENT_STAB, this));
226 aLoop->send(aMessage, false);
232 bool ModelAPI_Feature::isStable()
237 bool ModelAPI_Feature::customAction(const std::string& theActionId)
242 bool ModelAPI_Feature::isPreviewNeeded() const
247 void ModelAPI_Feature::init()
249 myIsDisabled = false;