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