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);
55 static const Events_ID kPreviewBlockedEvent = aLoop->eventByName(EVENT_PREVIEW_BLOCKED);
56 aLoop->registerListener(this, kPreviewBlockedEvent);
57 static const Events_ID kPreviewRequestedEvent = aLoop->eventByName(EVENT_PREVIEW_REQUESTED);
58 aLoop->registerListener(this, kPreviewRequestedEvent);
60 // Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
61 myIsParamUpdated = false;
63 myIsProcessed = false;
64 myIsPreviewBlocked = false;
67 bool Model_Update::addModified(FeaturePtr theFeature, FeaturePtr theReason) {
69 if (!theFeature->data()->isValid())
70 return false; // delete an extrusion created on the sketch
72 if (theFeature->isPersistentResult()) {
73 if (!std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures())
77 if (!theFeature->isPreviewNeeded() && !myIsFinish) {
78 myProcessOnFinish.insert(theFeature);
80 std::cout<<"*** Add process on finish "<<theFeature->name()<<std::endl;
82 updateArguments(theFeature);
83 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
84 theFeature->data()->execState(ModelAPI_StateDone);
85 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
86 aFactory->validate(theFeature); // need to be validated to update the "Apply" state if not previewed
90 if (myModified.find(theFeature) != myModified.end()) {
91 if (theReason.get()) {
93 std::cout<<"*** Add already modified "<<theFeature->name()<<std::endl;
95 myModified[theFeature].insert(theReason);
99 // do not add the disabled, but possibly the sub-elements are not disabled
100 bool aIsDisabled = theFeature->isDisabled();
102 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
103 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
104 // do not forget that in this case all were the reasons
105 aNewSet.insert(theFeature);
108 aNewSet.insert(theReason);
110 myModified[theFeature] = aNewSet;
112 std::cout<<"*** Add modified "<<theFeature->name()<<std::endl;
114 } else { // will be updated during the finish of the operation, or when it becomes enabled
115 if (theFeature->data()->execState() == ModelAPI_StateDone)
116 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
118 return true; // do not need iteration deeply if it is already marked as modified or so
120 std::cout<<"*** Set modified state "<<theFeature->name()<<std::endl;
123 // clear processed and fill modified recursively
124 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
125 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
126 for(; aRefIter != aRefs.cend(); aRefIter++) {
127 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
128 if (aReferenced.get()) {
129 addModified(aReferenced, theFeature);
132 // proccess also results
133 std::list<ResultPtr> allResults; // list of this feature and results
134 ModelAPI_Tools::allResults(theFeature, allResults);
135 std::list<ResultPtr>::iterator aRes = allResults.begin();
136 for(; aRes != allResults.end(); aRes++) {
137 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*aRes)->data()->refsToMe();
138 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
139 for(; aRefIter != aRefs.cend(); aRefIter++) {
140 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
141 if (aReferenced.get()) {
142 addModified(aReferenced, theFeature);
147 // also add part feature that contains this feature to the modified
148 if (theFeature->document()->kind() != "PartSet") {
149 FeaturePtr aPart = ModelAPI_Tools::findPartFeature(
150 ModelAPI_Session::get()->moduleDocument(), theFeature->document());
152 addModified(aPart, theFeature);
157 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
159 static Events_Loop* aLoop = Events_Loop::loop();
160 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
161 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
162 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
163 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
164 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
165 static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
166 static const Events_ID kPreviewBlockedEvent = aLoop->eventByName(EVENT_PREVIEW_BLOCKED);
167 static const Events_ID kPreviewRequestedEvent = aLoop->eventByName(EVENT_PREVIEW_REQUESTED);
170 std::cout<<"****** Event "<<theMessage->eventID().eventText()<<std::endl;
172 if (theMessage->eventID() == kStabilityEvent) {
173 updateStability(theMessage->sender());
176 if (theMessage->eventID() == kPreviewBlockedEvent) {
177 myIsPreviewBlocked = true;
180 if (theMessage->eventID() == kPreviewRequestedEvent) {
181 if (myIsPreviewBlocked) {
182 myIsPreviewBlocked = false;
184 myIsPreviewBlocked = true;
188 // creation is added to "update" to avoid recomputation twice: on create and immediately after on update
189 if (theMessage->eventID() == kCreatedEvent) {
190 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
191 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
192 const std::set<ObjectPtr>& anObjs = aMsg->objects();
193 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
194 for(; anObjIter != anObjs.cend(); anObjIter++) {
195 if (std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures())
196 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kUpdatedEvent);
200 if (theMessage->eventID() == kUpdatedEvent) {
201 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
202 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
203 const std::set<ObjectPtr>& anObjs = aMsg->objects();
204 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
205 bool aSomeModified = false; // check that features not changed: only redisplay is needed
206 for(; anObjIter != anObjs.cend(); anObjIter++) {
207 if (!(*anObjIter)->data()->isValid())
210 std::cout<<">>> in event updated "<<(*anObjIter)->data()->name()<<std::endl;
212 if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
213 myIsParamUpdated = true;
215 // on undo/redo, abort do not update persisten features
216 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
217 if (anUpdated.get()) {
218 if (addModified(anUpdated, FeaturePtr()))
219 aSomeModified = true;
220 } else { // process the updated result as update of features that refers to this result
221 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*anObjIter)->data()->refsToMe();
222 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
223 for(; aRefIter != aRefs.cend(); aRefIter++) {
224 if (!(*aRefIter)->owner()->data()->isValid())
226 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
227 if (anUpdated.get()) {
228 if (addModified(anUpdated, FeaturePtr()))
229 aSomeModified = true;
234 // this event is for solver update, not here, do not react immediately
238 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
239 theMessage->eventID() == kOpStartEvent) {
240 myIsPreviewBlocked = false;
242 if (theMessage->eventID() == kOpFinishEvent) {
244 // add features that wait for finish as modified
245 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aFeature = myProcessOnFinish.begin();
246 for(; aFeature != myProcessOnFinish.end(); aFeature++)
247 if ((*aFeature)->data()->isValid()) // there may be already removed wait for features
248 addModified(*aFeature, FeaturePtr());
251 myProcessOnFinish.clear(); // processed features must be only on finish, so clear anyway (to avoid reimport on load)
253 if (!(theMessage->eventID() == kOpStartEvent)) {
256 // remove all macros before clearing all created
257 std::set<FeaturePtr>::iterator anUpdatedIter = myWaitForFinish.begin();
258 while(anUpdatedIter != myWaitForFinish.end()) {
259 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
260 if (aFeature.get()) {
261 // remove macro on finish
262 if (aFeature->isMacro()) {
263 aFeature->document()->removeFeature(aFeature);
264 myWaitForFinish.erase(aFeature);
266 // to avoid the map update problems on "remove"
267 if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
268 anUpdatedIter = myWaitForFinish.begin();
276 // in the end of transaction everything is updated, so clear the old objects
277 myIsParamUpdated = false;
278 myWaitForFinish.clear();
282 void Model_Update::processFeatures()
284 // perform update of everything if it is not performed right now or any preview is blocked
285 if (!myIsProcessed && !myIsPreviewBlocked) {
286 myIsProcessed = true;
288 std::cout<<"****** Start processing"<<std::endl;
291 while(!myModified.empty()) {
292 processFeature(myModified.begin()->first);
294 myIsProcessed = false;
296 // flush updates just before "myModification" increment: to distinguish
297 // updates by "execute" produced by this updater and other updates, coming outside,
298 // which are really important for "processEvent" of this updater
299 static Events_Loop* aLoop = Events_Loop::loop();
300 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
301 aLoop->flush(kUpdatedEvent);
303 // flush to update display
304 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
305 aLoop->flush(EVENT_DISP);
307 std::cout<<"****** End processing"<<std::endl;
313 bool Model_Update::processFeature(FeaturePtr theFeature)
315 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
317 if (!theFeature->data()->isValid()) { // deleted feature, just remove from all containers
318 if (myModified.find(theFeature) != myModified.end())
319 myModified.erase(theFeature);
323 if (theFeature->isPersistentResult()) {
324 if (!std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures())
328 if (myProcessed.find(theFeature) == myProcessed.end()) {
329 myProcessed[theFeature] = 0;
331 int aCount = myProcessed[theFeature];
332 if (aCount > 100) { // too many repetition of processing (in VS it may crash on 330 with stack overflow)
334 "Feature '" + theFeature->data()->name() + "' is updtated in infinitive loop");
337 myProcessed[theFeature] = aCount + 1;
340 // check this feature is not yet checked or processed
341 bool aIsModified = myModified.find(theFeature) != myModified.end();
342 if (!aIsModified && myIsFinish) { // get info about the modification for features without preview
343 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
345 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
346 aNewSet.insert(theFeature); // contains itself, so, we don't know which was the reason and the reason is any
347 myModified[theFeature] = aNewSet;
352 std::cout<<"* Process feature "<<theFeature->name()<<std::endl;
355 // update the sketch plane before the sketch sub-elements are recomputed
356 // (otherwise sketch will update plane, modify subs, after executed, but with old subs edges)
357 if (aIsModified && theFeature->getKind() == "Sketch") {
359 std::cout<<"****** Update sketch args "<<theFeature->name()<<std::endl;
361 AttributeSelectionPtr anExtSel = theFeature->selection("External");
362 if (anExtSel.get()) {
363 ResultPtr aContext = anExtSel->context();
364 if (aContext.get() && aContext->document().get()) {
365 FeaturePtr anExtBase = aContext->document()->feature(aContext);
366 if (anExtBase.get()) {
367 processFeature(anExtBase);
371 updateArguments(theFeature);
374 if (!aIsModified) { // no modification is needed
378 // check all features this feature depended on (recursive call of updateFeature)
379 std::set<std::shared_ptr<ModelAPI_Feature> >& aReasons = myModified[theFeature];
380 if (aReasons.find(theFeature) == aReasons.end()) {
381 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aReasonIter = aReasons.begin();
382 for(; aReasonIter != aReasons.end(); aReasonIter++) {
383 if (*aReasonIter != theFeature && processFeature(*aReasonIter)) {
387 } else { // check all features this feature depended on because here which one was modified is unknown
388 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aDeps;
389 theFeature->data()->referencesToObjects(aDeps);
390 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > >::iterator
391 anAttrsIter = aDeps.begin();
392 for(; anAttrsIter != aDeps.end(); anAttrsIter++) {
393 std::list<std::shared_ptr<ModelAPI_Object> >::iterator aDepIter = anAttrsIter->second.begin();
394 for(; aDepIter != anAttrsIter->second.end(); aDepIter++) {
395 FeaturePtr aDepFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(*aDepIter);
396 if (!aDepFeat.get()) { // so, it depends on the result and process the feature owner of it
397 ResultPtr aDepRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aDepIter);
399 aDepFeat = (*aDepIter)->document()->feature(aDepRes);
402 if (aDepFeat.get()) {
403 if (processFeature(aDepFeat))
408 if (theFeature->getKind() == "Part") { // part is not depended on its subs directly, but subs must be iterated anyway
409 CompositeFeaturePtr aPart = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
410 int aNum = aPart->numberOfSubs();
411 for(int a = 0; a < aNum; a++) {
412 FeaturePtr aSub = aPart->subFeature(a);
414 if (processFeature(aSub))
421 // do not execute the composite that contains the current
422 bool isPostponedMain = false;
423 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
424 if (theFeature->getKind() == "ExtrusionSketch" && aCompos.get()) {
425 CompositeFeaturePtr aCurrentOwner =
426 ModelAPI_Tools::compositeOwner(theFeature->document()->currentFeature(false));
427 isPostponedMain = aCurrentOwner.get() && aCompos->isSub(aCurrentOwner);
431 std::cout<<"Update args "<<theFeature->name()<<std::endl;
433 // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
434 updateArguments(theFeature);
436 // add this feature to the processed right now to be able remove it from this list on
437 // update signal during this feature execution
438 myModified.erase(theFeature);
439 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
440 theFeature->data()->execState(ModelAPI_StateDone);
442 // this checking must be after the composite feature sub-elements processing:
443 // composite feature status may depend on it's subelements
444 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
446 std::cout<<"Invalid args "<<theFeature->name()<<std::endl;
448 theFeature->eraseResults();
449 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
450 return true; // so, feature is modified (results are erased)
453 // execute feature if it must be updated
454 ModelAPI_ExecState aState = theFeature->data()->execState();
455 if (aFactory->validate(theFeature)) {
456 if (!isPostponedMain) {
457 executeFeature(theFeature);
461 std::cout<<"Feature is not valid, erase results "<<theFeature->name()<<std::endl;
463 theFeature->eraseResults();
464 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
469 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
471 // make updated and redisplay all results
472 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
474 std::list<ResultPtr> allResults;
475 ModelAPI_Tools::allResults(theFeature, allResults);
476 std::list<ResultPtr>::iterator aRIter = allResults.begin();
477 for (; aRIter != allResults.cend(); aRIter++) {
478 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
479 if (!aRes->isDisabled()) {// update state only for enabled results (Placement Result Part may make the original Part Result as invalid)
480 aRes->data()->execState(theState);
482 if (theFeature->data()->updateID() > aRes->data()->updateID()) {
483 aRes->data()->setUpdateID(theFeature->data()->updateID());
485 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
487 // to redisplay "presentable" feature (for ex. distance constraint)
488 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
489 theFeature->data()->execState(theState);
492 /// Updates the state by the referenced object: if something bad with it, set state for this one
493 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
496 ModelAPI_ExecState aRefState = theTarget->data()->execState();
497 if (aRefState == ModelAPI_StateMustBeUpdated) {
498 if (theCurrent == ModelAPI_StateDone)
499 return ModelAPI_StateMustBeUpdated;
500 } else if (aRefState != ModelAPI_StateDone) {
501 return ModelAPI_StateInvalidArgument;
507 void Model_Update::updateArguments(FeaturePtr theFeature) {
508 // perform this method also for disabled features: to make "not done" state for
509 // features referenced to the active and modified features
511 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
513 ModelAPI_ExecState aState = theFeature->data()->execState();
514 if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
515 aState = ModelAPI_StateMustBeUpdated;
517 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
518 aState = ModelAPI_StateMustBeUpdated;
519 // check the parameters state
522 std::list<AttributePtr> anAttrinbutes =
523 theFeature->data()->attributes(ModelAPI_AttributeInteger::typeId());
524 std::list<AttributePtr>::iterator anIter = anAttrinbutes.begin();
525 for(; anIter != anAttrinbutes.end(); anIter++) {
526 AttributeIntegerPtr anAttribute =
527 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(*anIter);
528 if (anAttribute.get() && !anAttribute->text().empty()) {
529 if (myIsParamUpdated) {
530 ModelAPI_AttributeEvalMessage::send(anAttribute, this);
532 if (anAttribute->expressionInvalid()) {
533 aState = ModelAPI_StateInvalidArgument;
540 std::list<AttributePtr> aDoubles =
541 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
542 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
543 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
544 AttributeDoublePtr aDouble =
545 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
546 if (aDouble.get() && !aDouble->text().empty()) {
547 if (myIsParamUpdated) {
548 ModelAPI_AttributeEvalMessage::send(aDouble, this);
550 if (aDouble->expressionInvalid()) {
551 aState = ModelAPI_StateInvalidArgument;
558 std::list<AttributePtr> anAttributes =
559 theFeature->data()->attributes(GeomDataAPI_Point::typeId());
560 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
561 for(; anIter != anAttributes.end(); anIter++) {
562 AttributePointPtr aPointAttribute =
563 std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
564 if (aPointAttribute.get()) {
565 if (myIsParamUpdated) {
566 ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
568 if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
569 (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
570 (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
571 aState = ModelAPI_StateInvalidArgument;
577 std::list<AttributePtr> anAttributes =
578 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
579 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
580 for(; anIter != anAttributes.end(); anIter++) {
581 AttributePoint2DPtr aPoint2DAttribute =
582 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
583 if (aPoint2DAttribute.get()) {
584 if (myIsParamUpdated) {
585 ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
587 if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
588 (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
589 aState = ModelAPI_StateInvalidArgument;
593 // update the selection attributes if any
594 list<AttributePtr> aRefs =
595 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
596 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
597 for (; aRefsIter != aRefs.end(); aRefsIter++) {
598 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
599 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
600 ObjectPtr aContext = aSel->context();
601 // update argument only if the referenced object is ready to use
602 if (aContext.get() && !aContext->isDisabled() && isReason(theFeature, aContext)) {
603 if (!aSel->update()) { // this must be done on execution since it may be long operation
604 bool isObligatory = !aFactory->isNotObligatory(
605 theFeature->getKind(), theFeature->data()->id(aSel)) &&
606 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
608 aState = ModelAPI_StateInvalidArgument;
612 // update the selection list attributes if any
613 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
614 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
615 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
616 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
617 for(int a = aSel->size() - 1; a >= 0; a--) {
618 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
619 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
621 ObjectPtr aContext = aSelAttr->context();
622 // update argument only if the referenced object is ready to use
623 if (aContext.get() && !aContext->isDisabled() && isReason(theFeature, aContext)) {
624 if (!aSelAttr->update()) {
625 bool isObligatory = !aFactory->isNotObligatory(
626 theFeature->getKind(), theFeature->data()->id(aSel)) &&
627 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
629 aState = ModelAPI_StateInvalidArgument;
636 if (aState != ModelAPI_StateDone)
637 theFeature->data()->execState(aState);
640 bool Model_Update::isReason(std::shared_ptr<ModelAPI_Feature>& theFeature,
641 std::shared_ptr<ModelAPI_Object> theReason)
643 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
644 ::iterator aReasonsIt = myModified.find(theFeature);
645 if (aReasonsIt == myModified.end())
646 return false; // this case only for not-previewed items update state, nothing is changed in args for it
647 if (aReasonsIt->second.find(theFeature) != aReasonsIt->second.end())
648 return true; // any is reason if it contains itself
649 FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
650 if (!aReasFeat.get()) { // try to get feature of this result
651 ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
653 aReasFeat = theReason->document()->feature(aReasRes);
655 return aReasonsIt->second.find(aReasFeat) != aReasonsIt->second.end();
659 void Model_Update::executeFeature(FeaturePtr theFeature)
662 std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
664 // execute in try-catch to avoid internal problems of the feature
665 ModelAPI_ExecState aState = ModelAPI_StateDone;
666 theFeature->data()->execState(ModelAPI_StateDone);
668 theFeature->execute();
669 if (theFeature->data()->execState() != ModelAPI_StateDone) {
670 aState = ModelAPI_StateExecFailed;
672 aState = ModelAPI_StateDone;
675 aState = ModelAPI_StateExecFailed;
677 "Feature " + theFeature->getKind() + " has failed during the execution");
679 // The macro feature has to be deleted in any case even its execution is failed
680 myWaitForFinish.insert(theFeature);
681 if (aState != ModelAPI_StateDone) {
682 theFeature->eraseResults();
684 theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
685 redisplayWithResults(theFeature, aState);
688 void Model_Update::updateStability(void* theSender)
690 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
692 bool added = false; // object may be was crated
693 ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theSender);
694 if (aSender && aSender->document()) {
695 FeaturePtr aFeatureSender =
696 std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
697 if (aFeatureSender.get()) {
698 Model_Objects* aDocObjects =
699 std::dynamic_pointer_cast<Model_Document>(aSender->document())->objects();
701 //aDocObjects->synchronizeBackRefs();
702 // remove or add all concealment refs from this feature
703 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
704 aSender->data()->referencesToObjects(aRefs);
705 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIt = aRefs.begin();
706 for(; aRefIt != aRefs.end(); aRefIt++) {
707 if (!aFactory->isConcealed(aFeatureSender->getKind(), aRefIt->first))
708 continue; // take into account only concealed references (do not remove the sketch constraint and the edge on constraint edit)
709 std::list<ObjectPtr>& aRefFeaturesList = aRefIt->second;
710 std::list<ObjectPtr>::iterator aReferenced = aRefFeaturesList.begin();
711 for(; aReferenced != aRefFeaturesList.end(); aReferenced++) {
712 // stability is only on results: feature to feature reference mean nested
713 // features, that will remove nesting references
714 if (aReferenced->get() && (*aReferenced)->data()->isValid() &&
715 (*aReferenced)->groupName() != ModelAPI_Feature::group()) {
716 std::shared_ptr<Model_Data> aData =
717 std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
718 if (aFeatureSender->isStable()) {
719 aData->addBackReference(aFeatureSender, aRefIt->first);
721 aData->removeBackReference(aFeatureSender, aRefIt->first);
722 added = true; // remove of concealment may be caused creation of some result
731 static Events_Loop* aLoop = Events_Loop::loop();
732 static Events_ID kEventCreated = aLoop->eventByName(EVENT_OBJECT_CREATED);
733 aLoop->flush(kEventCreated);