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);
45 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
46 aLoop->registerListener(this, kOpStartEvent);
48 Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild automatically",
49 Config_Prop::Bool, "false");
50 isAutomatic = Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
53 void Model_Update::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
55 static Events_Loop* aLoop = Events_Loop::loop();
56 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
57 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
58 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
59 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
60 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
61 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
62 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
63 bool isAutomaticChanged = false;
64 if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
66 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
67 } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
68 if (isAutomatic == false) {
69 isAutomaticChanged = true;
72 } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent) {
73 boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
74 boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
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);
80 } else if (theMessage->eventID() == kOpStartEvent) {
81 myJustCreatedOrUpdated.clear();
82 return; // we don't need the update only on operation start (caused problems in PartSet_Listener::processEvent)
83 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
84 if (isAutomatic == false) { // Apply button now works as "Rebuild"
85 isAutomaticChanged = true;
91 return; // nothing to do: it is executed now
93 //Events_LongOp::start(this);
95 std::list<boost::shared_ptr<ModelAPI_Document> > aDocs;
96 boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
97 boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
98 if (aMsg) myInitial = aMsg->objects();
101 // on change flag all documents must be updated
103 aDocs = ModelAPI_Session::get()->allOpenedDocuments();
106 // collect all documents involved into the update process
107 set<boost::shared_ptr<ModelAPI_Object> >::iterator aFIter = myInitial.begin();
108 for (; aFIter != myInitial.end(); aFIter++) {
109 aDocs.push_back((*aFIter)->document());
111 // iterate all features of features-documents to update them (including hidden)
112 std::set<boost::shared_ptr<ModelAPI_Document> > alreadyUsed;
113 list<boost::shared_ptr<ModelAPI_Document> >::iterator aDIter = aDocs.begin();
114 for (; aDIter != aDocs.end(); aDIter++) {
115 if (alreadyUsed.find(*aDIter) != alreadyUsed.end())
117 alreadyUsed.insert(*aDIter);
118 int aNbFeatures = (*aDIter)->size(ModelAPI_Feature::group(), true);
119 for (int aFIndex = 0; aFIndex < aNbFeatures; aFIndex++) {
120 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(
121 (*aDIter)->object(ModelAPI_Feature::group(), aFIndex, true));
123 updateFeature(aFeature);
127 // flush to update display
128 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
129 aLoop->flush(EVENT_DISP);
130 //Events_LongOp::end(this);
131 if (isAutomaticChanged) isAutomatic = false;
133 if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
134 myJustCreatedOrUpdated.clear();
140 void Model_Update::redisplayWithResults(FeaturePtr theFeature) {
141 // maske updated and redisplay all results
142 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
143 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
144 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
145 for (; aRIter != aResults.cend(); aRIter++) {
146 boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
147 aRes->data()->mustBeUpdated(false);
148 myUpdated[aRes] = true;
149 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
151 // to redisplay "presentable" feature (for ex. distance constraint)
152 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
153 theFeature->data()->mustBeUpdated(false);
156 bool Model_Update::updateFeature(FeaturePtr theFeature)
158 // check it is already processed
159 if (myUpdated.find(theFeature) != myUpdated.end())
160 return myUpdated[theFeature];
161 // check all features this feature depended on (recursive call of updateFeature)
162 bool aMustbeUpdated = myInitial.find(theFeature) != myInitial.end();
163 if (theFeature) { // only real feature contains references to other objects
164 if (theFeature->data()->mustBeUpdated()) aMustbeUpdated = true;
166 // composite feature must be executed after sub-features execution
167 CompositeFeaturePtr aComposite =
168 boost::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
170 int aSubsNum = aComposite->numberOfSubs();
171 for(int a = 0; a < aSubsNum; a++) {
172 if (updateFeature(aComposite->subFeature(a)))
173 aMustbeUpdated = true;
176 // check all references: if referenced objects are updated, this object also must be updated
177 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
178 boost::shared_ptr<Model_Data> aData =
179 boost::dynamic_pointer_cast<Model_Data>(theFeature->data());
180 aData->referencesToObjects(aRefs);
181 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefs.begin();
182 for(; aRef != aRefs.end(); aRef++) {
183 std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
184 for(; aRefObj != aRef->second.end(); aRefObj++) {
185 if (updateObject(*aRefObj)) {
186 aMustbeUpdated = true;
191 // execute feature if it must be updated
192 if (aMustbeUpdated) {
193 if (boost::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures() ||
194 !theFeature->isPersistentResult()) {
195 ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
196 if (aFactory->validate(theFeature)) {
197 if (isAutomatic || (myJustCreatedOrUpdated.find(theFeature) != myJustCreatedOrUpdated.end()) ||
198 !theFeature->isPersistentResult() /* execute quick, not persistent results */)
200 // before execution update the selection attributes if any
201 list<AttributePtr> aRefs =
202 theFeature->data()->attributes(ModelAPI_AttributeSelection::type());
203 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
204 for (; aRefsIter != aRefs.end(); aRefsIter++) {
205 boost::shared_ptr<ModelAPI_AttributeSelection> aSel =
206 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
207 aSel->update(); // this must be done on execution since it may be long operation
209 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::type());
210 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
211 boost::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
212 boost::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
213 for(int a = aSel->size() - 1; a >= 0; a--) {
214 aSel->value(a)->update();
217 // execute in try-catch to avoid internal problems of the feature
219 theFeature->execute();
222 "Feature " + theFeature->getKind() + " has failed during the execution");
223 theFeature->eraseResults();
225 redisplayWithResults(theFeature);
226 } else { // must be updatet, but not updated yet
227 theFeature->data()->mustBeUpdated(true);
228 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
229 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
230 for (; aRIter != aResults.cend(); aRIter++) {
231 boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
232 aRes->data()->mustBeUpdated(true);
236 theFeature->eraseResults();
237 redisplayWithResults(theFeature); // result also must be updated
239 } else { // for automatically updated features (on abort, etc) it is necessary to redisplay anyway
240 redisplayWithResults(theFeature);
242 } else { // returns also true is results were updated: for sketch that refers to sub-features but results of sub-features were changed
243 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
244 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
245 for (; aRIter != aResults.cend(); aRIter++) {
246 if (myInitial.find(*aRIter) != myInitial.end()) {
247 aMustbeUpdated = true;
253 myUpdated[theFeature] = aMustbeUpdated;
254 return aMustbeUpdated;
257 bool Model_Update::updateObject(boost::shared_ptr<ModelAPI_Object> theObject)
259 if (myUpdated.find(theObject) != myUpdated.end())
260 return myUpdated[theObject]; // already processed
261 return myInitial.find(theObject) != myInitial.end();
263 /* remove algorithm for update of all features by dependencies tree
266 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
267 if (aFeature) { // for feature just call update Feature
268 return updateFeature(aFeature);
270 // check general object, possible just a result
271 if (myUpdated.find(theObject) != myUpdated.end())
272 return myUpdated[theObject]; // already processed
273 // check the feature of this object must be executed
274 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
276 FeaturePtr aResFeature = aResult->document()->feature(aResult);
278 return updateFeature(aResFeature);
281 if (myInitial.find(theObject) != myInitial.end())
283 return false; // nothing is known