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 <ModelAPI_Session.h>
21 #include <Events_Loop.h>
22 #include <Events_LongOp.h>
23 #include <Events_Error.h>
24 #include <Config_PropManager.h>
28 Model_Update MY_UPDATER_INSTANCE; /// the only one instance initialized on load of the library
30 Model_Update::Model_Update()
32 Events_Loop* aLoop = Events_Loop::loop();
33 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
34 aLoop->registerListener(this, kChangedEvent);
35 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
36 aLoop->registerListener(this, kRebuildEvent);
37 static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
38 aLoop->registerListener(this, kCreatedEvent);
39 static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
40 aLoop->registerListener(this, kUpdatedEvent);
41 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
42 aLoop->registerListener(this, kOpFinishEvent);
43 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
44 aLoop->registerListener(this, kOpAbortEvent);
46 Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild automatically",
47 Config_Prop::Bool, "false");
48 isAutomatic = Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
51 void Model_Update::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
53 static Events_Loop* aLoop = Events_Loop::loop();
54 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
55 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
56 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
57 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
58 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
59 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
60 bool isAutomaticChanged = false;
61 if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
63 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
64 } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
65 if (isAutomatic == false) {
66 isAutomaticChanged = true;
69 } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent) {
70 boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
71 boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
72 if (theMessage->eventID() == kCreatedEvent) {
73 myJustCreatedOrUpdated.clear();
75 const std::set<ObjectPtr>& anObjs = aMsg->objects();
76 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
77 for(; anObjIter != anObjs.cend(); anObjIter++)
78 myJustCreatedOrUpdated.insert(*anObjIter);
79 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
80 myJustCreatedOrUpdated.clear();
85 return; // nothing to do: it is executed now
87 //Events_LongOp::start(this);
89 list<boost::shared_ptr<ModelAPI_Document> > aDocs;
90 boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
91 boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
92 if (aMsg) myInitial = aMsg->objects();
95 // on change flag all documents must be updated
97 aDocs = ModelAPI_Session::get()->allOpenedDocuments();
100 // collect all documents involved into the update process
101 set<boost::shared_ptr<ModelAPI_Object> >::iterator aFIter = myInitial.begin();
102 for (; aFIter != myInitial.end(); aFIter++) {
103 aDocs.push_back((*aFIter)->document());
105 // iterate all features of features-documents to update them (including hidden)
106 list<boost::shared_ptr<ModelAPI_Document> >::iterator aDIter = aDocs.begin();
107 for (; aDIter != aDocs.end(); aDIter++) {
108 int aNbFeatures = (*aDIter)->size(ModelAPI_Feature::group(), true);
109 for (int aFIndex = 0; aFIndex < aNbFeatures; aFIndex++) {
110 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(
111 (*aDIter)->object(ModelAPI_Feature::group(), aFIndex, true));
113 updateFeature(aFeature);
117 if (theMessage->eventID() == kUpdatedEvent) {
118 // flush updates without execution now (updates are caused by this process)
119 aLoop->flush(kUpdatedEvent);
121 // flush to update display
122 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
123 aLoop->flush(EVENT_DISP);
124 //Events_LongOp::end(this);
125 if (isAutomaticChanged) isAutomatic = false;
129 bool Model_Update::updateFeature(FeaturePtr theFeature)
131 // check it is already processed
132 if (myUpdated.find(theFeature) != myUpdated.end())
133 return myUpdated[theFeature];
134 // check all features this feature depended on (recursive call of updateFeature)
135 bool aMustbeUpdated = myInitial.find(theFeature) != myInitial.end();
136 if (theFeature) { // only real feature contains references to other objects
137 if (theFeature->data()->mustBeUpdated()) aMustbeUpdated = true;
139 // composite feature must be executed after sub-features execution
140 CompositeFeaturePtr aComposite =
141 boost::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
143 int aSubsNum = aComposite->numberOfSubs();
144 for(int a = 0; a < aSubsNum; a++) {
145 if (updateFeature(aComposite->subFeature(a)))
146 aMustbeUpdated = true;
149 // check all references: if referenced objects are updated, this object also must be updated
150 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
151 boost::shared_ptr<Model_Data> aData =
152 boost::dynamic_pointer_cast<Model_Data>(theFeature->data());
153 aData->referencesToObjects(aRefs);
154 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefs.begin();
155 for(; aRef != aRefs.end(); aRef++) {
156 std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
157 for(; aRefObj != aRef->second.end(); aRefObj++) {
158 if (updateObject(*aRefObj)) {
159 aMustbeUpdated = true;
164 // execute feature if it must be updated
165 if (aMustbeUpdated) {
167 if (boost::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures() ||
168 !theFeature->isPersistentResult()) {
169 ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
170 if (aFactory->validate(theFeature)) {
171 if (isAutomatic || (myJustCreatedOrUpdated.find(theFeature) != myJustCreatedOrUpdated.end()) ||
172 !theFeature->isPersistentResult() /* execute quick, not persistent results */)
174 // before execution update the selection attributes if any
175 list<AttributePtr> aRefs =
176 theFeature->data()->attributes(ModelAPI_AttributeSelection::type());
177 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
178 for (; aRefsIter != aRefs.end(); aRefsIter++) {
179 boost::shared_ptr<ModelAPI_AttributeSelection> aSel =
180 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
181 aSel->update(); // this must be done on execution since it may be long operation
183 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::type());
184 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
185 boost::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
186 boost::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
187 for(int a = aSel->size() - 1; a >= 0; a--) {
188 aSel->value(a)->update();
191 // execute in try-catch to avoid internal problems of the feature
193 theFeature->execute();
196 "Feature " + theFeature->getKind() + " has failed during the execution");
197 theFeature->eraseResults();
199 theFeature->data()->mustBeUpdated(false);
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(false);
207 theFeature->data()->mustBeUpdated(true);
208 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
209 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
210 for (; aRIter != aResults.cend(); aRIter++) {
211 boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
212 aRes->data()->mustBeUpdated(true);
214 aMustbeUpdated = false;
217 theFeature->eraseResults();
220 // redisplay all results
221 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
222 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
223 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
224 for (; aRIter != aResults.cend(); aRIter++) {
225 boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
226 myUpdated[aRes] = true;
227 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
229 // to redisplay "presentable" feature (for ex. distance constraint)
230 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
231 } else { // returns also true is results were updated: for sketch that refers to sub-features but results of sub-features were changed
232 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
233 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
234 for (; aRIter != aResults.cend(); aRIter++) {
235 if (myInitial.find(*aRIter) != myInitial.end()) {
236 aMustbeUpdated = true;
242 myUpdated[theFeature] = aMustbeUpdated;
243 return aMustbeUpdated;
246 bool Model_Update::updateObject(boost::shared_ptr<ModelAPI_Object> theObject)
248 if (myUpdated.find(theObject) != myUpdated.end())
249 return myUpdated[theObject]; // already processed
250 return myInitial.find(theObject) != myInitial.end();
252 /* remove algorithm for update of all features by dependencies tree
255 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
256 if (aFeature) { // for feature just call update Feature
257 return updateFeature(aFeature);
259 // check general object, possible just a result
260 if (myUpdated.find(theObject) != myUpdated.end())
261 return myUpdated[theObject]; // already processed
262 // check the feature of this object must be executed
263 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
265 FeaturePtr aResFeature = aResult->document()->feature(aResult);
267 return updateFeature(aResFeature);
270 if (myInitial.find(theObject) != myInitial.end())
272 return false; // nothing is known