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 <ModelAPI_Feature.h>
8 #include <ModelAPI_Data.h>
9 #include <ModelAPI_Document.h>
10 #include <ModelAPI_Events.h>
11 #include <ModelAPI_AttributeReference.h>
12 #include <ModelAPI_AttributeRefList.h>
13 #include <ModelAPI_AttributeRefAttr.h>
14 #include <ModelAPI_Result.h>
15 #include <ModelAPI_Validator.h>
16 #include <Events_Loop.h>
17 #include <Events_LongOp.h>
18 #include <Events_Error.h>
19 #include <Config_PropManager.h>
23 Model_Update MY_UPDATER_INSTANCE; /// the only one instance initialized on load of the library
25 Model_Update::Model_Update()
27 Events_Loop* aLoop = Events_Loop::loop();
28 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
29 aLoop->registerListener(this, kChangedEvent);
30 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
31 aLoop->registerListener(this, kRebuildEvent);
32 static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
33 aLoop->registerListener(this, kCreatedEvent);
34 static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
35 aLoop->registerListener(this, kUpdatedEvent);
36 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
37 aLoop->registerListener(this, kOpFinishEvent);
38 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
39 aLoop->registerListener(this, kOpAbortEvent);
41 Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild automatically",
42 Config_Prop::Bool, "false");
43 isAutomatic = Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
46 void Model_Update::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
48 static Events_Loop* aLoop = Events_Loop::loop();
49 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
50 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
51 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
52 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
53 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
54 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
55 bool isAutomaticChanged = false;
56 if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
58 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
59 } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
60 if (isAutomatic == false) {
61 isAutomaticChanged = true;
64 } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent) {
65 boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
66 boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
67 const std::set<ObjectPtr>& anObjs = aMsg->objects();
68 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
69 for(; anObjIter != anObjs.cend(); anObjIter++)
70 myJustCreatedOrUpdated.insert(*anObjIter);
71 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
72 myJustCreatedOrUpdated.clear();
77 return; // nothing to do: it is executed now
79 //Events_LongOp::start(this);
81 list<boost::shared_ptr<ModelAPI_Document> > aDocs;
82 boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
83 boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
84 if (aMsg) myInitial = aMsg->objects();
87 // on change flag all documents must be updated
89 aDocs = ModelAPI_Session::get()->allOpenedDocuments();
92 // collect all documents involved into the update
93 set<boost::shared_ptr<ModelAPI_Object> >::iterator aFIter = myInitial.begin();
94 for (; aFIter != myInitial.end(); aFIter++) {
95 aDocs.push_back((*aFIter)->document());
97 // iterate all features of features-documents to update them (including hidden)
98 list<boost::shared_ptr<ModelAPI_Document> >::iterator aDIter = aDocs.begin();
99 for (; aDIter != aDocs.end(); aDIter++) {
100 int aNbFeatures = (*aDIter)->size(ModelAPI_Feature::group(), true);
101 for (int aFIndex = 0; aFIndex < aNbFeatures; aFIndex++) {
102 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(
103 (*aDIter)->object(ModelAPI_Feature::group(), aFIndex, true));
105 updateFeature(aFeature);
110 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
111 aLoop->flush(EVENT_DISP);
112 //Events_LongOp::end(this);
113 if (isAutomaticChanged) isAutomatic = false;
117 bool Model_Update::updateFeature(FeaturePtr theFeature)
119 // check it is already processed
120 if (myUpdated.find(theFeature) != myUpdated.end())
121 return myUpdated[theFeature];
122 // check all features this feature depended on (recursive call of updateFeature)
123 bool aMustbeUpdated = myInitial.find(theFeature) != myInitial.end();
124 if (theFeature) { // only real feature contains references to other objects
125 if (theFeature->data()->mustBeUpdated()) aMustbeUpdated = true;
127 list<boost::shared_ptr<ModelAPI_Attribute> > aRefs = theFeature->data()->attributes(
128 ModelAPI_AttributeReference::type());
129 list<boost::shared_ptr<ModelAPI_Attribute> >::iterator aRefsIter = aRefs.begin();
130 for (; aRefsIter != aRefs.end(); aRefsIter++) {
131 boost::shared_ptr<ModelAPI_Object> aSub = boost::dynamic_pointer_cast<
132 ModelAPI_AttributeReference>(*aRefsIter)->value();
133 if (updateObject(aSub)) {
134 aMustbeUpdated = true;
137 // reference to attribute or object
138 list<boost::shared_ptr<ModelAPI_Attribute> > aRefAttrs = theFeature->data()->attributes(
139 ModelAPI_AttributeRefAttr::type());
140 for (aRefsIter = aRefAttrs.begin(); aRefsIter != aRefAttrs.end(); aRefsIter++) {
141 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
142 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefsIter);
144 if (aRef->isObject()) {
145 boost::shared_ptr<ModelAPI_Object> aSub = aRef->object();
146 if (updateObject(aSub)) {
147 aMustbeUpdated = true;
149 } else if (aRef->attr()) { // reference to the attribute
150 boost::shared_ptr<ModelAPI_Object> aSub = aRef->attr()->owner();
151 if (updateObject(aSub)) {
152 aMustbeUpdated = true;
156 // lists of references
157 aRefs = theFeature->data()->attributes(ModelAPI_AttributeRefList::type());
158 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
159 list<ObjectPtr> aListRef = boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*aRefsIter)
161 list<ObjectPtr>::iterator aListIter = aListRef.begin();
162 for (; aListIter != aListRef.end(); aListIter++) {
163 boost::shared_ptr<ModelAPI_Object> aSub = *aListIter;
164 if (updateObject(aSub)) {
165 aMustbeUpdated = true;
169 // execute feature if it must be updated
170 if (aMustbeUpdated) {
172 if (boost::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures() ||
173 !theFeature->isPersistentResult()) {
174 ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
175 if (aFactory->validate(theFeature)) {
176 if (isAutomatic || (myJustCreatedOrUpdated.find(theFeature) != myJustCreatedOrUpdated.end()) ||
177 !theFeature->isPersistentResult() /* execute quick, not persistent results */) {
179 theFeature->execute();
182 "Feature " + theFeature->getKind() + " has failed during the execution");
183 theFeature->eraseResults();
185 theFeature->data()->mustBeUpdated(false);
186 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
187 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
188 for (; aRIter != aResults.cend(); aRIter++) {
189 boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
190 aRes->data()->mustBeUpdated(false);
193 theFeature->data()->mustBeUpdated(true);
194 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
195 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
196 for (; aRIter != aResults.cend(); aRIter++) {
197 boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
198 aRes->data()->mustBeUpdated(true);
200 aMustbeUpdated = false;
203 theFeature->eraseResults();
206 // redisplay all results
207 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
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 myUpdated[aRes] = true;
213 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
215 // to redisplay "presentable" feature (for ex. distance constraint)
216 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
217 } else { // returns also true is results were updated: for sketch that refers to sub-features but results of sub-features were changed
218 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
219 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
220 for (; aRIter != aResults.cend(); aRIter++) {
221 if (myInitial.find(*aRIter) != myInitial.end()) {
222 aMustbeUpdated = true;
228 myUpdated[theFeature] = aMustbeUpdated;
229 return aMustbeUpdated;
232 bool Model_Update::updateObject(boost::shared_ptr<ModelAPI_Object> theObject)
236 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
237 if (aFeature) { // for feature just call update Feature
238 return updateFeature(aFeature);
240 // check general object, possible just a result
241 if (myUpdated.find(theObject) != myUpdated.end())
242 return myUpdated[theObject]; // already processed
243 // check the feature of this object must be executed
244 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
246 FeaturePtr aResFeature = aResult->document()->feature(aResult);
248 return updateFeature(aResFeature);
251 if (myInitial.find(theObject) != myInitial.end())
253 return false; // nothing is known