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 <Model_AttributeSelection.h>
12 #include <ModelAPI_Feature.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_Document.h>
15 #include <ModelAPI_Events.h>
16 #include <ModelAPI_AttributeReference.h>
17 #include <ModelAPI_AttributeRefList.h>
18 #include <ModelAPI_AttributeRefAttr.h>
19 #include <ModelAPI_AttributeSelection.h>
20 #include <ModelAPI_AttributeSelectionList.h>
21 #include <ModelAPI_Result.h>
22 #include <ModelAPI_ResultPart.h>
23 #include <ModelAPI_Validator.h>
24 #include <ModelAPI_CompositeFeature.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_Tools.h>
27 #include <GeomDataAPI_Point.h>
28 #include <GeomDataAPI_Point2D.h>
29 #include <Events_Loop.h>
30 #include <Events_LongOp.h>
31 #include <Events_InfoMessage.h>
32 #include <Config_PropManager.h>
36 Model_Update MY_UPDATER_INSTANCE; /// the only one instance initialized on load of the library
39 Model_Update::Model_Update()
41 Events_Loop* aLoop = Events_Loop::loop();
42 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
43 aLoop->registerListener(this, kChangedEvent);
44 static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
45 aLoop->registerListener(this, kCreatedEvent);
46 static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
47 aLoop->registerListener(this, kUpdatedEvent);
48 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
49 aLoop->registerListener(this, kOpFinishEvent);
50 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
51 aLoop->registerListener(this, kOpAbortEvent);
52 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
53 aLoop->registerListener(this, kOpStartEvent);
54 static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
55 aLoop->registerListener(this, kStabilityEvent);
56 static const Events_ID kPreviewBlockedEvent = aLoop->eventByName(EVENT_PREVIEW_BLOCKED);
57 aLoop->registerListener(this, kPreviewBlockedEvent);
58 static const Events_ID kPreviewRequestedEvent = aLoop->eventByName(EVENT_PREVIEW_REQUESTED);
59 aLoop->registerListener(this, kPreviewRequestedEvent);
60 static const Events_ID kReorderEvent = aLoop->eventByName(EVENT_ORDER_UPDATED);
61 aLoop->registerListener(this, kReorderEvent);
62 static const Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
63 aLoop->registerListener(this, kUpdatedSel);
65 // Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
66 myIsParamUpdated = false;
68 myIsProcessed = false;
69 myIsPreviewBlocked = false;
72 bool Model_Update::addModified(FeaturePtr theFeature, FeaturePtr theReason) {
74 if (!theFeature->data()->isValid())
75 return false; // delete an extrusion created on the sketch
77 bool isNotExecuted = theFeature->isPersistentResult() &&
78 !std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures();
80 if (!theReason.get()) // no reason => no construction reason
82 if (myNotPersistentRefs.find(theFeature) == myNotPersistentRefs.end()) {
83 myNotPersistentRefs[theFeature].insert(theReason);
85 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
86 aNewSet.insert(theReason);
87 myNotPersistentRefs[theFeature] = aNewSet;
92 // update arguments for "apply button" state change
93 if ((!theFeature->isPreviewNeeded() && !myIsFinish) || myIsPreviewBlocked) {
94 myProcessOnFinish.insert(theFeature);
96 std::cout<<"*** Add process on finish "<<theFeature->name()<<std::endl;
98 // keeps the currently updated features to avoid infinitive cycling here: where feature on
99 // "updateArguments" sends "updated" (in selection attribute) and goes here again
100 static std::set<FeaturePtr> aCurrentlyUpdated;
101 if (aCurrentlyUpdated.find(theFeature) == aCurrentlyUpdated.end()) {
102 aCurrentlyUpdated.insert(theFeature);
103 updateArguments(theFeature);
104 aCurrentlyUpdated.erase(theFeature);
106 // make it without conditions otherwise the apply button may have a bad state
107 theFeature->data()->execState(ModelAPI_StateDone);
108 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
109 aFactory->validate(theFeature); // need to be validated to update the "Apply" state if not previewed
111 // to redisplay split's arguments presentation, even result is not computed
112 if (!theFeature->isPreviewNeeded()) {
113 static Events_Loop* aLoop = Events_Loop::loop();
114 static const Events_ID kRedisplayEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
115 ModelAPI_EventCreator::get()->sendUpdated(theFeature, kRedisplayEvent);
116 aLoop->flush(kRedisplayEvent);
119 if (!myIsPreviewBlocked)
122 if (myModified.find(theFeature) != myModified.end()) {
123 if (theReason.get()) {
125 //std::cout<<"*** Add already modified "<<theFeature->name()<<" reason "<<theReason->name()<<std::endl;
127 myModified[theFeature].insert(theReason);
131 // do not add the disabled, but possibly the sub-elements are not disabled
132 bool aIsDisabled = theFeature->isDisabled();
134 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
135 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated ||
136 theFeature->data()->execState() == ModelAPI_StateInvalidArgument || // issue 1519
137 theFeature->data()->execState() == ModelAPI_StateExecFailed) {
138 // do not forget that in this case all were the reasons
139 aNewSet.insert(theFeature);
142 aNewSet.insert(theReason);
144 myModified[theFeature] = aNewSet;
146 if (theReason.get()) {
147 //std::cout<<"*** Add modified "<<theFeature->name()<<" reason "<<theReason->name()<<std::endl;
149 //std::cout<<"*** Add modified "<<theFeature->name()<<std::endl;
152 } else { // will be updated during the finish of the operation, or when it becomes enabled
153 if (theFeature->data()->execState() == ModelAPI_StateDone ||
154 theFeature->data()->execState() == ModelAPI_StateExecFailed) // fix issue 1819
155 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
157 return true; // do not need iteration deeply if it is already marked as modified or so
159 //std::cout<<"*** Set modified state "<<theFeature->name()<<std::endl;
162 // clear processed and fill modified recursively
163 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
164 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
165 for(; aRefIter != aRefs.cend(); aRefIter++) {
166 if ((*aRefIter)->isArgument()) {
167 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
168 if (aReferenced.get()) {
169 addModified(aReferenced, theFeature);
173 // proccess also results
174 std::list<ResultPtr> allResults; // list of this feature and results
175 ModelAPI_Tools::allResults(theFeature, allResults);
176 std::list<ResultPtr>::iterator aRes = allResults.begin();
177 for(; aRes != allResults.end(); aRes++) {
178 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*aRes)->data()->refsToMe();
179 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
180 for(; aRefIter != aRefs.cend(); aRefIter++) {
181 if ((*aRefIter)->isArgument()) {
182 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
183 if (aReferenced.get()) {
184 addModified(aReferenced, theFeature);
190 // also add part feature that contains this feature to the modified
191 if (theFeature->document()->kind() != "PartSet") {
192 FeaturePtr aPart = ModelAPI_Tools::findPartFeature(
193 ModelAPI_Session::get()->moduleDocument(), theFeature->document());
195 addModified(aPart, theFeature);
200 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
202 static Events_Loop* aLoop = Events_Loop::loop();
203 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
204 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
205 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
206 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
207 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
208 static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
209 static const Events_ID kPreviewBlockedEvent = aLoop->eventByName(EVENT_PREVIEW_BLOCKED);
210 static const Events_ID kPreviewRequestedEvent = aLoop->eventByName(EVENT_PREVIEW_REQUESTED);
211 static const Events_ID kReorderEvent = aLoop->eventByName(EVENT_ORDER_UPDATED);
212 static const Events_ID kRedisplayEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
213 static const Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
216 std::cout<<"****** Event "<<theMessage->eventID().eventText()<<std::endl;
218 if (theMessage->eventID() == kStabilityEvent) {
219 updateStability(theMessage->sender());
222 if (theMessage->eventID() == kPreviewBlockedEvent) {
223 myIsPreviewBlocked = true;
226 if (theMessage->eventID() == kPreviewRequestedEvent) {
227 if (myIsPreviewBlocked) {
228 myIsPreviewBlocked = false;
230 myIsPreviewBlocked = true;
234 if (theMessage->eventID() == kUpdatedSel) {
235 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
236 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
237 updateSelection(aMsg->objects());
239 // creation is added to "update" to avoid recomputation twice: on create and immediately after on update
240 if (theMessage->eventID() == kCreatedEvent) {
241 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
242 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
243 const std::set<ObjectPtr>& anObjs = aMsg->objects();
244 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
245 for(; anObjIter != anObjs.cend(); anObjIter++) {
246 if (std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures()) {
247 if ((*anObjIter)->groupName() == ModelAPI_Feature::group()) { // results creation means enabling, not update
248 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kUpdatedEvent);
250 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kRedisplayEvent);
256 if (theMessage->eventID() == kUpdatedEvent) {
257 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
258 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
259 const std::set<ObjectPtr>& anObjs = aMsg->objects();
260 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
261 bool aSomeModified = false; // check that features not changed: only redisplay is needed
262 for(; anObjIter != anObjs.cend(); anObjIter++) {
263 if (!(*anObjIter)->data()->isValid())
266 std::cout<<">>> in event updated "<<(*anObjIter)->groupName()<<" "<<(*anObjIter)->data()->name()<<std::endl;
268 if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
269 myIsParamUpdated = true;
271 // on undo/redo, abort do not update persisten features
272 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
273 if (anUpdated.get()) {
274 if (addModified(anUpdated, FeaturePtr()))
275 aSomeModified = true;
276 } else { // process the updated result as update of features that refers to this result
277 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*anObjIter)->data()->refsToMe();
278 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
279 for(; aRefIter != aRefs.cend(); aRefIter++) {
280 if (!(*aRefIter)->owner()->data()->isValid())
282 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
283 if (anUpdated.get()) {
284 if (addModified(anUpdated, FeaturePtr()))
285 aSomeModified = true;
290 // this event is for solver update, not here, do not react immediately
294 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
295 theMessage->eventID() == kOpStartEvent) {
296 myIsPreviewBlocked = false;
298 if (theMessage->eventID() == kOpFinishEvent) {
300 // add features that wait for finish as modified
301 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aFeature = myProcessOnFinish.begin();
302 for(; aFeature != myProcessOnFinish.end(); aFeature++)
303 if ((*aFeature)->data()->isValid()) // there may be already removed wait for features
304 addModified(*aFeature, FeaturePtr());
307 myProcessOnFinish.clear(); // processed features must be only on finish, so clear anyway (to avoid reimport on load)
309 if (!(theMessage->eventID() == kOpStartEvent)) {
310 processFeatures(false);
312 // remove all macros before clearing all created
313 std::set<FeaturePtr>::iterator anUpdatedIter = myWaitForFinish.begin();
314 while(anUpdatedIter != myWaitForFinish.end()) {
315 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
316 if (aFeature.get()) {
317 // remove macro on finish
318 if (aFeature->isMacro()) {
319 aFeature->document()->removeFeature(aFeature);
320 myWaitForFinish.erase(aFeature);
322 // to avoid the map update problems on "remove"
323 if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
324 anUpdatedIter = myWaitForFinish.begin();
332 // the redisplay signal should be flushed in order to erase the feature presentation in the viewer
333 // if should be done after removeFeature() of document,
334 // by this reason, upper processFeatures() do not perform this flush
335 Events_Loop::loop()->flush(kRedisplayEvent);
337 // in the end of transaction everything is updated, so clear the old objects
338 myIsParamUpdated = false;
339 myWaitForFinish.clear();
340 } else if (theMessage->eventID() == kReorderEvent) {
341 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aMsg =
342 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
343 if (aMsg->reordered().get())
344 addModified(aMsg->reordered(), aMsg->reordered()); // to update all attributes
348 void Model_Update::processFeatures(const bool theFlushRedisplay)
350 // perform update of everything if it is not performed right now or any preview is blocked
351 if (!myIsProcessed && !myIsPreviewBlocked) {
352 myIsProcessed = true;
354 std::cout<<"****** Start processing"<<std::endl;
357 while(!myModified.empty()) {
358 processFeature(myModified.begin()->first);
360 myIsProcessed = false;
362 // to update the object browser if something is updated/created during executions
363 static Events_Loop* aLoop = Events_Loop::loop();
364 static const Events_ID kCreatedEvent= aLoop->eventByName(EVENT_OBJECT_CREATED);
365 aLoop->flush(kCreatedEvent);
366 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
367 aLoop->flush(kUpdatedEvent);
369 // flush to update display
370 if (theFlushRedisplay) {
371 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
372 aLoop->flush(EVENT_DISP);
375 std::cout<<"****** End processing"<<std::endl;
381 // collects all the feautres this feature depends on: reasons
382 static void allReasons(FeaturePtr theFeature, std::set<FeaturePtr>& theReasons) {
383 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aDeps;
384 theFeature->data()->referencesToObjects(aDeps);
385 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > >::iterator
386 anAttrsIter = aDeps.begin();
387 for(; anAttrsIter != aDeps.end(); anAttrsIter++) {
388 if (theFeature->attribute(anAttrsIter->first)->isArgument()) {
389 std::list<std::shared_ptr<ModelAPI_Object> >::iterator aDepIter = anAttrsIter->second.begin();
390 for(; aDepIter != anAttrsIter->second.end(); aDepIter++) {
391 FeaturePtr aDepFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(*aDepIter);
392 if (!aDepFeat.get()) { // so, it depends on the result and process the feature owner of it
393 ResultPtr aDepRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aDepIter);
395 aDepFeat = (*aDepIter)->document()->feature(aDepRes);
398 if (aDepFeat.get() && aDepFeat->data()->isValid()) {
399 theReasons.insert(aDepFeat);
404 if (theFeature->getKind() == "Part") { // part is not depended on its subs directly, but subs must be iterated anyway
405 CompositeFeaturePtr aPart = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
406 int aNum = aPart->numberOfSubs();
407 for(int a = 0; a < aNum; a++) {
408 FeaturePtr aSub = aPart->subFeature(a);
409 if (aSub.get() && aSub->data()->isValid()) {
410 theReasons.insert(aSub);
416 bool Model_Update::processFeature(FeaturePtr theFeature)
418 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
420 if (!theFeature->data()->isValid()) { // deleted feature, just remove from all containers
421 if (myModified.find(theFeature) != myModified.end())
422 myModified.erase(theFeature);
426 if (theFeature->isPersistentResult()) {
427 if (!std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures())
431 if (myProcessed.find(theFeature) == myProcessed.end()) {
432 myProcessed[theFeature] = 0;
434 int aCount = myProcessed[theFeature];
435 if (aCount > 100) { // too many repetition of processing (in VS it may crash on 330 with stack overflow)
436 Events_InfoMessage("Model_Update",
437 "Feature '%1' is updated in infinitive loop").arg(theFeature->data()->name()).send();
440 myProcessed[theFeature] = aCount + 1;
443 // check this feature is not yet checked or processed
444 bool aIsModified = myModified.find(theFeature) != myModified.end();
445 if (!aIsModified && myIsFinish) { // get info about the modification for features without preview
446 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
448 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
449 aNewSet.insert(theFeature); // contains itself, so, we don't know which was the reason and the reason is any
450 myModified[theFeature] = aNewSet;
455 std::cout<<"* Process feature "<<theFeature->name()<<std::endl;
458 // update the sketch plane before the sketch sub-elements are recomputed
459 // (otherwise sketch will update plane, modify subs, after executed, but with old subs edges)
460 if (aIsModified && theFeature->getKind() == "Sketch") {
462 std::cout<<"****** Update sketch args "<<theFeature->name()<<std::endl;
464 AttributeSelectionPtr anExtSel = theFeature->selection("External");
465 if (anExtSel.get()) {
466 ResultPtr aContext = anExtSel->context();
467 if (aContext.get() && aContext->document().get()) {
468 FeaturePtr anExtBase = aContext->document()->feature(aContext);
469 if (anExtBase.get()) {
470 processFeature(anExtBase);
474 updateArguments(theFeature);
475 // send event that sketch is prepared to be recomputed
476 static Events_ID anID = Events_Loop::eventByName("SketchPrepared");
477 std::shared_ptr<Events_Message> aMsg(new Events_Message(anID, this));
478 Events_Loop::loop()->send(aMsg);
481 if (!aIsModified) { // no modification is needed
485 // evaluate parameter before the sub-elements update: it updates dependencies on evaluation (#1085)
486 if (theFeature->getKind() == "Parameter") {
487 theFeature->execute();
490 bool isReferencedInvalid = false;
491 // check all features this feature depended on (recursive call of updateFeature)
492 std::set<FeaturePtr>& aReasons = myModified[theFeature];
493 bool allSubsUsed = aReasons.find(theFeature) != aReasons.end();
494 if (allSubsUsed) { // add all subs in aReasons and temporary remove "theFeature" to avoid processing itself
495 allReasons(theFeature, aReasons);
496 aReasons.erase(theFeature);
498 // take reasons one by one (they may be added during the feature process (circle by the radius of sketch)
499 std::set<FeaturePtr> aProcessedReasons;
500 while(!aReasons.empty()) {
501 FeaturePtr aReason = *(aReasons.begin());
503 //cout<<theFeature->name()<<" process next reason "<<aReason->name()<<endl;
505 if (aReason != theFeature && (aReason)->data()->isValid()) {
506 if (processFeature(aReason))
508 if (aReason->data()->execState() == ModelAPI_StateInvalidArgument)
509 isReferencedInvalid = true;
511 // searching for the next not used reason
512 aProcessedReasons.insert(aReason);
513 aReasons.erase(aReason);
515 // restore the modified reasons: they will be used in the update of arguments
516 if (allSubsUsed) { // restore theFeature in this set
517 aProcessedReasons.insert(theFeature);
519 myModified[theFeature] = aProcessedReasons;
521 // do not execute the composite that contains the current
522 bool isPostponedMain = false;
523 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
524 if (theFeature->getKind() == "ExtrusionSketch" && aCompos.get()) {
525 CompositeFeaturePtr aCurrentOwner =
526 ModelAPI_Tools::compositeOwner(theFeature->document()->currentFeature(false));
527 isPostponedMain = aCurrentOwner.get() && aCompos->isSub(aCurrentOwner);
531 std::cout<<"Update args "<<theFeature->name()<<std::endl;
533 // TestImport.py : after arguments are updated, theFeature may be removed
534 if (!theFeature->data()->isValid())
536 // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
537 updateArguments(theFeature);
539 // add this feature to the processed right now to be able remove it from this list on
540 // update signal during this feature execution
541 myModified.erase(theFeature);
542 if (myNotPersistentRefs.find(theFeature) != myNotPersistentRefs.end())
543 myNotPersistentRefs.erase(theFeature);
544 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
545 theFeature->data()->execState(ModelAPI_StateDone);
547 // this checking must be after the composite feature sub-elements processing:
548 // composite feature status may depend on it's subelements
549 if ((theFeature->data()->execState() == ModelAPI_StateInvalidArgument || isReferencedInvalid) &&
550 theFeature->getKind() != "Part") { // don't disable Part because it will make disabled all the features (performance and problems with the current feature)
552 std::cout<<"Invalid args "<<theFeature->name()<<std::endl;
554 theFeature->eraseResults();
555 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
556 return true; // so, feature is modified (results are erased)
559 // execute feature if it must be updated
560 ModelAPI_ExecState aState = theFeature->data()->execState();
561 if (aFactory->validate(theFeature)) {
562 if (!isPostponedMain) {
563 executeFeature(theFeature);
567 std::cout<<"Feature is not valid, erase results "<<theFeature->name()<<std::endl;
569 theFeature->eraseResults();
570 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
575 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
577 // make updated and redisplay all results
578 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
580 std::list<ResultPtr> allResults;
581 ModelAPI_Tools::allResults(theFeature, allResults);
582 std::list<ResultPtr>::iterator aRIter = allResults.begin();
583 for (; aRIter != allResults.cend(); aRIter++) {
584 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
585 if (!aRes->isDisabled()) {// update state only for enabled results (Placement Result Part may make the original Part Result as invalid)
586 aRes->data()->execState(theState);
588 if (theFeature->data()->updateID() > aRes->data()->updateID()) {
589 aRes->data()->setUpdateID(theFeature->data()->updateID());
591 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
593 // to redisplay "presentable" feature (for ex. distance constraint)
594 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
595 theFeature->data()->execState(theState);
598 /// Updates the state by the referenced object: if something bad with it, set state for this one
599 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
602 ModelAPI_ExecState aRefState = theTarget->data()->execState();
603 if (aRefState == ModelAPI_StateMustBeUpdated) {
604 if (theCurrent == ModelAPI_StateDone)
605 return ModelAPI_StateMustBeUpdated;
606 } else if (aRefState != ModelAPI_StateDone) {
607 return ModelAPI_StateInvalidArgument;
613 void Model_Update::updateArguments(FeaturePtr theFeature) {
614 // perform this method also for disabled features: to make "not done" state for
615 // features referenced to the active and modified features
617 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
619 ModelAPI_ExecState aState = theFeature->data()->execState();
620 if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
621 aState = ModelAPI_StateMustBeUpdated;
623 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
624 aState = ModelAPI_StateMustBeUpdated;
625 // check the parameters state
628 std::list<AttributePtr> anAttrinbutes =
629 theFeature->data()->attributes(ModelAPI_AttributeInteger::typeId());
630 std::list<AttributePtr>::iterator anIter = anAttrinbutes.begin();
631 for(; anIter != anAttrinbutes.end(); anIter++) {
632 AttributeIntegerPtr anAttribute =
633 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(*anIter);
634 if (anAttribute.get() && !anAttribute->text().empty()) {
635 if (myIsParamUpdated) {
636 ModelAPI_AttributeEvalMessage::send(anAttribute, this);
638 if (anAttribute->expressionInvalid()) {
639 aState = ModelAPI_StateInvalidArgument;
646 std::list<AttributePtr> aDoubles =
647 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
648 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
649 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
650 AttributeDoublePtr aDouble =
651 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
652 if (aDouble.get() && !aDouble->text().empty()) {
653 if (myIsParamUpdated) {
654 ModelAPI_AttributeEvalMessage::send(aDouble, this);
656 if (aDouble->expressionInvalid()) {
657 aState = ModelAPI_StateInvalidArgument;
664 std::list<AttributePtr> anAttributes =
665 theFeature->data()->attributes(GeomDataAPI_Point::typeId());
666 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
667 for(; anIter != anAttributes.end(); anIter++) {
668 AttributePointPtr aPointAttribute =
669 std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
670 if (aPointAttribute.get() && (!aPointAttribute->textX().empty() ||
671 !aPointAttribute->textY().empty() || !aPointAttribute->textZ().empty())) {
672 if (myIsParamUpdated) {
673 ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
675 if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
676 (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
677 (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
678 aState = ModelAPI_StateInvalidArgument;
684 std::list<AttributePtr> anAttributes =
685 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
686 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
687 for(; anIter != anAttributes.end(); anIter++) {
688 AttributePoint2DPtr aPoint2DAttribute =
689 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
690 if (aPoint2DAttribute.get()) {
691 if (myIsParamUpdated && (!aPoint2DAttribute->textX().empty() ||
692 !aPoint2DAttribute->textY().empty())) {
693 ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
695 if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
696 (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
697 aState = ModelAPI_StateInvalidArgument;
701 // update the selection attributes if any
702 list<AttributePtr> aRefs =
703 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
704 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
705 for (; aRefsIter != aRefs.end(); aRefsIter++) {
706 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
707 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
708 ObjectPtr aContext = aSel->context();
709 // update argument only if the referenced object is ready to use
710 if (aContext.get() && !aContext->isDisabled()) {
711 if (isReason(theFeature, aContext)) {
712 if (!aSel->update()) { // this must be done on execution since it may be long operation
713 bool isObligatory = !aFactory->isNotObligatory(
714 theFeature->getKind(), theFeature->data()->id(aSel)) &&
715 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
717 aState = ModelAPI_StateInvalidArgument;
720 } else if (aContext.get()) {
721 // here it may be not obligatory, but if the reference is wrong, it should not be correct
722 bool isObligatory = aFactory->isCase(theFeature, theFeature->data()->id(aSel));
724 aState = ModelAPI_StateInvalidArgument;
727 // update the selection list attributes if any
728 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
729 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
730 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
731 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
732 for(int a = aSel->size() - 1; a >= 0; a--) {
733 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
734 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
736 ObjectPtr aContext = aSelAttr->context();
737 // update argument only if the referenced object is ready to use
738 if (aContext.get() && !aContext->isDisabled()) {
739 if (isReason(theFeature, aContext)) {
740 if (!aSelAttr->update()) {
741 bool isObligatory = !aFactory->isNotObligatory(
742 theFeature->getKind(), theFeature->data()->id(aSel)) &&
743 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
745 aState = ModelAPI_StateInvalidArgument;
748 } else if (aContext.get()) {
749 // here it may be not obligatory, but if the reference is wrong, it should not be correct
750 bool isObligatory = aFactory->isCase(theFeature, theFeature->data()->id(aSel));
752 aState = ModelAPI_StateInvalidArgument;
758 if (aState != ModelAPI_StateDone)
759 theFeature->data()->execState(aState);
762 bool Model_Update::isReason(std::shared_ptr<ModelAPI_Feature>& theFeature,
763 std::shared_ptr<ModelAPI_Object> theReason)
765 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
766 ::iterator aReasonsIt = myModified.find(theFeature);
767 if (aReasonsIt != myModified.end()) {
768 if (aReasonsIt->second.find(theFeature) != aReasonsIt->second.end())
769 return true; // any is reason if it contains itself
770 FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
771 if (!aReasFeat.get()) { // try to get feature of this result
772 ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
774 aReasFeat = theReason->document()->feature(aReasRes);
776 if (aReasonsIt->second.find(aReasFeat) != aReasonsIt->second.end())
779 // another try: postponed modification by not-persistences
780 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
781 ::iterator aNotPersist = myNotPersistentRefs.find(theFeature);
782 if (aNotPersist != myNotPersistentRefs.end()) {
783 FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
784 if (!aReasFeat.get()) { // try to get feature of this result
785 ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
787 aReasFeat = theReason->document()->feature(aReasRes);
789 if (aNotPersist->second.find(aReasFeat) != aNotPersist->second.end())
793 return false; // this case only for not-previewed items update state, nothing is changed in args for it
796 void Model_Update::executeFeature(FeaturePtr theFeature)
799 std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
801 // execute in try-catch to avoid internal problems of the feature
802 ModelAPI_ExecState aState = ModelAPI_StateDone;
803 theFeature->data()->execState(ModelAPI_StateDone);
805 theFeature->execute();
806 if (theFeature->data()->execState() != ModelAPI_StateDone) {
807 aState = ModelAPI_StateExecFailed;
809 aState = ModelAPI_StateDone;
812 aState = ModelAPI_StateExecFailed;
813 Events_InfoMessage("Model_Update",
814 "Feature %1 has failed during the execution").arg(theFeature->getKind()).send();
816 // The macro feature has to be deleted in any case even its execution is failed
817 myWaitForFinish.insert(theFeature);
818 if (aState != ModelAPI_StateDone) {
819 theFeature->eraseResults();
821 theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
822 redisplayWithResults(theFeature, aState);
825 void Model_Update::updateStability(void* theSender)
827 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
829 bool added = false; // object may be was crated
830 ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theSender);
831 if (aSender && aSender->document()) {
832 FeaturePtr aFeatureSender =
833 std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
834 if (aFeatureSender.get()) {
835 Model_Objects* aDocObjects =
836 std::dynamic_pointer_cast<Model_Document>(aSender->document())->objects();
838 //aDocObjects->synchronizeBackRefs();
839 // remove or add all concealment refs from this feature
840 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
841 aSender->data()->referencesToObjects(aRefs);
842 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIt = aRefs.begin();
843 for(; aRefIt != aRefs.end(); aRefIt++) {
844 if (!aFactory->isConcealed(aFeatureSender->getKind(), aRefIt->first))
845 continue; // take into account only concealed references (do not remove the sketch constraint and the edge on constraint edit)
846 std::list<ObjectPtr>& aRefFeaturesList = aRefIt->second;
847 std::list<ObjectPtr>::iterator aReferenced = aRefFeaturesList.begin();
848 for(; aReferenced != aRefFeaturesList.end(); aReferenced++) {
849 // stability is only on results: feature to feature reference mean nested
850 // features, that will remove nesting references
851 if (aReferenced->get() && (*aReferenced)->data()->isValid() &&
852 (*aReferenced)->groupName() != ModelAPI_Feature::group()) {
853 std::shared_ptr<Model_Data> aData =
854 std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
855 if (aFeatureSender->isStable()) {
856 aData->addBackReference(aFeatureSender, aRefIt->first);
858 aData->removeBackReference(aFeatureSender, aRefIt->first);
859 added = true; // remove of concealment may be caused creation of some result
868 static Events_Loop* aLoop = Events_Loop::loop();
869 static Events_ID kEventCreated = aLoop->eventByName(EVENT_OBJECT_CREATED);
870 aLoop->flush(kEventCreated);
875 void Model_Update::updateSelection(const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects)
877 std::set<std::shared_ptr<ModelAPI_Object> >::iterator anObj = theObjects.begin();
878 for(; anObj != theObjects.end(); anObj++) {
879 list<AttributePtr> aRefs =
880 (*anObj)->data()->attributes(ModelAPI_AttributeSelection::typeId());
881 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
882 for (; aRefsIter != aRefs.end(); aRefsIter++) {
883 std::shared_ptr<Model_AttributeSelection> aSel =
884 std::dynamic_pointer_cast<Model_AttributeSelection>(*aRefsIter);
885 aSel->updateInHistory();
887 // update the selection list attributes if any
888 aRefs = (*anObj)->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
889 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
890 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
891 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
892 for(int a = aSel->size() - 1; a >= 0; a--) {
893 std::shared_ptr<Model_AttributeSelection> aSelAttr =
894 std::dynamic_pointer_cast<Model_AttributeSelection>(aSel->value(a));
896 aSelAttr->updateInHistory();