Salome HOME
Merge branch 'Dev_1.4.0' of newgeom:newgeom into Dev_1.4.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) && aState != ModelAPI_StateInvalidArgument) {
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 bool Model_Update::iterateUpdate(std::shared_ptr<ModelAPI_CompositeFeature> theFeature)
172 {
173   myProcessIterator.push_back(IterationItem());
174   IterationItem& aCurrent = *myProcessIterator.rbegin();
175   if (theFeature.get()) {
176     aCurrent.myMain = theFeature;
177     // two cycles: parameters must be processed first
178     for(int a = 0; a < theFeature->numberOfSubs(); a++) {
179       FeaturePtr aSub = theFeature->subFeature(a);
180       aCurrent.mySub = aSub;
181       if (aSub->getKind() == "Parameter")
182         updateFeature(aSub);
183     }
184     // number of subs can be changed in execution: like fillet
185     for(int a = 0; a < theFeature->numberOfSubs(); a++) {
186       FeaturePtr aSub = theFeature->subFeature(a);
187       aCurrent.mySub = aSub;
188       if (aSub->getKind() != "Parameter")
189        updateFeature(aSub);
190     }
191   } else {
192     DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
193     Model_Objects* anObjs = std::dynamic_pointer_cast<Model_Document>(aRootDoc)->objects();
194     if (!anObjs)
195       return false;
196     aCurrent.mySub = anObjs->firstFeature();
197     for (; aCurrent.mySub.get(); aCurrent.mySub = anObjs->nextFeature(aCurrent.mySub)) {
198       if (aCurrent.mySub->getKind() == "Parameter")
199         updateFeature(aCurrent.mySub);
200     }
201     aCurrent.mySub = anObjs->firstFeature();
202     for (; aCurrent.mySub.get(); aCurrent.mySub = anObjs->nextFeature(aCurrent.mySub)) {
203       if (aCurrent.mySub->getKind() != "Parameter")
204         updateFeature(aCurrent.mySub);
205     }
206   }
207   // processing is finished, so, remove the iterated
208   myProcessIterator.pop_back();
209   return true; // iteration is finished correctly
210 }
211
212 void Model_Update::processOperation(const bool theTotalUpdate, const bool theFinish)
213 {
214   if (theFinish) {
215     // the hardcode (DBC asked): hide the sketch referenced by extrusion on apply
216     std::set<std::shared_ptr<ModelAPI_Object> >::iterator aFIter;
217     for(aFIter = myWaitForFinish.begin(); aFIter != myWaitForFinish.end(); aFIter++)
218     {
219       FeaturePtr aF = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
220       if (aF && aF->data()->isValid() && aF->getKind() == "Extrusion") {
221         AttributeSelectionListPtr aBase = aF->selectionList("base");
222         if (aBase.get()) {
223           for(int a = aBase->size() - 1; a >= 0; a--) {
224             ResultPtr aSketchRes = aBase->value(a)->context();
225             if (aSketchRes) {
226               aSketchRes->setDisplayed(false);
227             }
228           }
229         }
230       }
231     }
232   }
233   // perform update of everything if needed
234   if (!myIsExecuted) {
235     #ifdef DEB_UPDATE
236       std::cout<<"****** Start processing"<<std::endl;
237     #endif
238     myIsExecuted = true;
239
240     bool isAutomaticChanged = false;
241
242     if (theTotalUpdate && !myIsAutomatic) { // Apply button now works as "Rebuild"
243       isAutomaticChanged = true;
244       myIsAutomatic = true;
245     }
246     // init iteration from the root document
247     myProcessed.clear(); // to avoid processing twice
248     iterateUpdate(CompositeFeaturePtr());
249
250     if (isAutomaticChanged) myIsAutomatic = false;
251     myProcessed.clear(); // to avoid keeping features in memory
252     myIsExecuted = false;
253
254     // flush to update display
255     static Events_Loop* aLoop = Events_Loop::loop();
256     static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
257     aLoop->flush(EVENT_DISP);
258     #ifdef DEB_UPDATE
259       std::cout<<"****** End processing"<<std::endl;
260     #endif
261   }
262 }
263
264 void Model_Update::updateFeature(FeaturePtr theFeature)
265 {
266   // check all features this feature depended on (recursive call of updateFeature)
267   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
268
269   if (myProcessed.find(theFeature) != myProcessed.end())
270     return;
271   myProcessed.insert(theFeature);
272   if (theFeature->isDisabled())
273     return;
274
275   #ifdef DEB_UPDATE
276     std::cout<<"Update Feature "<<theFeature->name()<<std::endl;
277   #endif
278   CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
279   // If automatice update is not needed and feature attributes were not updated right now,
280   // do not execute it and do not update arguments.
281   if (!myIsAutomatic && myJustUpdated.find(theFeature) == myJustUpdated.end() && !aCompos.get()) {
282     // execute will be performed later, but some features may have not-result 
283     // presentations, so call update for them (like coincidence in the sketcher)
284     static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
285     ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
286     return;
287   }
288
289   // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
290   updateArguments(theFeature);
291
292   // composite feature must be executed after sub-features execution
293   if (aCompos) {
294     if (!iterateUpdate(aCompos))
295       return; // iteration was interrupted, so, interrupt the update of this feature (it will be done later)
296     // reupdate arguments of composite feature: it may be changed during subs execution
297     if (theFeature->data()->execState() != ModelAPI_StateMustBeUpdated)
298       updateArguments(theFeature);
299   }
300   // this checking must be after the composite feature sub-elements processing:
301   // composite feature status may depend on it's subelements
302   if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
303     theFeature->eraseResults();
304     redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
305     return;
306   }
307
308   bool aJustUpdated = myJustUpdated.find(theFeature) != myJustUpdated.end();
309
310   if (myIsAutomatic && theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
311     aJustUpdated = true;
312
313   // On abort, undo or redo execute is not needed: results in document are updated automatically
314   // But redisplay is needed: results are updated, must be also updated in the viewer.
315   if (aJustUpdated && 
316       !std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures()) {
317     if (!theFeature->isPersistentResult()) { // not persistent must be re-executed on abort, etc.
318       ModelAPI_ExecState aState = theFeature->data()->execState();
319       if (aFactory->validate(theFeature)) {
320         executeFeature(theFeature);
321       } else {
322         theFeature->eraseResults();
323         redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
324       }
325     } else {
326       redisplayWithResults(theFeature, ModelAPI_StateNothing);
327       if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) { // it is done (in the tree)
328         theFeature->data()->execState(ModelAPI_StateDone);
329       }
330     }
331     return;
332   }
333
334   // execute feature if it must be updated
335   if (theFeature->isPreviewNeeded() || myIsFinish) {
336     if (aJustUpdated) {
337       ModelAPI_ExecState aState = theFeature->data()->execState();
338       if (aFactory->validate(theFeature)) {
339         #ifdef DEB_UPDATE
340           std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
341         #endif
342         executeFeature(theFeature);
343       } else {
344         theFeature->eraseResults();
345         redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
346       }
347     }
348   } else { // preview is not needed => make state Done
349     if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
350       theFeature->data()->execState(ModelAPI_StateDone);
351       if (aJustUpdated) // store that it must be updated on finish
352         myJustUpdated.insert(theFeature);
353     }
354   }
355 }
356
357 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState) 
358 {
359   // make updated and redisplay all results
360   static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
361   const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
362   std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
363   for (; aRIter != aResults.cend(); aRIter++) {
364     std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
365     if (!aRes->isDisabled()) {// update state only for enabled results (Placement Result Part may make the original Part Result as invalid)
366       aRes->data()->execState(theState);
367       if (theState == ModelAPI_StateDone) // feature become "done", so execution changed results
368         myJustUpdated.insert(aRes);
369     }
370     if (theFeature->data()->updateID() > aRes->data()->updateID()) {
371       aRes->data()->setUpdateID(theFeature->data()->updateID());
372     }
373     ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
374   }
375   // to redisplay "presentable" feature (for ex. distance constraint)
376   ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
377   theFeature->data()->execState(theState);
378 }
379
380 /// Updates the state by the referenced object: if something bad with it, set state for this one
381 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
382 {
383   if (theTarget) {
384     ModelAPI_ExecState aRefState = theTarget->data()->execState();
385     if (aRefState == ModelAPI_StateMustBeUpdated) {
386       if (theCurrent == ModelAPI_StateDone)
387         return ModelAPI_StateMustBeUpdated;
388     } else if (aRefState != ModelAPI_StateDone) {
389       return ModelAPI_StateInvalidArgument;
390     }
391   }
392   return theCurrent;
393 }
394
395 void Model_Update::updateArguments(FeaturePtr theFeature) {
396   // perform this method also for disabled features: to make "not done" state for
397   // features referenced to the active and modified features
398
399   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
400
401   ModelAPI_ExecState aState = theFeature->data()->execState();
402   if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
403     aState = ModelAPI_StateMustBeUpdated;
404   }
405   if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
406     aState = ModelAPI_StateMustBeUpdated;
407   // check the parameters state
408   // Double
409   std::list<AttributePtr> aDoubles =
410     theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
411   std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
412   for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
413     AttributeDoublePtr aDouble =
414       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
415     if (aDouble.get() && !aDouble->text().empty()) {
416       if (myIsParamUpdated) {
417         ModelAPI_AttributeEvalMessage::send(aDouble, this);
418       }
419       if (aDouble->expressionInvalid()) {
420         aState = ModelAPI_StateInvalidArgument;
421       }
422     }
423   }
424   // Point
425   {
426     std::list<AttributePtr> anAttributes =
427       theFeature->data()->attributes(GeomDataAPI_Point::typeId());
428     std::list<AttributePtr>::iterator anIter = anAttributes.begin();
429     for(; anIter != anAttributes.end(); anIter++) {
430       AttributePointPtr aPointAttribute =
431         std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
432       if (aPointAttribute.get()) {
433         if (myIsParamUpdated) {
434           ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
435         }
436         if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
437           (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
438           (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
439           aState = ModelAPI_StateInvalidArgument;
440       }
441     }
442   }
443   // Point2D
444   {
445     std::list<AttributePtr> anAttributes =
446       theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
447     std::list<AttributePtr>::iterator anIter = anAttributes.begin();
448     for(; anIter != anAttributes.end(); anIter++) {
449       AttributePoint2DPtr aPoint2DAttribute =
450         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
451       if (aPoint2DAttribute.get()) {
452         if (myIsParamUpdated) {
453           ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
454         }
455         if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
456           (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
457           aState = ModelAPI_StateInvalidArgument;
458       }
459     }
460   }
461
462   //if (aState == ModelAPI_StateDone) {// all referenced objects are ready to be used
463   //std::cout<<"Execute feature "<<theFeature->getKind()<<std::endl;
464   // before execution update the selection attributes if any
465   list<AttributePtr> aRefs = 
466     theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
467   list<AttributePtr>::iterator aRefsIter = aRefs.begin();
468   for (; aRefsIter != aRefs.end(); aRefsIter++) {
469     std::shared_ptr<ModelAPI_AttributeSelection> aSel =
470       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
471     ObjectPtr aContext = aSel->context();
472     // update argument only if the referenced object is changed
473     if (aContext.get() && !aContext->isDisabled()) {
474       bool isObligatory = !aFactory->isNotObligatory(
475         theFeature->getKind(), theFeature->data()->id(aSel)) &&
476         aFactory->isCase(theFeature, theFeature->data()->id(aSel));
477       if (myJustUpdated.find(aContext) != myJustUpdated.end() ||
478           aContext->data()->updateID() > theFeature->data()->updateID()) {
479         if (aState == ModelAPI_StateDone)
480           aState = ModelAPI_StateMustBeUpdated;
481         if (!aSel->update()) { // this must be done on execution since it may be long operation
482           if (isObligatory)
483             aState = ModelAPI_StateInvalidArgument;
484         }
485       } else if (aSel->isInvalid()) { // not needed to update, but invalid (stated previously)
486         aState = ModelAPI_StateInvalidArgument;
487       }
488     }
489   }
490   aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
491   for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
492     std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
493       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
494     for(int a = aSel->size() - 1; a >= 0; a--) {
495       std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
496         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
497       if (aSelAttr) {
498         ObjectPtr aContext = aSelAttr->context();
499         // update argument onlt if the referenced object is changed
500         if (aContext.get() && !aContext->isDisabled()) {
501           bool isObligatory = !aFactory->isNotObligatory(
502             theFeature->getKind(), theFeature->data()->id(aSel)) &&
503             aFactory->isCase(theFeature, theFeature->data()->id(aSel));
504           if ((myJustUpdated.find(aContext) != myJustUpdated.end() ||
505                aContext->data()->updateID() > theFeature->data()->updateID())) {
506             if (aState == ModelAPI_StateDone)
507                 aState = ModelAPI_StateMustBeUpdated;
508             if (!aSelAttr->update()) {
509               if (isObligatory)
510                 aState = ModelAPI_StateInvalidArgument;
511             }
512           } else if (aSelAttr->isInvalid()) {
513             aState = ModelAPI_StateInvalidArgument;
514           }
515         }
516       }
517     }
518   }
519   // check all references: if referenced objects are updated, this object also must be updated
520   // also check state of referenced objects: if they are not ready, inherit corresponding state
521   std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefsObj;
522   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theFeature->data());
523   aData->referencesToObjects(aRefsObj);
524   std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefsObj.begin();
525   for(; aRef != aRefsObj.end(); aRef++) {
526     std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
527     for(; aRefObj != aRef->second.end(); aRefObj++) {
528       // if reference is null, it may mean that this reference is to other document
529       // the does not supported by RefList: parameters may be recomputed
530       if (!aRefObj->get() && theFeature->firstResult().get() && 
531                theFeature->firstResult()->groupName() == ModelAPI_ResultParameter::group()) {
532           if (aState == ModelAPI_StateDone)
533             aState = ModelAPI_StateMustBeUpdated;
534       } else if (myJustUpdated.find(*aRefObj) != myJustUpdated.end() || 
535              (aRefObj->get() && (*aRefObj)->data()->updateID() > theFeature->data()->updateID())) {
536           if (aState == ModelAPI_StateDone)
537             aState = ModelAPI_StateMustBeUpdated;
538       }
539       aState = stateByReference(*aRefObj, aState);
540     }
541   }
542   // composites sub-elements
543   CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
544   // composite feature must be executed after sub-features execution
545   if (aCompos) {
546     // number of subs can be changed in execution: like fillet
547     for(int a = 0; a < aCompos->numberOfSubs(); a++) {
548       FeaturePtr aSub = aCompos->subFeature(a);
549       if (aSub.get() && aState == ModelAPI_StateDone) {
550         if (myJustUpdated.find(aSub) != myJustUpdated.end() || 
551               (aSub->data()->updateID() > theFeature->data()->updateID())) {
552           aState = ModelAPI_StateMustBeUpdated;
553         }
554         // also check that all results of subs were updated: composite also depends on the results
555         const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
556         std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aResults.begin();
557         for(; aResIter != aResults.end(); aResIter++) {
558           if (aResIter->get() && (*aResIter)->data()->isValid() && !(*aResIter)->isDisabled() &&
559                 (myJustUpdated.find(*aResIter) != myJustUpdated.end() || 
560                   ((*aResIter)->data()->updateID() > theFeature->data()->updateID()))) {
561             aState = ModelAPI_StateMustBeUpdated;
562           }
563         }
564       }
565     }
566   }
567
568
569   if (aState != ModelAPI_StateDone)
570     theFeature->data()->execState(aState);
571 }
572
573 void Model_Update::executeFeature(FeaturePtr theFeature)
574 {
575   // execute in try-catch to avoid internal problems of the feature
576   ModelAPI_ExecState aState = ModelAPI_StateDone;
577   theFeature->data()->execState(ModelAPI_StateDone);
578   try {
579     theFeature->execute();
580     myJustUpdated.erase(theFeature);
581     if (theFeature->data()->execState() != ModelAPI_StateDone) {
582       aState = ModelAPI_StateExecFailed;
583     } else {
584       aState = ModelAPI_StateDone;
585       myWaitForFinish.insert(theFeature);
586     }
587   } catch(...) {
588     aState = ModelAPI_StateExecFailed;
589     Events_Error::send(
590       "Feature " + theFeature->getKind() + " has failed during the execution");
591   }
592   if (aState != ModelAPI_StateDone) {
593     theFeature->eraseResults();
594   }
595   theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
596   redisplayWithResults(theFeature, aState);
597 }