]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Update.cpp
Salome HOME
Fix the problem with import feature "Apply" button enabling problem. And reexecution...
[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 kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
44   aLoop->registerListener(this, kCreatedEvent);
45   static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
46   aLoop->registerListener(this, kUpdatedEvent);
47   static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
48   aLoop->registerListener(this, kOpFinishEvent);
49   static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
50   aLoop->registerListener(this, kOpAbortEvent);
51   static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
52   aLoop->registerListener(this, kOpStartEvent);
53   static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
54   aLoop->registerListener(this, kStabilityEvent);
55
56   //  Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
57   myIsParamUpdated = false;
58   myIsFinish = false;
59   myIsProcessed = false;
60 }
61
62 void Model_Update::addModified(FeaturePtr theFeature, FeaturePtr theReason) {
63   if (!theFeature->isPreviewNeeded() && !myIsFinish) {
64     myProcessOnFinish.insert(theFeature);
65 #ifdef DEB_UPDATE
66       std::cout<<"*** Add process on finish "<<theFeature->name()<<std::endl;
67 #endif
68     updateArguments(theFeature);
69     if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
70       theFeature->data()->execState(ModelAPI_StateDone);
71       static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
72       aFactory->validate(theFeature); // need to be validated to update the "Apply" state if not previewed
73     }
74     return;
75   }
76   if (myModified.find(theFeature) != myModified.end()) {
77     if (theReason.get()) {
78 #ifdef DEB_UPDATE
79       std::cout<<"*** Add already modified "<<theFeature->name()<<std::endl;
80 #endif
81       myModified[theFeature].insert(theReason);
82     }
83     return; // already is marked as modified, so, nothing to do, it will be processed
84   }
85   // do not add the disabled, but possibly the sub-elements are not disabled
86   bool aIsDisabled = theFeature->isDisabled();
87   if (!aIsDisabled) {
88     std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
89     if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
90       // do not forget that in this case all were the reasons
91       aNewSet.insert(theFeature);
92     } else {
93       if (theReason.get())
94         aNewSet.insert(theReason);
95     }
96     myModified[theFeature] = aNewSet;
97 #ifdef DEB_UPDATE
98     std::cout<<"*** Add modified "<<theFeature->name()<<std::endl;
99 #endif
100   } else { // will be updated during the finish of the operation, or when it becomes enabled
101     if (theFeature->data()->execState() == ModelAPI_StateDone)
102       theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
103     else 
104       return; // do not need iteration deeply if it is already marked as modified or so
105 #ifdef DEB_UPDATE
106     std::cout<<"*** Set modified state "<<theFeature->name()<<std::endl;
107 #endif
108   }
109   // clear processed and fill modified recursively
110   const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
111   std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
112   for(; aRefIter != aRefs.cend(); aRefIter++) {
113     FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
114     if (aReferenced.get()) {
115       addModified(aReferenced, theFeature);
116     }
117   }
118   // proccess also results
119   std::list<ResultPtr> allResults; // list of this feature and results
120   ModelAPI_Tools::allResults(theFeature, allResults);
121   std::list<ResultPtr>::iterator aRes = allResults.begin();
122   for(; aRes != allResults.end(); aRes++) {
123     const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*aRes)->data()->refsToMe();
124     std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
125     for(; aRefIter != aRefs.cend(); aRefIter++) {
126       FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
127       if (aReferenced.get()) {
128         addModified(aReferenced, theFeature);
129       }
130     }
131   }
132
133   // also add part feature that contains this feature to the modified
134   if (theFeature->document()->kind() != "PartSet") {
135     FeaturePtr aPart = ModelAPI_Tools::findPartFeature(
136       ModelAPI_Session::get()->moduleDocument(), theFeature->document());
137     if (aPart.get())
138       addModified(aPart, theFeature);
139   }
140   return;
141 }
142
143 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
144 {
145   static Events_Loop* aLoop = Events_Loop::loop();
146   static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
147   static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
148   static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
149   static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
150   static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
151   static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
152 #ifdef DEB_UPDATE
153   std::cout<<"****** Event "<<theMessage->eventID().eventText()<<std::endl;
154 #endif
155   if (theMessage->eventID() == kStabilityEvent) {
156     updateStability(theMessage->sender());
157     return;
158   }
159   // creation is added to "update" to avoid recomputation twice: on create and immediately after on update
160   if (theMessage->eventID() == kCreatedEvent) {
161     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
162         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
163     const std::set<ObjectPtr>& anObjs = aMsg->objects();
164     std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
165     for(; anObjIter != anObjs.cend(); anObjIter++) {
166       if (std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures())
167         ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kUpdatedEvent);
168     }
169     return;
170   }
171   if (theMessage->eventID() == kUpdatedEvent) {
172     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
173         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
174     const std::set<ObjectPtr>& anObjs = aMsg->objects();
175     std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
176     bool aSomeModified = false; // check that features not changed: only redisplay is needed
177     for(; anObjIter != anObjs.cend(); anObjIter++) {
178       if (!(*anObjIter)->data()->isValid())
179         continue;
180 #ifdef DEB_UPDATE
181       std::cout<<">>> in event updated "<<(*anObjIter)->data()->name()<<std::endl;
182 #endif
183       if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
184         myIsParamUpdated = true;
185       }
186       // on undo/redo, abort do not update persisten features
187       bool anUpdateOnlyNotPersistent = 
188         !std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures();
189       FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
190       if (anUpdated.get()) {
191         if (!anUpdateOnlyNotPersistent || !anUpdated->isPersistentResult()) {
192           addModified(anUpdated, FeaturePtr());
193           aSomeModified = true;
194         }
195       } else { // process the updated result as update of features that refers to this result
196         const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*anObjIter)->data()->refsToMe();
197         std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
198         for(; aRefIter != aRefs.cend(); aRefIter++) {
199           if (!(*aRefIter)->owner()->data()->isValid())
200             continue;
201           FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
202           if (anUpdated.get() && (!anUpdateOnlyNotPersistent || !anUpdated->isPersistentResult())) {
203             addModified(anUpdated, FeaturePtr());
204             aSomeModified = true;
205           }
206         }
207       }
208     }
209     // this event is for solver update, not here, do not react immediately
210     if (aSomeModified) {
211         processFeatures();
212     }
213   } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
214       theMessage->eventID() == kOpStartEvent) {
215
216     if (theMessage->eventID() == kOpFinishEvent) {
217       myIsFinish = true;
218       // add features that wait for finish as modified
219       std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aFeature = myProcessOnFinish.begin();
220       for(; aFeature != myProcessOnFinish.end(); aFeature++)
221         if ((*aFeature)->data()->isValid()) // there may be already removed wait for features
222           addModified(*aFeature, FeaturePtr());
223       myIsFinish = false;
224     }
225     myProcessOnFinish.clear(); // processed features must be only on finish, so clear anyway (to avoid reimport on load)
226
227     if (!(theMessage->eventID() == kOpStartEvent)) {
228       processFeatures();
229     }
230     // remove all macros before clearing all created
231     std::set<FeaturePtr>::iterator anUpdatedIter = myWaitForFinish.begin();
232     while(anUpdatedIter != myWaitForFinish.end()) {
233       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
234       if (aFeature.get()) {
235         // remove macro on finish
236         if (aFeature->isMacro()) {
237           aFeature->document()->removeFeature(aFeature);
238           myWaitForFinish.erase(aFeature);
239         }
240         // to avoid the map update problems on "remove"
241         if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
242           anUpdatedIter = myWaitForFinish.begin();
243         } else {
244           anUpdatedIter++;
245         }
246       } else {
247         anUpdatedIter++;
248       }
249     }
250     // in the end of transaction everything is updated, so clear the old objects
251     myIsParamUpdated = false;
252     myWaitForFinish.clear();
253   }
254 }
255
256 void Model_Update::processFeatures()
257 {
258   if (!myIsProcessed) { // perform update of everything if it is not performed right now
259     myIsProcessed = true;
260     #ifdef DEB_UPDATE
261       std::cout<<"****** Start processing"<<std::endl;
262     #endif
263
264     while(!myModified.empty()) {
265       processFeature(myModified.begin()->first);
266     }
267     myIsProcessed = false;
268
269     // flush updates just before "myModification" increment: to distinguish
270     // updates by "execute" produced by this updater and other updates, coming outside,
271     // which are really important for "processEvent" of this updater
272     static Events_Loop* aLoop = Events_Loop::loop();
273     static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
274     aLoop->flush(kUpdatedEvent);
275
276     // flush to update display
277     static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
278     aLoop->flush(EVENT_DISP);
279     #ifdef DEB_UPDATE
280       std::cout<<"****** End processing"<<std::endl;
281     #endif
282   }
283 }
284
285 bool Model_Update::processFeature(FeaturePtr theFeature)
286 {
287   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
288
289   if (!theFeature->data()->isValid()) { // deleted feature, just remove from all containers
290     if (myModified.find(theFeature) != myModified.end())
291       myModified.erase(theFeature);
292     return false;
293   }
294
295   // check this feature is not yet checked or processed
296   bool aIsModified = myModified.find(theFeature) != myModified.end();
297   if (!aIsModified && myIsFinish) { // get info about the modification for features without preview
298     if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
299       aIsModified = true;
300       std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
301       aNewSet.insert(theFeature); // contains itself, so, we don't know which was the reason and the reason is any
302       myModified[theFeature] = aNewSet;
303     }
304   }
305
306 #ifdef DEB_UPDATE
307     std::cout<<"* Process feature "<<theFeature->name()<<std::endl;
308 #endif
309
310   // update the sketch plane before the sketch sub-elements are recomputed
311   // (otherwise sketch will update plane, modify subs, after executed, but with old subs edges)
312   if (aIsModified && theFeature->getKind() == "Sketch") {
313 #ifdef DEB_UPDATE
314     std::cout<<"****** Update sketch args "<<theFeature->name()<<std::endl;
315 #endif
316     AttributeSelectionPtr anExtSel = theFeature->selection("External");
317     if (anExtSel.get()) {
318       ResultPtr aContext = anExtSel->context();
319       if (aContext.get() && aContext->document().get()) {
320         FeaturePtr anExtBase = aContext->document()->feature(aContext);
321         if (anExtBase.get()) {
322           processFeature(anExtBase);
323         }
324       }
325     }
326     updateArguments(theFeature);
327   }
328
329   if (!aIsModified) { // no modification is needed
330     return false;
331   }
332
333   // check all features this feature depended on (recursive call of updateFeature)
334   std::set<std::shared_ptr<ModelAPI_Feature> >& aReasons = myModified[theFeature];
335   if (aReasons.find(theFeature) == aReasons.end()) {
336     std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aReasonIter = aReasons.begin();
337     for(; aReasonIter != aReasons.end(); aReasonIter++) {
338       if (*aReasonIter != theFeature && processFeature(*aReasonIter)) {
339         aIsModified = true;
340       }
341     }
342   } else { // check all features this feature depended on because here which one was modified is unknown
343     std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aDeps;
344     theFeature->data()->referencesToObjects(aDeps);
345     std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > >::iterator
346       anAttrsIter = aDeps.begin();
347     for(; anAttrsIter != aDeps.end(); anAttrsIter++) {
348       std::list<std::shared_ptr<ModelAPI_Object> >::iterator aDepIter = anAttrsIter->second.begin();
349       for(; aDepIter != anAttrsIter->second.end(); aDepIter++) {
350         FeaturePtr aDepFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(*aDepIter);
351         if (!aDepFeat.get()) { // so, it depends on the result and process the feature owner of it
352           ResultPtr aDepRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aDepIter);
353           if (aDepRes.get()) {
354             aDepFeat = (*aDepIter)->document()->feature(aDepRes);
355           }
356         }
357         if (aDepFeat.get()) {
358           if (processFeature(aDepFeat))
359             aIsModified = true;
360         }
361       }
362     }
363     if (theFeature->getKind() == "Part") { // part is not depended on its subs directly, but subs must be iterated anyway
364       CompositeFeaturePtr aPart = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
365       int aNum = aPart->numberOfSubs();
366       for(int a = 0; a < aNum; a++) {
367         FeaturePtr aSub = aPart->subFeature(a);
368         if (aSub.get()) {
369           if (processFeature(aSub))
370             aIsModified = true;
371         }
372       }
373     }
374   }
375
376   // do not execute the composite that contains the current
377   bool isPostponedMain = false;
378   CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
379   if (theFeature->getKind() == "ExtrusionSketch" && aCompos.get()) {
380     CompositeFeaturePtr aCurrentOwner = 
381       ModelAPI_Tools::compositeOwner(theFeature->document()->currentFeature(false));
382     isPostponedMain = aCurrentOwner.get() && aCompos->isSub(aCurrentOwner);
383   }
384
385   #ifdef DEB_UPDATE
386     std::cout<<"Update args "<<theFeature->name()<<std::endl;
387   #endif
388   // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
389   updateArguments(theFeature);
390
391   // add this feature to the processed right now to be able remove it from this list on
392   // update signal during this feature execution
393   myModified.erase(theFeature);
394   if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
395     theFeature->data()->execState(ModelAPI_StateDone);
396
397   // this checking must be after the composite feature sub-elements processing:
398   // composite feature status may depend on it's subelements
399   if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
400     theFeature->eraseResults();
401     redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
402     return true; // so, feature is modified (results are erased)
403   }
404
405   // On abort, undo or redo execute is not needed: results in document are updated automatically
406   // But redisplay is needed: results are updated, must be also updated in the viewer.
407   if (!std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures()) {
408     if (!theFeature->isPersistentResult()) { // not persistent must be re-executed on abort, etc.
409       ModelAPI_ExecState aState = theFeature->data()->execState();
410       if (aFactory->validate(theFeature)) {
411         executeFeature(theFeature);
412       } else {
413         theFeature->eraseResults();
414         redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
415       }
416     } else {
417       redisplayWithResults(theFeature, ModelAPI_StateNothing);
418       if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) { // it is done (in the tree)
419         theFeature->data()->execState(ModelAPI_StateDone);
420       }
421     }
422   } else {
423     // execute feature if it must be updated
424     ModelAPI_ExecState aState = theFeature->data()->execState();
425     if (aFactory->validate(theFeature)) {
426       if (!isPostponedMain) {
427         executeFeature(theFeature);
428       }
429     } else {
430       #ifdef DEB_UPDATE
431         std::cout<<"Feature is not valid, erase results "<<theFeature->name()<<std::endl;
432       #endif
433       theFeature->eraseResults();
434       redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
435     }
436   }
437   return true;
438 }
439
440 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState) 
441 {
442   // make updated and redisplay all results
443   static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
444
445   std::list<ResultPtr> allResults;
446   ModelAPI_Tools::allResults(theFeature, allResults);
447   std::list<ResultPtr>::iterator aRIter = allResults.begin();
448   for (; aRIter != allResults.cend(); aRIter++) {
449     std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
450     if (!aRes->isDisabled()) {// update state only for enabled results (Placement Result Part may make the original Part Result as invalid)
451       aRes->data()->execState(theState);
452     }
453     if (theFeature->data()->updateID() > aRes->data()->updateID()) {
454       aRes->data()->setUpdateID(theFeature->data()->updateID());
455     }
456     ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
457   }
458   // to redisplay "presentable" feature (for ex. distance constraint)
459   ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
460   theFeature->data()->execState(theState);
461 }
462
463 /// Updates the state by the referenced object: if something bad with it, set state for this one
464 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
465 {
466   if (theTarget) {
467     ModelAPI_ExecState aRefState = theTarget->data()->execState();
468     if (aRefState == ModelAPI_StateMustBeUpdated) {
469       if (theCurrent == ModelAPI_StateDone)
470         return ModelAPI_StateMustBeUpdated;
471     } else if (aRefState != ModelAPI_StateDone) {
472       return ModelAPI_StateInvalidArgument;
473     }
474   }
475   return theCurrent;
476 }
477
478 void Model_Update::updateArguments(FeaturePtr theFeature) {
479   // perform this method also for disabled features: to make "not done" state for
480   // features referenced to the active and modified features
481
482   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
483
484   ModelAPI_ExecState aState = theFeature->data()->execState();
485   if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
486     aState = ModelAPI_StateMustBeUpdated;
487   }
488   if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
489     aState = ModelAPI_StateMustBeUpdated;
490   // check the parameters state
491   // Integer
492   {
493     std::list<AttributePtr> anAttrinbutes =
494       theFeature->data()->attributes(ModelAPI_AttributeInteger::typeId());
495     std::list<AttributePtr>::iterator anIter = anAttrinbutes.begin();
496     for(; anIter != anAttrinbutes.end(); anIter++) {
497       AttributeIntegerPtr anAttribute =
498         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(*anIter);
499       if (anAttribute.get() && !anAttribute->text().empty()) {
500         if (myIsParamUpdated) {
501           ModelAPI_AttributeEvalMessage::send(anAttribute, this);
502         }
503         if (anAttribute->expressionInvalid()) {
504           aState = ModelAPI_StateInvalidArgument;
505         }
506       }
507     }
508   }
509   // Double
510   {
511     std::list<AttributePtr> aDoubles =
512       theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
513     std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
514     for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
515       AttributeDoublePtr aDouble =
516         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
517       if (aDouble.get() && !aDouble->text().empty()) {
518         if (myIsParamUpdated) {
519           ModelAPI_AttributeEvalMessage::send(aDouble, this);
520         }
521         if (aDouble->expressionInvalid()) {
522           aState = ModelAPI_StateInvalidArgument;
523         }
524       }
525     }
526   }
527   // Point
528   {
529     std::list<AttributePtr> anAttributes =
530       theFeature->data()->attributes(GeomDataAPI_Point::typeId());
531     std::list<AttributePtr>::iterator anIter = anAttributes.begin();
532     for(; anIter != anAttributes.end(); anIter++) {
533       AttributePointPtr aPointAttribute =
534         std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
535       if (aPointAttribute.get()) {
536         if (myIsParamUpdated) {
537           ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
538         }
539         if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
540           (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
541           (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
542           aState = ModelAPI_StateInvalidArgument;
543       }
544     }
545   }
546   // Point2D
547   {
548     std::list<AttributePtr> anAttributes =
549       theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
550     std::list<AttributePtr>::iterator anIter = anAttributes.begin();
551     for(; anIter != anAttributes.end(); anIter++) {
552       AttributePoint2DPtr aPoint2DAttribute =
553         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
554       if (aPoint2DAttribute.get()) {
555         if (myIsParamUpdated) {
556           ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
557         }
558         if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
559           (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
560           aState = ModelAPI_StateInvalidArgument;
561       }
562     }
563   }
564   // update the selection attributes if any
565   list<AttributePtr> aRefs = 
566     theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
567   list<AttributePtr>::iterator aRefsIter = aRefs.begin();
568   for (; aRefsIter != aRefs.end(); aRefsIter++) {
569     std::shared_ptr<ModelAPI_AttributeSelection> aSel =
570       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
571     ObjectPtr aContext = aSel->context();
572     // update argument only if the referenced object is ready to use
573     if (aContext.get() && !aContext->isDisabled() && isReason(theFeature, aContext)) {
574       if (!aSel->update()) { // this must be done on execution since it may be long operation
575         bool isObligatory = !aFactory->isNotObligatory(
576           theFeature->getKind(), theFeature->data()->id(aSel)) &&
577           aFactory->isCase(theFeature, theFeature->data()->id(aSel));
578         if (isObligatory)
579           aState = ModelAPI_StateInvalidArgument;
580       }
581     }
582   }
583   // update the selection list attributes if any
584   aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
585   for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
586     std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
587       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
588     for(int a = aSel->size() - 1; a >= 0; a--) {
589       std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
590         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
591       if (aSelAttr) {
592         ObjectPtr aContext = aSelAttr->context();
593         // update argument only if the referenced object is ready to use
594         if (aContext.get() && !aContext->isDisabled() && isReason(theFeature, aContext)) {
595           if (!aSelAttr->update()) {
596             bool isObligatory = !aFactory->isNotObligatory(
597               theFeature->getKind(), theFeature->data()->id(aSel)) &&
598               aFactory->isCase(theFeature, theFeature->data()->id(aSel));
599             if (isObligatory)
600               aState = ModelAPI_StateInvalidArgument;
601           }
602         }
603       }
604     }
605   }
606
607   if (aState != ModelAPI_StateDone)
608     theFeature->data()->execState(aState);
609 }
610
611 bool Model_Update::isReason(std::shared_ptr<ModelAPI_Feature>& theFeature, 
612      std::shared_ptr<ModelAPI_Object> theReason) 
613 {
614   std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
615     ::iterator aReasonsIt = myModified.find(theFeature);
616   if (aReasonsIt == myModified.end())
617     return false; // this case only for not-previewed items update state, nothing is changed in args for it
618   if (aReasonsIt->second.find(theFeature) != aReasonsIt->second.end())
619     return true; // any is reason if it contains itself
620   FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
621   if (!aReasFeat.get()) { // try to get feature of this result
622     ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
623     if (aReasRes.get())
624       aReasFeat = theReason->document()->feature(aReasRes);
625   }
626   return aReasonsIt->second.find(aReasFeat) != aReasonsIt->second.end();
627
628 }
629
630 void Model_Update::executeFeature(FeaturePtr theFeature)
631 {
632 #ifdef DEB_UPDATE
633   std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
634 #endif
635   // execute in try-catch to avoid internal problems of the feature
636   ModelAPI_ExecState aState = ModelAPI_StateDone;
637   theFeature->data()->execState(ModelAPI_StateDone);
638   try {
639     theFeature->execute();
640     if (theFeature->data()->execState() != ModelAPI_StateDone) {
641       aState = ModelAPI_StateExecFailed;
642     } else {
643       aState = ModelAPI_StateDone;
644     }
645   } catch(...) {
646     aState = ModelAPI_StateExecFailed;
647     Events_Error::send(
648       "Feature " + theFeature->getKind() + " has failed during the execution");
649   }
650   // The macro feature has to be deleted in any case even its execution is failed 
651   myWaitForFinish.insert(theFeature);
652   if (aState != ModelAPI_StateDone) {
653     theFeature->eraseResults();
654   }
655   theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
656   redisplayWithResults(theFeature, aState);
657 }
658
659 void Model_Update::updateStability(void* theSender)
660 {
661   if (theSender) {
662     bool added = false; // object may be was crated
663     ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theSender);
664     if (aSender && aSender->document()) {
665       FeaturePtr aFeatureSender = 
666         std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
667       if (aFeatureSender.get()) {
668         Model_Objects* aDocObjects = 
669           std::dynamic_pointer_cast<Model_Document>(aSender->document())->objects();
670         if (aDocObjects) {
671           //aDocObjects->synchronizeBackRefs();
672           // remove or add all concealment refs from this feature
673           std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
674           aSender->data()->referencesToObjects(aRefs);
675           std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIt = aRefs.begin();
676           for(; aRefIt != aRefs.end(); aRefIt++) {
677             std::list<ObjectPtr>& aRefFeaturesList = aRefIt->second;
678             std::list<ObjectPtr>::iterator aReferenced = aRefFeaturesList.begin();
679             for(; aReferenced != aRefFeaturesList.end(); aReferenced++) {
680                // stability is only on results: feature to feature reference mean nested 
681               // features, that will remove nesting references
682               if (aReferenced->get() && (*aReferenced)->data()->isValid() && 
683                 (*aReferenced)->groupName() != ModelAPI_Feature::group()) {
684                 std::shared_ptr<Model_Data> aData = 
685                   std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
686                 if (aFeatureSender->isStable()) {
687                   aData->addBackReference(aFeatureSender, aRefIt->first);
688                 } else {
689                   aData->removeBackReference(aFeatureSender, aRefIt->first);
690                   added = true; // remove of concealment may be caused creation of some result
691                 }
692               }
693             }
694           }
695         }
696       }
697     }
698     if (added) {
699       static Events_Loop* aLoop = Events_Loop::loop();
700       static Events_ID kEventCreated = aLoop->eventByName(EVENT_OBJECT_CREATED);
701       aLoop->flush(kEventCreated);
702     }
703   }
704 }