Salome HOME
Debug of movement of part results
[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       #ifdef DEB_UPDATE
105       if ((*anObjIter)->data() && (*anObjIter)->data()->isValid()) {
106         std::cout<<"Add updated "<<(*anObjIter)->groupName()<<" "
107           <<(*anObjIter)->data()->name()<<std::endl;
108       }
109       #endif
110     }
111     // this event is for solver update, not here, do not react immediately
112     if (!isOnlyResults && !(theMessage->eventID() == kMovedEvent))
113       processOperation(false);
114   } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
115       theMessage->eventID() == kOpStartEvent) {
116
117     if (!(theMessage->eventID() == kOpStartEvent)) {
118       myIsFinish = true;
119       processOperation(true, theMessage->eventID() == kOpFinishEvent);
120       myIsFinish = false;
121     }
122     // remove all macros before clearing all created
123     std::set<ObjectPtr>::iterator anUpdatedIter = myWaitForFinish.begin();
124     while(anUpdatedIter != myWaitForFinish.end()) {
125       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
126       if (aFeature.get()) {
127         // remove macro on finish
128         if (aFeature->isMacro()) {
129           aFeature->document()->removeFeature(aFeature);
130           myWaitForFinish.erase(aFeature);
131         }
132         // to avoid the map update problems on "remove"
133         if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
134           anUpdatedIter = myWaitForFinish.begin();
135         } else {
136           anUpdatedIter++;
137         }
138       } else {
139         anUpdatedIter++;
140       }
141     }
142     // in the end of transaction everything is updated, so clear the old objects (the only one
143     // place where results are cleared)
144     myIsParamUpdated = false;
145     myJustUpdated.clear();
146     myWaitForFinish.clear();
147   }
148 }
149
150 void Model_Update::processOperation(const bool theTotalUpdate, const bool theFinish)
151 {
152   if (theFinish) {
153     // the hardcode (DBC asked): hide the sketch referenced by extrusion on apply
154     std::set<std::shared_ptr<ModelAPI_Object> >::iterator aFIter;
155     for(aFIter = myWaitForFinish.begin(); aFIter != myWaitForFinish.end(); aFIter++)
156     {
157       FeaturePtr aF = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
158       if (aF && aF->data()->isValid() && aF->getKind() == "Extrusion") {
159         AttributeSelectionListPtr aBase = aF->selectionList("base");
160         if (aBase.get()) {
161           for(int a = aBase->size() - 1; a >= 0; a--) {
162             ResultPtr aSketchRes = aBase->value(a)->context();
163             if (aSketchRes) {
164               aSketchRes->setDisplayed(false);
165             }
166           }
167         }
168       }
169     }
170   }
171   // perform update of everything if needed
172   if (!myIsExecuted) {
173     #ifdef DEB_UPDATE
174       std::cout<<"****** Start processing"<<std::endl;
175     #endif
176     myIsExecuted = true;
177
178     bool isAutomaticChanged = false;
179
180     if (theTotalUpdate && !myIsAutomatic) { // Apply button now works as "Rebuild"
181       isAutomaticChanged = true;
182       myIsAutomatic = true;
183     }
184
185     // iterate all features in the root document to update each
186     DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
187     Model_Objects* anObjs = std::dynamic_pointer_cast<Model_Document>(aRootDoc)->objects();
188     if (!anObjs) return;
189     // two cycles: parameters are first to process
190     FeaturePtr aFeatureIter = anObjs->firstFeature();
191     std::set<FeaturePtr> aProcessedFeatures; // to avoid processing twice
192     for (; aFeatureIter.get(); aFeatureIter = anObjs->nextFeature(aFeatureIter)) {
193       if (aFeatureIter->getKind() == "Parameter")
194         updateFeature(aFeatureIter, aProcessedFeatures);
195     }
196     aFeatureIter = anObjs->firstFeature();
197     for (; aFeatureIter.get(); aFeatureIter = anObjs->nextFeature(aFeatureIter)) {
198       if (aFeatureIter->getKind() != "Parameter")
199         updateFeature(aFeatureIter, aProcessedFeatures);
200     }
201
202     if (isAutomaticChanged) myIsAutomatic = false;
203     myIsExecuted = false;
204
205     // flush to update display
206     static Events_Loop* aLoop = Events_Loop::loop();
207     static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
208     aLoop->flush(EVENT_DISP);
209     #ifdef DEB_UPDATE
210       std::cout<<"****** End processing"<<std::endl;
211     #endif
212   }
213 }
214
215 void Model_Update::updateFeature(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed)
216 {
217   // check all features this feature depended on (recursive call of updateFeature)
218   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
219
220   if (theProcessed.find(theFeature) != theProcessed.end())
221     return;
222   theProcessed.insert(theFeature);
223   if (theFeature->isDisabled())
224     return;
225
226   #ifdef DEB_UPDATE
227     std::cout<<"Update Feature "<<theFeature->name()<<std::endl;
228   #endif
229   CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
230   // If automatice update is not needed and feature attributes were not updated right now,
231   // do not execute it and do not update arguments.
232   if (!myIsAutomatic && myJustUpdated.find(theFeature) == myJustUpdated.end() && !aCompos.get()) {
233     // execute will be performed later, but some features may have not-result 
234     // presentations, so call update for them (like coincidence in the sketcher)
235     static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
236     ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
237     return;
238   }
239
240   // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
241   updateArguments(theFeature);
242
243   // composite feature must be executed after sub-features execution
244   if (aCompos) {
245     // two cycles: parameters must be processed first
246     for(int a = 0; a < aCompos->numberOfSubs(); a++) {
247       FeaturePtr aSub = aCompos->subFeature(a);
248       if (aSub->getKind() == "Parameter")
249         updateFeature(aSub, theProcessed);
250     }
251     // number of subs can be changed in execution: like fillet
252     for(int a = 0; a < aCompos->numberOfSubs(); a++) {
253       FeaturePtr aSub = aCompos->subFeature(a);
254       if (aSub->getKind() != "Parameter")
255        updateFeature(aSub, theProcessed);
256     }
257   }
258   // this checking must be after the composite feature sub-elements processing:
259   // composite feature status may depend on it's subelements
260   if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
261     theFeature->eraseResults();
262     redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
263     return;
264   }
265
266   bool aJustUpdated = myJustUpdated.find(theFeature) != myJustUpdated.end();
267
268   if (myIsAutomatic && theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
269     aJustUpdated = true;
270
271   // On abort, undo or redo execute is not needed: results in document are updated automatically
272   // But redisplay is needed: results are updated, must be also updated in the viewer.
273   if (aJustUpdated && 
274       !std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures()) {
275     if (!theFeature->isPersistentResult()) { // not persistent must be re-executed on abort, etc.
276       ModelAPI_ExecState aState = theFeature->data()->execState();
277       if (aFactory->validate(theFeature)) {
278         executeFeature(theFeature);
279       } else {
280         theFeature->eraseResults();
281         redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
282       }
283     } else {
284       redisplayWithResults(theFeature, ModelAPI_StateNothing);
285       if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) { // it is done (in the tree)
286         theFeature->data()->execState(ModelAPI_StateDone);
287       }
288     }
289     return;
290   }
291
292   // execute feature if it must be updated
293   if (theFeature->isPreviewNeeded() || myIsFinish) {
294     if (aJustUpdated) {
295       ModelAPI_ExecState aState = theFeature->data()->execState();
296       if (aFactory->validate(theFeature)) {
297         #ifdef DEB_UPDATE
298           std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
299         #endif
300         executeFeature(theFeature);
301       } else {
302         theFeature->eraseResults();
303         redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
304       }
305     }
306   } else { // preview is not needed => make state Done
307     if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
308       theFeature->data()->execState(ModelAPI_StateDone);
309       if (aJustUpdated) // store that it must be updated on finish
310         myJustUpdated.insert(theFeature);
311     }
312   }
313 }
314
315 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState) 
316 {
317   // make updated and redisplay all results
318   static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
319   const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
320   std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
321   for (; aRIter != aResults.cend(); aRIter++) {
322     std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
323     if (!aRes->isDisabled()) // update state only for enabled results (Placement Result Part may make the original Part Result as invalid)
324       aRes->data()->execState(theState);
325     if (theFeature->data()->updateID() > aRes->data()->updateID()) {
326       aRes->data()->setUpdateID(theFeature->data()->updateID());
327     }
328     ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
329   }
330   // to redisplay "presentable" feature (for ex. distance constraint)
331   ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
332   theFeature->data()->execState(theState);
333 }
334
335 /// Updates the state by the referenced object: if something bad with it, set state for this one
336 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
337 {
338   if (theTarget) {
339     ModelAPI_ExecState aRefState = theTarget->data()->execState();
340     if (aRefState == ModelAPI_StateMustBeUpdated) {
341       if (theCurrent == ModelAPI_StateDone)
342         return ModelAPI_StateMustBeUpdated;
343     } else if (aRefState != ModelAPI_StateDone) {
344       return ModelAPI_StateInvalidArgument;
345     }
346   }
347   return theCurrent;
348 }
349
350 void Model_Update::updateArguments(FeaturePtr theFeature) {
351   // perform this method also for disabled features: to make "not done" state for
352   // features referenced to the active and modified features
353
354   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
355
356   ModelAPI_ExecState aState = theFeature->data()->execState();
357   if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
358     aState = ModelAPI_StateMustBeUpdated;
359   }
360   if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
361     aState = ModelAPI_StateMustBeUpdated;
362   // check the parameters state
363   // Double
364   std::list<AttributePtr> aDoubles =
365     theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
366   std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
367   for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
368     AttributeDoublePtr aDouble =
369       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
370     if (aDouble.get() && !aDouble->text().empty()) {
371       if (myIsParamUpdated) {
372         ModelAPI_AttributeEvalMessage::send(aDouble, this);
373       }
374       if (aDouble->expressionInvalid()) {
375         aState = ModelAPI_StateInvalidArgument;
376       }
377     }
378   }
379   // Point
380   {
381     std::list<AttributePtr> anAttributes =
382       theFeature->data()->attributes(GeomDataAPI_Point::typeId());
383     std::list<AttributePtr>::iterator anIter = anAttributes.begin();
384     for(; anIter != anAttributes.end(); anIter++) {
385       AttributePointPtr aPointAttribute =
386         std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
387       if (aPointAttribute.get()) {
388         if (myIsParamUpdated) {
389           ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
390         }
391         if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
392           (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
393           (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
394           aState = ModelAPI_StateInvalidArgument;
395       }
396     }
397   }
398   // Point2D
399   {
400     std::list<AttributePtr> anAttributes =
401       theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
402     std::list<AttributePtr>::iterator anIter = anAttributes.begin();
403     for(; anIter != anAttributes.end(); anIter++) {
404       AttributePoint2DPtr aPoint2DAttribute =
405         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
406       if (aPoint2DAttribute.get()) {
407         if (myIsParamUpdated) {
408           ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
409         }
410         if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
411           (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
412           aState = ModelAPI_StateInvalidArgument;
413       }
414     }
415   }
416
417   //if (aState == ModelAPI_StateDone) {// all referenced objects are ready to be used
418   //std::cout<<"Execute feature "<<theFeature->getKind()<<std::endl;
419   // before execution update the selection attributes if any
420   list<AttributePtr> aRefs = 
421     theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
422   list<AttributePtr>::iterator aRefsIter = aRefs.begin();
423   for (; aRefsIter != aRefs.end(); aRefsIter++) {
424     std::shared_ptr<ModelAPI_AttributeSelection> aSel =
425       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
426     ObjectPtr aContext = aSel->context();
427     // update argument onlt if the referenced object is changed
428     if (aContext.get() && !aContext->isDisabled() && 
429       (myJustUpdated.find(aContext) != myJustUpdated.end() ||
430       aContext->data()->updateID() > theFeature->data()->updateID())) {
431         if (aState == ModelAPI_StateDone)
432           aState = ModelAPI_StateMustBeUpdated;
433         if (!aSel->update()) { // this must be done on execution since it may be long operation
434           if (!aFactory->isNotObligatory(theFeature->getKind(), theFeature->data()->id(aSel)) &&
435             aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
436             aState = ModelAPI_StateInvalidArgument;
437         }
438     }
439   }
440   aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
441   for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
442     std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
443       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
444     for(int a = aSel->size() - 1; a >= 0; a--) {
445       std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
446         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
447       if (aSelAttr) {
448         ObjectPtr aContext = aSelAttr->context();
449         // update argument onlt if the referenced object is changed
450         if (aContext.get() && !aContext->isDisabled() &&
451             (myJustUpdated.find(aContext) != myJustUpdated.end() ||
452              aContext->data()->updateID() > theFeature->data()->updateID())) {
453             if (aState == ModelAPI_StateDone)
454               aState = ModelAPI_StateMustBeUpdated;
455             if (!aSelAttr->update()) {
456               if (!aFactory->isNotObligatory(
457                 theFeature->getKind(), theFeature->data()->id(aSel)) &&
458                 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
459                 aState = ModelAPI_StateInvalidArgument;
460             }
461         }
462       }
463     }
464   }
465   // check all references: if referenced objects are updated, this object also must be updated
466   // also check state of referenced objects: if they are not ready, inherit corresponding state
467   std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefsObj;
468   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theFeature->data());
469   aData->referencesToObjects(aRefsObj);
470   std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefsObj.begin();
471   for(; aRef != aRefsObj.end(); aRef++) {
472     std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
473     for(; aRefObj != aRef->second.end(); aRefObj++) {
474       // if reference is null, it may mean that this reference is to other document
475       // the does not supported by RefList: parameters may be recomputed
476       if (!aRefObj->get() && theFeature->firstResult().get() && 
477                theFeature->firstResult()->groupName() == ModelAPI_ResultParameter::group()) {
478           if (aState == ModelAPI_StateDone)
479             aState = ModelAPI_StateMustBeUpdated;
480       } else if (myJustUpdated.find(*aRefObj) != myJustUpdated.end() || 
481              (aRefObj->get() && (*aRefObj)->data()->updateID() > theFeature->data()->updateID())) {
482           if (aState == ModelAPI_StateDone)
483             aState = ModelAPI_StateMustBeUpdated;
484       }
485       aState = stateByReference(*aRefObj, aState);
486     }
487   }
488   // composites sub-elements
489   CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
490   // composite feature must be executed after sub-features execution
491   if (aCompos) {
492     // number of subs can be changed in execution: like fillet
493     for(int a = 0; a < aCompos->numberOfSubs(); a++) {
494       FeaturePtr aSub = aCompos->subFeature(a);
495       if (aSub.get() && aState == ModelAPI_StateDone) {
496         if (myJustUpdated.find(aSub) != myJustUpdated.end() || 
497               (aSub->data()->updateID() > theFeature->data()->updateID())) {
498           aState = ModelAPI_StateMustBeUpdated;
499         }
500         // also check that all results of subs were updated: composite also depends on the results
501         const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
502         std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aResults.begin();
503         for(; aResIter != aResults.end(); aResIter++) {
504           if (aResIter->get() && (*aResIter)->data()->isValid() && !(*aResIter)->isDisabled() &&
505                 (myJustUpdated.find(*aResIter) != myJustUpdated.end() || 
506                   ((*aResIter)->data()->updateID() > theFeature->data()->updateID()))) {
507             aState = ModelAPI_StateMustBeUpdated;
508           }
509         }
510       }
511     }
512   }
513
514
515   if (aState != ModelAPI_StateDone)
516     theFeature->data()->execState(aState);
517 }
518
519 void Model_Update::executeFeature(FeaturePtr theFeature)
520 {
521   // execute in try-catch to avoid internal problems of the feature
522   ModelAPI_ExecState aState = ModelAPI_StateDone;
523   theFeature->data()->execState(ModelAPI_StateDone);
524   try {
525     theFeature->execute();
526     myJustUpdated.erase(theFeature);
527     if (theFeature->data()->execState() != ModelAPI_StateDone) {
528       aState = ModelAPI_StateExecFailed;
529     } else {
530       aState = ModelAPI_StateDone;
531       myWaitForFinish.insert(theFeature);
532     }
533   } catch(...) {
534     aState = ModelAPI_StateExecFailed;
535     Events_Error::send(
536       "Feature " + theFeature->getKind() + " has failed during the execution");
537   }
538   if (aState != ModelAPI_StateDone) {
539     theFeature->eraseResults();
540   }
541   theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
542   redisplayWithResults(theFeature, aState);
543 }