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 Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild immediately",
59 Config_Prop::Boolean, "false");
61 // Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
62 myIsParamUpdated = false;
67 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
69 static Events_Loop* aLoop = Events_Loop::loop();
70 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
71 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
72 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
73 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
74 static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
75 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
76 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
77 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
79 std::cout<<"****** Event "<<theMessage->eventID().eventText()<<std::endl;
81 if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
83 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
84 if (aPropVal != myIsAutomatic) { // something is changed
85 // myIsAutomatic = aPropVal;
86 if (myIsAutomatic) // higher level of automatization => to rebuild
87 processOperation(false);
89 } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
90 processOperation(true);
91 } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent ||
92 theMessage->eventID() == kMovedEvent) {
93 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
94 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
95 const std::set<ObjectPtr>& anObjs = aMsg->objects();
96 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
97 bool isOnlyResults = true; // check that only results were changed: only redisplay is needed
98 for(; anObjIter != anObjs.cend(); anObjIter++) {
99 if (!std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIter).get()) {
100 isOnlyResults = false;
102 if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
103 myIsParamUpdated = true;
105 // on undo/redo, abort do not update persisten features
106 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
107 if (std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures() ||
108 (anUpdated.get() && !anUpdated->isPersistentResult())) {
109 // created objects are always must be up to date (python box feature)
110 // and updated not in internal uptation chain
111 myUpdated[*anObjIter] = myModification;
113 // something is updated during the execution: re-execute it (sketch update by parameters or
114 // Box macro that updates the upper features during the execution)
116 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
117 if (anUpdated.get() && anUpdated->data()->isValid())
118 iterateUpdateBreak(anUpdated);
121 if (myIsExecuted) std::cout<<"During execution ";
122 if ((*anObjIter)->data() && (*anObjIter)->data()->isValid()) {
123 std::cout<<"add updated "<<(*anObjIter)->groupName()<<" "
124 <<(*anObjIter)->data()->name()<<std::endl;
130 // this event is for solver update, not here, do not react immediately
131 if (!isOnlyResults && !(theMessage->eventID() == kMovedEvent))
132 processOperation(false);
133 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
134 theMessage->eventID() == kOpStartEvent) {
136 if (!(theMessage->eventID() == kOpStartEvent)) {
138 processOperation(true, theMessage->eventID() == kOpFinishEvent);
141 // remove all macros before clearing all created
142 std::set<ObjectPtr>::iterator anUpdatedIter = myWaitForFinish.begin();
143 while(anUpdatedIter != myWaitForFinish.end()) {
144 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
145 if (aFeature.get()) {
146 // remove macro on finish
147 if (aFeature->isMacro()) {
148 aFeature->document()->removeFeature(aFeature);
149 myWaitForFinish.erase(aFeature);
151 // to avoid the map update problems on "remove"
152 if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
153 anUpdatedIter = myWaitForFinish.begin();
161 // in the end of transaction everything is updated, so clear the old objects (the only one
162 // place where results are cleared)
163 myIsParamUpdated = false;
166 myWaitForFinish.clear();
170 bool Model_Update::iterateUpdate(std::shared_ptr<ModelAPI_CompositeFeature> theFeature)
172 myProcessIterator.push_back(IterationItem(theFeature));
173 IterationItem& aCurrent = *myProcessIterator.rbegin();
174 // two cycles: parameters must be processed first
175 for(aCurrent.startIteration(true); aCurrent.more(); aCurrent.next()) {
176 if (aCurrent.current()->getKind() == "Parameter")
177 updateFeature(aCurrent.current());
179 // number of subs can be changed in execution: like fillet
180 for(aCurrent.startIteration(false); aCurrent.more(); aCurrent.next()) {
181 FeaturePtr aSub = aCurrent.current();
182 if (aSub->getKind() != "Parameter")
185 // processing is finished, so, remove the iterated
186 bool aResult = !aCurrent.isBreaked(); // iteration is finished correctly, not breaked
187 myProcessIterator.pop_back();
191 void Model_Update::iterateUpdateBreak(std::shared_ptr<ModelAPI_Feature> theFeature)
193 // checking that this feature is before the current iterated one: otherwise break is not needed
194 std::list<IterationItem>::reverse_iterator aProcessed = myProcessIterator.rbegin();
195 for(; aProcessed != myProcessIterator.rend(); aProcessed++) {
196 if (aProcessed->isIterated(theFeature)) {
197 if (aProcessed->isEarlierThanCurrent(theFeature)) {
198 // break all lower level iterators
199 std::list<IterationItem>::reverse_iterator aBreaked = myProcessIterator.rbegin();
200 for(; aBreaked != aProcessed; aBreaked++) {
201 aBreaked->setBreaked();
203 // for the current breaked, set iteration to this feature precisely
204 aBreaked->setCurrentBefore(theFeature);
207 // the iterator that contains breaked is found, so, nothing else is needed
211 // if this feature is not found in the list of the currently iterated, try to break the parent
212 FeaturePtr aParent = ModelAPI_Tools::compositeOwner(theFeature);
214 iterateUpdateBreak(aParent);
217 void Model_Update::processOperation(const bool theTotalUpdate, const bool theFinish)
221 // the hardcode (DBC asked): hide the sketch referenced by extrusion on apply
222 std::set<std::shared_ptr<ModelAPI_Object> >::iterator aFIter;
223 for(aFIter = myWaitForFinish.begin(); aFIter != myWaitForFinish.end(); aFIter++)
225 FeaturePtr aF = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
226 if (aF && aF->data()->isValid() &&
227 (aF->getKind() == "Extrusion" || aF->getKind() == "Revolution")) {
228 AttributeSelectionListPtr aBase = aF->selectionList("base");
230 for(int a = aBase->size() - 1; a >= 0; a--) {
231 ResultPtr aSketchRes = aBase->value(a)->context();
233 aSketchRes->setDisplayed(false);
240 // perform update of everything if needed
243 std::cout<<"****** Start processing"<<std::endl;
247 bool isAutomaticChanged = false;
249 if (theTotalUpdate && !myIsAutomatic) { // Apply button now works as "Rebuild"
250 isAutomaticChanged = true;
251 myIsAutomatic = true;
253 // init iteration from the root document
254 iterateUpdate(CompositeFeaturePtr());
256 if (isAutomaticChanged) myIsAutomatic = false;
257 myIsExecuted = false;
260 // flush to update display
261 static Events_Loop* aLoop = Events_Loop::loop();
262 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
263 aLoop->flush(EVENT_DISP);
265 std::cout<<"****** End processing"<<std::endl;
270 void Model_Update::updateFeature(FeaturePtr theFeature)
272 // check all features this feature depended on (recursive call of updateFeature)
273 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
275 if (theFeature->isDisabled())
279 std::cout<<"Update Feature "<<theFeature->name()<<std::endl;
281 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
282 // If automatice update is not needed and feature attributes were not updated right now,
283 // do not execute it and do not update arguments.
284 if (!myIsAutomatic &&
285 (myUpdated.find(theFeature) == myUpdated.end() || myUpdated[theFeature] != myModification)
287 // execute will be performed later, but some features may have not-result
288 // presentations, so call update for them (like coincidence in the sketcher)
289 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
290 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
294 // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
295 updateArguments(theFeature);
297 // composite feature must be executed after sub-features execution
299 if (!iterateUpdate(aCompos))
300 return; // iteration was interrupted, so, interrupt the update of this feature (it will be done later)
301 // reupdate arguments of composite feature: it may be changed during subs execution
303 // issue 955: extrusion fuse sketch naming must be updated after the sketch update
304 // so, comment this: if (theFeature->data()->execState() != ModelAPI_StateMustBeUpdated)
305 updateArguments(theFeature);
307 // this checking must be after the composite feature sub-elements processing:
308 // composite feature status may depend on it's subelements
309 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
310 theFeature->eraseResults();
311 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
315 // only the currently updated features are executed
316 bool aJustUpdated = myUpdated.find(theFeature) != myUpdated.end();
318 // if preview is not needed, the created feature was not updated before, so, myModification is not actual for this
319 if (theFeature->isPreviewNeeded()) {
320 aJustUpdated = myUpdated[theFeature] == myModification;
324 if (myIsAutomatic && theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
327 // On abort, undo or redo execute is not needed: results in document are updated automatically
328 // But redisplay is needed: results are updated, must be also updated in the viewer.
330 !std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures()) {
331 if (!theFeature->isPersistentResult()) { // not persistent must be re-executed on abort, etc.
332 ModelAPI_ExecState aState = theFeature->data()->execState();
333 if (aFactory->validate(theFeature)) {
334 executeFeature(theFeature);
336 theFeature->eraseResults();
337 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
340 redisplayWithResults(theFeature, ModelAPI_StateNothing);
341 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) { // it is done (in the tree)
342 theFeature->data()->execState(ModelAPI_StateDone);
344 // it will be not updated with new modifications: only the currently updated features are updated
345 //if (myUpdated.find(theFeature) != myUpdated.end()) {
346 // myUpdated.erase(theFeature); // do not update this persistent feature even in the future
352 // execute feature if it must be updated
353 if (theFeature->isPreviewNeeded() || myIsFinish) {
355 ModelAPI_ExecState aState = theFeature->data()->execState();
356 if (aFactory->validate(theFeature)) {
358 std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
360 executeFeature(theFeature);
363 std::cout<<"Feature is not valid, erase results "<<theFeature->name()<<std::endl;
365 theFeature->eraseResults();
366 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
369 } else { // preview is not needed => make state Done
370 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
371 theFeature->data()->execState(ModelAPI_StateDone);
372 if (aJustUpdated) // store that it must be updated on finish
373 myUpdated[theFeature] = myModification;
378 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
380 // make updated and redisplay all results
381 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
382 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
383 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
384 for (; aRIter != aResults.cend(); aRIter++) {
385 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
386 if (!aRes->isDisabled()) {// update state only for enabled results (Placement Result Part may make the original Part Result as invalid)
387 aRes->data()->execState(theState);
388 if (theState == ModelAPI_StateDone) // feature become "done", so execution changed results
389 myUpdated[aRes] = myModification;
391 if (theFeature->data()->updateID() > aRes->data()->updateID()) {
392 aRes->data()->setUpdateID(theFeature->data()->updateID());
394 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
395 // iterate sub-bodies of compsolid
396 ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
398 int aNumSub = aComp->numberOfSubs();
399 for(int a = 0; a < aNumSub; a++) {
400 ResultPtr aSub = aComp->subResult(a);
401 if (!aSub->isDisabled()) {// update state only for enabled results (Placement Result Part may make the original Part Result as invalid)
402 aSub->data()->execState(theState);
403 if (theState == ModelAPI_StateDone) // feature become "done", so execution changed results
404 myUpdated[aSub] = myModification;
406 if (theFeature->data()->updateID() > aSub->data()->updateID()) {
407 aSub->data()->setUpdateID(theFeature->data()->updateID());
409 ModelAPI_EventCreator::get()->sendUpdated(aSub, EVENT_DISP);
413 // to redisplay "presentable" feature (for ex. distance constraint)
414 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
415 theFeature->data()->execState(theState);
418 /// Updates the state by the referenced object: if something bad with it, set state for this one
419 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
422 ModelAPI_ExecState aRefState = theTarget->data()->execState();
423 if (aRefState == ModelAPI_StateMustBeUpdated) {
424 if (theCurrent == ModelAPI_StateDone)
425 return ModelAPI_StateMustBeUpdated;
426 } else if (aRefState != ModelAPI_StateDone) {
427 return ModelAPI_StateInvalidArgument;
433 bool Model_Update::isOlder(std::shared_ptr<ModelAPI_Feature> theFeature,
434 std::shared_ptr<ModelAPI_Object> theArgument)
436 int aFeatureID = theFeature->data()->updateID();
437 int anArgID = theArgument->data()->updateID();
438 if (aFeatureID < anArgID)
440 std::map<std::shared_ptr<ModelAPI_Object>, int >::iterator anAIter = myUpdated.find(theArgument);
441 if (anAIter == myUpdated.end())
443 std::map<std::shared_ptr<ModelAPI_Object>, int >::iterator aFIter = myUpdated.find(theFeature);
444 if (aFIter == myUpdated.end())
445 return true; // argument is updated, but feature is not updated at all
446 return aFIter->second < anAIter->second;
449 void Model_Update::updateArguments(FeaturePtr theFeature) {
450 // perform this method also for disabled features: to make "not done" state for
451 // features referenced to the active and modified features
453 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
455 ModelAPI_ExecState aState = theFeature->data()->execState();
456 if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
457 aState = ModelAPI_StateMustBeUpdated;
459 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
460 aState = ModelAPI_StateMustBeUpdated;
461 // check the parameters state
463 std::list<AttributePtr> aDoubles =
464 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
465 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
466 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
467 AttributeDoublePtr aDouble =
468 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
469 if (aDouble.get() && !aDouble->text().empty()) {
470 if (myIsParamUpdated) {
471 ModelAPI_AttributeEvalMessage::send(aDouble, this);
473 if (aDouble->expressionInvalid()) {
474 aState = ModelAPI_StateInvalidArgument;
480 std::list<AttributePtr> anAttributes =
481 theFeature->data()->attributes(GeomDataAPI_Point::typeId());
482 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
483 for(; anIter != anAttributes.end(); anIter++) {
484 AttributePointPtr aPointAttribute =
485 std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
486 if (aPointAttribute.get()) {
487 if (myIsParamUpdated) {
488 ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
490 if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
491 (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
492 (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
493 aState = ModelAPI_StateInvalidArgument;
499 std::list<AttributePtr> anAttributes =
500 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
501 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
502 for(; anIter != anAttributes.end(); anIter++) {
503 AttributePoint2DPtr aPoint2DAttribute =
504 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
505 if (aPoint2DAttribute.get()) {
506 if (myIsParamUpdated) {
507 ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
509 if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
510 (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
511 aState = ModelAPI_StateInvalidArgument;
516 //if (aState == ModelAPI_StateDone) {// all referenced objects are ready to be used
517 //std::cout<<"Execute feature "<<theFeature->getKind()<<std::endl;
518 // before execution update the selection attributes if any
519 list<AttributePtr> aRefs =
520 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
521 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
522 for (; aRefsIter != aRefs.end(); aRefsIter++) {
523 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
524 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
525 ObjectPtr aContext = aSel->context();
526 // update argument only if the referenced object is changed
527 if (aContext.get() && !aContext->isDisabled()) {
528 bool isObligatory = !aFactory->isNotObligatory(
529 theFeature->getKind(), theFeature->data()->id(aSel)) &&
530 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
531 if (isOlder(theFeature, aContext)) {
532 if (aState == ModelAPI_StateDone)
533 aState = ModelAPI_StateMustBeUpdated;
534 if (!aSel->update()) { // this must be done on execution since it may be long operation
536 aState = ModelAPI_StateInvalidArgument;
538 } else if (aSel->isInvalid()) { // not needed to update, but invalid (stated previously)
539 aState = ModelAPI_StateInvalidArgument;
543 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
544 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
545 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
546 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
547 for(int a = aSel->size() - 1; a >= 0; a--) {
548 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
549 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
551 ObjectPtr aContext = aSelAttr->context();
552 // update argument onlt if the referenced object is changed
553 if (aContext.get() && !aContext->isDisabled()) {
554 bool isObligatory = !aFactory->isNotObligatory(
555 theFeature->getKind(), theFeature->data()->id(aSel)) &&
556 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
557 if (isOlder(theFeature, aContext)) {
558 if (aState == ModelAPI_StateDone)
559 aState = ModelAPI_StateMustBeUpdated;
560 if (!aSelAttr->update()) {
562 aState = ModelAPI_StateInvalidArgument;
564 } else if (aSelAttr->isInvalid()) {
565 aState = ModelAPI_StateInvalidArgument;
571 // check all references: if referenced objects are updated, this object also must be updated
572 // also check state of referenced objects: if they are not ready, inherit corresponding state
573 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefsObj;
574 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theFeature->data());
575 aData->referencesToObjects(aRefsObj);
576 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefsObj.begin();
577 for(; aRef != aRefsObj.end(); aRef++) {
578 std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
579 for(; aRefObj != aRef->second.end(); aRefObj++) {
580 // if reference is null, it may mean that this reference is to other document
581 // the does not supported by RefList: parameters may be recomputed
582 if (!aRefObj->get() && theFeature->firstResult().get() &&
583 theFeature->firstResult()->groupName() == ModelAPI_ResultParameter::group()) {
584 if (aState == ModelAPI_StateDone)
585 aState = ModelAPI_StateMustBeUpdated;
586 } else if (aRefObj->get() && isOlder(theFeature, *aRefObj)) {
587 if (aState == ModelAPI_StateDone)
588 aState = ModelAPI_StateMustBeUpdated;
590 aState = stateByReference(*aRefObj, aState);
593 // composites sub-elements
594 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
595 // composite feature must be executed after sub-features execution
597 // number of subs can be changed in execution: like fillet
598 int aNumSubs = aCompos->numberOfSubs();
599 for(int a = 0; a < aNumSubs; a++) {
600 FeaturePtr aSub = aCompos->subFeature(a);
601 if (aSub.get() && aState == ModelAPI_StateDone) {
602 if (isOlder(theFeature, aSub)) {
603 aState = ModelAPI_StateMustBeUpdated;
605 // also check that all results of subs were updated: composite also depends on the results
606 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
607 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aResults.begin();
608 for(; aResIter != aResults.end(); aResIter++) {
609 if (aResIter->get() && (*aResIter)->data()->isValid() && !(*aResIter)->isDisabled() &&
610 isOlder(theFeature, *aResIter)) {
611 aState = ModelAPI_StateMustBeUpdated;
615 if (a == aNumSubs - 1) // in case number of subs is changed, just recheck before end
616 aNumSubs = aCompos->numberOfSubs();
621 if (aState != ModelAPI_StateDone)
622 theFeature->data()->execState(aState);
625 void Model_Update::executeFeature(FeaturePtr theFeature)
627 // execute in try-catch to avoid internal problems of the feature
628 ModelAPI_ExecState aState = ModelAPI_StateDone;
629 theFeature->data()->execState(ModelAPI_StateDone);
631 theFeature->execute();
632 if (theFeature->data()->execState() != ModelAPI_StateDone) {
633 aState = ModelAPI_StateExecFailed;
635 aState = ModelAPI_StateDone;
636 myWaitForFinish.insert(theFeature);
639 aState = ModelAPI_StateExecFailed;
641 "Feature " + theFeature->getKind() + " has failed during the execution");
643 if (aState != ModelAPI_StateDone) {
644 theFeature->eraseResults();
646 theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
647 redisplayWithResults(theFeature, aState);
650 ///////////////// Updated items iterator ////////////////////////
651 Model_Update::IterationItem::IterationItem(std::shared_ptr<ModelAPI_CompositeFeature> theFeature)
657 if (!myMain.get() && ModelAPI_Session::get()->hasModuleDocument()) { // no document => nothing to do
658 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
659 myObjects = std::dynamic_pointer_cast<Model_Document>(aRootDoc)->objects();
664 void Model_Update::IterationItem::next()
666 if (mySkipNext) { // ignore one next
673 int aNumSubs = myMain->numberOfSubs();
674 if (myIndex == aNumSubs)
676 // skip sub-objects, that are subs not only for this: sketch elements relatively to Part
677 for(FeaturePtr aSub = myMain->subFeature(myIndex); aSub.get();
678 aSub = myMain->subFeature(myIndex)) {
679 aSub = myMain->subFeature(myIndex);
680 CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(aSub);
681 if (!anOwner.get() || anOwner == myMain) {
685 if (myIndex == aNumSubs)
688 } else if (mySub.get()) {
690 mySub = myObjects->nextFeature(mySub);
691 CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(mySub);
692 // skip sub-objects, that are subs not only for this: sketch elements relatively to PartSet
693 if (!anOwner.get()) {
701 bool Model_Update::IterationItem::more()
706 return myIndex < myMain->numberOfSubs();
707 return mySub.get() != NULL;
710 FeaturePtr Model_Update::IterationItem::current()
713 return myMain->subFeature(myIndex);
717 void Model_Update::IterationItem::setBreaked()
723 void Model_Update::IterationItem::startIteration(const bool theVirtual)
725 myIsVirtual = theVirtual;
728 } else if (myObjects) {
729 mySub = myObjects->firstFeature();
733 bool Model_Update::IterationItem::isIterated(FeaturePtr theFeature)
736 if (myMain->isSub(theFeature)) {
737 CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(theFeature);
738 if (!anOwner.get() || anOwner == myMain)
743 // for the root document just check that this feature in this document and it is not sub
744 return myObjects->owner() == theFeature->document() &&
745 !ModelAPI_Tools::compositeOwner(theFeature).get();
748 bool Model_Update::IterationItem::isEarlierThanCurrent(FeaturePtr theFeature)
751 for(int a = 0; a < myIndex; a++) {
752 if (myMain->subFeature(a) == theFeature)
756 return !mySub.get() && !myObjects->isLater(theFeature, mySub);
761 void Model_Update::IterationItem::setCurrentBefore(FeaturePtr theFeature)
764 for(int a = 0; a < myIndex; a++) {
765 if (myMain->subFeature(a) == theFeature) {