Salome HOME
Merge branch 'Dev_1.3.0' of newgeom:newgeom into Dev_1.3.0
[modules/shaper.git] / src / Model / Model_Update.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Update.cxx
4 // Created:     25 Jun 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include <Model_Update.h>
8 #include <Model_Document.h>
9 #include <Model_Data.h>
10 #include <Model_Objects.h>
11 #include <ModelAPI_Feature.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_Document.h>
14 #include <ModelAPI_Events.h>
15 #include <ModelAPI_AttributeReference.h>
16 #include <ModelAPI_AttributeRefList.h>
17 #include <ModelAPI_AttributeRefAttr.h>
18 #include <ModelAPI_AttributeSelection.h>
19 #include <ModelAPI_AttributeSelectionList.h>
20 #include <ModelAPI_Result.h>
21 #include <ModelAPI_ResultPart.h>
22 #include <ModelAPI_Validator.h>
23 #include <ModelAPI_CompositeFeature.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Tools.h>
26 #include <GeomDataAPI_Point.h>
27 #include <GeomDataAPI_Point2D.h>
28 #include <Events_Loop.h>
29 #include <Events_LongOp.h>
30 #include <Events_Error.h>
31 #include <Config_PropManager.h>
32
33 using namespace std;
34
35 Model_Update MY_UPDATER_INSTANCE;  /// the only one instance initialized on load of the library
36 #define DEB_UPDATE
37
38 Model_Update::Model_Update()
39 {
40   Events_Loop* aLoop = Events_Loop::loop();
41   static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
42   aLoop->registerListener(this, kChangedEvent);
43   static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
44   aLoop->registerListener(this, kRebuildEvent);
45   static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
46   aLoop->registerListener(this, kCreatedEvent);
47   static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
48   aLoop->registerListener(this, kUpdatedEvent);
49   static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
50   aLoop->registerListener(this, kMovedEvent);
51   static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
52   aLoop->registerListener(this, kOpFinishEvent);
53   static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
54   aLoop->registerListener(this, kOpAbortEvent);
55   static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
56   aLoop->registerListener(this, kOpStartEvent);
57
58   Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild immediately",
59                                    Config_Prop::Boolean, "false");
60   myIsAutomatic =
61     Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
62   myIsParamUpdated = false;
63   myIsFinish = false;
64 }
65
66 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
67 {
68   static Events_Loop* aLoop = Events_Loop::loop();
69   static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
70   static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
71   static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
72   static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
73   static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
74   static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
75   static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
76   static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
77   if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
78     bool aPropVal =
79       Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
80     if (aPropVal != myIsAutomatic) { // something is changed
81       myIsAutomatic = aPropVal;
82       if (myIsAutomatic) // higher level of automatization => to rebuild
83         processOperation(false);
84     }
85   } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
86     processOperation(true);
87   } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent ||
88              theMessage->eventID() == kMovedEvent) {
89     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
90         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
91     const std::set<ObjectPtr>& anObjs = aMsg->objects();
92     std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
93     bool isOnlyResults = true; // check that only results were changed: only redisplay is needed
94     for(; anObjIter != anObjs.cend(); anObjIter++) {
95       if (!std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIter).get()) {
96         isOnlyResults = false;
97       }
98       if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
99         myIsParamUpdated = true;
100       }
101       // created objects are always must be up to date (python box feature)
102       // and updated not in internal uptation chain
103       myJustUpdated.insert(*anObjIter);
104
105       // something is updated during the execution: re-execute it (sketch update by parameters)
106       if (myIsExecuted) { 
107         FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
108         if (anUpdated.get() &&  anUpdated->data()->isValid() &&
109             myProcessed.find(anUpdated) != myProcessed.end()) {
110             if (anUpdated->isPreviewNeeded() || myIsFinish) {
111               ModelAPI_ExecState aState = anUpdated->data()->execState();
112               static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
113               if (aFactory->validate(anUpdated)) {
114                 #ifdef DEB_UPDATE
115                   std::cout<<"Execute immideately "<<anUpdated->name()<<std::endl;
116                 #endif
117                 executeFeature(anUpdated);
118               } else {
119                 anUpdated->eraseResults();
120                 redisplayWithResults(anUpdated, ModelAPI_StateInvalidArgument); // result also must be updated
121               }
122             }
123         }
124       }
125       #ifdef DEB_UPDATE
126       if ((*anObjIter)->data() && (*anObjIter)->data()->isValid()) {
127         std::cout<<"Add updated "<<(*anObjIter)->groupName()<<" "
128           <<(*anObjIter)->data()->name()<<std::endl;
129       }
130       #endif
131     }
132     // this event is for solver update, not here, do not react immediately
133     if (!isOnlyResults && !(theMessage->eventID() == kMovedEvent))
134       processOperation(false);
135   } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
136       theMessage->eventID() == kOpStartEvent) {
137
138     if (!(theMessage->eventID() == kOpStartEvent)) {
139       myIsFinish = true;
140       processOperation(true, theMessage->eventID() == kOpFinishEvent);
141       myIsFinish = false;
142     }
143     // remove all macros before clearing all created
144     std::set<ObjectPtr>::iterator anUpdatedIter = myWaitForFinish.begin();
145     while(anUpdatedIter != myWaitForFinish.end()) {
146       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
147       if (aFeature.get()) {
148         // remove macro on finish
149         if (aFeature->isMacro()) {
150           aFeature->document()->removeFeature(aFeature);
151           myWaitForFinish.erase(aFeature);
152         }
153         // to avoid the map update problems on "remove"
154         if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
155           anUpdatedIter = myWaitForFinish.begin();
156         } else {
157           anUpdatedIter++;
158         }
159       } else {
160         anUpdatedIter++;
161       }
162     }
163     // in the end of transaction everything is updated, so clear the old objects (the only one
164     // place where results are cleared)
165     myIsParamUpdated = false;
166     myJustUpdated.clear();
167     myWaitForFinish.clear();
168   }
169 }
170
171 void Model_Update::processOperation(const bool theTotalUpdate, const bool theFinish)
172 {
173   if (theFinish) {
174     // the hardcode (DBC asked): hide the sketch referenced by extrusion on apply
175     std::set<std::shared_ptr<ModelAPI_Object> >::iterator aFIter;
176     for(aFIter = myWaitForFinish.begin(); aFIter != myWaitForFinish.end(); aFIter++)
177     {
178       FeaturePtr aF = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
179       if (aF && aF->data()->isValid() && aF->getKind() == "Extrusion") {
180         AttributeSelectionListPtr aBase = aF->selectionList("base");
181         if (aBase.get()) {
182           for(int a = aBase->size() - 1; a >= 0; a--) {
183             ResultPtr aSketchRes = aBase->value(a)->context();
184             if (aSketchRes) {
185               aSketchRes->setDisplayed(false);
186             }
187           }
188         }
189       }
190     }
191   }
192   // perform update of everything if needed
193   if (!myIsExecuted) {
194     #ifdef DEB_UPDATE
195       std::cout<<"****** Start processing"<<std::endl;
196     #endif
197     myIsExecuted = true;
198
199     bool isAutomaticChanged = false;
200
201     if (theTotalUpdate && !myIsAutomatic) { // Apply button now works as "Rebuild"
202       isAutomaticChanged = true;
203       myIsAutomatic = true;
204     }
205
206     // iterate all features in the root document to update each
207     DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
208     Model_Objects* anObjs = std::dynamic_pointer_cast<Model_Document>(aRootDoc)->objects();
209     if (!anObjs) return;
210     // two cycles: parameters are first to process
211     FeaturePtr aFeatureIter = anObjs->firstFeature();
212     myProcessed.clear(); // to avoid processing twice
213     for (; aFeatureIter.get(); aFeatureIter = anObjs->nextFeature(aFeatureIter)) {
214       if (aFeatureIter->getKind() == "Parameter")
215         updateFeature(aFeatureIter);
216     }
217     aFeatureIter = anObjs->firstFeature();
218     for (; aFeatureIter.get(); aFeatureIter = anObjs->nextFeature(aFeatureIter)) {
219       if (aFeatureIter->getKind() != "Parameter")
220         updateFeature(aFeatureIter);
221     }
222
223     if (isAutomaticChanged) myIsAutomatic = false;
224     myIsExecuted = false;
225
226     // flush to update display
227     static Events_Loop* aLoop = Events_Loop::loop();
228     static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
229     aLoop->flush(EVENT_DISP);
230     #ifdef DEB_UPDATE
231       std::cout<<"****** End processing"<<std::endl;
232     #endif
233   }
234 }
235
236 void Model_Update::updateFeature(FeaturePtr theFeature)
237 {
238   // check all features this feature depended on (recursive call of updateFeature)
239   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
240
241   if (myProcessed.find(theFeature) != myProcessed.end())
242     return;
243   myProcessed.insert(theFeature);
244   if (theFeature->isDisabled())
245     return;
246
247   #ifdef DEB_UPDATE
248     std::cout<<"Update Feature "<<theFeature->name()<<std::endl;
249   #endif
250   CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
251   // If automatice update is not needed and feature attributes were not updated right now,
252   // do not execute it and do not update arguments.
253   if (!myIsAutomatic && myJustUpdated.find(theFeature) == myJustUpdated.end() && !aCompos.get()) {
254     // execute will be performed later, but some features may have not-result 
255     // presentations, so call update for them (like coincidence in the sketcher)
256     static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
257     ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
258     return;
259   }
260
261   // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
262   updateArguments(theFeature);
263
264   // composite feature must be executed after sub-features execution
265   if (aCompos) {
266     // two cycles: parameters must be processed first
267     for(int a = 0; a < aCompos->numberOfSubs(); a++) {
268       FeaturePtr aSub = aCompos->subFeature(a);
269       if (aSub->getKind() == "Parameter")
270         updateFeature(aSub);
271     }
272     // number of subs can be changed in execution: like fillet
273     for(int a = 0; a < aCompos->numberOfSubs(); a++) {
274       FeaturePtr aSub = aCompos->subFeature(a);
275       if (aSub->getKind() != "Parameter")
276        updateFeature(aSub);
277     }
278     // reupdate arguments of composite feature: it may be changed during subs execution
279     if (theFeature->data()->execState() != ModelAPI_StateMustBeUpdated)
280       updateArguments(theFeature);
281   }
282   // this checking must be after the composite feature sub-elements processing:
283   // composite feature status may depend on it's subelements
284   if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
285     theFeature->eraseResults();
286     redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
287     return;
288   }
289
290   bool aJustUpdated = myJustUpdated.find(theFeature) != myJustUpdated.end();
291
292   if (myIsAutomatic && theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
293     aJustUpdated = true;
294
295   // On abort, undo or redo execute is not needed: results in document are updated automatically
296   // But redisplay is needed: results are updated, must be also updated in the viewer.
297   if (aJustUpdated && 
298       !std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures()) {
299     if (!theFeature->isPersistentResult()) { // not persistent must be re-executed on abort, etc.
300       ModelAPI_ExecState aState = theFeature->data()->execState();
301       if (aFactory->validate(theFeature)) {
302         executeFeature(theFeature);
303       } else {
304         theFeature->eraseResults();
305         redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
306       }
307     } else {
308       redisplayWithResults(theFeature, ModelAPI_StateNothing);
309       if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) { // it is done (in the tree)
310         theFeature->data()->execState(ModelAPI_StateDone);
311       }
312     }
313     return;
314   }
315
316   // execute feature if it must be updated
317   if (theFeature->isPreviewNeeded() || myIsFinish) {
318     if (aJustUpdated) {
319       ModelAPI_ExecState aState = theFeature->data()->execState();
320       if (aFactory->validate(theFeature)) {
321         #ifdef DEB_UPDATE
322           std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
323         #endif
324         executeFeature(theFeature);
325       } else {
326         theFeature->eraseResults();
327         redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
328       }
329     }
330   } else { // preview is not needed => make state Done
331     if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
332       theFeature->data()->execState(ModelAPI_StateDone);
333       if (aJustUpdated) // store that it must be updated on finish
334         myJustUpdated.insert(theFeature);
335     }
336   }
337 }
338
339 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState) 
340 {
341   // make updated and redisplay all results
342   static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
343   const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
344   std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
345   for (; aRIter != aResults.cend(); aRIter++) {
346     std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
347     if (!aRes->isDisabled()) {// update state only for enabled results (Placement Result Part may make the original Part Result as invalid)
348       aRes->data()->execState(theState);
349       if (theState == ModelAPI_StateDone) // feature become "done", so execution changed results
350         myJustUpdated.insert(aRes);
351     }
352     if (theFeature->data()->updateID() > aRes->data()->updateID()) {
353       aRes->data()->setUpdateID(theFeature->data()->updateID());
354     }
355     ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
356   }
357   // to redisplay "presentable" feature (for ex. distance constraint)
358   ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
359   theFeature->data()->execState(theState);
360 }
361
362 /// Updates the state by the referenced object: if something bad with it, set state for this one
363 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
364 {
365   if (theTarget) {
366     ModelAPI_ExecState aRefState = theTarget->data()->execState();
367     if (aRefState == ModelAPI_StateMustBeUpdated) {
368       if (theCurrent == ModelAPI_StateDone)
369         return ModelAPI_StateMustBeUpdated;
370     } else if (aRefState != ModelAPI_StateDone) {
371       return ModelAPI_StateInvalidArgument;
372     }
373   }
374   return theCurrent;
375 }
376
377 void Model_Update::updateArguments(FeaturePtr theFeature) {
378   // perform this method also for disabled features: to make "not done" state for
379   // features referenced to the active and modified features
380
381   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
382
383   ModelAPI_ExecState aState = theFeature->data()->execState();
384   if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
385     aState = ModelAPI_StateMustBeUpdated;
386   }
387   if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
388     aState = ModelAPI_StateMustBeUpdated;
389   // check the parameters state
390   // Double
391   std::list<AttributePtr> aDoubles =
392     theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
393   std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
394   for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
395     AttributeDoublePtr aDouble =
396       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
397     if (aDouble.get() && !aDouble->text().empty()) {
398       if (myIsParamUpdated) {
399         ModelAPI_AttributeEvalMessage::send(aDouble, this);
400       }
401       if (aDouble->expressionInvalid()) {
402         aState = ModelAPI_StateInvalidArgument;
403       }
404     }
405   }
406   // Point
407   {
408     std::list<AttributePtr> anAttributes =
409       theFeature->data()->attributes(GeomDataAPI_Point::typeId());
410     std::list<AttributePtr>::iterator anIter = anAttributes.begin();
411     for(; anIter != anAttributes.end(); anIter++) {
412       AttributePointPtr aPointAttribute =
413         std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
414       if (aPointAttribute.get()) {
415         if (myIsParamUpdated) {
416           ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
417         }
418         if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
419           (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
420           (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
421           aState = ModelAPI_StateInvalidArgument;
422       }
423     }
424   }
425   // Point2D
426   {
427     std::list<AttributePtr> anAttributes =
428       theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
429     std::list<AttributePtr>::iterator anIter = anAttributes.begin();
430     for(; anIter != anAttributes.end(); anIter++) {
431       AttributePoint2DPtr aPoint2DAttribute =
432         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
433       if (aPoint2DAttribute.get()) {
434         if (myIsParamUpdated) {
435           ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
436         }
437         if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
438           (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
439           aState = ModelAPI_StateInvalidArgument;
440       }
441     }
442   }
443
444   //if (aState == ModelAPI_StateDone) {// all referenced objects are ready to be used
445   //std::cout<<"Execute feature "<<theFeature->getKind()<<std::endl;
446   // before execution update the selection attributes if any
447   list<AttributePtr> aRefs = 
448     theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
449   list<AttributePtr>::iterator aRefsIter = aRefs.begin();
450   for (; aRefsIter != aRefs.end(); aRefsIter++) {
451     std::shared_ptr<ModelAPI_AttributeSelection> aSel =
452       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
453     ObjectPtr aContext = aSel->context();
454     // update argument onlt if the referenced object is changed
455     if (aContext.get() && !aContext->isDisabled() && 
456       (myJustUpdated.find(aContext) != myJustUpdated.end() ||
457       aContext->data()->updateID() > theFeature->data()->updateID())) {
458         if (aState == ModelAPI_StateDone)
459           aState = ModelAPI_StateMustBeUpdated;
460         if (!aSel->update()) { // this must be done on execution since it may be long operation
461           if (!aFactory->isNotObligatory(theFeature->getKind(), theFeature->data()->id(aSel)) &&
462             aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
463             aState = ModelAPI_StateInvalidArgument;
464         }
465     }
466   }
467   aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
468   for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
469     std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
470       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
471     for(int a = aSel->size() - 1; a >= 0; a--) {
472       std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
473         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
474       if (aSelAttr) {
475         ObjectPtr aContext = aSelAttr->context();
476         // update argument onlt if the referenced object is changed
477         if (aContext.get() && !aContext->isDisabled() &&
478             (myJustUpdated.find(aContext) != myJustUpdated.end() ||
479              aContext->data()->updateID() > theFeature->data()->updateID())) {
480             if (aState == ModelAPI_StateDone)
481               aState = ModelAPI_StateMustBeUpdated;
482             if (!aSelAttr->update()) {
483               if (!aFactory->isNotObligatory(
484                 theFeature->getKind(), theFeature->data()->id(aSel)) &&
485                 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
486                 aState = ModelAPI_StateInvalidArgument;
487             }
488         }
489       }
490     }
491   }
492   // check all references: if referenced objects are updated, this object also must be updated
493   // also check state of referenced objects: if they are not ready, inherit corresponding state
494   std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefsObj;
495   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theFeature->data());
496   aData->referencesToObjects(aRefsObj);
497   std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefsObj.begin();
498   for(; aRef != aRefsObj.end(); aRef++) {
499     std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
500     for(; aRefObj != aRef->second.end(); aRefObj++) {
501       // if reference is null, it may mean that this reference is to other document
502       // the does not supported by RefList: parameters may be recomputed
503       if (!aRefObj->get() && theFeature->firstResult().get() && 
504                theFeature->firstResult()->groupName() == ModelAPI_ResultParameter::group()) {
505           if (aState == ModelAPI_StateDone)
506             aState = ModelAPI_StateMustBeUpdated;
507       } else if (myJustUpdated.find(*aRefObj) != myJustUpdated.end() || 
508              (aRefObj->get() && (*aRefObj)->data()->updateID() > theFeature->data()->updateID())) {
509           if (aState == ModelAPI_StateDone)
510             aState = ModelAPI_StateMustBeUpdated;
511       }
512       aState = stateByReference(*aRefObj, aState);
513     }
514   }
515   // composites sub-elements
516   CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
517   // composite feature must be executed after sub-features execution
518   if (aCompos) {
519     // number of subs can be changed in execution: like fillet
520     for(int a = 0; a < aCompos->numberOfSubs(); a++) {
521       FeaturePtr aSub = aCompos->subFeature(a);
522       if (aSub.get() && aState == ModelAPI_StateDone) {
523         if (myJustUpdated.find(aSub) != myJustUpdated.end() || 
524               (aSub->data()->updateID() > theFeature->data()->updateID())) {
525           aState = ModelAPI_StateMustBeUpdated;
526         }
527         // also check that all results of subs were updated: composite also depends on the results
528         const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
529         std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aResults.begin();
530         for(; aResIter != aResults.end(); aResIter++) {
531           if (aResIter->get() && (*aResIter)->data()->isValid() && !(*aResIter)->isDisabled() &&
532                 (myJustUpdated.find(*aResIter) != myJustUpdated.end() || 
533                   ((*aResIter)->data()->updateID() > theFeature->data()->updateID()))) {
534             aState = ModelAPI_StateMustBeUpdated;
535           }
536         }
537       }
538     }
539   }
540
541
542   if (aState != ModelAPI_StateDone)
543     theFeature->data()->execState(aState);
544 }
545
546 void Model_Update::executeFeature(FeaturePtr theFeature)
547 {
548   // execute in try-catch to avoid internal problems of the feature
549   ModelAPI_ExecState aState = ModelAPI_StateDone;
550   theFeature->data()->execState(ModelAPI_StateDone);
551   try {
552     theFeature->execute();
553     myJustUpdated.erase(theFeature);
554     if (theFeature->data()->execState() != ModelAPI_StateDone) {
555       aState = ModelAPI_StateExecFailed;
556     } else {
557       aState = ModelAPI_StateDone;
558       myWaitForFinish.insert(theFeature);
559     }
560   } catch(...) {
561     aState = ModelAPI_StateExecFailed;
562     Events_Error::send(
563       "Feature " + theFeature->getKind() + " has failed during the execution");
564   }
565   if (aState != ModelAPI_StateDone) {
566     theFeature->eraseResults();
567   }
568   theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
569   redisplayWithResults(theFeature, aState);
570 }