1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Update.cxx
4 // Created: 25 Jun 2014
5 // Author: Mikhail PONIKAROV
7 #include <Model_Update.h>
8 #include <Model_Document.h>
9 #include <Model_Data.h>
10 #include <ModelAPI_Feature.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Document.h>
13 #include <ModelAPI_Events.h>
14 #include <ModelAPI_AttributeReference.h>
15 #include <ModelAPI_AttributeRefList.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeSelection.h>
18 #include <ModelAPI_AttributeSelectionList.h>
19 #include <ModelAPI_Result.h>
20 #include <ModelAPI_Validator.h>
21 #include <ModelAPI_CompositeFeature.h>
22 #include <ModelAPI_Session.h>
23 #include <Events_Loop.h>
24 #include <Events_LongOp.h>
25 #include <Events_Error.h>
26 #include <Config_PropManager.h>
30 Model_Update MY_UPDATER_INSTANCE; /// the only one instance initialized on load of the library
32 Model_Update::Model_Update()
34 Events_Loop* aLoop = Events_Loop::loop();
35 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
36 aLoop->registerListener(this, kChangedEvent);
37 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
38 aLoop->registerListener(this, kRebuildEvent);
39 static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
40 aLoop->registerListener(this, kCreatedEvent);
41 static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
42 aLoop->registerListener(this, kUpdatedEvent);
43 static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
44 aLoop->registerListener(this, kMovedEvent);
45 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
46 aLoop->registerListener(this, kOpFinishEvent);
47 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
48 aLoop->registerListener(this, kOpAbortEvent);
49 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
50 aLoop->registerListener(this, kOpStartEvent);
52 Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild immediately",
53 Config_Prop::Bool, "false");
54 isAutomatic = Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
57 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
59 static Events_Loop* aLoop = Events_Loop::loop();
60 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
61 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
62 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
63 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
64 static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
65 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
66 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
67 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
68 bool isAutomaticChanged = false;
69 if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
71 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
72 if (aPropVal == isAutomatic)
74 isAutomatic = aPropVal;
75 } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
76 if (isAutomatic == false) {
77 isAutomaticChanged = true;
80 } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent ||
81 theMessage->eventID() == kMovedEvent) {
82 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
83 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
84 const std::set<ObjectPtr>& anObjs = aMsg->objects();
85 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
86 for(; anObjIter != anObjs.cend(); anObjIter++) {
87 myJustCreatedOrUpdated.insert(*anObjIter);
88 // TODO(mpv): check the next line. Came into dev 0.6.1 from BR_PYTHON_PLUGIN
89 // (*anObjIter)->data()->mustBeUpdated(true); // object must be updated because it was changed
91 if (theMessage->eventID() == kMovedEvent)
92 return; // this event is for solver update, not here
93 } else if (theMessage->eventID() == kOpStartEvent) {
94 myJustCreatedOrUpdated.clear();
95 return; // we don't need the update only on operation start (caused problems in PartSet_Listener::processEvent)
96 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
97 if (isAutomatic == false) { // Apply button now works as "Rebuild"
98 isAutomaticChanged = true;
101 // the hardcode (DBC asked): hide the sketch referenced by extrusion on apply
102 if (theMessage->eventID() == kOpFinishEvent) {
103 std::set<std::shared_ptr<ModelAPI_Object> >::iterator aFIter;
104 for(aFIter = myJustCreatedOrUpdated.begin(); aFIter != myJustCreatedOrUpdated.end(); aFIter++)
106 FeaturePtr aF = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
107 if (aF && aF->getKind() == "Extrusion") {
108 AttributeSelectionListPtr aBase = aF->selectionList("base");
110 for(int a = aBase->size() - 1; a >= 0; a--) {
111 ResultPtr aSketchRes = aBase->value(a)->context();
113 static Events_ID HIDE_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TOHIDE);
114 ModelAPI_EventCreator::get()->sendUpdated(aSketchRes, HIDE_DISP);
124 return; // nothing to do: it is executed now
126 //Events_LongOp::start(this);
128 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
129 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
130 if (aMsg) myInitial = aMsg->objects();
134 // iterate all documents: features in Root first, then - subs
135 updateInDoc(ModelAPI_Session::get()->moduleDocument());
138 // flush to update display
139 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
140 aLoop->flush(EVENT_DISP);
141 //Events_LongOp::end(this);
142 if (isAutomaticChanged) isAutomatic = false;
144 if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
145 myJustCreatedOrUpdated.clear();
151 void Model_Update::updateInDoc(std::shared_ptr<ModelAPI_Document> theDoc)
153 // all features one by one
154 int aNbFeatures = theDoc->size(ModelAPI_Feature::group(), true);
155 for (int aFIndex = 0; aFIndex < aNbFeatures; aFIndex++) {
156 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
157 theDoc->object(ModelAPI_Feature::group(), aFIndex, true));
159 updateFeature(aFeature);
161 // all sub-documents one by one
162 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(theDoc);
164 const std::set<std::string> aSubs = aDoc->subDocuments(true);
165 for(std::set<std::string>::iterator aSub = aSubs.begin(); aSub != aSubs.end(); aSub++) {
166 DocumentPtr aSubDoc = theDoc->subDocument(*aSub);
168 updateInDoc(aSubDoc);
174 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
176 // make updated and redisplay all results
177 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
178 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
179 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
180 for (; aRIter != aResults.cend(); aRIter++) {
181 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
182 aRes->data()->execState(theState);
183 myUpdated[aRes] = true;
184 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
186 // to redisplay "presentable" feature (for ex. distance constraint)
187 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
188 theFeature->data()->execState(theState);
191 /// Updates the state by the referenced object: if something bad with it, set state for this one
192 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
195 ModelAPI_ExecState aRefState = theTarget->data()->execState();
196 if (aRefState == ModelAPI_StateMustBeUpdated) {
197 return ModelAPI_StateMustBeUpdated;
198 } else if (aRefState != ModelAPI_StateDone) {
199 return ModelAPI_StateInvalidArgument;
205 bool Model_Update::updateFeature(FeaturePtr theFeature)
207 // check it is already processed
208 if (myUpdated.find(theFeature) != myUpdated.end())
209 return myUpdated[theFeature];
210 // check all features this feature depended on (recursive call of updateFeature)
211 ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
212 bool aMustbeUpdated = myInitial.find(theFeature) != myInitial.end();
213 if (theFeature) { // only real feature contains references to other objects
214 if (theFeature->data()->execState() != ModelAPI_StateDone)
215 aMustbeUpdated = true;
217 // composite feature must be executed after sub-features execution
218 CompositeFeaturePtr aComposite =
219 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
221 int aSubsNum = aComposite->numberOfSubs();
222 for(int a = 0; a < aSubsNum; a++) {
223 if (updateFeature(aComposite->subFeature(a)))
224 aMustbeUpdated = true;
227 ModelAPI_ExecState aState = ModelAPI_StateDone;
228 // check all references: if referenced objects are updated, this object also must be updated
229 // also check state of referenced objects: if they are not ready, inherit corresponding state
230 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
231 std::shared_ptr<Model_Data> aData =
232 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
233 aData->referencesToObjects(aRefs);
234 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefs.begin();
235 for(; aRef != aRefs.end(); aRef++) {
236 std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
237 for(; aRefObj != aRef->second.end(); aRefObj++) {
238 if (updateObject(*aRefObj)) {
239 aMustbeUpdated = true;
241 aState = stateByReference(*aRefObj, aState);
245 //std::cout<<"Update feature "<<theFeature->getKind()<<" must be updated = "<<aMustbeUpdated<<std::endl;
246 // execute feature if it must be updated
247 if (aMustbeUpdated) {
248 if (std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures() ||
249 !theFeature->isPersistentResult()) {
250 if (aFactory->validate(theFeature)) {
252 (myJustCreatedOrUpdated.find(theFeature) != myJustCreatedOrUpdated.end()) ||
253 !theFeature->isPersistentResult() /* execute quick, not persistent results */)
255 if (aState == ModelAPI_StateDone) {// all referenced objects are ready to be used
256 //std::cout<<"Execute feature "<<theFeature->getKind()<<std::endl;
257 // before execution update the selection attributes if any
258 list<AttributePtr> aRefs =
259 theFeature->data()->attributes(ModelAPI_AttributeSelection::type());
260 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
261 for (; aRefsIter != aRefs.end(); aRefsIter++) {
262 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
263 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
264 if (!aSel->update()) { // this must be done on execution since it may be long operation
265 if (!aFactory->isNotObligatory(theFeature->getKind(), theFeature->data()->id(aSel)) &&
266 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
267 aState = ModelAPI_StateInvalidArgument;
270 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::type());
271 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
272 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
273 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
274 for(int a = aSel->size() - 1; a >= 0; a--) {
275 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
276 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
278 if (!aSelAttr->update()) {
279 if (!aFactory->isNotObligatory(
280 theFeature->getKind(), theFeature->data()->id(aSel)) &&
281 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
282 aState = ModelAPI_StateInvalidArgument;
287 // for sketch after update of plane (by update of selection attribute)
288 // but before execute, all sub-elements also must be updated (due to the plane changes)
290 int aSubsNum = aComposite->numberOfSubs();
291 for(int a = 0; a < aSubsNum; a++) {
292 FeaturePtr aSub = aComposite->subFeature(a);
293 bool aWasModified = myUpdated[aSub];
294 myUpdated.erase(myUpdated.find(aSub)); // erase to update for sure (plane may be changed)
295 myInitial.insert(aSub);
297 myUpdated[aSub] = aWasModified; // restore value
299 // re-execute after update: solver may update the previous values, so, shapes must be
301 for(int a = 0; a < aSubsNum; a++) {
302 if (aComposite->subFeature(a) && aFactory->validate(aComposite->subFeature(a)))
303 aComposite->subFeature(a)->execute();
308 // execute in try-catch to avoid internal problems of the feature
309 if (aState == ModelAPI_StateDone) {
310 theFeature->data()->execState(ModelAPI_StateDone);
312 theFeature->execute();
313 if (theFeature->data()->execState() != ModelAPI_StateDone) {
314 aState = ModelAPI_StateExecFailed;
317 aState = ModelAPI_StateExecFailed;
319 "Feature " + theFeature->getKind() + " has failed during the execution");
322 if (aState != ModelAPI_StateDone) {
323 theFeature->eraseResults();
325 redisplayWithResults(theFeature, aState);
326 } else { // must be updatet, but not updated yet
327 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
328 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
329 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
330 for (; aRIter != aResults.cend(); aRIter++) {
331 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
332 aRes->data()->execState(ModelAPI_StateMustBeUpdated);
336 theFeature->eraseResults();
337 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
339 } else { // for automatically updated features (on abort, etc) it is necessary to redisplay anyway
340 redisplayWithResults(theFeature, ModelAPI_StateNothing);
343 // returns also true is results were updated: for sketch that
344 // refers to sub-features but results of sub-features were changed
345 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
346 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
347 for (; aRIter != aResults.cend(); aRIter++) {
348 if (myInitial.find(*aRIter) != myInitial.end()) {
349 aMustbeUpdated = true;
355 myUpdated[theFeature] = aMustbeUpdated;
356 return aMustbeUpdated;
359 bool Model_Update::updateObject(std::shared_ptr<ModelAPI_Object> theObject, const bool theCyclic)
361 if (myUpdated.find(theObject) != myUpdated.end())
362 return myUpdated[theObject]; // already processed
365 if (theCyclic) { // algorithm for update of all features by dependencies tree
368 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
369 if (aFeature) { // for feature just call update Feature
370 return updateFeature(aFeature);
372 // check general object, possible just a result
373 if (myUpdated.find(theObject) != myUpdated.end())
374 return myUpdated[theObject]; // already processed
375 // check the feature of this object must be executed
376 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
378 FeaturePtr aResFeature = aResult->document()->feature(aResult);
380 return updateFeature(aResFeature);
385 return myInitial.find(theObject) != myInitial.end();