Salome HOME
Initial implementation of support of any level of hierarchy in Result Bodies.
[modules/shaper.git] / src / Model / Model_Update.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <Model_Update.h>
22 #include <Model_Document.h>
23 #include <Model_Data.h>
24 #include <Model_Objects.h>
25 #include <Model_AttributeSelection.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_Events.h>
30 #include <ModelAPI_AttributeReference.h>
31 #include <ModelAPI_AttributeRefList.h>
32 #include <ModelAPI_AttributeRefAttr.h>
33 #include <ModelAPI_AttributeSelection.h>
34 #include <ModelAPI_AttributeSelectionList.h>
35 #include <ModelAPI_Result.h>
36 #include <ModelAPI_ResultPart.h>
37 #include <ModelAPI_Validator.h>
38 #include <ModelAPI_CompositeFeature.h>
39 #include <ModelAPI_Session.h>
40 #include <ModelAPI_Tools.h>
41 #include <GeomAPI_Shape.h>
42 #include <GeomDataAPI_Point.h>
43 #include <GeomDataAPI_Point2D.h>
44 #include <Events_Loop.h>
45 #include <Events_LongOp.h>
46 #include <Events_InfoMessage.h>
47 #include <Config_PropManager.h>
48
49 Model_Update MY_UPDATER_INSTANCE;  /// the only one instance initialized on load of the library
50 //#define DEB_UPDATE
51
52 Model_Update::Model_Update()
53 {
54   Events_Loop* aLoop = Events_Loop::loop();
55   static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
56   aLoop->registerListener(this, kChangedEvent);
57   static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
58   aLoop->registerListener(this, kCreatedEvent);
59   static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
60   aLoop->registerListener(this, kUpdatedEvent);
61   static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
62   aLoop->registerListener(this, kOpFinishEvent);
63   static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
64   aLoop->registerListener(this, kOpAbortEvent);
65   static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
66   aLoop->registerListener(this, kOpStartEvent);
67   static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
68   aLoop->registerListener(this, kStabilityEvent);
69   static const Events_ID kPreviewBlockedEvent = aLoop->eventByName(EVENT_PREVIEW_BLOCKED);
70   aLoop->registerListener(this, kPreviewBlockedEvent);
71   static const Events_ID kPreviewRequestedEvent = aLoop->eventByName(EVENT_PREVIEW_REQUESTED);
72   aLoop->registerListener(this, kPreviewRequestedEvent);
73   static const Events_ID kReorderEvent = aLoop->eventByName(EVENT_ORDER_UPDATED);
74   aLoop->registerListener(this, kReorderEvent);
75   static const Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
76   aLoop->registerListener(this, kUpdatedSel);
77
78   //  Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
79   myIsParamUpdated = false;
80   myIsFinish = false;
81   myIsProcessed = false;
82   myIsPreviewBlocked = false;
83 }
84
85 bool Model_Update::addModified(FeaturePtr theFeature, FeaturePtr theReason) {
86
87   if (!theFeature->data()->isValid())
88     return false; // delete an extrusion created on the sketch
89
90
91   bool isNotExecuted = theFeature->isPersistentResult() &&
92     !std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures();
93   if (isNotExecuted) {
94     redisplayWithResults(theFeature, ModelAPI_StateNothing, false); // redisplay even not executed
95     if (!theReason.get()) // no reason => no construction reason
96       return false;
97     if (myNotPersistentRefs.find(theFeature) == myNotPersistentRefs.end()) {
98       myNotPersistentRefs[theFeature].insert(theReason);
99     } else {
100       std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
101       aNewSet.insert(theReason);
102       myNotPersistentRefs[theFeature] = aNewSet;
103     }
104     return false;
105   }
106
107   // update arguments for "apply button" state change
108   if ((!theFeature->isPreviewNeeded() && !myIsFinish) || myIsPreviewBlocked) {
109     if (theReason.get())
110       myProcessOnFinish[theFeature].insert(theReason);
111     else if (myProcessOnFinish.find(theFeature) == myProcessOnFinish.end())
112       myProcessOnFinish[theFeature] = std::set<std::shared_ptr<ModelAPI_Feature> >();
113 #ifdef DEB_UPDATE
114       std::cout<<"*** Add process on finish "<<theFeature->name()<<std::endl;
115 #endif
116     // keeps the currently updated features to avoid infinitive cycling here: where feature on
117     // "updateArguments" sends "updated" (in selection attribute) and goes here again
118     static std::set<FeaturePtr> aCurrentlyUpdated;
119     if (aCurrentlyUpdated.find(theFeature) == aCurrentlyUpdated.end()) {
120       aCurrentlyUpdated.insert(theFeature);
121       updateArguments(theFeature);
122       aCurrentlyUpdated.erase(theFeature);
123     }
124     // make it without conditions otherwise the apply button may have a bad state
125     theFeature->data()->execState(ModelAPI_StateDone);
126     static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
127      // need to be validated to update the "Apply" state if not previewed
128     aFactory->validate(theFeature);
129
130     // to redisplay split's arguments presentation, even result is not computed
131     if (!theFeature->isPreviewNeeded()) {
132       static Events_Loop* aLoop = Events_Loop::loop();
133       static const Events_ID kRedisplayEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
134       ModelAPI_EventCreator::get()->sendUpdated(theFeature, kRedisplayEvent);
135       aLoop->flush(kRedisplayEvent);
136     }
137
138     if (!myIsPreviewBlocked)
139       return true;
140   }
141   if (myModified.find(theFeature) != myModified.end()) {
142     if (theReason.get()) {
143 #ifdef DEB_UPDATE
144       //std::cout<<"*** Add already modified "
145       //   <<theFeature->name()<<" reason "<<theReason->name()<<std::endl;
146 #endif
147       myModified[theFeature].insert(theReason);
148     }
149     return true;
150   }
151   // do not add the disabled, but possibly the sub-elements are not disabled
152   bool aIsDisabled = theFeature->isDisabled();
153   if (!aIsDisabled) {
154     std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
155     if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated ||
156         theFeature->data()->execState() == ModelAPI_StateInvalidArgument || // issue 1519
157         theFeature->data()->execState() == ModelAPI_StateExecFailed) {
158       // do not forget that in this case all were the reasons
159       aNewSet.insert(theFeature);
160     } else {
161       if (theReason.get())
162         aNewSet.insert(theReason);
163     }
164       myModified[theFeature] = aNewSet;
165 #ifdef DEB_UPDATE
166     if (theReason.get()) {
167       //std::cout<<"*** Add modified "<<theFeature->name()
168       //  <<" reason "<<theReason->name()<<std::endl;
169     } else {
170       //std::cout<<"*** Add modified "<<theFeature->name()<<std::endl;
171     }
172 #endif
173   } else { // will be updated during the finish of the operation, or when it becomes enabled
174     if (theFeature->data()->execState() == ModelAPI_StateDone ||
175         theFeature->data()->execState() == ModelAPI_StateExecFailed) // fix issue 1819
176       theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
177     else
178       return true; // do not need iteration deeply if it is already marked as modified or so
179 #ifdef DEB_UPDATE
180     //std::cout<<"*** Set modified state "<<theFeature->name()<<std::endl;
181 #endif
182   }
183   // clear processed and fill modified recursively
184   const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
185   std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
186   for(; aRefIter != aRefs.cend(); aRefIter++) {
187     if ((*aRefIter)->isArgument()) {
188       FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
189       if (aReferenced.get()) {
190         addModified(aReferenced, theFeature);
191       }
192     }
193   }
194   // proccess also results
195   std::list<ResultPtr> allResults; // list of this feature and results
196   ModelAPI_Tools::allResults(theFeature, allResults);
197   std::list<ResultPtr>::iterator aRes = allResults.begin();
198   for(; aRes != allResults.end(); aRes++) {
199     const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*aRes)->data()->refsToMe();
200     std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
201     for(; aRefIter != aRefs.cend(); aRefIter++) {
202       if ((*aRefIter)->isArgument()) {
203         FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
204         if (aReferenced.get()) {
205           addModified(aReferenced, theFeature);
206         }
207       }
208     }
209   }
210
211   // also add part feature that contains this feature to the modified
212   if (theFeature->document()->kind() != "PartSet") {
213     FeaturePtr aPart = ModelAPI_Tools::findPartFeature(
214       ModelAPI_Session::get()->moduleDocument(), theFeature->document());
215     if (aPart.get())
216       addModified(aPart, theFeature);
217   }
218   return true;
219 }
220
221 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
222 {
223   static Events_Loop* aLoop = Events_Loop::loop();
224   static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
225   static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
226   static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
227   static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
228   static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
229   static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
230   static const Events_ID kPreviewBlockedEvent = aLoop->eventByName(EVENT_PREVIEW_BLOCKED);
231   static const Events_ID kPreviewRequestedEvent = aLoop->eventByName(EVENT_PREVIEW_REQUESTED);
232   static const Events_ID kReorderEvent = aLoop->eventByName(EVENT_ORDER_UPDATED);
233   static const Events_ID kRedisplayEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
234   static const Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
235
236 #ifdef DEB_UPDATE
237   std::cout<<"****** Event "<<theMessage->eventID().eventText()<<std::endl;
238 #endif
239   if (theMessage->eventID() == kStabilityEvent) {
240     updateStability(theMessage->sender());
241     return;
242   }
243   if (theMessage->eventID() == kPreviewBlockedEvent) {
244     myIsPreviewBlocked = true;
245     return;
246   }
247   if (theMessage->eventID() == kPreviewRequestedEvent) {
248     if (myIsPreviewBlocked) {
249       myIsPreviewBlocked = false;
250       processFeatures();
251       myIsPreviewBlocked = true;
252     }
253     return;
254   }
255   if (theMessage->eventID() == kUpdatedSel) {
256     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
257         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
258     updateSelection(aMsg->objects());
259   }
260   // creation is added to "update" to avoid recomputation twice:
261   // on create and immediately after on update
262   if (theMessage->eventID() == kCreatedEvent) {
263     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
264         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
265     const std::set<ObjectPtr>& anObjs = aMsg->objects();
266     std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
267     for(; anObjIter != anObjs.cend(); anObjIter++) {
268       if (std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures()) {
269         if ((*anObjIter)->groupName() == ModelAPI_Feature::group()) {
270           // results creation means enabling, not update
271           ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kUpdatedEvent);
272         } else {
273           ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kRedisplayEvent);
274         }
275       }
276     }
277     return;
278   }
279   if (theMessage->eventID() == kUpdatedEvent) {
280     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
281         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
282     const std::set<ObjectPtr>& anObjs = aMsg->objects();
283     std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
284     bool aSomeModified = false; // check that features not changed: only redisplay is needed
285     for(; anObjIter != anObjs.cend(); anObjIter++) {
286       if (!(*anObjIter)->data()->isValid())
287         continue;
288 #ifdef DEB_UPDATE
289       std::cout<<">>> in event updated "<<(*anObjIter)->groupName()<<
290         " "<<(*anObjIter)->data()->name()<<std::endl;
291 #endif
292       if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
293         myIsParamUpdated = true;
294       }
295       // on undo/redo, abort do not update persisten features
296       FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
297       if (anUpdated.get()) {
298         if (addModified(anUpdated, FeaturePtr()))
299           aSomeModified = true;
300       } else {
301         // process the updated result as update of features that refers to this result
302         const std::set<std::shared_ptr<ModelAPI_Attribute> >&
303           aRefs = (*anObjIter)->data()->refsToMe();
304         std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
305         FeaturePtr aReason;
306         ResultPtr aReasonResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIter);
307         if (aReasonResult.get())
308           aReason = (*anObjIter)->document()->feature(aReasonResult);
309         for(; aRefIter != aRefs.cend(); aRefIter++) {
310           if (!(*aRefIter)->owner()->data()->isValid())
311             continue;
312           FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
313           if (anUpdated.get()) {
314             if (addModified(anUpdated, aReason))
315               aSomeModified = true;
316           }
317         }
318       }
319     }
320     // this event is for solver update, not here, do not react immediately
321     if (aSomeModified) {
322         processFeatures();
323     }
324   } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
325       theMessage->eventID() == kOpStartEvent) {
326     myIsPreviewBlocked = false;
327
328     if (theMessage->eventID() == kOpFinishEvent) {
329       myIsFinish = true;
330       // add features that wait for finish as modified
331       std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >::
332         iterator aFeature = myProcessOnFinish.begin();
333       for(; aFeature != myProcessOnFinish.end(); aFeature++) {
334         if (aFeature->first->data()->isValid()) {// there may be already removed while wait
335           if (aFeature->second.empty()) {
336             addModified(aFeature->first, FeaturePtr());
337             continue;
338           }
339           std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aReasons;
340           for(aReasons = aFeature->second.begin(); aReasons != aFeature->second.end(); aReasons++) {
341             addModified(aFeature->first, *aReasons);
342           }
343         }
344       }
345       myIsFinish = false;
346     }
347     // processed features must be only on finish, so clear anyway (to avoid reimport on load)
348     myProcessOnFinish.clear();
349
350     // #2156: current must be sketch, left after the macro execution
351     DocumentPtr anActiveDoc = ModelAPI_Session::get()->activeDocument();
352     FeaturePtr aCurrent;
353     if (anActiveDoc.get())
354       aCurrent = anActiveDoc->currentFeature(false);
355
356     if (!(theMessage->eventID() == kOpStartEvent)) {
357       processFeatures(false);
358     }
359
360     if (anActiveDoc.get() && aCurrent.get() && aCurrent->data()->isValid()) {
361       if (anActiveDoc->currentFeature(false) != aCurrent)
362         anActiveDoc->setCurrentFeature(aCurrent, false); // #2156 make the current feature back
363     }
364
365
366     // remove all macros before clearing all created
367     std::set<FeaturePtr>::iterator anUpdatedIter = myWaitForFinish.begin();
368     while(anUpdatedIter != myWaitForFinish.end()) {
369       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
370       if (aFeature.get()) {
371         // remove macro on finish
372         if (aFeature->isMacro()) {
373           aFeature->document()->removeFeature(aFeature);
374           myWaitForFinish.erase(aFeature);
375         }
376         // to avoid the map update problems on "remove"
377         if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
378           anUpdatedIter = myWaitForFinish.begin();
379         } else {
380           anUpdatedIter++;
381         }
382       } else {
383         anUpdatedIter++;
384       }
385     }
386     // the redisplay signal should be flushed in order
387     // to erase the feature presentation in the viewer
388     // if should be done after removeFeature() of document,
389     // by this reason, upper processFeatures() do not perform this flush
390     Events_Loop::loop()->flush(kRedisplayEvent);
391
392     // in the end of transaction everything is updated, so clear the old objects
393     myIsParamUpdated = false;
394     myWaitForFinish.clear();
395   } else if (theMessage->eventID() == kReorderEvent) {
396     std::shared_ptr<ModelAPI_OrderUpdatedMessage> aMsg =
397       std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
398     if (aMsg->reordered().get())
399       addModified(aMsg->reordered(), aMsg->reordered()); // to update all attributes
400   }
401 }
402
403 void Model_Update::processFeatures(const bool theFlushRedisplay)
404 {
405    // perform update of everything if it is not performed right now or any preview is blocked
406   if (!myIsProcessed && !myIsPreviewBlocked) {
407     myIsProcessed = true;
408     #ifdef DEB_UPDATE
409       std::cout<<"****** Start processing"<<std::endl;
410     #endif
411
412     while(!myModified.empty()) {
413       processFeature(myModified.begin()->first);
414     }
415     myIsProcessed = false;
416
417     // to update the object browser if something is updated/created during executions
418     static Events_Loop* aLoop = Events_Loop::loop();
419     static const Events_ID kCreatedEvent= aLoop->eventByName(EVENT_OBJECT_CREATED);
420     aLoop->flush(kCreatedEvent);
421     static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
422     aLoop->flush(kUpdatedEvent);
423
424     // flush to update display
425     if (theFlushRedisplay) {
426       static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
427       aLoop->flush(EVENT_DISP);
428     }
429     #ifdef DEB_UPDATE
430       std::cout<<"****** End processing"<<std::endl;
431     #endif
432     myProcessed.clear();
433   }
434 }
435
436 // collects all the feautres this feature depends on: reasons
437 static void allReasons(FeaturePtr theFeature, std::set<FeaturePtr>& theReasons) {
438   std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aDeps;
439   theFeature->data()->referencesToObjects(aDeps);
440   std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > >::iterator
441     anAttrsIter = aDeps.begin();
442   for(; anAttrsIter != aDeps.end(); anAttrsIter++) {
443     if (theFeature->attribute(anAttrsIter->first)->isArgument()) {
444       std::list<std::shared_ptr<ModelAPI_Object> >::iterator aDepIter = anAttrsIter->second.begin();
445       for(; aDepIter != anAttrsIter->second.end(); aDepIter++) {
446         FeaturePtr aDepFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(*aDepIter);
447         if (!aDepFeat.get()) { // so, it depends on the result and process the feature owner of it
448           ResultPtr aDepRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aDepIter);
449           if (aDepRes.get()) {
450             aDepFeat = (*aDepIter)->document()->feature(aDepRes);
451           }
452         }
453         if (aDepFeat.get() && aDepFeat->data()->isValid()) {
454           theReasons.insert(aDepFeat);
455         }
456       }
457     }
458   }
459   if (theFeature->getKind() == "Part") {
460     // part is not depended on its subs directly, but subs must be iterated anyway
461     CompositeFeaturePtr aPart = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
462     int aNum = aPart->numberOfSubs();
463     for(int a = 0; a < aNum; a++) {
464       FeaturePtr aSub = aPart->subFeature(a);
465       if (aSub.get() && aSub->data()->isValid()) {
466         theReasons.insert(aSub);
467       }
468     }
469   }
470 }
471
472 bool Model_Update::processFeature(FeaturePtr theFeature)
473 {
474   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
475
476   if (!theFeature->data()->isValid()) { // deleted feature, just remove from all containers
477     if (myModified.find(theFeature) != myModified.end())
478       myModified.erase(theFeature);
479     return false;
480   }
481
482   if (theFeature->isPersistentResult()) {
483     if (!std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures())
484       return false;
485   }
486
487   if (myProcessed.find(theFeature) == myProcessed.end()) {
488     myProcessed[theFeature] = 0;
489   } else {
490     int aCount = myProcessed[theFeature];
491     if (aCount > 100) {
492       // too many repetition of processing (in VS it may crash on 330 with stack overflow)
493       Events_InfoMessage("Model_Update",
494         "Feature '%1' is updated in infinitive loop").arg(theFeature->data()->name()).send();
495       // to stop iteration
496       myModified.clear();
497       return false;
498     }
499     myProcessed[theFeature] = aCount + 1;
500   }
501
502   // check this feature is not yet checked or processed
503   bool aIsModified = myModified.find(theFeature) != myModified.end();
504   if (!aIsModified && myIsFinish) { // get info about the modification for features without preview
505     if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
506       aIsModified = true;
507       std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
508       // contains itself, so, we don't know which was the reason and the reason is any
509       aNewSet.insert(theFeature);
510       myModified[theFeature] = aNewSet;
511     }
512   }
513
514 #ifdef DEB_UPDATE
515     std::cout<<"* Process feature "<<theFeature->name()<<std::endl;
516 #endif
517
518   // update the sketch plane before the sketch sub-elements are recomputed
519   // (otherwise sketch will update plane, modify subs, after executed, but with old subs edges)
520   if (aIsModified && theFeature->getKind() == "Sketch") {
521 #ifdef DEB_UPDATE
522     std::cout<<"****** Update sketch args "<<theFeature->name()<<std::endl;
523 #endif
524     AttributeSelectionPtr anExtSel = theFeature->selection("External");
525     if (anExtSel.get()) {
526       ResultPtr aContext = anExtSel->context();
527       if (aContext.get() && aContext->document().get()) {
528         FeaturePtr anExtBase = aContext->document()->feature(aContext);
529         if (anExtBase.get()) {
530           processFeature(anExtBase);
531         }
532       }
533     }
534     std::shared_ptr<GeomAPI_Shape> aShapeBefore = anExtSel->value();
535     if (!aShapeBefore.get() && anExtSel->context()) aShapeBefore = anExtSel->context()->shape();
536     updateArguments(theFeature);
537     std::shared_ptr<GeomAPI_Shape> aShapeAfter = anExtSel->value();
538     if (!aShapeAfter.get() && anExtSel->context()) aShapeAfter = anExtSel->context()->shape();
539     // if selected plane is changed, try to re-take external references of all subs of the sketch
540     if (aShapeBefore.get() && !aShapeBefore->isEqual(aShapeAfter)) {
541       std::set<FeaturePtr> aWholeR;
542       allReasons(theFeature, aWholeR);
543       std::set<FeaturePtr>::iterator aRIter = aWholeR.begin();
544       for(; aRIter != aWholeR.end(); aRIter++) {
545         if ((*aRIter)->data()->selection("External"))
546           (*aRIter)->attributeChanged("External");
547       }
548     }
549   }
550
551   if (!aIsModified) { // no modification is needed
552     return false;
553   }
554
555   // evaluate parameter before the sub-elements update:
556   // it updates dependencies on evaluation (#1085)
557   if (theFeature->getKind() == "Parameter") {
558     theFeature->execute();
559   }
560
561   bool isReferencedInvalid = false;
562   // check all features this feature depended on (recursive call of updateFeature)
563   std::set<FeaturePtr>& aReasons = myModified[theFeature];
564   bool allSubsUsed = aReasons.find(theFeature) != aReasons.end();
565   if (allSubsUsed) {
566     // add all subs in aReasons and temporary remove "theFeature" to avoid processing itself
567     allReasons(theFeature, aReasons);
568     aReasons.erase(theFeature);
569   }
570   // take reasons one by one (they may be added during the feature process
571   // (circle by the radius of sketch)
572   std::set<FeaturePtr> aProcessedReasons;
573   while(!aReasons.empty()) {
574     FeaturePtr aReason = *(aReasons.begin());
575 #ifdef DEB_UPDATE
576     //cout<<theFeature->name()<<" process next reason "<<aReason->name()<<endl;
577 #endif
578     if (aReason != theFeature && (aReason)->data()->isValid()) {
579       if (processFeature(aReason))
580         aIsModified = true;
581       // check validity of aReason once again because it may be removed by dependent feature
582       // (e.g. by SketchPlugin_IntersectionPoint)
583       if (!aReason->data()->isValid() ||
584           aReason->data()->execState() == ModelAPI_StateInvalidArgument)
585         isReferencedInvalid = true;
586     }
587     // searching for the next not used reason
588     aProcessedReasons.insert(aReason);
589     aReasons.erase(aReason);
590   }
591   // restore the modified reasons: they will be used in the update of arguments
592   if (allSubsUsed) { // restore theFeature in this set
593     aProcessedReasons.insert(theFeature);
594   }
595   myModified[theFeature] = aProcessedReasons;
596
597   // do not execute the composite that contains the current
598   bool isPostponedMain = false;
599   CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
600   if (theFeature->getKind() == "ExtrusionSketch" && aCompos.get()) {
601     CompositeFeaturePtr aCurrentOwner =
602       ModelAPI_Tools::compositeOwner(theFeature->document()->currentFeature(false));
603     isPostponedMain = aCurrentOwner.get() && aCompos->isSub(aCurrentOwner);
604   } else if (theFeature->getKind() == "Sketch" &&
605     std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures()) {
606     // send event that sketch is prepared to be recomputed
607     static Events_ID anID = Events_Loop::eventByName("SketchPrepared");
608     std::shared_ptr<Events_Message> aMsg(new Events_Message(anID, this));
609     Events_Loop* aLoop = Events_Loop::loop();
610     // in case it is finish operation, flush for the sketch other events (#2450)
611     aLoop->flush(aLoop->eventByName(EVENT_OBJECT_UPDATED));
612     aLoop->send(aMsg);
613     // check that sub-elements of sketch are updated => sketch must be re-processed
614     std::set<FeaturePtr> aWholeR;
615     allReasons(theFeature, aWholeR);
616     std::set<FeaturePtr>::iterator aRIter = aWholeR.begin();
617     for(; aRIter != aWholeR.end(); aRIter++) {
618       if (myModified.find(*aRIter) != myModified.end()) {
619         processFeature(theFeature);
620         return true;
621       }
622     }
623   }
624
625 #ifdef DEB_UPDATE
626   std::cout<<"Update args "<<theFeature->name()<<std::endl;
627 #endif
628   // TestImport.py : after arguments are updated, theFeature may be removed
629   if (!theFeature->data()->isValid())
630     return false;
631   // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
632   updateArguments(theFeature);
633
634   // add this feature to the processed right now to be able remove it from this list on
635   // update signal during this feature execution
636   myModified.erase(theFeature);
637   if (myNotPersistentRefs.find(theFeature) != myNotPersistentRefs.end())
638     myNotPersistentRefs.erase(theFeature);
639   if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
640     theFeature->data()->execState(ModelAPI_StateDone);
641
642   // this checking must be after the composite feature sub-elements processing:
643   // composite feature status may depend on it's subelements
644   if ((theFeature->data()->execState() == ModelAPI_StateInvalidArgument || isReferencedInvalid) &&
645     theFeature->getKind() != "Part") {
646       // don't disable Part because it will make disabled all the features
647       // (performance and problems with the current feature)
648   #ifdef DEB_UPDATE
649     std::cout<<"Invalid args "<<theFeature->name()<<std::endl;
650   #endif
651     theFeature->eraseResults(false);
652     redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
653     return true; // so, feature is modified (results are erased)
654   }
655
656   // execute feature if it must be updated
657   ModelAPI_ExecState aState = theFeature->data()->execState();
658   if (aFactory->validate(theFeature)) {
659     if (!isPostponedMain) {
660       executeFeature(theFeature);
661     }
662   } else {
663     #ifdef DEB_UPDATE
664       std::cout<<"Feature is not valid, erase results "<<theFeature->name()<<std::endl;
665     #endif
666     theFeature->eraseResults(false);
667     redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
668   }
669   return true;
670 }
671
672 void Model_Update::redisplayWithResults(
673   FeaturePtr theFeature, const ModelAPI_ExecState theState, bool theUpdateState)
674 {
675   // make updated and redisplay all results
676   static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
677
678   std::list<ResultPtr> allResults;
679   ModelAPI_Tools::allResults(theFeature, allResults);
680   std::list<ResultPtr>::iterator aRIter = allResults.begin();
681   for (; aRIter != allResults.cend(); aRIter++) {
682     std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
683     if (!aRes->isDisabled()) {
684       // update state only for enabled results
685       // (Placement Result Part may make the original Part Result as invalid)
686       if (theUpdateState)
687         aRes->data()->execState(theState);
688     }
689     if (theFeature->data()->updateID() > aRes->data()->updateID()) {
690       aRes->data()->setUpdateID(theFeature->data()->updateID());
691     }
692     ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
693   }
694   // to redisplay "presentable" feature (for ex. distance constraint)
695   ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
696   if (theUpdateState)
697     theFeature->data()->execState(theState);
698 }
699
700 /// Updates the state by the referenced object: if something bad with it, set state for this one
701 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
702 {
703   if (theTarget) {
704     ModelAPI_ExecState aRefState = theTarget->data()->execState();
705     if (aRefState == ModelAPI_StateMustBeUpdated) {
706       if (theCurrent == ModelAPI_StateDone)
707         return ModelAPI_StateMustBeUpdated;
708     } else if (aRefState != ModelAPI_StateDone) {
709       return ModelAPI_StateInvalidArgument;
710     }
711   }
712   return theCurrent;
713 }
714
715 void Model_Update::updateArguments(FeaturePtr theFeature) {
716   // perform this method also for disabled features: to make "not done" state for
717   // features referenced to the active and modified features
718   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
719
720   ModelAPI_ExecState aState = theFeature->data()->execState();
721   if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
722     aState = ModelAPI_StateMustBeUpdated;
723   }
724   if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
725     aState = ModelAPI_StateMustBeUpdated;
726   // check the parameters state
727   // Integer
728   {
729     std::list<AttributePtr> anAttrinbutes =
730       theFeature->data()->attributes(ModelAPI_AttributeInteger::typeId());
731     std::list<AttributePtr>::iterator anIter = anAttrinbutes.begin();
732     for(; anIter != anAttrinbutes.end(); anIter++) {
733       AttributeIntegerPtr anAttribute =
734         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(*anIter);
735       if (anAttribute.get() && !anAttribute->text().empty()) {
736         if (myIsParamUpdated) {
737           ModelAPI_AttributeEvalMessage::send(anAttribute, this);
738         }
739         if (anAttribute->expressionInvalid()) {
740           aState = ModelAPI_StateInvalidArgument;
741         }
742       }
743     }
744   }
745   // Double
746   {
747     std::list<AttributePtr> aDoubles =
748       theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
749     std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
750     for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
751       AttributeDoublePtr aDouble =
752         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
753       if (aDouble.get() && !aDouble->text().empty()) {
754         if (myIsParamUpdated) {
755           ModelAPI_AttributeEvalMessage::send(aDouble, this);
756         }
757         if (aDouble->expressionInvalid()) {
758           aState = ModelAPI_StateInvalidArgument;
759         }
760       }
761     }
762   }
763   // Point
764   {
765     std::list<AttributePtr> anAttributes =
766       theFeature->data()->attributes(GeomDataAPI_Point::typeId());
767     std::list<AttributePtr>::iterator anIter = anAttributes.begin();
768     for(; anIter != anAttributes.end(); anIter++) {
769       AttributePointPtr aPointAttribute =
770         std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
771       if (aPointAttribute.get() && (!aPointAttribute->textX().empty() ||
772           !aPointAttribute->textY().empty() || !aPointAttribute->textZ().empty())) {
773         if (myIsParamUpdated) {
774           ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
775         }
776         if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
777           (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
778           (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
779           aState = ModelAPI_StateInvalidArgument;
780       }
781     }
782   }
783   // Point2D
784   {
785     std::list<AttributePtr> anAttributes =
786       theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
787     std::list<AttributePtr>::iterator anIter = anAttributes.begin();
788     for(; anIter != anAttributes.end(); anIter++) {
789       AttributePoint2DPtr aPoint2DAttribute =
790         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
791       if (aPoint2DAttribute.get()) {
792         if (myIsParamUpdated && (!aPoint2DAttribute->textX().empty() ||
793             !aPoint2DAttribute->textY().empty())) {
794           ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
795         }
796         if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
797           (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
798           aState = ModelAPI_StateInvalidArgument;
799       }
800     }
801   }
802   // update the selection attributes if any
803   std::list<AttributePtr> aRefs =
804     theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
805   std::list<AttributePtr>::iterator aRefsIter = aRefs.begin();
806   for (; aRefsIter != aRefs.end(); aRefsIter++) {
807     std::shared_ptr<ModelAPI_AttributeSelection> aSel =
808       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
809     ObjectPtr aContext = aSel->context();
810     // update argument only if the referenced object is ready to use
811     if (aContext.get() && !aContext->isDisabled()) {
812       if (isReason(theFeature, aContext)) {
813         if (!aSel->update()) { // this must be done on execution since it may be long operation
814           bool isObligatory = !aFactory->isNotObligatory(
815             theFeature->getKind(), theFeature->data()->id(aSel)) &&
816             aFactory->isCase(theFeature, theFeature->data()->id(aSel));
817           if (isObligatory)
818             aState = ModelAPI_StateInvalidArgument;
819         }
820       }
821     } else if (aContext.get()) {
822       // here it may be not obligatory, but if the reference is wrong, it should not be correct
823       bool isObligatory = aFactory->isCase(theFeature, theFeature->data()->id(aSel));
824       if (isObligatory)
825         aState = ModelAPI_StateInvalidArgument;
826     }
827   }
828   // update the selection list attributes if any
829   aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
830   for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
831     std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
832       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
833     for(int a = aSel->size() - 1; a >= 0; a--) {
834       std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
835         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
836       if (aSelAttr) {
837         ObjectPtr aContext = aSelAttr->context();
838         // update argument only if the referenced object is ready to use
839         if (aContext.get() && !aContext->isDisabled() && !aSelAttr->isInvalid()) {
840           if (isReason(theFeature, aContext)) {
841             if (!aSelAttr->update()) {
842               bool isObligatory = !aFactory->isNotObligatory(
843                 theFeature->getKind(), theFeature->data()->id(aSel)) &&
844                 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
845               if (isObligatory)
846                 aState = ModelAPI_StateInvalidArgument;
847             }
848           }
849         } else if (aContext.get() || aSelAttr->isInvalid()) {
850           // here it may be not obligatory, but if the reference is wrong, it should not be correct
851           bool isObligatory = aFactory->isCase(theFeature, theFeature->data()->id(aSel));
852           if (isObligatory)
853             aState = ModelAPI_StateInvalidArgument;
854         }
855       }
856     }
857   }
858
859   if (aState != ModelAPI_StateDone)
860     theFeature->data()->execState(aState);
861 }
862
863 bool Model_Update::isReason(std::shared_ptr<ModelAPI_Feature>& theFeature,
864      std::shared_ptr<ModelAPI_Object> theReason)
865 {
866   std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
867     ::iterator aReasonsIt = myModified.find(theFeature);
868   if (aReasonsIt != myModified.end()) {
869     if (aReasonsIt->second.find(theFeature) != aReasonsIt->second.end())
870       return true; // any is reason if it contains itself
871     FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
872     if (!aReasFeat.get()) { // try to get feature of this result
873       ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
874       if (aReasRes.get())
875         aReasFeat = theReason->document()->feature(aReasRes);
876     }
877     if (aReasonsIt->second.find(aReasFeat) != aReasonsIt->second.end())
878       return true;
879   }
880   // another try: postponed modification by not-persistences
881   std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
882     ::iterator aNotPersist = myNotPersistentRefs.find(theFeature);
883   if (aNotPersist != myNotPersistentRefs.end()) {
884     FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
885     if (!aReasFeat.get()) { // try to get feature of this result
886       ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
887       if (aReasRes.get())
888         aReasFeat = theReason->document()->feature(aReasRes);
889     }
890     if (aNotPersist->second.find(aReasFeat) != aNotPersist->second.end())
891       return true;
892   }
893
894   // this case only for not-previewed items update state, nothing is changed in args for it
895   return false;
896 }
897
898 void Model_Update::executeFeature(FeaturePtr theFeature)
899 {
900 #ifdef DEB_UPDATE
901   std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
902 #endif
903   // execute in try-catch to avoid internal problems of the feature
904   ModelAPI_ExecState aState = ModelAPI_StateDone;
905   theFeature->data()->execState(ModelAPI_StateDone);
906   try {
907     theFeature->execute();
908     if (theFeature->data()->execState() != ModelAPI_StateDone) {
909       aState = ModelAPI_StateExecFailed;
910     } else {
911       aState = ModelAPI_StateDone;
912     }
913   } catch(...) {
914     aState = ModelAPI_StateExecFailed;
915     Events_InfoMessage("Model_Update",
916       "Feature %1 has failed during the execution").arg(theFeature->getKind()).send();
917   }
918   // The macro feature has to be deleted in any case even its execution is failed
919   myWaitForFinish.insert(theFeature);
920   if (aState != ModelAPI_StateDone) {
921     theFeature->eraseResults(false);
922   }
923   theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
924   redisplayWithResults(theFeature, aState);
925 }
926
927 void Model_Update::updateStability(void* theSender)
928 {
929   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
930   if (theSender) {
931     bool added = false; // object may be was crated
932     ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theSender);
933     if (aSender && aSender->document()) {
934       FeaturePtr aFeatureSender =
935         std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
936       if (aFeatureSender.get()) {
937         Model_Objects* aDocObjects =
938           std::dynamic_pointer_cast<Model_Document>(aSender->document())->objects();
939         if (aDocObjects) {
940           //aDocObjects->synchronizeBackRefs();
941           // remove or add all concealment refs from this feature
942           std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
943           aSender->data()->referencesToObjects(aRefs);
944           std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
945             aRefIt = aRefs.begin();
946           for(; aRefIt != aRefs.end(); aRefIt++) {
947             if (!aFactory->isConcealed(aFeatureSender->getKind(), aRefIt->first))
948               // take into account only concealed references
949               // (do not remove the sketch constraint and the edge on constraint edit)
950               continue;
951             std::list<ObjectPtr>& aRefFeaturesList = aRefIt->second;
952             std::list<ObjectPtr>::iterator aReferenced = aRefFeaturesList.begin();
953             for(; aReferenced != aRefFeaturesList.end(); aReferenced++) {
954                // stability is only on results: feature to feature reference mean nested
955               // features, that will remove nesting references
956               if (aReferenced->get() && (*aReferenced)->data()->isValid() &&
957                 (*aReferenced)->groupName() != ModelAPI_Feature::group()) {
958                 std::shared_ptr<Model_Data> aData =
959                   std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
960                 if (aFeatureSender->isStable()) {
961                   aData->addBackReference(aFeatureSender, aRefIt->first);
962                 } else {
963                   aData->removeBackReference(aFeatureSender, aRefIt->first);
964                   added = true; // remove of concealment may be caused creation of some result
965                 }
966               }
967             }
968           }
969         }
970       }
971     }
972     if (added) {
973       static Events_Loop* aLoop = Events_Loop::loop();
974       static Events_ID kEventCreated = aLoop->eventByName(EVENT_OBJECT_CREATED);
975       aLoop->flush(kEventCreated);
976     }
977   }
978 }
979
980 void Model_Update::updateSelection(const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects)
981 {
982   std::set<std::shared_ptr<ModelAPI_Object> >::iterator anObj = theObjects.begin();
983   for(; anObj != theObjects.end(); anObj++) {
984     std::list<AttributePtr> aRefs =
985       (*anObj)->data()->attributes(ModelAPI_AttributeSelection::typeId());
986     std::list<AttributePtr>::iterator aRefsIter = aRefs.begin();
987     for (; aRefsIter != aRefs.end(); aRefsIter++) {
988       std::shared_ptr<Model_AttributeSelection> aSel =
989         std::dynamic_pointer_cast<Model_AttributeSelection>(*aRefsIter);
990       aSel->updateInHistory();
991     }
992     // update the selection list attributes if any
993     aRefs = (*anObj)->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
994     for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
995       std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
996         std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
997       for(int a = aSel->size() - 1; a >= 0; a--) {
998         std::shared_ptr<Model_AttributeSelection> aSelAttr =
999           std::dynamic_pointer_cast<Model_AttributeSelection>(aSel->value(a));
1000         if (aSelAttr.get())
1001           aSelAttr->updateInHistory();
1002       }
1003     }
1004   }
1005 }