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 kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
44 aLoop->registerListener(this, kCreatedEvent);
45 static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
46 aLoop->registerListener(this, kUpdatedEvent);
47 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
48 aLoop->registerListener(this, kOpFinishEvent);
49 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
50 aLoop->registerListener(this, kOpAbortEvent);
51 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
52 aLoop->registerListener(this, kOpStartEvent);
53 static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
54 aLoop->registerListener(this, kStabilityEvent);
56 // Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
57 myIsParamUpdated = false;
59 myIsProcessed = false;
62 void Model_Update::addModified(FeaturePtr theFeature, FeaturePtr theReason) {
63 if (!theFeature->isPreviewNeeded() && !myIsFinish) {
64 myProcessOnFinish.insert(theFeature);
66 std::cout<<"*** Add process on finish "<<theFeature->name()<<std::endl;
68 updateArguments(theFeature);
69 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
70 theFeature->data()->execState(ModelAPI_StateDone);
71 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
72 aFactory->validate(theFeature); // need to be validated to update the "Apply" state if not previewed
76 if (myModified.find(theFeature) != myModified.end()) {
77 if (theReason.get()) {
79 std::cout<<"*** Add already modified "<<theFeature->name()<<std::endl;
81 myModified[theFeature].insert(theReason);
83 return; // already is marked as modified, so, nothing to do, it will be processed
85 // do not add the disabled, but possibly the sub-elements are not disabled
86 bool aIsDisabled = theFeature->isDisabled();
88 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
89 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
90 // do not forget that in this case all were the reasons
91 aNewSet.insert(theFeature);
94 aNewSet.insert(theReason);
96 myModified[theFeature] = aNewSet;
98 std::cout<<"*** Add modified "<<theFeature->name()<<std::endl;
100 } else { // will be updated during the finish of the operation, or when it becomes enabled
101 if (theFeature->data()->execState() == ModelAPI_StateDone)
102 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
104 return; // do not need iteration deeply if it is already marked as modified or so
106 std::cout<<"*** Set modified state "<<theFeature->name()<<std::endl;
109 // clear processed and fill modified recursively
110 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
111 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
112 for(; aRefIter != aRefs.cend(); aRefIter++) {
113 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
114 if (aReferenced.get()) {
115 addModified(aReferenced, theFeature);
118 // proccess also results
119 std::list<ResultPtr> allResults; // list of this feature and results
120 ModelAPI_Tools::allResults(theFeature, allResults);
121 std::list<ResultPtr>::iterator aRes = allResults.begin();
122 for(; aRes != allResults.end(); aRes++) {
123 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*aRes)->data()->refsToMe();
124 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
125 for(; aRefIter != aRefs.cend(); aRefIter++) {
126 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
127 if (aReferenced.get()) {
128 addModified(aReferenced, theFeature);
133 // also add part feature that contains this feature to the modified
134 if (theFeature->document()->kind() != "PartSet") {
135 FeaturePtr aPart = ModelAPI_Tools::findPartFeature(
136 ModelAPI_Session::get()->moduleDocument(), theFeature->document());
138 addModified(aPart, theFeature);
143 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
145 static Events_Loop* aLoop = Events_Loop::loop();
146 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
147 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
148 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
149 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
150 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
151 static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
153 std::cout<<"****** Event "<<theMessage->eventID().eventText()<<std::endl;
155 if (theMessage->eventID() == kStabilityEvent) {
156 updateStability(theMessage->sender());
159 // creation is added to "update" to avoid recomputation twice: on create and immediately after on update
160 if (theMessage->eventID() == kCreatedEvent) {
161 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
162 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
163 const std::set<ObjectPtr>& anObjs = aMsg->objects();
164 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
165 for(; anObjIter != anObjs.cend(); anObjIter++) {
166 if (std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures())
167 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kUpdatedEvent);
171 if (theMessage->eventID() == kUpdatedEvent) {
172 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
173 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
174 const std::set<ObjectPtr>& anObjs = aMsg->objects();
175 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
176 bool aSomeModified = false; // check that features not changed: only redisplay is needed
177 for(; anObjIter != anObjs.cend(); anObjIter++) {
178 if (!(*anObjIter)->data()->isValid())
181 std::cout<<">>> in event updated "<<(*anObjIter)->data()->name()<<std::endl;
183 if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
184 myIsParamUpdated = true;
186 // on undo/redo, abort do not update persisten features
187 bool anUpdateOnlyNotPersistent =
188 !std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures();
189 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
190 if (anUpdated.get()) {
191 if (!anUpdateOnlyNotPersistent || !anUpdated->isPersistentResult()) {
192 addModified(anUpdated, FeaturePtr());
193 aSomeModified = true;
195 } else { // process the updated result as update of features that refers to this result
196 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*anObjIter)->data()->refsToMe();
197 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
198 for(; aRefIter != aRefs.cend(); aRefIter++) {
199 if (!(*aRefIter)->owner()->data()->isValid())
201 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
202 if (anUpdated.get() && (!anUpdateOnlyNotPersistent || !anUpdated->isPersistentResult())) {
203 addModified(anUpdated, FeaturePtr());
204 aSomeModified = true;
209 // this event is for solver update, not here, do not react immediately
213 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
214 theMessage->eventID() == kOpStartEvent) {
216 if (theMessage->eventID() == kOpFinishEvent) {
218 // add features that wait for finish as modified
219 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aFeature = myProcessOnFinish.begin();
220 for(; aFeature != myProcessOnFinish.end(); aFeature++)
221 if ((*aFeature)->data()->isValid()) // there may be already removed wait for features
222 addModified(*aFeature, FeaturePtr());
225 myProcessOnFinish.clear(); // processed features must be only on finish, so clear anyway (to avoid reimport on load)
227 if (!(theMessage->eventID() == kOpStartEvent)) {
230 // remove all macros before clearing all created
231 std::set<FeaturePtr>::iterator anUpdatedIter = myWaitForFinish.begin();
232 while(anUpdatedIter != myWaitForFinish.end()) {
233 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
234 if (aFeature.get()) {
235 // remove macro on finish
236 if (aFeature->isMacro()) {
237 aFeature->document()->removeFeature(aFeature);
238 myWaitForFinish.erase(aFeature);
240 // to avoid the map update problems on "remove"
241 if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
242 anUpdatedIter = myWaitForFinish.begin();
250 // in the end of transaction everything is updated, so clear the old objects
251 myIsParamUpdated = false;
252 myWaitForFinish.clear();
256 void Model_Update::processFeatures()
258 if (!myIsProcessed) { // perform update of everything if it is not performed right now
259 myIsProcessed = true;
261 std::cout<<"****** Start processing"<<std::endl;
264 while(!myModified.empty()) {
265 processFeature(myModified.begin()->first);
267 myIsProcessed = false;
269 // flush updates just before "myModification" increment: to distinguish
270 // updates by "execute" produced by this updater and other updates, coming outside,
271 // which are really important for "processEvent" of this updater
272 static Events_Loop* aLoop = Events_Loop::loop();
273 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
274 aLoop->flush(kUpdatedEvent);
276 // flush to update display
277 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
278 aLoop->flush(EVENT_DISP);
280 std::cout<<"****** End processing"<<std::endl;
285 bool Model_Update::processFeature(FeaturePtr theFeature)
287 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
289 if (!theFeature->data()->isValid()) { // deleted feature, just remove from all containers
290 if (myModified.find(theFeature) != myModified.end())
291 myModified.erase(theFeature);
295 // check this feature is not yet checked or processed
296 bool aIsModified = myModified.find(theFeature) != myModified.end();
297 if (!aIsModified && myIsFinish) { // get info about the modification for features without preview
298 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
300 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
301 aNewSet.insert(theFeature); // contains itself, so, we don't know which was the reason and the reason is any
302 myModified[theFeature] = aNewSet;
307 std::cout<<"* Process feature "<<theFeature->name()<<std::endl;
310 // update the sketch plane before the sketch sub-elements are recomputed
311 // (otherwise sketch will update plane, modify subs, after executed, but with old subs edges)
312 if (aIsModified && theFeature->getKind() == "Sketch") {
314 std::cout<<"****** Update sketch args "<<theFeature->name()<<std::endl;
316 AttributeSelectionPtr anExtSel = theFeature->selection("External");
317 if (anExtSel.get()) {
318 ResultPtr aContext = anExtSel->context();
319 if (aContext.get() && aContext->document().get()) {
320 FeaturePtr anExtBase = aContext->document()->feature(aContext);
321 if (anExtBase.get()) {
322 processFeature(anExtBase);
326 updateArguments(theFeature);
329 if (!aIsModified) { // no modification is needed
333 // check all features this feature depended on (recursive call of updateFeature)
334 std::set<std::shared_ptr<ModelAPI_Feature> >& aReasons = myModified[theFeature];
335 if (aReasons.find(theFeature) == aReasons.end()) {
336 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aReasonIter = aReasons.begin();
337 for(; aReasonIter != aReasons.end(); aReasonIter++) {
338 if (*aReasonIter != theFeature && processFeature(*aReasonIter)) {
342 } else { // check all features this feature depended on because here which one was modified is unknown
343 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aDeps;
344 theFeature->data()->referencesToObjects(aDeps);
345 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > >::iterator
346 anAttrsIter = aDeps.begin();
347 for(; anAttrsIter != aDeps.end(); anAttrsIter++) {
348 std::list<std::shared_ptr<ModelAPI_Object> >::iterator aDepIter = anAttrsIter->second.begin();
349 for(; aDepIter != anAttrsIter->second.end(); aDepIter++) {
350 FeaturePtr aDepFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(*aDepIter);
351 if (!aDepFeat.get()) { // so, it depends on the result and process the feature owner of it
352 ResultPtr aDepRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aDepIter);
354 aDepFeat = (*aDepIter)->document()->feature(aDepRes);
357 if (aDepFeat.get()) {
358 if (processFeature(aDepFeat))
363 if (theFeature->getKind() == "Part") { // part is not depended on its subs directly, but subs must be iterated anyway
364 CompositeFeaturePtr aPart = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
365 int aNum = aPart->numberOfSubs();
366 for(int a = 0; a < aNum; a++) {
367 FeaturePtr aSub = aPart->subFeature(a);
369 if (processFeature(aSub))
376 // do not execute the composite that contains the current
377 bool isPostponedMain = false;
378 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
379 if (theFeature->getKind() == "ExtrusionSketch" && aCompos.get()) {
380 CompositeFeaturePtr aCurrentOwner =
381 ModelAPI_Tools::compositeOwner(theFeature->document()->currentFeature(false));
382 isPostponedMain = aCurrentOwner.get() && aCompos->isSub(aCurrentOwner);
386 std::cout<<"Update args "<<theFeature->name()<<std::endl;
388 // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
389 updateArguments(theFeature);
391 // add this feature to the processed right now to be able remove it from this list on
392 // update signal during this feature execution
393 myModified.erase(theFeature);
394 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
395 theFeature->data()->execState(ModelAPI_StateDone);
397 // this checking must be after the composite feature sub-elements processing:
398 // composite feature status may depend on it's subelements
399 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
400 theFeature->eraseResults();
401 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
402 return true; // so, feature is modified (results are erased)
405 // On abort, undo or redo execute is not needed: results in document are updated automatically
406 // But redisplay is needed: results are updated, must be also updated in the viewer.
407 if (!std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures()) {
408 if (!theFeature->isPersistentResult()) { // not persistent must be re-executed on abort, etc.
409 ModelAPI_ExecState aState = theFeature->data()->execState();
410 if (aFactory->validate(theFeature)) {
411 executeFeature(theFeature);
413 theFeature->eraseResults();
414 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
417 redisplayWithResults(theFeature, ModelAPI_StateNothing);
418 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) { // it is done (in the tree)
419 theFeature->data()->execState(ModelAPI_StateDone);
423 // execute feature if it must be updated
424 ModelAPI_ExecState aState = theFeature->data()->execState();
425 if (aFactory->validate(theFeature)) {
426 if (!isPostponedMain) {
427 executeFeature(theFeature);
431 std::cout<<"Feature is not valid, erase results "<<theFeature->name()<<std::endl;
433 theFeature->eraseResults();
434 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
440 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
442 // make updated and redisplay all results
443 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
445 std::list<ResultPtr> allResults;
446 ModelAPI_Tools::allResults(theFeature, allResults);
447 std::list<ResultPtr>::iterator aRIter = allResults.begin();
448 for (; aRIter != allResults.cend(); aRIter++) {
449 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
450 if (!aRes->isDisabled()) {// update state only for enabled results (Placement Result Part may make the original Part Result as invalid)
451 aRes->data()->execState(theState);
453 if (theFeature->data()->updateID() > aRes->data()->updateID()) {
454 aRes->data()->setUpdateID(theFeature->data()->updateID());
456 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
458 // to redisplay "presentable" feature (for ex. distance constraint)
459 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
460 theFeature->data()->execState(theState);
463 /// Updates the state by the referenced object: if something bad with it, set state for this one
464 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
467 ModelAPI_ExecState aRefState = theTarget->data()->execState();
468 if (aRefState == ModelAPI_StateMustBeUpdated) {
469 if (theCurrent == ModelAPI_StateDone)
470 return ModelAPI_StateMustBeUpdated;
471 } else if (aRefState != ModelAPI_StateDone) {
472 return ModelAPI_StateInvalidArgument;
478 void Model_Update::updateArguments(FeaturePtr theFeature) {
479 // perform this method also for disabled features: to make "not done" state for
480 // features referenced to the active and modified features
482 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
484 ModelAPI_ExecState aState = theFeature->data()->execState();
485 if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
486 aState = ModelAPI_StateMustBeUpdated;
488 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
489 aState = ModelAPI_StateMustBeUpdated;
490 // check the parameters state
493 std::list<AttributePtr> anAttrinbutes =
494 theFeature->data()->attributes(ModelAPI_AttributeInteger::typeId());
495 std::list<AttributePtr>::iterator anIter = anAttrinbutes.begin();
496 for(; anIter != anAttrinbutes.end(); anIter++) {
497 AttributeIntegerPtr anAttribute =
498 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(*anIter);
499 if (anAttribute.get() && !anAttribute->text().empty()) {
500 if (myIsParamUpdated) {
501 ModelAPI_AttributeEvalMessage::send(anAttribute, this);
503 if (anAttribute->expressionInvalid()) {
504 aState = ModelAPI_StateInvalidArgument;
511 std::list<AttributePtr> aDoubles =
512 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
513 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
514 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
515 AttributeDoublePtr aDouble =
516 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
517 if (aDouble.get() && !aDouble->text().empty()) {
518 if (myIsParamUpdated) {
519 ModelAPI_AttributeEvalMessage::send(aDouble, this);
521 if (aDouble->expressionInvalid()) {
522 aState = ModelAPI_StateInvalidArgument;
529 std::list<AttributePtr> anAttributes =
530 theFeature->data()->attributes(GeomDataAPI_Point::typeId());
531 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
532 for(; anIter != anAttributes.end(); anIter++) {
533 AttributePointPtr aPointAttribute =
534 std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
535 if (aPointAttribute.get()) {
536 if (myIsParamUpdated) {
537 ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
539 if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
540 (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
541 (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
542 aState = ModelAPI_StateInvalidArgument;
548 std::list<AttributePtr> anAttributes =
549 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
550 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
551 for(; anIter != anAttributes.end(); anIter++) {
552 AttributePoint2DPtr aPoint2DAttribute =
553 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
554 if (aPoint2DAttribute.get()) {
555 if (myIsParamUpdated) {
556 ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
558 if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
559 (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
560 aState = ModelAPI_StateInvalidArgument;
564 // update the selection attributes if any
565 list<AttributePtr> aRefs =
566 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
567 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
568 for (; aRefsIter != aRefs.end(); aRefsIter++) {
569 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
570 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
571 ObjectPtr aContext = aSel->context();
572 // update argument only if the referenced object is ready to use
573 if (aContext.get() && !aContext->isDisabled() && isReason(theFeature, aContext)) {
574 if (!aSel->update()) { // this must be done on execution since it may be long operation
575 bool isObligatory = !aFactory->isNotObligatory(
576 theFeature->getKind(), theFeature->data()->id(aSel)) &&
577 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
579 aState = ModelAPI_StateInvalidArgument;
583 // update the selection list attributes if any
584 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
585 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
586 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
587 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
588 for(int a = aSel->size() - 1; a >= 0; a--) {
589 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
590 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
592 ObjectPtr aContext = aSelAttr->context();
593 // update argument only if the referenced object is ready to use
594 if (aContext.get() && !aContext->isDisabled() && isReason(theFeature, aContext)) {
595 if (!aSelAttr->update()) {
596 bool isObligatory = !aFactory->isNotObligatory(
597 theFeature->getKind(), theFeature->data()->id(aSel)) &&
598 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
600 aState = ModelAPI_StateInvalidArgument;
607 if (aState != ModelAPI_StateDone)
608 theFeature->data()->execState(aState);
611 bool Model_Update::isReason(std::shared_ptr<ModelAPI_Feature>& theFeature,
612 std::shared_ptr<ModelAPI_Object> theReason)
614 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
615 ::iterator aReasonsIt = myModified.find(theFeature);
616 if (aReasonsIt == myModified.end())
617 return false; // this case only for not-previewed items update state, nothing is changed in args for it
618 if (aReasonsIt->second.find(theFeature) != aReasonsIt->second.end())
619 return true; // any is reason if it contains itself
620 FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
621 if (!aReasFeat.get()) { // try to get feature of this result
622 ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
624 aReasFeat = theReason->document()->feature(aReasRes);
626 return aReasonsIt->second.find(aReasFeat) != aReasonsIt->second.end();
630 void Model_Update::executeFeature(FeaturePtr theFeature)
633 std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
635 // execute in try-catch to avoid internal problems of the feature
636 ModelAPI_ExecState aState = ModelAPI_StateDone;
637 theFeature->data()->execState(ModelAPI_StateDone);
639 theFeature->execute();
640 if (theFeature->data()->execState() != ModelAPI_StateDone) {
641 aState = ModelAPI_StateExecFailed;
643 aState = ModelAPI_StateDone;
646 aState = ModelAPI_StateExecFailed;
648 "Feature " + theFeature->getKind() + " has failed during the execution");
650 // The macro feature has to be deleted in any case even its execution is failed
651 myWaitForFinish.insert(theFeature);
652 if (aState != ModelAPI_StateDone) {
653 theFeature->eraseResults();
655 theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
656 redisplayWithResults(theFeature, aState);
659 void Model_Update::updateStability(void* theSender)
662 bool added = false; // object may be was crated
663 ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theSender);
664 if (aSender && aSender->document()) {
665 FeaturePtr aFeatureSender =
666 std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
667 if (aFeatureSender.get()) {
668 Model_Objects* aDocObjects =
669 std::dynamic_pointer_cast<Model_Document>(aSender->document())->objects();
671 //aDocObjects->synchronizeBackRefs();
672 // remove or add all concealment refs from this feature
673 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
674 aSender->data()->referencesToObjects(aRefs);
675 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIt = aRefs.begin();
676 for(; aRefIt != aRefs.end(); aRefIt++) {
677 std::list<ObjectPtr>& aRefFeaturesList = aRefIt->second;
678 std::list<ObjectPtr>::iterator aReferenced = aRefFeaturesList.begin();
679 for(; aReferenced != aRefFeaturesList.end(); aReferenced++) {
680 // stability is only on results: feature to feature reference mean nested
681 // features, that will remove nesting references
682 if (aReferenced->get() && (*aReferenced)->data()->isValid() &&
683 (*aReferenced)->groupName() != ModelAPI_Feature::group()) {
684 std::shared_ptr<Model_Data> aData =
685 std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
686 if (aFeatureSender->isStable()) {
687 aData->addBackReference(aFeatureSender, aRefIt->first);
689 aData->removeBackReference(aFeatureSender, aRefIt->first);
690 added = true; // remove of concealment may be caused creation of some result
699 static Events_Loop* aLoop = Events_Loop::loop();
700 static Events_ID kEventCreated = aLoop->eventByName(EVENT_OBJECT_CREATED);
701 aLoop->flush(kEventCreated);