1 // File: Model_Update.cxx
2 // Created: 25 Jun 2014
3 // Author: Mikhail PONIKAROV
5 #include <Model_Update.h>
6 #include <Model_Document.h>
7 #include <Model_Data.h>
8 #include <ModelAPI_Feature.h>
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_Events.h>
12 #include <ModelAPI_AttributeReference.h>
13 #include <ModelAPI_AttributeRefList.h>
14 #include <ModelAPI_AttributeRefAttr.h>
15 #include <ModelAPI_AttributeSelection.h>
16 #include <ModelAPI_AttributeSelectionList.h>
17 #include <ModelAPI_Result.h>
18 #include <ModelAPI_Validator.h>
19 #include <ModelAPI_CompositeFeature.h>
20 #include <Events_Loop.h>
21 #include <Events_LongOp.h>
22 #include <Events_Error.h>
23 #include <Config_PropManager.h>
27 Model_Update MY_UPDATER_INSTANCE; /// the only one instance initialized on load of the library
29 Model_Update::Model_Update()
31 Events_Loop* aLoop = Events_Loop::loop();
32 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
33 aLoop->registerListener(this, kChangedEvent);
34 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
35 aLoop->registerListener(this, kRebuildEvent);
36 static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
37 aLoop->registerListener(this, kCreatedEvent);
38 static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
39 aLoop->registerListener(this, kUpdatedEvent);
40 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
41 aLoop->registerListener(this, kOpFinishEvent);
42 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
43 aLoop->registerListener(this, kOpAbortEvent);
45 Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild automatically",
46 Config_Prop::Bool, "false");
47 isAutomatic = Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
50 void Model_Update::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
52 static Events_Loop* aLoop = Events_Loop::loop();
53 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
54 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
55 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
56 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
57 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
58 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
59 bool isAutomaticChanged = false;
60 if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
62 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
63 } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
64 if (isAutomatic == false) {
65 isAutomaticChanged = true;
68 } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent) {
69 boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
70 boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
71 const std::set<ObjectPtr>& anObjs = aMsg->objects();
72 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
73 for(; anObjIter != anObjs.cend(); anObjIter++)
74 myJustCreatedOrUpdated.insert(*anObjIter);
75 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
76 myJustCreatedOrUpdated.clear();
81 return; // nothing to do: it is executed now
83 //Events_LongOp::start(this);
85 list<boost::shared_ptr<ModelAPI_Document> > aDocs;
86 boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
87 boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
88 if (aMsg) myInitial = aMsg->objects();
91 // on change flag all documents must be updated
93 aDocs = ModelAPI_Session::get()->allOpenedDocuments();
96 // collect all documents involved into the update process
97 set<boost::shared_ptr<ModelAPI_Object> >::iterator aFIter = myInitial.begin();
98 for (; aFIter != myInitial.end(); aFIter++) {
99 aDocs.push_back((*aFIter)->document());
101 // iterate all features of features-documents to update them (including hidden)
102 list<boost::shared_ptr<ModelAPI_Document> >::iterator aDIter = aDocs.begin();
103 for (; aDIter != aDocs.end(); aDIter++) {
104 int aNbFeatures = (*aDIter)->size(ModelAPI_Feature::group(), true);
105 for (int aFIndex = 0; aFIndex < aNbFeatures; aFIndex++) {
106 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(
107 (*aDIter)->object(ModelAPI_Feature::group(), aFIndex, true));
109 updateFeature(aFeature);
114 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
115 aLoop->flush(EVENT_DISP);
116 //Events_LongOp::end(this);
117 if (isAutomaticChanged) isAutomatic = false;
121 bool Model_Update::updateFeature(FeaturePtr theFeature)
123 // check it is already processed
124 if (myUpdated.find(theFeature) != myUpdated.end())
125 return myUpdated[theFeature];
126 // check all features this feature depended on (recursive call of updateFeature)
127 bool aMustbeUpdated = myInitial.find(theFeature) != myInitial.end();
128 if (theFeature) { // only real feature contains references to other objects
129 if (theFeature->data()->mustBeUpdated()) aMustbeUpdated = true;
131 // composite feature must be executed after sub-features execution
132 CompositeFeaturePtr aComposite =
133 boost::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
135 int aSubsNum = aComposite->numberOfSubs();
136 for(int a = 0; a < aSubsNum; a++) {
137 if (updateFeature(aComposite->subFeature(a)))
138 aMustbeUpdated = true;
141 // check all references: if referenced objects are updated, this object also must be updated
142 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
143 boost::shared_ptr<Model_Data> aData =
144 boost::dynamic_pointer_cast<Model_Data>(theFeature->data());
145 aData->referencesToObjects(aRefs);
146 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefs.begin();
147 for(; aRef != aRefs.end(); aRef++) {
148 std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
149 for(; aRefObj != aRef->second.end(); aRefObj++) {
150 if (updateObject(*aRefObj)) {
151 aMustbeUpdated = true;
156 // execute feature if it must be updated
157 if (aMustbeUpdated) {
159 if (boost::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures() ||
160 !theFeature->isPersistentResult()) {
161 ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
162 if (aFactory->validate(theFeature)) {
163 if (isAutomatic || (myJustCreatedOrUpdated.find(theFeature) != myJustCreatedOrUpdated.end()) ||
164 !theFeature->isPersistentResult() /* execute quick, not persistent results */)
166 // before execution update the selection attributes if any
167 list<AttributePtr> aRefs =
168 theFeature->data()->attributes(ModelAPI_AttributeSelection::type());
169 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
170 for (; aRefsIter != aRefs.end(); aRefsIter++) {
171 boost::shared_ptr<ModelAPI_AttributeSelection> aSel =
172 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
173 aSel->update(); // this must be done on execution since it may be long operation
175 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::type());
176 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
177 boost::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
178 boost::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
179 for(int a = aSel->size() - 1; a >= 0; a--) {
180 aSel->value(a)->update();
183 // execute in try-catch to avoid internal problems of the feature
185 theFeature->execute();
188 "Feature " + theFeature->getKind() + " has failed during the execution");
189 theFeature->eraseResults();
191 theFeature->data()->mustBeUpdated(false);
192 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
193 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
194 for (; aRIter != aResults.cend(); aRIter++) {
195 boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
196 aRes->data()->mustBeUpdated(false);
199 theFeature->data()->mustBeUpdated(true);
200 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
201 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
202 for (; aRIter != aResults.cend(); aRIter++) {
203 boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
204 aRes->data()->mustBeUpdated(true);
206 aMustbeUpdated = false;
209 theFeature->eraseResults();
212 // redisplay all results
213 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
214 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
215 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
216 for (; aRIter != aResults.cend(); aRIter++) {
217 boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
218 myUpdated[aRes] = true;
219 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
221 // to redisplay "presentable" feature (for ex. distance constraint)
222 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
223 } else { // returns also true is results were updated: for sketch that refers to sub-features but results of sub-features were changed
224 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
225 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
226 for (; aRIter != aResults.cend(); aRIter++) {
227 if (myInitial.find(*aRIter) != myInitial.end()) {
228 aMustbeUpdated = true;
234 myUpdated[theFeature] = aMustbeUpdated;
235 return aMustbeUpdated;
238 bool Model_Update::updateObject(boost::shared_ptr<ModelAPI_Object> theObject)
240 if (myUpdated.find(theObject) != myUpdated.end())
241 return myUpdated[theObject]; // already processed
242 return myInitial.find(theObject) != myInitial.end();
244 /* remove algorithm for update of all features by dependencies tree
247 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
248 if (aFeature) { // for feature just call update Feature
249 return updateFeature(aFeature);
251 // check general object, possible just a result
252 if (myUpdated.find(theObject) != myUpdated.end())
253 return myUpdated[theObject]; // already processed
254 // check the feature of this object must be executed
255 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
257 FeaturePtr aResFeature = aResult->document()->feature(aResult);
259 return updateFeature(aResFeature);
262 if (myInitial.find(theObject) != myInitial.end())
264 return false; // nothing is known