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->data()->isValid())
64 return; // delete an extrusion created on the sketch
65 if (!theFeature->isPreviewNeeded() && !myIsFinish) {
66 myProcessOnFinish.insert(theFeature);
68 std::cout<<"*** Add process on finish "<<theFeature->name()<<std::endl;
70 updateArguments(theFeature);
71 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
72 theFeature->data()->execState(ModelAPI_StateDone);
73 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
74 aFactory->validate(theFeature); // need to be validated to update the "Apply" state if not previewed
78 if (myModified.find(theFeature) != myModified.end()) {
79 if (theReason.get()) {
81 std::cout<<"*** Add already modified "<<theFeature->name()<<std::endl;
83 myModified[theFeature].insert(theReason);
85 return; // already is marked as modified, so, nothing to do, it will be processed
87 // do not add the disabled, but possibly the sub-elements are not disabled
88 bool aIsDisabled = theFeature->isDisabled();
90 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
91 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
92 // do not forget that in this case all were the reasons
93 aNewSet.insert(theFeature);
96 aNewSet.insert(theReason);
98 myModified[theFeature] = aNewSet;
100 std::cout<<"*** Add modified "<<theFeature->name()<<std::endl;
102 } else { // will be updated during the finish of the operation, or when it becomes enabled
103 if (theFeature->data()->execState() == ModelAPI_StateDone)
104 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
106 return; // do not need iteration deeply if it is already marked as modified or so
108 std::cout<<"*** Set modified state "<<theFeature->name()<<std::endl;
111 // clear processed and fill modified recursively
112 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
113 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
114 for(; aRefIter != aRefs.cend(); aRefIter++) {
115 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
116 if (aReferenced.get()) {
117 addModified(aReferenced, theFeature);
120 // proccess also results
121 std::list<ResultPtr> allResults; // list of this feature and results
122 ModelAPI_Tools::allResults(theFeature, allResults);
123 std::list<ResultPtr>::iterator aRes = allResults.begin();
124 for(; aRes != allResults.end(); aRes++) {
125 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*aRes)->data()->refsToMe();
126 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
127 for(; aRefIter != aRefs.cend(); aRefIter++) {
128 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
129 if (aReferenced.get()) {
130 addModified(aReferenced, theFeature);
135 // also add part feature that contains this feature to the modified
136 if (theFeature->document()->kind() != "PartSet") {
137 FeaturePtr aPart = ModelAPI_Tools::findPartFeature(
138 ModelAPI_Session::get()->moduleDocument(), theFeature->document());
140 addModified(aPart, theFeature);
145 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
147 static Events_Loop* aLoop = Events_Loop::loop();
148 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
149 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
150 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
151 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
152 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
153 static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
155 std::cout<<"****** Event "<<theMessage->eventID().eventText()<<std::endl;
157 if (theMessage->eventID() == kStabilityEvent) {
158 updateStability(theMessage->sender());
161 // creation is added to "update" to avoid recomputation twice: on create and immediately after on update
162 if (theMessage->eventID() == kCreatedEvent) {
163 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
164 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
165 const std::set<ObjectPtr>& anObjs = aMsg->objects();
166 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
167 for(; anObjIter != anObjs.cend(); anObjIter++) {
168 if (std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures())
169 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kUpdatedEvent);
173 if (theMessage->eventID() == kUpdatedEvent) {
174 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
175 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
176 const std::set<ObjectPtr>& anObjs = aMsg->objects();
177 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
178 bool aSomeModified = false; // check that features not changed: only redisplay is needed
179 for(; anObjIter != anObjs.cend(); anObjIter++) {
180 if (!(*anObjIter)->data()->isValid())
183 std::cout<<">>> in event updated "<<(*anObjIter)->data()->name()<<std::endl;
185 if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
186 myIsParamUpdated = true;
188 // on undo/redo, abort do not update persisten features
189 bool anUpdateOnlyNotPersistent =
190 !std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures();
191 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
192 if (anUpdated.get()) {
193 if (!anUpdateOnlyNotPersistent || !anUpdated->isPersistentResult()) {
194 addModified(anUpdated, FeaturePtr());
195 aSomeModified = true;
197 } else { // process the updated result as update of features that refers to this result
198 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*anObjIter)->data()->refsToMe();
199 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
200 for(; aRefIter != aRefs.cend(); aRefIter++) {
201 if (!(*aRefIter)->owner()->data()->isValid())
203 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
204 if (anUpdated.get() && (!anUpdateOnlyNotPersistent || !anUpdated->isPersistentResult())) {
205 addModified(anUpdated, FeaturePtr());
206 aSomeModified = true;
211 // this event is for solver update, not here, do not react immediately
215 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
216 theMessage->eventID() == kOpStartEvent) {
218 if (theMessage->eventID() == kOpFinishEvent) {
220 // add features that wait for finish as modified
221 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aFeature = myProcessOnFinish.begin();
222 for(; aFeature != myProcessOnFinish.end(); aFeature++)
223 if ((*aFeature)->data()->isValid()) // there may be already removed wait for features
224 addModified(*aFeature, FeaturePtr());
227 myProcessOnFinish.clear(); // processed features must be only on finish, so clear anyway (to avoid reimport on load)
229 if (!(theMessage->eventID() == kOpStartEvent)) {
232 // remove all macros before clearing all created
233 std::set<FeaturePtr>::iterator anUpdatedIter = myWaitForFinish.begin();
234 while(anUpdatedIter != myWaitForFinish.end()) {
235 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
236 if (aFeature.get()) {
237 // remove macro on finish
238 if (aFeature->isMacro()) {
239 aFeature->document()->removeFeature(aFeature);
240 myWaitForFinish.erase(aFeature);
242 // to avoid the map update problems on "remove"
243 if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
244 anUpdatedIter = myWaitForFinish.begin();
252 // in the end of transaction everything is updated, so clear the old objects
253 myIsParamUpdated = false;
254 myWaitForFinish.clear();
258 void Model_Update::processFeatures()
260 if (!myIsProcessed) { // perform update of everything if it is not performed right now
261 myIsProcessed = true;
263 std::cout<<"****** Start processing"<<std::endl;
266 while(!myModified.empty()) {
267 processFeature(myModified.begin()->first);
269 myIsProcessed = false;
271 // flush updates just before "myModification" increment: to distinguish
272 // updates by "execute" produced by this updater and other updates, coming outside,
273 // which are really important for "processEvent" of this updater
274 static Events_Loop* aLoop = Events_Loop::loop();
275 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
276 aLoop->flush(kUpdatedEvent);
278 // flush to update display
279 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
280 aLoop->flush(EVENT_DISP);
282 std::cout<<"****** End processing"<<std::endl;
287 bool Model_Update::processFeature(FeaturePtr theFeature)
289 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
291 if (!theFeature->data()->isValid()) { // deleted feature, just remove from all containers
292 if (myModified.find(theFeature) != myModified.end())
293 myModified.erase(theFeature);
297 // check this feature is not yet checked or processed
298 bool aIsModified = myModified.find(theFeature) != myModified.end();
299 if (!aIsModified && myIsFinish) { // get info about the modification for features without preview
300 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
302 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
303 aNewSet.insert(theFeature); // contains itself, so, we don't know which was the reason and the reason is any
304 myModified[theFeature] = aNewSet;
309 std::cout<<"* Process feature "<<theFeature->name()<<std::endl;
312 // update the sketch plane before the sketch sub-elements are recomputed
313 // (otherwise sketch will update plane, modify subs, after executed, but with old subs edges)
314 if (aIsModified && theFeature->getKind() == "Sketch") {
316 std::cout<<"****** Update sketch args "<<theFeature->name()<<std::endl;
318 AttributeSelectionPtr anExtSel = theFeature->selection("External");
319 if (anExtSel.get()) {
320 ResultPtr aContext = anExtSel->context();
321 if (aContext.get() && aContext->document().get()) {
322 FeaturePtr anExtBase = aContext->document()->feature(aContext);
323 if (anExtBase.get()) {
324 processFeature(anExtBase);
328 updateArguments(theFeature);
331 if (!aIsModified) { // no modification is needed
335 // check all features this feature depended on (recursive call of updateFeature)
336 std::set<std::shared_ptr<ModelAPI_Feature> >& aReasons = myModified[theFeature];
337 if (aReasons.find(theFeature) == aReasons.end()) {
338 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aReasonIter = aReasons.begin();
339 for(; aReasonIter != aReasons.end(); aReasonIter++) {
340 if (*aReasonIter != theFeature && processFeature(*aReasonIter)) {
344 } else { // check all features this feature depended on because here which one was modified is unknown
345 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aDeps;
346 theFeature->data()->referencesToObjects(aDeps);
347 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > >::iterator
348 anAttrsIter = aDeps.begin();
349 for(; anAttrsIter != aDeps.end(); anAttrsIter++) {
350 std::list<std::shared_ptr<ModelAPI_Object> >::iterator aDepIter = anAttrsIter->second.begin();
351 for(; aDepIter != anAttrsIter->second.end(); aDepIter++) {
352 FeaturePtr aDepFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(*aDepIter);
353 if (!aDepFeat.get()) { // so, it depends on the result and process the feature owner of it
354 ResultPtr aDepRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aDepIter);
356 aDepFeat = (*aDepIter)->document()->feature(aDepRes);
359 if (aDepFeat.get()) {
360 if (processFeature(aDepFeat))
365 if (theFeature->getKind() == "Part") { // part is not depended on its subs directly, but subs must be iterated anyway
366 CompositeFeaturePtr aPart = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
367 int aNum = aPart->numberOfSubs();
368 for(int a = 0; a < aNum; a++) {
369 FeaturePtr aSub = aPart->subFeature(a);
371 if (processFeature(aSub))
378 // do not execute the composite that contains the current
379 bool isPostponedMain = false;
380 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
381 if (theFeature->getKind() == "ExtrusionSketch" && aCompos.get()) {
382 CompositeFeaturePtr aCurrentOwner =
383 ModelAPI_Tools::compositeOwner(theFeature->document()->currentFeature(false));
384 isPostponedMain = aCurrentOwner.get() && aCompos->isSub(aCurrentOwner);
388 std::cout<<"Update args "<<theFeature->name()<<std::endl;
390 // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
391 updateArguments(theFeature);
393 // add this feature to the processed right now to be able remove it from this list on
394 // update signal during this feature execution
395 myModified.erase(theFeature);
396 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
397 theFeature->data()->execState(ModelAPI_StateDone);
399 // this checking must be after the composite feature sub-elements processing:
400 // composite feature status may depend on it's subelements
401 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
402 theFeature->eraseResults();
403 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
404 return true; // so, feature is modified (results are erased)
407 // On abort, undo or redo execute is not needed: results in document are updated automatically
408 // But redisplay is needed: results are updated, must be also updated in the viewer.
409 if (!std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures()) {
410 if (!theFeature->isPersistentResult()) { // not persistent must be re-executed on abort, etc.
411 ModelAPI_ExecState aState = theFeature->data()->execState();
412 if (aFactory->validate(theFeature)) {
413 executeFeature(theFeature);
415 theFeature->eraseResults();
416 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
419 redisplayWithResults(theFeature, ModelAPI_StateNothing);
420 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) { // it is done (in the tree)
421 theFeature->data()->execState(ModelAPI_StateDone);
425 // execute feature if it must be updated
426 ModelAPI_ExecState aState = theFeature->data()->execState();
427 if (aFactory->validate(theFeature)) {
428 if (!isPostponedMain) {
429 executeFeature(theFeature);
433 std::cout<<"Feature is not valid, erase results "<<theFeature->name()<<std::endl;
435 theFeature->eraseResults();
436 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
442 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
444 // make updated and redisplay all results
445 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
447 std::list<ResultPtr> allResults;
448 ModelAPI_Tools::allResults(theFeature, allResults);
449 std::list<ResultPtr>::iterator aRIter = allResults.begin();
450 for (; aRIter != allResults.cend(); aRIter++) {
451 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
452 if (!aRes->isDisabled()) {// update state only for enabled results (Placement Result Part may make the original Part Result as invalid)
453 aRes->data()->execState(theState);
455 if (theFeature->data()->updateID() > aRes->data()->updateID()) {
456 aRes->data()->setUpdateID(theFeature->data()->updateID());
458 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
460 // to redisplay "presentable" feature (for ex. distance constraint)
461 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
462 theFeature->data()->execState(theState);
465 /// Updates the state by the referenced object: if something bad with it, set state for this one
466 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
469 ModelAPI_ExecState aRefState = theTarget->data()->execState();
470 if (aRefState == ModelAPI_StateMustBeUpdated) {
471 if (theCurrent == ModelAPI_StateDone)
472 return ModelAPI_StateMustBeUpdated;
473 } else if (aRefState != ModelAPI_StateDone) {
474 return ModelAPI_StateInvalidArgument;
480 void Model_Update::updateArguments(FeaturePtr theFeature) {
481 // perform this method also for disabled features: to make "not done" state for
482 // features referenced to the active and modified features
484 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
486 ModelAPI_ExecState aState = theFeature->data()->execState();
487 if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
488 aState = ModelAPI_StateMustBeUpdated;
490 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
491 aState = ModelAPI_StateMustBeUpdated;
492 // check the parameters state
495 std::list<AttributePtr> anAttrinbutes =
496 theFeature->data()->attributes(ModelAPI_AttributeInteger::typeId());
497 std::list<AttributePtr>::iterator anIter = anAttrinbutes.begin();
498 for(; anIter != anAttrinbutes.end(); anIter++) {
499 AttributeIntegerPtr anAttribute =
500 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(*anIter);
501 if (anAttribute.get() && !anAttribute->text().empty()) {
502 if (myIsParamUpdated) {
503 ModelAPI_AttributeEvalMessage::send(anAttribute, this);
505 if (anAttribute->expressionInvalid()) {
506 aState = ModelAPI_StateInvalidArgument;
513 std::list<AttributePtr> aDoubles =
514 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
515 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
516 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
517 AttributeDoublePtr aDouble =
518 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
519 if (aDouble.get() && !aDouble->text().empty()) {
520 if (myIsParamUpdated) {
521 ModelAPI_AttributeEvalMessage::send(aDouble, this);
523 if (aDouble->expressionInvalid()) {
524 aState = ModelAPI_StateInvalidArgument;
531 std::list<AttributePtr> anAttributes =
532 theFeature->data()->attributes(GeomDataAPI_Point::typeId());
533 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
534 for(; anIter != anAttributes.end(); anIter++) {
535 AttributePointPtr aPointAttribute =
536 std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
537 if (aPointAttribute.get()) {
538 if (myIsParamUpdated) {
539 ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
541 if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
542 (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
543 (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
544 aState = ModelAPI_StateInvalidArgument;
550 std::list<AttributePtr> anAttributes =
551 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
552 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
553 for(; anIter != anAttributes.end(); anIter++) {
554 AttributePoint2DPtr aPoint2DAttribute =
555 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
556 if (aPoint2DAttribute.get()) {
557 if (myIsParamUpdated) {
558 ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
560 if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
561 (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
562 aState = ModelAPI_StateInvalidArgument;
566 // update the selection attributes if any
567 list<AttributePtr> aRefs =
568 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
569 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
570 for (; aRefsIter != aRefs.end(); aRefsIter++) {
571 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
572 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
573 ObjectPtr aContext = aSel->context();
574 // update argument only if the referenced object is ready to use
575 if (aContext.get() && !aContext->isDisabled() && isReason(theFeature, aContext)) {
576 if (!aSel->update()) { // this must be done on execution since it may be long operation
577 bool isObligatory = !aFactory->isNotObligatory(
578 theFeature->getKind(), theFeature->data()->id(aSel)) &&
579 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
581 aState = ModelAPI_StateInvalidArgument;
585 // update the selection list attributes if any
586 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
587 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
588 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
589 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
590 for(int a = aSel->size() - 1; a >= 0; a--) {
591 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
592 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
594 ObjectPtr aContext = aSelAttr->context();
595 // update argument only if the referenced object is ready to use
596 if (aContext.get() && !aContext->isDisabled() && isReason(theFeature, aContext)) {
597 if (!aSelAttr->update()) {
598 bool isObligatory = !aFactory->isNotObligatory(
599 theFeature->getKind(), theFeature->data()->id(aSel)) &&
600 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
602 aState = ModelAPI_StateInvalidArgument;
609 if (aState != ModelAPI_StateDone)
610 theFeature->data()->execState(aState);
613 bool Model_Update::isReason(std::shared_ptr<ModelAPI_Feature>& theFeature,
614 std::shared_ptr<ModelAPI_Object> theReason)
616 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
617 ::iterator aReasonsIt = myModified.find(theFeature);
618 if (aReasonsIt == myModified.end())
619 return false; // this case only for not-previewed items update state, nothing is changed in args for it
620 if (aReasonsIt->second.find(theFeature) != aReasonsIt->second.end())
621 return true; // any is reason if it contains itself
622 FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
623 if (!aReasFeat.get()) { // try to get feature of this result
624 ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
626 aReasFeat = theReason->document()->feature(aReasRes);
628 return aReasonsIt->second.find(aReasFeat) != aReasonsIt->second.end();
632 void Model_Update::executeFeature(FeaturePtr theFeature)
635 std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
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;
648 aState = ModelAPI_StateExecFailed;
650 "Feature " + theFeature->getKind() + " has failed during the execution");
652 // The macro feature has to be deleted in any case even its execution is failed
653 myWaitForFinish.insert(theFeature);
654 if (aState != ModelAPI_StateDone) {
655 theFeature->eraseResults();
657 theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
658 redisplayWithResults(theFeature, aState);
661 void Model_Update::updateStability(void* theSender)
664 bool added = false; // object may be was crated
665 ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theSender);
666 if (aSender && aSender->document()) {
667 FeaturePtr aFeatureSender =
668 std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
669 if (aFeatureSender.get()) {
670 Model_Objects* aDocObjects =
671 std::dynamic_pointer_cast<Model_Document>(aSender->document())->objects();
673 //aDocObjects->synchronizeBackRefs();
674 // remove or add all concealment refs from this feature
675 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
676 aSender->data()->referencesToObjects(aRefs);
677 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIt = aRefs.begin();
678 for(; aRefIt != aRefs.end(); aRefIt++) {
679 std::list<ObjectPtr>& aRefFeaturesList = aRefIt->second;
680 std::list<ObjectPtr>::iterator aReferenced = aRefFeaturesList.begin();
681 for(; aReferenced != aRefFeaturesList.end(); aReferenced++) {
682 // stability is only on results: feature to feature reference mean nested
683 // features, that will remove nesting references
684 if (aReferenced->get() && (*aReferenced)->data()->isValid() &&
685 (*aReferenced)->groupName() != ModelAPI_Feature::group()) {
686 std::shared_ptr<Model_Data> aData =
687 std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
688 if (aFeatureSender->isStable()) {
689 aData->addBackReference(aFeatureSender, aRefIt->first);
691 aData->removeBackReference(aFeatureSender, aRefIt->first);
692 added = true; // remove of concealment may be caused creation of some result
701 static Events_Loop* aLoop = Events_Loop::loop();
702 static Events_ID kEventCreated = aLoop->eventByName(EVENT_OBJECT_CREATED);
703 aLoop->flush(kEventCreated);