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_InfoMessage.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);
59 static const Events_ID kReorderEvent = aLoop->eventByName(EVENT_ORDER_UPDATED);
60 aLoop->registerListener(this, kReorderEvent);
62 // Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
63 myIsParamUpdated = false;
65 myIsProcessed = false;
66 myIsPreviewBlocked = false;
69 bool Model_Update::addModified(FeaturePtr theFeature, FeaturePtr theReason) {
71 if (!theFeature->data()->isValid())
72 return false; // delete an extrusion created on the sketch
74 bool isNotExecuted = theFeature->isPersistentResult() &&
75 !std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures();
77 if (!theReason.get()) // no reason => no construction reason
79 if (myNotPersistentRefs.find(theFeature) == myNotPersistentRefs.end()) {
80 myNotPersistentRefs[theFeature].insert(theReason);
82 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
83 aNewSet.insert(theReason);
84 myNotPersistentRefs[theFeature] = aNewSet;
89 // update arguments for "apply button" state change
90 if ((!theFeature->isPreviewNeeded() && !myIsFinish) || myIsPreviewBlocked) {
91 myProcessOnFinish.insert(theFeature);
93 std::cout<<"*** Add process on finish "<<theFeature->name()<<std::endl;
95 // keeps the currently updated features to avoid infinitive cycling here: where feature on
96 // "updateArguments" sends "updated" (in selection attribute) and goes here again
97 static std::set<FeaturePtr> aCurrentlyUpdated;
98 if (aCurrentlyUpdated.find(theFeature) == aCurrentlyUpdated.end()) {
99 aCurrentlyUpdated.insert(theFeature);
100 updateArguments(theFeature);
101 aCurrentlyUpdated.erase(theFeature);
103 // make it without conditions otherwise the apply button may have a bad state
104 theFeature->data()->execState(ModelAPI_StateDone);
105 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
106 aFactory->validate(theFeature); // need to be validated to update the "Apply" state if not previewed
108 // to redisplay split's arguments presentation, even result is not computed
109 if (!theFeature->isPreviewNeeded()) {
110 static Events_Loop* aLoop = Events_Loop::loop();
111 static const Events_ID kRedisplayEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
112 ModelAPI_EventCreator::get()->sendUpdated(theFeature, kRedisplayEvent);
113 aLoop->flush(kRedisplayEvent);
116 if (!myIsPreviewBlocked)
119 if (myModified.find(theFeature) != myModified.end()) {
120 if (theReason.get()) {
122 //std::cout<<"*** Add already modified "<<theFeature->name()<<" reason "<<theReason->name()<<std::endl;
124 myModified[theFeature].insert(theReason);
128 // do not add the disabled, but possibly the sub-elements are not disabled
129 bool aIsDisabled = theFeature->isDisabled();
131 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
132 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated ||
133 theFeature->data()->execState() == ModelAPI_StateInvalidArgument) { // issue 1519
134 // do not forget that in this case all were the reasons
135 aNewSet.insert(theFeature);
138 aNewSet.insert(theReason);
140 myModified[theFeature] = aNewSet;
142 if (theReason.get()) {
143 //std::cout<<"*** Add modified "<<theFeature->name()<<" reason "<<theReason->name()<<std::endl;
145 //std::cout<<"*** Add modified "<<theFeature->name()<<std::endl;
148 } else { // will be updated during the finish of the operation, or when it becomes enabled
149 if (theFeature->data()->execState() == ModelAPI_StateDone)
150 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
152 return true; // do not need iteration deeply if it is already marked as modified or so
154 //std::cout<<"*** Set modified state "<<theFeature->name()<<std::endl;
157 // clear processed and fill modified recursively
158 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
159 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
160 for(; aRefIter != aRefs.cend(); aRefIter++) {
161 if ((*aRefIter)->isArgument()) {
162 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
163 if (aReferenced.get()) {
164 addModified(aReferenced, theFeature);
168 // proccess also results
169 std::list<ResultPtr> allResults; // list of this feature and results
170 ModelAPI_Tools::allResults(theFeature, allResults);
171 std::list<ResultPtr>::iterator aRes = allResults.begin();
172 for(; aRes != allResults.end(); aRes++) {
173 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*aRes)->data()->refsToMe();
174 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
175 for(; aRefIter != aRefs.cend(); aRefIter++) {
176 if ((*aRefIter)->isArgument()) {
177 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
178 if (aReferenced.get()) {
179 addModified(aReferenced, theFeature);
185 // also add part feature that contains this feature to the modified
186 if (theFeature->document()->kind() != "PartSet") {
187 FeaturePtr aPart = ModelAPI_Tools::findPartFeature(
188 ModelAPI_Session::get()->moduleDocument(), theFeature->document());
190 addModified(aPart, theFeature);
195 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
197 static Events_Loop* aLoop = Events_Loop::loop();
198 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
199 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
200 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
201 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
202 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
203 static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
204 static const Events_ID kPreviewBlockedEvent = aLoop->eventByName(EVENT_PREVIEW_BLOCKED);
205 static const Events_ID kPreviewRequestedEvent = aLoop->eventByName(EVENT_PREVIEW_REQUESTED);
206 static const Events_ID kReorderEvent = aLoop->eventByName(EVENT_ORDER_UPDATED);
207 static const Events_ID kRedisplayEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
210 std::cout<<"****** Event "<<theMessage->eventID().eventText()<<std::endl;
212 if (theMessage->eventID() == kStabilityEvent) {
213 updateStability(theMessage->sender());
216 if (theMessage->eventID() == kPreviewBlockedEvent) {
217 myIsPreviewBlocked = true;
220 if (theMessage->eventID() == kPreviewRequestedEvent) {
221 if (myIsPreviewBlocked) {
222 myIsPreviewBlocked = false;
224 myIsPreviewBlocked = true;
228 // creation is added to "update" to avoid recomputation twice: on create and immediately after on update
229 if (theMessage->eventID() == kCreatedEvent) {
230 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
231 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
232 const std::set<ObjectPtr>& anObjs = aMsg->objects();
233 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
234 for(; anObjIter != anObjs.cend(); anObjIter++) {
235 if (std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures()) {
236 if ((*anObjIter)->groupName() == ModelAPI_Feature::group()) { // results creation means enabling, not update
237 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kUpdatedEvent);
239 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kRedisplayEvent);
245 if (theMessage->eventID() == kUpdatedEvent) {
246 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
247 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
248 const std::set<ObjectPtr>& anObjs = aMsg->objects();
249 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
250 bool aSomeModified = false; // check that features not changed: only redisplay is needed
251 for(; anObjIter != anObjs.cend(); anObjIter++) {
252 if (!(*anObjIter)->data()->isValid())
255 std::cout<<">>> in event updated "<<(*anObjIter)->groupName()<<" "<<(*anObjIter)->data()->name()<<std::endl;
257 if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
258 myIsParamUpdated = true;
260 // on undo/redo, abort do not update persisten features
261 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
262 if (anUpdated.get()) {
263 if (addModified(anUpdated, FeaturePtr()))
264 aSomeModified = true;
265 } else { // process the updated result as update of features that refers to this result
266 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*anObjIter)->data()->refsToMe();
267 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
268 for(; aRefIter != aRefs.cend(); aRefIter++) {
269 if (!(*aRefIter)->owner()->data()->isValid())
271 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
272 if (anUpdated.get()) {
273 if (addModified(anUpdated, FeaturePtr()))
274 aSomeModified = true;
279 // this event is for solver update, not here, do not react immediately
283 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
284 theMessage->eventID() == kOpStartEvent) {
285 myIsPreviewBlocked = false;
287 if (theMessage->eventID() == kOpFinishEvent) {
289 // add features that wait for finish as modified
290 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aFeature = myProcessOnFinish.begin();
291 for(; aFeature != myProcessOnFinish.end(); aFeature++)
292 if ((*aFeature)->data()->isValid()) // there may be already removed wait for features
293 addModified(*aFeature, FeaturePtr());
296 myProcessOnFinish.clear(); // processed features must be only on finish, so clear anyway (to avoid reimport on load)
298 if (!(theMessage->eventID() == kOpStartEvent)) {
299 processFeatures(false);
301 // remove all macros before clearing all created
302 std::set<FeaturePtr>::iterator anUpdatedIter = myWaitForFinish.begin();
303 while(anUpdatedIter != myWaitForFinish.end()) {
304 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
305 if (aFeature.get()) {
306 // remove macro on finish
307 if (aFeature->isMacro()) {
308 aFeature->document()->removeFeature(aFeature);
309 myWaitForFinish.erase(aFeature);
311 // to avoid the map update problems on "remove"
312 if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
313 anUpdatedIter = myWaitForFinish.begin();
321 // the redisplay signal should be flushed in order to erase the feature presentation in the viewer
322 // if should be done after removeFeature() of document,
323 // by this reason, upper processFeatures() do not perform this flush
324 Events_Loop::loop()->flush(kRedisplayEvent);
326 // in the end of transaction everything is updated, so clear the old objects
327 myIsParamUpdated = false;
328 myWaitForFinish.clear();
329 } else if (theMessage->eventID() == kReorderEvent) {
330 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aMsg =
331 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
332 if (aMsg->reordered().get())
333 addModified(aMsg->reordered(), aMsg->reordered()); // to update all attributes
337 void Model_Update::processFeatures(const bool theFlushRedisplay)
339 // perform update of everything if it is not performed right now or any preview is blocked
340 if (!myIsProcessed && !myIsPreviewBlocked) {
341 myIsProcessed = true;
343 std::cout<<"****** Start processing"<<std::endl;
346 while(!myModified.empty()) {
347 processFeature(myModified.begin()->first);
349 myIsProcessed = false;
351 // to update the object browser if something is updated/created during executions
352 static Events_Loop* aLoop = Events_Loop::loop();
353 static const Events_ID kCreatedEvent= aLoop->eventByName(EVENT_OBJECT_CREATED);
354 aLoop->flush(kCreatedEvent);
355 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
356 aLoop->flush(kUpdatedEvent);
358 // flush to update display
359 if (theFlushRedisplay) {
360 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
361 aLoop->flush(EVENT_DISP);
364 std::cout<<"****** End processing"<<std::endl;
370 // collects all the feautres this feature depends on: reasons
371 static void allReasons(FeaturePtr theFeature, std::set<FeaturePtr>& theReasons) {
372 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aDeps;
373 theFeature->data()->referencesToObjects(aDeps);
374 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > >::iterator
375 anAttrsIter = aDeps.begin();
376 for(; anAttrsIter != aDeps.end(); anAttrsIter++) {
377 if (theFeature->attribute(anAttrsIter->first)->isArgument()) {
378 std::list<std::shared_ptr<ModelAPI_Object> >::iterator aDepIter = anAttrsIter->second.begin();
379 for(; aDepIter != anAttrsIter->second.end(); aDepIter++) {
380 FeaturePtr aDepFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(*aDepIter);
381 if (!aDepFeat.get()) { // so, it depends on the result and process the feature owner of it
382 ResultPtr aDepRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aDepIter);
384 aDepFeat = (*aDepIter)->document()->feature(aDepRes);
387 if (aDepFeat.get() && aDepFeat->data()->isValid()) {
388 theReasons.insert(aDepFeat);
393 if (theFeature->getKind() == "Part") { // part is not depended on its subs directly, but subs must be iterated anyway
394 CompositeFeaturePtr aPart = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
395 int aNum = aPart->numberOfSubs();
396 for(int a = 0; a < aNum; a++) {
397 FeaturePtr aSub = aPart->subFeature(a);
398 if (aSub.get() && aSub->data()->isValid()) {
399 theReasons.insert(aSub);
405 bool Model_Update::processFeature(FeaturePtr theFeature)
407 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
409 if (!theFeature->data()->isValid()) { // deleted feature, just remove from all containers
410 if (myModified.find(theFeature) != myModified.end())
411 myModified.erase(theFeature);
415 if (theFeature->isPersistentResult()) {
416 if (!std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures())
420 if (myProcessed.find(theFeature) == myProcessed.end()) {
421 myProcessed[theFeature] = 0;
423 int aCount = myProcessed[theFeature];
424 if (aCount > 100) { // too many repetition of processing (in VS it may crash on 330 with stack overflow)
425 Events_InfoMessage("Model_Update",
426 "Feature '%1' is updated in infinitive loop").arg(theFeature->data()->name()).send();
429 myProcessed[theFeature] = aCount + 1;
432 // check this feature is not yet checked or processed
433 bool aIsModified = myModified.find(theFeature) != myModified.end();
434 if (!aIsModified && myIsFinish) { // get info about the modification for features without preview
435 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
437 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
438 aNewSet.insert(theFeature); // contains itself, so, we don't know which was the reason and the reason is any
439 myModified[theFeature] = aNewSet;
444 std::cout<<"* Process feature "<<theFeature->name()<<std::endl;
447 // update the sketch plane before the sketch sub-elements are recomputed
448 // (otherwise sketch will update plane, modify subs, after executed, but with old subs edges)
449 if (aIsModified && theFeature->getKind() == "Sketch") {
451 std::cout<<"****** Update sketch args "<<theFeature->name()<<std::endl;
453 AttributeSelectionPtr anExtSel = theFeature->selection("External");
454 if (anExtSel.get()) {
455 ResultPtr aContext = anExtSel->context();
456 if (aContext.get() && aContext->document().get()) {
457 FeaturePtr anExtBase = aContext->document()->feature(aContext);
458 if (anExtBase.get()) {
459 processFeature(anExtBase);
463 updateArguments(theFeature);
464 // send event that sketch is prepared to be recomputed
465 static Events_ID anID = Events_Loop::eventByName("SketchPrepared");
466 std::shared_ptr<Events_Message> aMsg(new Events_Message(anID, this));
467 Events_Loop::loop()->send(aMsg);
470 if (!aIsModified) { // no modification is needed
474 // evaluate parameter before the sub-elements update: it updates dependencies on evaluation (#1085)
475 if (theFeature->getKind() == "Parameter") {
476 theFeature->execute();
479 bool isReferencedInvalid = false;
480 // check all features this feature depended on (recursive call of updateFeature)
481 std::set<FeaturePtr>& aReasons = myModified[theFeature];
482 bool allSubsUsed = aReasons.find(theFeature) != aReasons.end();
483 if (allSubsUsed) { // add all subs in aReasons and temporary remove "theFeature" to avoid processing itself
484 allReasons(theFeature, aReasons);
485 aReasons.erase(theFeature);
487 // take reasons one by one (they may be added during the feature process (circle by the radius of sketch)
488 std::set<FeaturePtr> aProcessedReasons;
489 while(!aReasons.empty()) {
490 FeaturePtr aReason = *(aReasons.begin());
492 //cout<<theFeature->name()<<" process next reason "<<aReason->name()<<endl;
494 if (aReason != theFeature && (aReason)->data()->isValid()) {
495 if (processFeature(aReason))
497 if (aReason->data()->execState() == ModelAPI_StateInvalidArgument)
498 isReferencedInvalid = true;
500 // searching for the next not used reason
501 aProcessedReasons.insert(aReason);
502 aReasons.erase(aReason);
504 // restore the modified reasons: they will be used in the update of arguments
505 if (allSubsUsed) { // restore theFeature in this set
506 aProcessedReasons.insert(theFeature);
508 myModified[theFeature] = aProcessedReasons;
510 // do not execute the composite that contains the current
511 bool isPostponedMain = false;
512 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
513 if (theFeature->getKind() == "ExtrusionSketch" && aCompos.get()) {
514 CompositeFeaturePtr aCurrentOwner =
515 ModelAPI_Tools::compositeOwner(theFeature->document()->currentFeature(false));
516 isPostponedMain = aCurrentOwner.get() && aCompos->isSub(aCurrentOwner);
520 std::cout<<"Update args "<<theFeature->name()<<std::endl;
522 // TestImport.py : after arguments are updated, theFeature may be removed
523 if (!theFeature->data()->isValid())
525 // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
526 updateArguments(theFeature);
528 // add this feature to the processed right now to be able remove it from this list on
529 // update signal during this feature execution
530 myModified.erase(theFeature);
531 if (myNotPersistentRefs.find(theFeature) != myNotPersistentRefs.end())
532 myNotPersistentRefs.erase(theFeature);
533 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
534 theFeature->data()->execState(ModelAPI_StateDone);
536 // this checking must be after the composite feature sub-elements processing:
537 // composite feature status may depend on it's subelements
538 if ((theFeature->data()->execState() == ModelAPI_StateInvalidArgument || isReferencedInvalid) &&
539 theFeature->getKind() != "Part") { // don't disable Part because it will make disabled all the features (performance and problems with the current feature)
541 std::cout<<"Invalid args "<<theFeature->name()<<std::endl;
543 theFeature->eraseResults();
544 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
545 return true; // so, feature is modified (results are erased)
548 // execute feature if it must be updated
549 ModelAPI_ExecState aState = theFeature->data()->execState();
550 if (aFactory->validate(theFeature)) {
551 if (!isPostponedMain) {
552 executeFeature(theFeature);
556 std::cout<<"Feature is not valid, erase results "<<theFeature->name()<<std::endl;
558 theFeature->eraseResults();
559 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
564 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
566 // make updated and redisplay all results
567 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
569 std::list<ResultPtr> allResults;
570 ModelAPI_Tools::allResults(theFeature, allResults);
571 std::list<ResultPtr>::iterator aRIter = allResults.begin();
572 for (; aRIter != allResults.cend(); aRIter++) {
573 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
574 if (!aRes->isDisabled()) {// update state only for enabled results (Placement Result Part may make the original Part Result as invalid)
575 aRes->data()->execState(theState);
577 if (theFeature->data()->updateID() > aRes->data()->updateID()) {
578 aRes->data()->setUpdateID(theFeature->data()->updateID());
580 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
582 // to redisplay "presentable" feature (for ex. distance constraint)
583 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
584 theFeature->data()->execState(theState);
587 /// Updates the state by the referenced object: if something bad with it, set state for this one
588 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
591 ModelAPI_ExecState aRefState = theTarget->data()->execState();
592 if (aRefState == ModelAPI_StateMustBeUpdated) {
593 if (theCurrent == ModelAPI_StateDone)
594 return ModelAPI_StateMustBeUpdated;
595 } else if (aRefState != ModelAPI_StateDone) {
596 return ModelAPI_StateInvalidArgument;
602 void Model_Update::updateArguments(FeaturePtr theFeature) {
603 // perform this method also for disabled features: to make "not done" state for
604 // features referenced to the active and modified features
606 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
608 ModelAPI_ExecState aState = theFeature->data()->execState();
609 if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
610 aState = ModelAPI_StateMustBeUpdated;
612 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
613 aState = ModelAPI_StateMustBeUpdated;
614 // check the parameters state
617 std::list<AttributePtr> anAttrinbutes =
618 theFeature->data()->attributes(ModelAPI_AttributeInteger::typeId());
619 std::list<AttributePtr>::iterator anIter = anAttrinbutes.begin();
620 for(; anIter != anAttrinbutes.end(); anIter++) {
621 AttributeIntegerPtr anAttribute =
622 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(*anIter);
623 if (anAttribute.get() && !anAttribute->text().empty()) {
624 if (myIsParamUpdated) {
625 ModelAPI_AttributeEvalMessage::send(anAttribute, this);
627 if (anAttribute->expressionInvalid()) {
628 aState = ModelAPI_StateInvalidArgument;
635 std::list<AttributePtr> aDoubles =
636 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
637 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
638 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
639 AttributeDoublePtr aDouble =
640 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
641 if (aDouble.get() && !aDouble->text().empty()) {
642 if (myIsParamUpdated) {
643 ModelAPI_AttributeEvalMessage::send(aDouble, this);
645 if (aDouble->expressionInvalid()) {
646 aState = ModelAPI_StateInvalidArgument;
653 std::list<AttributePtr> anAttributes =
654 theFeature->data()->attributes(GeomDataAPI_Point::typeId());
655 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
656 for(; anIter != anAttributes.end(); anIter++) {
657 AttributePointPtr aPointAttribute =
658 std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
659 if (aPointAttribute.get() && (!aPointAttribute->textX().empty() ||
660 !aPointAttribute->textY().empty() || !aPointAttribute->textZ().empty())) {
661 if (myIsParamUpdated) {
662 ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
664 if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
665 (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
666 (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
667 aState = ModelAPI_StateInvalidArgument;
673 std::list<AttributePtr> anAttributes =
674 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
675 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
676 for(; anIter != anAttributes.end(); anIter++) {
677 AttributePoint2DPtr aPoint2DAttribute =
678 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
679 if (aPoint2DAttribute.get()) {
680 if (myIsParamUpdated && (!aPoint2DAttribute->textX().empty() ||
681 !aPoint2DAttribute->textY().empty())) {
682 ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
684 if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
685 (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
686 aState = ModelAPI_StateInvalidArgument;
690 // update the selection attributes if any
691 list<AttributePtr> aRefs =
692 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
693 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
694 for (; aRefsIter != aRefs.end(); aRefsIter++) {
695 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
696 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
697 ObjectPtr aContext = aSel->context();
698 // update argument only if the referenced object is ready to use
699 if (aContext.get() && !aContext->isDisabled()) {
700 if (isReason(theFeature, aContext)) {
701 if (!aSel->update()) { // this must be done on execution since it may be long operation
702 bool isObligatory = !aFactory->isNotObligatory(
703 theFeature->getKind(), theFeature->data()->id(aSel)) &&
704 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
706 aState = ModelAPI_StateInvalidArgument;
709 } else if (aContext.get()) {
710 // here it may be not obligatory, but if the reference is wrong, it should not be correct
711 bool isObligatory = aFactory->isCase(theFeature, theFeature->data()->id(aSel));
713 aState = ModelAPI_StateInvalidArgument;
716 // update the selection list attributes if any
717 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
718 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
719 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
720 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
721 for(int a = aSel->size() - 1; a >= 0; a--) {
722 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
723 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
725 ObjectPtr aContext = aSelAttr->context();
726 // update argument only if the referenced object is ready to use
727 if (aContext.get() && !aContext->isDisabled()) {
728 if (isReason(theFeature, aContext)) {
729 if (!aSelAttr->update()) {
730 bool isObligatory = !aFactory->isNotObligatory(
731 theFeature->getKind(), theFeature->data()->id(aSel)) &&
732 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
734 aState = ModelAPI_StateInvalidArgument;
737 } else if (aContext.get()) {
738 // here it may be not obligatory, but if the reference is wrong, it should not be correct
739 bool isObligatory = aFactory->isCase(theFeature, theFeature->data()->id(aSel));
741 aState = ModelAPI_StateInvalidArgument;
747 if (aState != ModelAPI_StateDone)
748 theFeature->data()->execState(aState);
751 bool Model_Update::isReason(std::shared_ptr<ModelAPI_Feature>& theFeature,
752 std::shared_ptr<ModelAPI_Object> theReason)
754 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
755 ::iterator aReasonsIt = myModified.find(theFeature);
756 if (aReasonsIt != myModified.end()) {
757 if (aReasonsIt->second.find(theFeature) != aReasonsIt->second.end())
758 return true; // any is reason if it contains itself
759 FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
760 if (!aReasFeat.get()) { // try to get feature of this result
761 ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
763 aReasFeat = theReason->document()->feature(aReasRes);
765 if (aReasonsIt->second.find(aReasFeat) != aReasonsIt->second.end())
768 // another try: postponed modification by not-persistences
769 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
770 ::iterator aNotPersist = myNotPersistentRefs.find(theFeature);
771 if (aNotPersist != myNotPersistentRefs.end()) {
772 FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
773 if (!aReasFeat.get()) { // try to get feature of this result
774 ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
776 aReasFeat = theReason->document()->feature(aReasRes);
778 if (aNotPersist->second.find(aReasFeat) != aNotPersist->second.end())
782 return false; // this case only for not-previewed items update state, nothing is changed in args for it
785 void Model_Update::executeFeature(FeaturePtr theFeature)
788 std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
790 // execute in try-catch to avoid internal problems of the feature
791 ModelAPI_ExecState aState = ModelAPI_StateDone;
792 theFeature->data()->execState(ModelAPI_StateDone);
794 theFeature->execute();
795 if (theFeature->data()->execState() != ModelAPI_StateDone) {
796 aState = ModelAPI_StateExecFailed;
798 aState = ModelAPI_StateDone;
801 aState = ModelAPI_StateExecFailed;
802 Events_InfoMessage("Model_Update",
803 "Feature %1 has failed during the execution").arg(theFeature->getKind()).send();
805 // The macro feature has to be deleted in any case even its execution is failed
806 myWaitForFinish.insert(theFeature);
807 if (aState != ModelAPI_StateDone) {
808 theFeature->eraseResults();
810 theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
811 redisplayWithResults(theFeature, aState);
814 void Model_Update::updateStability(void* theSender)
816 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
818 bool added = false; // object may be was crated
819 ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theSender);
820 if (aSender && aSender->document()) {
821 FeaturePtr aFeatureSender =
822 std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
823 if (aFeatureSender.get()) {
824 Model_Objects* aDocObjects =
825 std::dynamic_pointer_cast<Model_Document>(aSender->document())->objects();
827 //aDocObjects->synchronizeBackRefs();
828 // remove or add all concealment refs from this feature
829 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
830 aSender->data()->referencesToObjects(aRefs);
831 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIt = aRefs.begin();
832 for(; aRefIt != aRefs.end(); aRefIt++) {
833 if (!aFactory->isConcealed(aFeatureSender->getKind(), aRefIt->first))
834 continue; // take into account only concealed references (do not remove the sketch constraint and the edge on constraint edit)
835 std::list<ObjectPtr>& aRefFeaturesList = aRefIt->second;
836 std::list<ObjectPtr>::iterator aReferenced = aRefFeaturesList.begin();
837 for(; aReferenced != aRefFeaturesList.end(); aReferenced++) {
838 // stability is only on results: feature to feature reference mean nested
839 // features, that will remove nesting references
840 if (aReferenced->get() && (*aReferenced)->data()->isValid() &&
841 (*aReferenced)->groupName() != ModelAPI_Feature::group()) {
842 std::shared_ptr<Model_Data> aData =
843 std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
844 if (aFeatureSender->isStable()) {
845 aData->addBackReference(aFeatureSender, aRefIt->first);
847 aData->removeBackReference(aFeatureSender, aRefIt->first);
848 added = true; // remove of concealment may be caused creation of some result
857 static Events_Loop* aLoop = Events_Loop::loop();
858 static Events_ID kEventCreated = aLoop->eventByName(EVENT_OBJECT_CREATED);
859 aLoop->flush(kEventCreated);