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