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>
15 const std::list<std::shared_ptr<ModelAPI_Result> >& ModelAPI_Feature::results()
20 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::firstResult()
22 return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.begin());
25 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::lastResult()
27 return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.rbegin());
30 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult)
32 static Events_Loop* aLoop = Events_Loop::loop();
33 static Events_ID EVENT_UPD = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
34 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
36 if (firstResult() == theResult) {
38 } else if (!myResults.empty()) { // all except first become disabled
39 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
40 *aResIter = theResult;
41 aECreator->sendUpdated(theResult, EVENT_UPD);
42 for(aResIter++; aResIter != myResults.end(); aResIter++) {
43 (*aResIter)->setDisabled((*aResIter), true);
46 myResults.push_back(theResult);
48 // in any case result becomes enabled
49 theResult->setDisabled(theResult, false);
50 // flush vidualisation changes
51 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
52 aLoop->flush(aRedispEvent);
55 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult,
58 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
59 for (int anIndex = 0; anIndex < theIndex; anIndex++) {
62 if (aResIter == myResults.end()) { // append
63 myResults.push_back(theResult);
65 *aResIter = theResult;
67 theResult->setDisabled(theResult, false);
68 // flush visualisation changes
69 static Events_Loop* aLoop = Events_Loop::loop();
70 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
71 aLoop->flush(aRedispEvent);
74 void ModelAPI_Feature::removeResult(const std::shared_ptr<ModelAPI_Result>& theResult)
76 theResult->setDisabled(theResult, true);
77 // flush visualisation changes
78 static Events_Loop* aLoop = Events_Loop::loop();
79 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
80 aLoop->flush(aRedispEvent);
83 void ModelAPI_Feature::eraseResultFromList(const std::shared_ptr<ModelAPI_Result>& theResult)
85 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
86 for(; aResIter != myResults.end(); aResIter++) {
87 ResultPtr aRes = *aResIter;
88 if (aRes == theResult) {
89 std::string aGroup = aRes->groupName();
90 aRes->data()->erase();
91 myResults.erase(aResIter);
93 static Events_Loop* aLoop = Events_Loop::loop();
94 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
95 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
96 ModelAPI_EventCreator::get()->sendDeleted(document(), aGroup);
97 aECreator->sendUpdated(aRes, EVENT_DISP);
103 void ModelAPI_Feature::removeResults(const int theSinceIndex, const bool theFlush)
105 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
106 for(int anIndex = 0; anIndex < theSinceIndex && aResIter != myResults.end(); anIndex++)
110 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aNextIter = aResIter;
111 while( aNextIter != myResults.end()) {
112 aGroup = (*aNextIter)->groupName();
113 // remove previously erased results: to enable later if needed only actual (of history change)
114 if (theSinceIndex == 0 && (*aNextIter)->isDisabled()) {
115 aNextIter = myResults.erase(aNextIter);
117 (*aNextIter)->setDisabled(*aNextIter, true); // just disable results
121 if (!aGroup.empty() && theFlush) {
122 // flush visualisation changes
123 static Events_Loop* aLoop = Events_Loop::loop();
124 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
125 aLoop->flush(aRedispEvent);
126 static Events_ID aDelEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
127 aLoop->flush(aDelEvent);
131 void ModelAPI_Feature::eraseResults()
136 const std::string& ModelAPI_Feature::documentToAdd()
138 // empty to use the current document
139 static const std::string anEmpty;
143 void ModelAPI_Feature::erase()
145 // if this is the current feature, make the upper feature as current before removing
146 if (document().get() && document()->currentFeature(false).get() == this) {
147 document()->setCurrentFeatureUp();
150 static Events_Loop* aLoop = Events_Loop::loop();
151 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
152 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
154 while (!myResults.empty()) { // remove one by one with messages
155 std::shared_ptr<ModelAPI_Result> aRes = *(myResults.begin());
156 aRes->setDisabled(aRes, true); // to avoid activation of the Part result
157 myResults.erase(myResults.begin());
159 ModelAPI_Object::erase();
162 ModelAPI_Feature::~ModelAPI_Feature()
167 FeaturePtr ModelAPI_Feature::feature(ObjectPtr theObject)
169 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
171 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
173 DocumentPtr aDoc = aResult->document();
174 return aDoc->feature(aResult);
180 bool ModelAPI_Feature::isMacro() const
185 bool ModelAPI_Feature::setDisabled(const bool theFlag)
187 if (myIsDisabled != theFlag) {
188 myIsDisabled = theFlag;
190 removeResults(0, false); // flush will be in setCurrentFeature
192 // enable all disabled previously results
193 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
194 for(; aResIter != myResults.end(); aResIter++) {
195 (*aResIter)->setDisabled(*aResIter, false);
203 bool ModelAPI_Feature::isDisabled() const
208 bool ModelAPI_Feature::isPreviewNeeded() const