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 kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
42 aLoop->registerListener(this, kMovedEvent);
43 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
44 aLoop->registerListener(this, kOpFinishEvent);
45 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
46 aLoop->registerListener(this, kOpAbortEvent);
47 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
48 aLoop->registerListener(this, kOpStartEvent);
50 Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild immediately",
51 Config_Prop::Bool, "false");
52 isAutomatic = Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
55 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
57 static Events_Loop* aLoop = Events_Loop::loop();
58 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
59 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
60 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
61 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
62 static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
63 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
64 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
65 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
66 bool isAutomaticChanged = false;
67 if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
69 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
70 } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
71 if (isAutomatic == false) {
72 isAutomaticChanged = true;
75 } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent ||
76 theMessage->eventID() == kMovedEvent) {
77 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
78 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
79 const std::set<ObjectPtr>& anObjs = aMsg->objects();
80 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
81 for(; anObjIter != anObjs.cend(); anObjIter++) {
82 myJustCreatedOrUpdated.insert(*anObjIter);
83 (*anObjIter)->data()->mustBeUpdated(true); // object must be updated because it was changed
85 if (theMessage->eventID() == kMovedEvent)
86 return; // this event is for solver update, not here
87 } else if (theMessage->eventID() == kOpStartEvent) {
88 myJustCreatedOrUpdated.clear();
89 return; // we don't need the update only on operation start (caused problems in PartSet_Listener::processEvent)
90 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
91 if (isAutomatic == false) { // Apply button now works as "Rebuild"
92 isAutomaticChanged = true;
95 // the hardcode (DBC asked): hide the sketch referenced by extrusion on apply
96 if (theMessage->eventID() == kOpFinishEvent) {
97 std::set<std::shared_ptr<ModelAPI_Object> >::iterator aFIter;
98 for(aFIter = myJustCreatedOrUpdated.begin(); aFIter != myJustCreatedOrUpdated.end(); aFIter++)
100 FeaturePtr aF = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
101 if (aF && aF->getKind() == "Extrusion") {
102 if (aF->selection("extrusion_face")) {
103 ResultPtr aSketchRes = aF->selection("extrusion_face")->context();
105 static Events_ID HIDE_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TOHIDE);
106 ModelAPI_EventCreator::get()->sendUpdated(aSketchRes, HIDE_DISP);
115 return; // nothing to do: it is executed now
117 //Events_LongOp::start(this);
119 std::list<std::shared_ptr<ModelAPI_Document> > aDocs;
120 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
121 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
122 if (aMsg) myInitial = aMsg->objects();
125 // on change flag all documents must be updated
127 aDocs = ModelAPI_Session::get()->allOpenedDocuments();
130 // collect all documents involved into the update process
131 set<std::shared_ptr<ModelAPI_Object> >::iterator aFIter = myInitial.begin();
132 for (; aFIter != myInitial.end(); aFIter++) {
133 aDocs.push_back((*aFIter)->document());
135 // iterate all features of features-documents to update them (including hidden)
136 std::set<std::shared_ptr<ModelAPI_Document> > alreadyUsed;
137 list<std::shared_ptr<ModelAPI_Document> >::iterator aDIter = aDocs.begin();
138 for (; aDIter != aDocs.end(); aDIter++) {
139 if (alreadyUsed.find(*aDIter) != alreadyUsed.end())
141 alreadyUsed.insert(*aDIter);
142 int aNbFeatures = (*aDIter)->size(ModelAPI_Feature::group(), true);
143 for (int aFIndex = 0; aFIndex < aNbFeatures; aFIndex++) {
144 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
145 (*aDIter)->object(ModelAPI_Feature::group(), aFIndex, true));
147 updateFeature(aFeature);
151 // flush to update display
152 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
153 aLoop->flush(EVENT_DISP);
154 //Events_LongOp::end(this);
155 if (isAutomaticChanged) isAutomatic = false;
157 if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
158 myJustCreatedOrUpdated.clear();
164 void Model_Update::redisplayWithResults(FeaturePtr theFeature) {
165 // maske updated and redisplay all results
166 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
167 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
168 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
169 for (; aRIter != aResults.cend(); aRIter++) {
170 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
171 aRes->data()->mustBeUpdated(false);
172 myUpdated[aRes] = true;
173 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
175 // to redisplay "presentable" feature (for ex. distance constraint)
176 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
177 theFeature->data()->mustBeUpdated(false);
180 bool Model_Update::updateFeature(FeaturePtr theFeature)
182 // check it is already processed
183 if (myUpdated.find(theFeature) != myUpdated.end())
184 return myUpdated[theFeature];
185 // check all features this feature depended on (recursive call of updateFeature)
186 ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
187 bool aMustbeUpdated = myInitial.find(theFeature) != myInitial.end();
188 if (theFeature) { // only real feature contains references to other objects
189 if (theFeature->data()->mustBeUpdated()) aMustbeUpdated = true;
191 // composite feature must be executed after sub-features execution
192 CompositeFeaturePtr aComposite =
193 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
195 int aSubsNum = aComposite->numberOfSubs();
196 for(int a = 0; a < aSubsNum; a++) {
197 if (updateFeature(aComposite->subFeature(a)))
198 aMustbeUpdated = true;
201 // check all references: if referenced objects are updated, this object also must be updated
202 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
203 std::shared_ptr<Model_Data> aData =
204 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
205 aData->referencesToObjects(aRefs);
206 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefs.begin();
207 for(; aRef != aRefs.end(); aRef++) {
208 std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
209 for(; aRefObj != aRef->second.end(); aRefObj++) {
210 if (updateObject(*aRefObj)) {
211 aMustbeUpdated = true;
216 //std::cout<<"Update feature "<<theFeature->getKind()<<" must be updated = "<<aMustbeUpdated<<std::endl;
217 // execute feature if it must be updated
218 if (aMustbeUpdated) {
219 if (std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures() ||
220 !theFeature->isPersistentResult()) {
221 if (aFactory->validate(theFeature)) {
222 if (isAutomatic || (myJustCreatedOrUpdated.find(theFeature) != myJustCreatedOrUpdated.end()) ||
223 !theFeature->isPersistentResult() /* execute quick, not persistent results */)
225 //std::cout<<"Execute feature "<<theFeature->getKind()<<std::endl;
226 // before execution update the selection attributes if any
227 list<AttributePtr> aRefs =
228 theFeature->data()->attributes(ModelAPI_AttributeSelection::type());
229 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
230 for (; aRefsIter != aRefs.end(); aRefsIter++) {
231 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
232 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
233 aSel->update(); // this must be done on execution since it may be long operation
235 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::type());
236 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
237 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
238 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
239 for(int a = aSel->size() - 1; a >= 0; a--) {
240 aSel->value(a)->update();
243 // for sketch after update of plane (by update of selection attribute)
244 // but before execute, all sub-elements also must be updated (due to the plane changes)
246 int aSubsNum = aComposite->numberOfSubs();
247 for(int a = 0; a < aSubsNum; a++) {
248 FeaturePtr aSub = aComposite->subFeature(a);
249 bool aWasModified = myUpdated[aSub];
250 myUpdated.erase(myUpdated.find(aSub)); // erase to update for sure (plane may be changed)
251 myInitial.insert(aSub);
253 myUpdated[aSub] = aWasModified; // restore value
255 // re-execute after update: solver may update the previous values, so, shapes must be
257 for(int a = 0; a < aSubsNum; a++) {
258 if (aComposite->subFeature(a) && aFactory->validate(aComposite->subFeature(a)))
259 aComposite->subFeature(a)->execute();
263 // execute in try-catch to avoid internal problems of the feature
265 theFeature->execute();
268 "Feature " + theFeature->getKind() + " has failed during the execution");
269 theFeature->eraseResults();
271 redisplayWithResults(theFeature);
272 } else { // must be updatet, but not updated yet
273 theFeature->data()->mustBeUpdated(true);
274 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
275 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
276 for (; aRIter != aResults.cend(); aRIter++) {
277 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
278 aRes->data()->mustBeUpdated(true);
282 theFeature->eraseResults();
283 redisplayWithResults(theFeature); // result also must be updated
285 } else { // for automatically updated features (on abort, etc) it is necessary to redisplay anyway
286 redisplayWithResults(theFeature);
289 // returns also true is results were updated: for sketch that
290 // refers to sub-features but results of sub-features were changed
291 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
292 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
293 for (; aRIter != aResults.cend(); aRIter++) {
294 if (myInitial.find(*aRIter) != myInitial.end()) {
295 aMustbeUpdated = true;
301 myUpdated[theFeature] = aMustbeUpdated;
302 return aMustbeUpdated;
305 bool Model_Update::updateObject(std::shared_ptr<ModelAPI_Object> theObject, const bool theCyclic)
307 if (myUpdated.find(theObject) != myUpdated.end())
308 return myUpdated[theObject]; // already processed
311 if (theCyclic) { // algorithm for update of all features by dependencies tree
314 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
315 if (aFeature) { // for feature just call update Feature
316 return updateFeature(aFeature);
318 // check general object, possible just a result
319 if (myUpdated.find(theObject) != myUpdated.end())
320 return myUpdated[theObject]; // already processed
321 // check the feature of this object must be executed
322 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
324 FeaturePtr aResFeature = aResult->document()->feature(aResult);
326 return updateFeature(aResFeature);
331 return myInitial.find(theObject) != myInitial.end();