1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Update.cxx
4 // Created: 25 Jun 2014
5 // Author: Mikhail PONIKAROV
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>
35 Model_Update MY_UPDATER_INSTANCE; /// the only one instance initialized on load of the library
38 Model_Update::Model_Update()
40 Events_Loop* aLoop = Events_Loop::loop();
41 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
42 aLoop->registerListener(this, kChangedEvent);
43 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
44 aLoop->registerListener(this, kRebuildEvent);
45 static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
46 aLoop->registerListener(this, kCreatedEvent);
47 static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
48 aLoop->registerListener(this, kUpdatedEvent);
49 static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
50 aLoop->registerListener(this, kMovedEvent);
51 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
52 aLoop->registerListener(this, kOpFinishEvent);
53 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
54 aLoop->registerListener(this, kOpAbortEvent);
55 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
56 aLoop->registerListener(this, kOpStartEvent);
58 /* not needed now with history line
59 Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild immediately",
60 Config_Prop::Boolean, "false");*/
62 // Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
63 myIsParamUpdated = false;
68 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
70 static Events_Loop* aLoop = Events_Loop::loop();
71 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
72 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
73 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
74 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
75 static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
76 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
77 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
78 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
80 std::cout<<"****** Event "<<theMessage->eventID().eventText()<<std::endl;
82 if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
84 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
85 if (aPropVal != myIsAutomatic) { // something is changed
86 // myIsAutomatic = aPropVal;
87 if (myIsAutomatic) // higher level of automatization => to rebuild
88 processOperation(false);
91 } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
92 processOperation(true);
93 } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent ||
94 theMessage->eventID() == kMovedEvent) {
95 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
96 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
97 const std::set<ObjectPtr>& anObjs = aMsg->objects();
98 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
99 bool isOnlyResults = true; // check that only results were changed: only redisplay is needed
100 for(; anObjIter != anObjs.cend(); anObjIter++) {
101 if (!std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIter).get()) {
102 isOnlyResults = false;
104 if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
105 myIsParamUpdated = true;
107 // on undo/redo, abort do not update persisten features
108 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
109 if (std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures() ||
110 (anUpdated.get() && !anUpdated->isPersistentResult())) {
111 // created objects are always must be up to date (python box feature)
112 // and updated not in internal uptation chain
113 myUpdated[*anObjIter] = myModification;
115 // something is updated during the execution: re-execute it (sketch update by parameters or
116 // Box macro that updates the upper features during the execution)
118 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
119 if (anUpdated.get() && anUpdated->data()->isValid())
120 iterateUpdateBreak(anUpdated);
123 if (myIsExecuted) std::cout<<"During execution ";
124 if ((*anObjIter)->data() && (*anObjIter)->data()->isValid()) {
125 std::cout<<"add updated "<<(*anObjIter)->groupName()<<" "
126 <<(*anObjIter)->data()->name()<<std::endl;
132 // this event is for solver update, not here, do not react immediately
133 if (!isOnlyResults && !(theMessage->eventID() == kMovedEvent))
134 processOperation(false);
135 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
136 theMessage->eventID() == kOpStartEvent) {
138 if (!(theMessage->eventID() == kOpStartEvent)) {
140 processOperation(true, theMessage->eventID() == kOpFinishEvent);
143 // remove all macros before clearing all created
144 std::set<ObjectPtr>::iterator anUpdatedIter = myWaitForFinish.begin();
145 while(anUpdatedIter != myWaitForFinish.end()) {
146 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
147 if (aFeature.get()) {
148 // remove macro on finish
149 if (aFeature->isMacro()) {
150 aFeature->document()->removeFeature(aFeature);
151 myWaitForFinish.erase(aFeature);
153 // to avoid the map update problems on "remove"
154 if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
155 anUpdatedIter = myWaitForFinish.begin();
163 // in the end of transaction everything is updated, so clear the old objects (the only one
164 // place where results are cleared)
165 myIsParamUpdated = false;
168 myWaitForFinish.clear();
172 bool Model_Update::iterateUpdate(std::shared_ptr<ModelAPI_CompositeFeature> theFeature)
174 myProcessIterator.push_back(IterationItem(theFeature));
175 IterationItem& aCurrent = *myProcessIterator.rbegin();
176 // two cycles: parameters must be processed first
177 for(aCurrent.startIteration(true); aCurrent.more(); aCurrent.next()) {
178 if (aCurrent.current()->getKind() == "Parameter")
179 updateFeature(aCurrent.current());
181 // number of subs can be changed in execution: like fillet
182 for(aCurrent.startIteration(false); aCurrent.more(); aCurrent.next()) {
183 FeaturePtr aSub = aCurrent.current();
184 if (aSub->getKind() != "Parameter")
187 // processing is finished, so, remove the iterated
188 bool aResult = !aCurrent.isBreaked(); // iteration is finished correctly, not breaked
189 myProcessIterator.pop_back();
193 void Model_Update::iterateUpdateBreak(std::shared_ptr<ModelAPI_Feature> theFeature)
195 // checking that this feature is before the current iterated one: otherwise break is not needed
196 std::list<IterationItem>::reverse_iterator aProcessed = myProcessIterator.rbegin();
197 for(; aProcessed != myProcessIterator.rend(); aProcessed++) {
198 if (aProcessed->isIterated(theFeature)) {
199 if (aProcessed->isEarlierThanCurrent(theFeature)) {
200 // break all lower level iterators
201 std::list<IterationItem>::reverse_iterator aBreaked = myProcessIterator.rbegin();
202 for(; aBreaked != aProcessed; aBreaked++) {
203 aBreaked->setBreaked();
205 // for the current breaked, set iteration to this feature precisely
206 aBreaked->setCurrentBefore(theFeature);
209 // the iterator that contains breaked is found, so, nothing else is needed
213 // if this feature is not found in the list of the currently iterated, try to break the parent
214 FeaturePtr aParent = ModelAPI_Tools::compositeOwner(theFeature);
216 iterateUpdateBreak(aParent);
219 void Model_Update::processOperation(const bool theTotalUpdate, const bool theFinish)
221 /* cancel hardcode due to issue 948
223 // the hardcode (DBC asked): hide the sketch referenced by extrusion on apply
224 std::set<std::shared_ptr<ModelAPI_Object> >::iterator aFIter;
225 for(aFIter = myWaitForFinish.begin(); aFIter != myWaitForFinish.end(); aFIter++)
227 FeaturePtr aF = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
228 if (aF && aF->data()->isValid() &&
229 (aF->getKind() == "Extrusion" || aF->getKind() == "Revolution")) {
230 AttributeSelectionListPtr aBase = aF->selectionList("base");
232 for(int a = aBase->size() - 1; a >= 0; a--) {
233 ResultPtr aSketchRes = aBase->value(a)->context();
235 aSketchRes->setDisplayed(false);
242 // perform update of everything if needed
245 std::cout<<"****** Start processing"<<std::endl;
249 bool isAutomaticChanged = false;
251 if (theTotalUpdate && !myIsAutomatic) { // Apply button now works as "Rebuild"
252 isAutomaticChanged = true;
253 myIsAutomatic = true;
255 // init iteration from the root document
256 iterateUpdate(CompositeFeaturePtr());
258 if (isAutomaticChanged) myIsAutomatic = false;
259 myIsExecuted = false;
262 // flush to update display
263 static Events_Loop* aLoop = Events_Loop::loop();
264 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
265 aLoop->flush(EVENT_DISP);
267 std::cout<<"****** End processing"<<std::endl;
272 void Model_Update::updateFeature(FeaturePtr theFeature)
274 // check all features this feature depended on (recursive call of updateFeature)
275 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
277 if (theFeature->isDisabled())
280 // do not execute the composite that contains the current
281 //CompositeFeaturePtr aMain = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
282 //bool isPostponedMain = aMain.get() && aMain->isSub(theFeature->document()->currentFeature(false));
285 std::cout<<"Update Feature "<<theFeature->name()<<std::endl;
287 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
288 // If automatice update is not needed and feature attributes were not updated right now,
289 // do not execute it and do not update arguments.
290 if (!myIsAutomatic &&
291 (myUpdated.find(theFeature) == myUpdated.end() || myUpdated[theFeature] != myModification)
293 // execute will be performed later, but some features may have not-result
294 // presentations, so call update for them (like coincidence in the sketcher)
295 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
296 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
300 // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
301 updateArguments(theFeature);
303 // composite feature must be executed after sub-features execution
305 if (!iterateUpdate(aCompos))
306 return; // iteration was interrupted, so, interrupt the update of this feature (it will be done later)
307 // reupdate arguments of composite feature: it may be changed during subs execution
309 // issue 955: extrusion fuse sketch naming must be updated after the sketch update
310 // so, comment this: if (theFeature->data()->execState() != ModelAPI_StateMustBeUpdated)
311 updateArguments(theFeature);
313 // this checking must be after the composite feature sub-elements processing:
314 // composite feature status may depend on it's subelements
315 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
316 theFeature->eraseResults();
317 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
321 // only the currently updated features are executed
322 bool aJustUpdated = myUpdated.find(theFeature) != myUpdated.end();
324 // if preview is not needed, the created feature was not updated before, so, myModification is not actual for this
325 if (theFeature->isPreviewNeeded()) {
326 aJustUpdated = myUpdated[theFeature] == myModification;
330 if (myIsAutomatic && theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
333 // On abort, undo or redo execute is not needed: results in document are updated automatically
334 // But redisplay is needed: results are updated, must be also updated in the viewer.
336 !std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures()) {
337 if (!theFeature->isPersistentResult()) { // not persistent must be re-executed on abort, etc.
338 ModelAPI_ExecState aState = theFeature->data()->execState();
339 if (aFactory->validate(theFeature)) {
340 executeFeature(theFeature);
342 theFeature->eraseResults();
343 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
346 redisplayWithResults(theFeature, ModelAPI_StateNothing);
347 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) { // it is done (in the tree)
348 theFeature->data()->execState(ModelAPI_StateDone);
350 // it will be not updated with new modifications: only the currently updated features are updated
351 //if (myUpdated.find(theFeature) != myUpdated.end()) {
352 // myUpdated.erase(theFeature); // do not update this persistent feature even in the future
358 // execute feature if it must be updated
359 if (theFeature->isPreviewNeeded() || myIsFinish) {
361 ModelAPI_ExecState aState = theFeature->data()->execState();
362 if (aFactory->validate(theFeature)) {
363 //if (!isPostponedMain) {
365 std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
367 executeFeature(theFeature);
371 std::cout<<"Feature is not valid, erase results "<<theFeature->name()<<std::endl;
373 theFeature->eraseResults();
374 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
377 } else { // preview is not needed => make state Done
378 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
379 theFeature->data()->execState(ModelAPI_StateDone);
380 if (aJustUpdated) {// store that it must be updated on finish
381 myUpdated[theFeature] = myModification;
382 aFactory->validate(theFeature); // need to be validated to update the "Apply" state if not previewed
388 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
390 // make updated and redisplay all results
391 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
392 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
393 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
394 for (; aRIter != aResults.cend(); aRIter++) {
395 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
396 if (!aRes->isDisabled()) {// update state only for enabled results (Placement Result Part may make the original Part Result as invalid)
397 aRes->data()->execState(theState);
398 if (theState == ModelAPI_StateDone) // feature become "done", so execution changed results
399 myUpdated[aRes] = myModification;
401 if (theFeature->data()->updateID() > aRes->data()->updateID()) {
402 aRes->data()->setUpdateID(theFeature->data()->updateID());
404 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
405 // iterate sub-bodies of compsolid
406 ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
408 int aNumSub = aComp->numberOfSubs();
409 for(int a = 0; a < aNumSub; a++) {
410 ResultPtr aSub = aComp->subResult(a);
411 if (!aSub->isDisabled()) {// update state only for enabled results (Placement Result Part may make the original Part Result as invalid)
412 aSub->data()->execState(theState);
413 if (theState == ModelAPI_StateDone) // feature become "done", so execution changed results
414 myUpdated[aSub] = myModification;
416 if (theFeature->data()->updateID() > aSub->data()->updateID()) {
417 aSub->data()->setUpdateID(theFeature->data()->updateID());
419 ModelAPI_EventCreator::get()->sendUpdated(aSub, EVENT_DISP);
423 // to redisplay "presentable" feature (for ex. distance constraint)
424 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
425 theFeature->data()->execState(theState);
428 /// Updates the state by the referenced object: if something bad with it, set state for this one
429 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
432 ModelAPI_ExecState aRefState = theTarget->data()->execState();
433 if (aRefState == ModelAPI_StateMustBeUpdated) {
434 if (theCurrent == ModelAPI_StateDone)
435 return ModelAPI_StateMustBeUpdated;
436 } else if (aRefState != ModelAPI_StateDone) {
437 return ModelAPI_StateInvalidArgument;
443 bool Model_Update::isOlder(std::shared_ptr<ModelAPI_Feature> theFeature,
444 std::shared_ptr<ModelAPI_Object> theArgument)
446 int aFeatureID = theFeature->data()->updateID();
447 int anArgID = theArgument->data()->updateID();
448 if (aFeatureID < anArgID)
450 std::map<std::shared_ptr<ModelAPI_Object>, int >::iterator anAIter = myUpdated.find(theArgument);
451 if (anAIter == myUpdated.end())
453 std::map<std::shared_ptr<ModelAPI_Object>, int >::iterator aFIter = myUpdated.find(theFeature);
454 if (aFIter == myUpdated.end())
455 return true; // argument is updated, but feature is not updated at all
456 return aFIter->second < anAIter->second;
459 void Model_Update::updateArguments(FeaturePtr theFeature) {
460 // perform this method also for disabled features: to make "not done" state for
461 // features referenced to the active and modified features
463 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
465 ModelAPI_ExecState aState = theFeature->data()->execState();
466 if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
467 aState = ModelAPI_StateMustBeUpdated;
469 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
470 aState = ModelAPI_StateMustBeUpdated;
471 // check the parameters state
473 std::list<AttributePtr> aDoubles =
474 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
475 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
476 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
477 AttributeDoublePtr aDouble =
478 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
479 if (aDouble.get() && !aDouble->text().empty()) {
480 if (myIsParamUpdated) {
481 ModelAPI_AttributeEvalMessage::send(aDouble, this);
483 if (aDouble->expressionInvalid()) {
484 aState = ModelAPI_StateInvalidArgument;
490 std::list<AttributePtr> anAttributes =
491 theFeature->data()->attributes(GeomDataAPI_Point::typeId());
492 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
493 for(; anIter != anAttributes.end(); anIter++) {
494 AttributePointPtr aPointAttribute =
495 std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
496 if (aPointAttribute.get()) {
497 if (myIsParamUpdated) {
498 ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
500 if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
501 (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
502 (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
503 aState = ModelAPI_StateInvalidArgument;
509 std::list<AttributePtr> anAttributes =
510 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
511 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
512 for(; anIter != anAttributes.end(); anIter++) {
513 AttributePoint2DPtr aPoint2DAttribute =
514 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
515 if (aPoint2DAttribute.get()) {
516 if (myIsParamUpdated) {
517 ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
519 if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
520 (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
521 aState = ModelAPI_StateInvalidArgument;
526 //if (aState == ModelAPI_StateDone) {// all referenced objects are ready to be used
527 //std::cout<<"Execute feature "<<theFeature->getKind()<<std::endl;
528 // before execution update the selection attributes if any
529 list<AttributePtr> aRefs =
530 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
531 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
532 for (; aRefsIter != aRefs.end(); aRefsIter++) {
533 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
534 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
535 ObjectPtr aContext = aSel->context();
536 // update argument only if the referenced object is changed
537 if (aContext.get() && !aContext->isDisabled()) {
538 bool isObligatory = !aFactory->isNotObligatory(
539 theFeature->getKind(), theFeature->data()->id(aSel)) &&
540 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
541 if (isOlder(theFeature, aContext)) {
542 if (aState == ModelAPI_StateDone)
543 aState = ModelAPI_StateMustBeUpdated;
544 if (!aSel->update()) { // this must be done on execution since it may be long operation
546 aState = ModelAPI_StateInvalidArgument;
548 } else if (aSel->isInvalid()) { // not needed to update, but invalid (stated previously)
549 aState = ModelAPI_StateInvalidArgument;
553 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
554 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
555 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
556 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
557 for(int a = aSel->size() - 1; a >= 0; a--) {
558 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
559 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
561 ObjectPtr aContext = aSelAttr->context();
562 // update argument onlt if the referenced object is changed
563 if (aContext.get() && !aContext->isDisabled()) {
564 bool isObligatory = !aFactory->isNotObligatory(
565 theFeature->getKind(), theFeature->data()->id(aSel)) &&
566 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
567 if (isOlder(theFeature, aContext)) {
568 if (aState == ModelAPI_StateDone)
569 aState = ModelAPI_StateMustBeUpdated;
570 if (!aSelAttr->update()) {
572 aState = ModelAPI_StateInvalidArgument;
574 } else if (aSelAttr->isInvalid()) {
575 aState = ModelAPI_StateInvalidArgument;
581 // check all references: if referenced objects are updated, this object also must be updated
582 // also check state of referenced objects: if they are not ready, inherit corresponding state
583 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefsObj;
584 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theFeature->data());
585 aData->referencesToObjects(aRefsObj);
586 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefsObj.begin();
587 for(; aRef != aRefsObj.end(); aRef++) {
588 std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
589 for(; aRefObj != aRef->second.end(); aRefObj++) {
590 // if reference is null, it may mean that this reference is to other document
591 // the does not supported by RefList: parameters may be recomputed
592 if (!aRefObj->get() && theFeature->firstResult().get() &&
593 theFeature->firstResult()->groupName() == ModelAPI_ResultParameter::group()) {
594 if (aState == ModelAPI_StateDone)
595 aState = ModelAPI_StateMustBeUpdated;
596 } else if (aRefObj->get() && isOlder(theFeature, *aRefObj)) {
597 if (aState == ModelAPI_StateDone)
598 aState = ModelAPI_StateMustBeUpdated;
600 aState = stateByReference(*aRefObj, aState);
603 // composites sub-elements
604 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
605 // composite feature must be executed after sub-features execution
607 // number of subs can be changed in execution: like fillet
608 int aNumSubs = aCompos->numberOfSubs();
609 for(int a = 0; a < aNumSubs; a++) {
610 FeaturePtr aSub = aCompos->subFeature(a);
611 if (aSub.get() && aState == ModelAPI_StateDone) {
612 if (isOlder(theFeature, aSub)) {
613 aState = ModelAPI_StateMustBeUpdated;
615 // also check that all results of subs were updated: composite also depends on the results
616 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
617 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aResults.begin();
618 for(; aResIter != aResults.end(); aResIter++) {
619 if (aResIter->get() && (*aResIter)->data()->isValid() && !(*aResIter)->isDisabled() &&
620 isOlder(theFeature, *aResIter)) {
621 aState = ModelAPI_StateMustBeUpdated;
625 if (a == aNumSubs - 1) // in case number of subs is changed, just recheck before end
626 aNumSubs = aCompos->numberOfSubs();
631 if (aState != ModelAPI_StateDone)
632 theFeature->data()->execState(aState);
635 void Model_Update::executeFeature(FeaturePtr theFeature)
637 // execute in try-catch to avoid internal problems of the feature
638 ModelAPI_ExecState aState = ModelAPI_StateDone;
639 theFeature->data()->execState(ModelAPI_StateDone);
641 theFeature->execute();
642 if (theFeature->data()->execState() != ModelAPI_StateDone) {
643 aState = ModelAPI_StateExecFailed;
645 aState = ModelAPI_StateDone;
646 myWaitForFinish.insert(theFeature);
649 aState = ModelAPI_StateExecFailed;
651 "Feature " + theFeature->getKind() + " has failed during the execution");
653 if (aState != ModelAPI_StateDone) {
654 theFeature->eraseResults();
656 theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
657 redisplayWithResults(theFeature, aState);
660 ///////////////// Updated items iterator ////////////////////////
661 Model_Update::IterationItem::IterationItem(std::shared_ptr<ModelAPI_CompositeFeature> theFeature)
667 if (!myMain.get() && ModelAPI_Session::get()->hasModuleDocument()) { // no document => nothing to do
668 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
669 myObjects = std::dynamic_pointer_cast<Model_Document>(aRootDoc)->objects();
674 void Model_Update::IterationItem::next()
676 if (mySkipNext) { // ignore one next
683 int aNumSubs = myMain->numberOfSubs();
684 if (myIndex == aNumSubs)
686 // skip sub-objects, that are subs not only for this: sketch elements relatively to Part
687 for(FeaturePtr aSub = myMain->subFeature(myIndex); aSub.get();
688 aSub = myMain->subFeature(myIndex)) {
689 aSub = myMain->subFeature(myIndex);
690 CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(aSub);
691 if (!anOwner.get() || anOwner == myMain) {
695 if (myIndex == aNumSubs)
698 } else if (mySub.get()) {
700 mySub = myObjects->nextFeature(mySub);
701 CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(mySub);
702 // skip sub-objects, that are subs not only for this: sketch elements relatively to PartSet
703 if (!anOwner.get()) {
711 bool Model_Update::IterationItem::more()
716 return myIndex < myMain->numberOfSubs();
717 return mySub.get() != NULL;
720 FeaturePtr Model_Update::IterationItem::current()
723 return myMain->subFeature(myIndex);
727 void Model_Update::IterationItem::setBreaked()
733 void Model_Update::IterationItem::startIteration(const bool theVirtual)
735 myIsVirtual = theVirtual;
738 } else if (myObjects) {
739 mySub = myObjects->firstFeature();
743 bool Model_Update::IterationItem::isIterated(FeaturePtr theFeature)
746 if (myMain->isSub(theFeature)) {
747 CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(theFeature);
748 if (!anOwner.get() || anOwner == myMain)
753 // for the root document just check that this feature in this document and it is not sub
754 return myObjects->owner() == theFeature->document() &&
755 !ModelAPI_Tools::compositeOwner(theFeature).get();
758 bool Model_Update::IterationItem::isEarlierThanCurrent(FeaturePtr theFeature)
761 for(int a = 0; a < myIndex; a++) {
762 if (myMain->subFeature(a) == theFeature)
766 return !mySub.get() && !myObjects->isLater(theFeature, mySub);
771 void Model_Update::IterationItem::setCurrentBefore(FeaturePtr theFeature)
774 for(int a = 0; a < myIndex; a++) {
775 if (myMain->subFeature(a) == theFeature) {