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