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 <GeomAPI_Shape.h>
28 #include <GeomDataAPI_Point.h>
29 #include <GeomDataAPI_Point2D.h>
30 #include <Events_Loop.h>
31 #include <Events_LongOp.h>
32 #include <Events_InfoMessage.h>
33 #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);
61 static const Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
62 aLoop->registerListener(this, kUpdatedSel);
64 // Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
65 myIsParamUpdated = false;
67 myIsProcessed = false;
68 myIsPreviewBlocked = false;
71 bool Model_Update::addModified(FeaturePtr theFeature, FeaturePtr theReason) {
73 if (!theFeature->data()->isValid())
74 return false; // delete an extrusion created on the sketch
76 bool isNotExecuted = theFeature->isPersistentResult() &&
77 !std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures();
79 if (!theReason.get()) // no reason => no construction reason
81 if (myNotPersistentRefs.find(theFeature) == myNotPersistentRefs.end()) {
82 myNotPersistentRefs[theFeature].insert(theReason);
84 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
85 aNewSet.insert(theReason);
86 myNotPersistentRefs[theFeature] = aNewSet;
91 // update arguments for "apply button" state change
92 if ((!theFeature->isPreviewNeeded() && !myIsFinish) || myIsPreviewBlocked) {
93 myProcessOnFinish.insert(theFeature);
95 std::cout<<"*** Add process on finish "<<theFeature->name()<<std::endl;
97 // keeps the currently updated features to avoid infinitive cycling here: where feature on
98 // "updateArguments" sends "updated" (in selection attribute) and goes here again
99 static std::set<FeaturePtr> aCurrentlyUpdated;
100 if (aCurrentlyUpdated.find(theFeature) == aCurrentlyUpdated.end()) {
101 aCurrentlyUpdated.insert(theFeature);
102 updateArguments(theFeature);
103 aCurrentlyUpdated.erase(theFeature);
105 // make it without conditions otherwise the apply button may have a bad state
106 theFeature->data()->execState(ModelAPI_StateDone);
107 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
108 // need to be validated to update the "Apply" state if not previewed
109 aFactory->validate(theFeature);
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 "
126 // <<theFeature->name()<<" reason "<<theReason->name()<<std::endl;
128 myModified[theFeature].insert(theReason);
132 // do not add the disabled, but possibly the sub-elements are not disabled
133 bool aIsDisabled = theFeature->isDisabled();
135 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
136 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated ||
137 theFeature->data()->execState() == ModelAPI_StateInvalidArgument || // issue 1519
138 theFeature->data()->execState() == ModelAPI_StateExecFailed) {
139 // do not forget that in this case all were the reasons
140 aNewSet.insert(theFeature);
143 aNewSet.insert(theReason);
145 myModified[theFeature] = aNewSet;
147 if (theReason.get()) {
148 //std::cout<<"*** Add modified "<<theFeature->name()
149 // <<" reason "<<theReason->name()<<std::endl;
151 //std::cout<<"*** Add modified "<<theFeature->name()<<std::endl;
154 } else { // will be updated during the finish of the operation, or when it becomes enabled
155 if (theFeature->data()->execState() == ModelAPI_StateDone ||
156 theFeature->data()->execState() == ModelAPI_StateExecFailed) // fix issue 1819
157 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
159 return true; // do not need iteration deeply if it is already marked as modified or so
161 //std::cout<<"*** Set modified state "<<theFeature->name()<<std::endl;
164 // clear processed and fill modified recursively
165 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
166 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
167 for(; aRefIter != aRefs.cend(); aRefIter++) {
168 if ((*aRefIter)->isArgument()) {
169 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
170 if (aReferenced.get()) {
171 addModified(aReferenced, theFeature);
175 // proccess also results
176 std::list<ResultPtr> allResults; // list of this feature and results
177 ModelAPI_Tools::allResults(theFeature, allResults);
178 std::list<ResultPtr>::iterator aRes = allResults.begin();
179 for(; aRes != allResults.end(); aRes++) {
180 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*aRes)->data()->refsToMe();
181 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
182 for(; aRefIter != aRefs.cend(); aRefIter++) {
183 if ((*aRefIter)->isArgument()) {
184 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
185 if (aReferenced.get()) {
186 addModified(aReferenced, theFeature);
192 // also add part feature that contains this feature to the modified
193 if (theFeature->document()->kind() != "PartSet") {
194 FeaturePtr aPart = ModelAPI_Tools::findPartFeature(
195 ModelAPI_Session::get()->moduleDocument(), theFeature->document());
197 addModified(aPart, theFeature);
202 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
204 static Events_Loop* aLoop = Events_Loop::loop();
205 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
206 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
207 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
208 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
209 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
210 static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
211 static const Events_ID kPreviewBlockedEvent = aLoop->eventByName(EVENT_PREVIEW_BLOCKED);
212 static const Events_ID kPreviewRequestedEvent = aLoop->eventByName(EVENT_PREVIEW_REQUESTED);
213 static const Events_ID kReorderEvent = aLoop->eventByName(EVENT_ORDER_UPDATED);
214 static const Events_ID kRedisplayEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
215 static const Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
218 std::cout<<"****** Event "<<theMessage->eventID().eventText()<<std::endl;
220 if (theMessage->eventID() == kStabilityEvent) {
221 updateStability(theMessage->sender());
224 if (theMessage->eventID() == kPreviewBlockedEvent) {
225 myIsPreviewBlocked = true;
228 if (theMessage->eventID() == kPreviewRequestedEvent) {
229 if (myIsPreviewBlocked) {
230 myIsPreviewBlocked = false;
232 myIsPreviewBlocked = true;
236 if (theMessage->eventID() == kUpdatedSel) {
237 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
238 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
239 updateSelection(aMsg->objects());
241 // creation is added to "update" to avoid recomputation twice:
242 // on create and immediately after on update
243 if (theMessage->eventID() == kCreatedEvent) {
244 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
245 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
246 const std::set<ObjectPtr>& anObjs = aMsg->objects();
247 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
248 for(; anObjIter != anObjs.cend(); anObjIter++) {
249 if (std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures()) {
250 if ((*anObjIter)->groupName() == ModelAPI_Feature::group()) {
251 // results creation means enabling, not update
252 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kUpdatedEvent);
254 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kRedisplayEvent);
260 if (theMessage->eventID() == kUpdatedEvent) {
261 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
262 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
263 const std::set<ObjectPtr>& anObjs = aMsg->objects();
264 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
265 bool aSomeModified = false; // check that features not changed: only redisplay is needed
266 for(; anObjIter != anObjs.cend(); anObjIter++) {
267 if (!(*anObjIter)->data()->isValid())
270 std::cout<<">>> in event updated "<<(*anObjIter)->groupName()<<
271 " "<<(*anObjIter)->data()->name()<<std::endl;
273 if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
274 myIsParamUpdated = true;
276 // on undo/redo, abort do not update persisten features
277 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
278 if (anUpdated.get()) {
279 if (addModified(anUpdated, FeaturePtr()))
280 aSomeModified = true;
282 // process the updated result as update of features that refers to this result
283 const std::set<std::shared_ptr<ModelAPI_Attribute> >&
284 aRefs = (*anObjIter)->data()->refsToMe();
285 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
286 for(; aRefIter != aRefs.cend(); aRefIter++) {
287 if (!(*aRefIter)->owner()->data()->isValid())
289 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
290 if (anUpdated.get()) {
291 if (addModified(anUpdated, FeaturePtr()))
292 aSomeModified = true;
297 // this event is for solver update, not here, do not react immediately
301 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
302 theMessage->eventID() == kOpStartEvent) {
303 myIsPreviewBlocked = false;
305 if (theMessage->eventID() == kOpFinishEvent) {
307 // add features that wait for finish as modified
308 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aFeature = myProcessOnFinish.begin();
309 for(; aFeature != myProcessOnFinish.end(); aFeature++)
310 if ((*aFeature)->data()->isValid()) // there may be already removed wait for features
311 addModified(*aFeature, FeaturePtr());
314 // processed features must be only on finish, so clear anyway (to avoid reimport on load)
315 myProcessOnFinish.clear();
317 if (!(theMessage->eventID() == kOpStartEvent)) {
318 processFeatures(false);
320 // remove all macros before clearing all created
321 std::set<FeaturePtr>::iterator anUpdatedIter = myWaitForFinish.begin();
322 while(anUpdatedIter != myWaitForFinish.end()) {
323 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
324 if (aFeature.get()) {
325 // remove macro on finish
326 if (aFeature->isMacro()) {
327 aFeature->document()->removeFeature(aFeature);
328 myWaitForFinish.erase(aFeature);
330 // to avoid the map update problems on "remove"
331 if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
332 anUpdatedIter = myWaitForFinish.begin();
340 // the redisplay signal should be flushed in order
341 // to erase the feature presentation in the viewer
342 // if should be done after removeFeature() of document,
343 // by this reason, upper processFeatures() do not perform this flush
344 Events_Loop::loop()->flush(kRedisplayEvent);
346 // in the end of transaction everything is updated, so clear the old objects
347 myIsParamUpdated = false;
348 myWaitForFinish.clear();
349 } else if (theMessage->eventID() == kReorderEvent) {
350 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aMsg =
351 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
352 if (aMsg->reordered().get())
353 addModified(aMsg->reordered(), aMsg->reordered()); // to update all attributes
357 void Model_Update::processFeatures(const bool theFlushRedisplay)
359 // perform update of everything if it is not performed right now or any preview is blocked
360 if (!myIsProcessed && !myIsPreviewBlocked) {
361 myIsProcessed = true;
363 std::cout<<"****** Start processing"<<std::endl;
366 while(!myModified.empty()) {
367 processFeature(myModified.begin()->first);
369 myIsProcessed = false;
371 // to update the object browser if something is updated/created during executions
372 static Events_Loop* aLoop = Events_Loop::loop();
373 static const Events_ID kCreatedEvent= aLoop->eventByName(EVENT_OBJECT_CREATED);
374 aLoop->flush(kCreatedEvent);
375 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
376 aLoop->flush(kUpdatedEvent);
378 // flush to update display
379 if (theFlushRedisplay) {
380 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
381 aLoop->flush(EVENT_DISP);
384 std::cout<<"****** End processing"<<std::endl;
390 // collects all the feautres this feature depends on: reasons
391 static void allReasons(FeaturePtr theFeature, std::set<FeaturePtr>& theReasons) {
392 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aDeps;
393 theFeature->data()->referencesToObjects(aDeps);
394 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > >::iterator
395 anAttrsIter = aDeps.begin();
396 for(; anAttrsIter != aDeps.end(); anAttrsIter++) {
397 if (theFeature->attribute(anAttrsIter->first)->isArgument()) {
398 std::list<std::shared_ptr<ModelAPI_Object> >::iterator aDepIter = anAttrsIter->second.begin();
399 for(; aDepIter != anAttrsIter->second.end(); aDepIter++) {
400 FeaturePtr aDepFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(*aDepIter);
401 if (!aDepFeat.get()) { // so, it depends on the result and process the feature owner of it
402 ResultPtr aDepRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aDepIter);
404 aDepFeat = (*aDepIter)->document()->feature(aDepRes);
407 if (aDepFeat.get() && aDepFeat->data()->isValid()) {
408 theReasons.insert(aDepFeat);
413 if (theFeature->getKind() == "Part") {
414 // part is not depended on its subs directly, but subs must be iterated anyway
415 CompositeFeaturePtr aPart = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
416 int aNum = aPart->numberOfSubs();
417 for(int a = 0; a < aNum; a++) {
418 FeaturePtr aSub = aPart->subFeature(a);
419 if (aSub.get() && aSub->data()->isValid()) {
420 theReasons.insert(aSub);
426 bool Model_Update::processFeature(FeaturePtr theFeature)
428 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
430 if (!theFeature->data()->isValid()) { // deleted feature, just remove from all containers
431 if (myModified.find(theFeature) != myModified.end())
432 myModified.erase(theFeature);
436 if (theFeature->isPersistentResult()) {
437 if (!std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures())
441 if (myProcessed.find(theFeature) == myProcessed.end()) {
442 myProcessed[theFeature] = 0;
444 int aCount = myProcessed[theFeature];
446 // too many repetition of processing (in VS it may crash on 330 with stack overflow)
447 Events_InfoMessage("Model_Update",
448 "Feature '%1' is updated in infinitive loop").arg(theFeature->data()->name()).send();
453 myProcessed[theFeature] = aCount + 1;
456 // check this feature is not yet checked or processed
457 bool aIsModified = myModified.find(theFeature) != myModified.end();
458 if (!aIsModified && myIsFinish) { // get info about the modification for features without preview
459 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
461 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
462 // contains itself, so, we don't know which was the reason and the reason is any
463 aNewSet.insert(theFeature);
464 myModified[theFeature] = aNewSet;
469 std::cout<<"* Process feature "<<theFeature->name()<<std::endl;
472 // update the sketch plane before the sketch sub-elements are recomputed
473 // (otherwise sketch will update plane, modify subs, after executed, but with old subs edges)
474 if (aIsModified && theFeature->getKind() == "Sketch") {
476 std::cout<<"****** Update sketch args "<<theFeature->name()<<std::endl;
478 AttributeSelectionPtr anExtSel = theFeature->selection("External");
479 if (anExtSel.get()) {
480 ResultPtr aContext = anExtSel->context();
481 if (aContext.get() && aContext->document().get()) {
482 FeaturePtr anExtBase = aContext->document()->feature(aContext);
483 if (anExtBase.get()) {
484 processFeature(anExtBase);
488 std::shared_ptr<GeomAPI_Shape> aShapeBefore = anExtSel->value();
489 if (!aShapeBefore.get() && anExtSel->context()) aShapeBefore = anExtSel->context()->shape();
490 updateArguments(theFeature);
491 std::shared_ptr<GeomAPI_Shape> aShapeAfter = anExtSel->value();
492 if (!aShapeAfter.get() && anExtSel->context()) aShapeAfter = anExtSel->context()->shape();
493 // if selected plane is changed, try to re-take external references of all subs of the sketch
494 if (aShapeBefore.get() && !aShapeBefore->isEqual(aShapeAfter)) {
495 std::set<FeaturePtr> aWholeR;
496 allReasons(theFeature, aWholeR);
497 std::set<FeaturePtr>::iterator aRIter = aWholeR.begin();
498 for(; aRIter != aWholeR.end(); aRIter++) {
499 if ((*aRIter)->data()->selection("External"))
500 (*aRIter)->attributeChanged("External");
505 if (!aIsModified) { // no modification is needed
509 // evaluate parameter before the sub-elements update:
510 // it updates dependencies on evaluation (#1085)
511 if (theFeature->getKind() == "Parameter") {
512 theFeature->execute();
515 bool isReferencedInvalid = false;
516 // check all features this feature depended on (recursive call of updateFeature)
517 std::set<FeaturePtr>& aReasons = myModified[theFeature];
518 bool allSubsUsed = aReasons.find(theFeature) != aReasons.end();
520 // add all subs in aReasons and temporary remove "theFeature" to avoid processing itself
521 allReasons(theFeature, aReasons);
522 aReasons.erase(theFeature);
524 // take reasons one by one (they may be added during the feature process
525 // (circle by the radius of sketch)
526 std::set<FeaturePtr> aProcessedReasons;
527 while(!aReasons.empty()) {
528 FeaturePtr aReason = *(aReasons.begin());
530 //cout<<theFeature->name()<<" process next reason "<<aReason->name()<<endl;
532 if (aReason != theFeature && (aReason)->data()->isValid()) {
533 if (processFeature(aReason))
535 if (aReason->data()->execState() == ModelAPI_StateInvalidArgument)
536 isReferencedInvalid = true;
538 // searching for the next not used reason
539 aProcessedReasons.insert(aReason);
540 aReasons.erase(aReason);
542 // restore the modified reasons: they will be used in the update of arguments
543 if (allSubsUsed) { // restore theFeature in this set
544 aProcessedReasons.insert(theFeature);
546 myModified[theFeature] = aProcessedReasons;
548 // do not execute the composite that contains the current
549 bool isPostponedMain = false;
550 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
551 if (theFeature->getKind() == "ExtrusionSketch" && aCompos.get()) {
552 CompositeFeaturePtr aCurrentOwner =
553 ModelAPI_Tools::compositeOwner(theFeature->document()->currentFeature(false));
554 isPostponedMain = aCurrentOwner.get() && aCompos->isSub(aCurrentOwner);
558 std::cout<<"Update args "<<theFeature->name()<<std::endl;
560 // TestImport.py : after arguments are updated, theFeature may be removed
561 if (!theFeature->data()->isValid())
563 // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
564 updateArguments(theFeature);
566 // add this feature to the processed right now to be able remove it from this list on
567 // update signal during this feature execution
568 myModified.erase(theFeature);
569 if (myNotPersistentRefs.find(theFeature) != myNotPersistentRefs.end())
570 myNotPersistentRefs.erase(theFeature);
571 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
572 theFeature->data()->execState(ModelAPI_StateDone);
574 // this checking must be after the composite feature sub-elements processing:
575 // composite feature status may depend on it's subelements
576 if ((theFeature->data()->execState() == ModelAPI_StateInvalidArgument || isReferencedInvalid) &&
577 theFeature->getKind() != "Part") {
578 // don't disable Part because it will make disabled all the features
579 // (performance and problems with the current feature)
581 std::cout<<"Invalid args "<<theFeature->name()<<std::endl;
583 theFeature->eraseResults(false);
584 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
585 return true; // so, feature is modified (results are erased)
588 // execute feature if it must be updated
589 ModelAPI_ExecState aState = theFeature->data()->execState();
590 if (aFactory->validate(theFeature)) {
591 if (!isPostponedMain) {
592 executeFeature(theFeature);
596 std::cout<<"Feature is not valid, erase results "<<theFeature->name()<<std::endl;
598 theFeature->eraseResults(false);
599 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
604 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
606 // make updated and redisplay all results
607 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
609 std::list<ResultPtr> allResults;
610 ModelAPI_Tools::allResults(theFeature, allResults);
611 std::list<ResultPtr>::iterator aRIter = allResults.begin();
612 for (; aRIter != allResults.cend(); aRIter++) {
613 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
614 if (!aRes->isDisabled()) {
615 // update state only for enabled results
616 // (Placement Result Part may make the original Part Result as invalid)
617 aRes->data()->execState(theState);
619 if (theFeature->data()->updateID() > aRes->data()->updateID()) {
620 aRes->data()->setUpdateID(theFeature->data()->updateID());
622 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
624 // to redisplay "presentable" feature (for ex. distance constraint)
625 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
626 theFeature->data()->execState(theState);
629 /// Updates the state by the referenced object: if something bad with it, set state for this one
630 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
633 ModelAPI_ExecState aRefState = theTarget->data()->execState();
634 if (aRefState == ModelAPI_StateMustBeUpdated) {
635 if (theCurrent == ModelAPI_StateDone)
636 return ModelAPI_StateMustBeUpdated;
637 } else if (aRefState != ModelAPI_StateDone) {
638 return ModelAPI_StateInvalidArgument;
644 void Model_Update::updateArguments(FeaturePtr theFeature) {
645 // perform this method also for disabled features: to make "not done" state for
646 // features referenced to the active and modified features
647 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
649 ModelAPI_ExecState aState = theFeature->data()->execState();
650 if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
651 aState = ModelAPI_StateMustBeUpdated;
653 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
654 aState = ModelAPI_StateMustBeUpdated;
655 // check the parameters state
658 std::list<AttributePtr> anAttrinbutes =
659 theFeature->data()->attributes(ModelAPI_AttributeInteger::typeId());
660 std::list<AttributePtr>::iterator anIter = anAttrinbutes.begin();
661 for(; anIter != anAttrinbutes.end(); anIter++) {
662 AttributeIntegerPtr anAttribute =
663 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(*anIter);
664 if (anAttribute.get() && !anAttribute->text().empty()) {
665 if (myIsParamUpdated) {
666 ModelAPI_AttributeEvalMessage::send(anAttribute, this);
668 if (anAttribute->expressionInvalid()) {
669 aState = ModelAPI_StateInvalidArgument;
676 std::list<AttributePtr> aDoubles =
677 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
678 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
679 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
680 AttributeDoublePtr aDouble =
681 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
682 if (aDouble.get() && !aDouble->text().empty()) {
683 if (myIsParamUpdated) {
684 ModelAPI_AttributeEvalMessage::send(aDouble, this);
686 if (aDouble->expressionInvalid()) {
687 aState = ModelAPI_StateInvalidArgument;
694 std::list<AttributePtr> anAttributes =
695 theFeature->data()->attributes(GeomDataAPI_Point::typeId());
696 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
697 for(; anIter != anAttributes.end(); anIter++) {
698 AttributePointPtr aPointAttribute =
699 std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
700 if (aPointAttribute.get() && (!aPointAttribute->textX().empty() ||
701 !aPointAttribute->textY().empty() || !aPointAttribute->textZ().empty())) {
702 if (myIsParamUpdated) {
703 ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
705 if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
706 (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
707 (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
708 aState = ModelAPI_StateInvalidArgument;
714 std::list<AttributePtr> anAttributes =
715 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
716 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
717 for(; anIter != anAttributes.end(); anIter++) {
718 AttributePoint2DPtr aPoint2DAttribute =
719 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
720 if (aPoint2DAttribute.get()) {
721 if (myIsParamUpdated && (!aPoint2DAttribute->textX().empty() ||
722 !aPoint2DAttribute->textY().empty())) {
723 ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
725 if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
726 (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
727 aState = ModelAPI_StateInvalidArgument;
731 // update the selection attributes if any
732 std::list<AttributePtr> aRefs =
733 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
734 std::list<AttributePtr>::iterator aRefsIter = aRefs.begin();
735 for (; aRefsIter != aRefs.end(); aRefsIter++) {
736 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
737 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
738 ObjectPtr aContext = aSel->context();
739 // update argument only if the referenced object is ready to use
740 if (aContext.get() && !aContext->isDisabled()) {
741 if (isReason(theFeature, aContext)) {
742 if (!aSel->update()) { // this must be done on execution since it may be long operation
743 bool isObligatory = !aFactory->isNotObligatory(
744 theFeature->getKind(), theFeature->data()->id(aSel)) &&
745 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
747 aState = ModelAPI_StateInvalidArgument;
750 } else if (aContext.get()) {
751 // here it may be not obligatory, but if the reference is wrong, it should not be correct
752 bool isObligatory = aFactory->isCase(theFeature, theFeature->data()->id(aSel));
754 aState = ModelAPI_StateInvalidArgument;
757 // update the selection list attributes if any
758 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
759 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
760 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
761 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
762 for(int a = aSel->size() - 1; a >= 0; a--) {
763 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
764 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
766 ObjectPtr aContext = aSelAttr->context();
767 // update argument only if the referenced object is ready to use
768 if (aContext.get() && !aContext->isDisabled()) {
769 if (isReason(theFeature, aContext)) {
770 if (!aSelAttr->update()) {
771 bool isObligatory = !aFactory->isNotObligatory(
772 theFeature->getKind(), theFeature->data()->id(aSel)) &&
773 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
775 aState = ModelAPI_StateInvalidArgument;
778 } else if (aContext.get()) {
779 // here it may be not obligatory, but if the reference is wrong, it should not be correct
780 bool isObligatory = aFactory->isCase(theFeature, theFeature->data()->id(aSel));
782 aState = ModelAPI_StateInvalidArgument;
788 if (aState != ModelAPI_StateDone)
789 theFeature->data()->execState(aState);
792 bool Model_Update::isReason(std::shared_ptr<ModelAPI_Feature>& theFeature,
793 std::shared_ptr<ModelAPI_Object> theReason)
795 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
796 ::iterator aReasonsIt = myModified.find(theFeature);
797 if (aReasonsIt != myModified.end()) {
798 if (aReasonsIt->second.find(theFeature) != aReasonsIt->second.end())
799 return true; // any is reason if it contains itself
800 FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
801 if (!aReasFeat.get()) { // try to get feature of this result
802 ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
804 aReasFeat = theReason->document()->feature(aReasRes);
806 if (aReasonsIt->second.find(aReasFeat) != aReasonsIt->second.end())
809 // another try: postponed modification by not-persistences
810 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
811 ::iterator aNotPersist = myNotPersistentRefs.find(theFeature);
812 if (aNotPersist != myNotPersistentRefs.end()) {
813 FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
814 if (!aReasFeat.get()) { // try to get feature of this result
815 ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
817 aReasFeat = theReason->document()->feature(aReasRes);
819 if (aNotPersist->second.find(aReasFeat) != aNotPersist->second.end())
823 // this case only for not-previewed items update state, nothing is changed in args for it
827 void Model_Update::executeFeature(FeaturePtr theFeature)
830 std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
832 // execute in try-catch to avoid internal problems of the feature
833 ModelAPI_ExecState aState = ModelAPI_StateDone;
834 theFeature->data()->execState(ModelAPI_StateDone);
836 theFeature->execute();
837 if (theFeature->data()->execState() != ModelAPI_StateDone) {
838 aState = ModelAPI_StateExecFailed;
840 aState = ModelAPI_StateDone;
843 aState = ModelAPI_StateExecFailed;
844 Events_InfoMessage("Model_Update",
845 "Feature %1 has failed during the execution").arg(theFeature->getKind()).send();
847 // The macro feature has to be deleted in any case even its execution is failed
848 myWaitForFinish.insert(theFeature);
849 if (aState != ModelAPI_StateDone) {
850 theFeature->eraseResults(false);
852 theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
853 redisplayWithResults(theFeature, aState);
856 void Model_Update::updateStability(void* theSender)
858 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
860 bool added = false; // object may be was crated
861 ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theSender);
862 if (aSender && aSender->document()) {
863 FeaturePtr aFeatureSender =
864 std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
865 if (aFeatureSender.get()) {
866 Model_Objects* aDocObjects =
867 std::dynamic_pointer_cast<Model_Document>(aSender->document())->objects();
869 //aDocObjects->synchronizeBackRefs();
870 // remove or add all concealment refs from this feature
871 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
872 aSender->data()->referencesToObjects(aRefs);
873 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
874 aRefIt = aRefs.begin();
875 for(; aRefIt != aRefs.end(); aRefIt++) {
876 if (!aFactory->isConcealed(aFeatureSender->getKind(), aRefIt->first))
877 // take into account only concealed references
878 // (do not remove the sketch constraint and the edge on constraint edit)
880 std::list<ObjectPtr>& aRefFeaturesList = aRefIt->second;
881 std::list<ObjectPtr>::iterator aReferenced = aRefFeaturesList.begin();
882 for(; aReferenced != aRefFeaturesList.end(); aReferenced++) {
883 // stability is only on results: feature to feature reference mean nested
884 // features, that will remove nesting references
885 if (aReferenced->get() && (*aReferenced)->data()->isValid() &&
886 (*aReferenced)->groupName() != ModelAPI_Feature::group()) {
887 std::shared_ptr<Model_Data> aData =
888 std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
889 if (aFeatureSender->isStable()) {
890 aData->addBackReference(aFeatureSender, aRefIt->first);
892 aData->removeBackReference(aFeatureSender, aRefIt->first);
893 added = true; // remove of concealment may be caused creation of some result
902 static Events_Loop* aLoop = Events_Loop::loop();
903 static Events_ID kEventCreated = aLoop->eventByName(EVENT_OBJECT_CREATED);
904 aLoop->flush(kEventCreated);
909 void Model_Update::updateSelection(const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects)
911 std::set<std::shared_ptr<ModelAPI_Object> >::iterator anObj = theObjects.begin();
912 for(; anObj != theObjects.end(); anObj++) {
913 std::list<AttributePtr> aRefs =
914 (*anObj)->data()->attributes(ModelAPI_AttributeSelection::typeId());
915 std::list<AttributePtr>::iterator aRefsIter = aRefs.begin();
916 for (; aRefsIter != aRefs.end(); aRefsIter++) {
917 std::shared_ptr<Model_AttributeSelection> aSel =
918 std::dynamic_pointer_cast<Model_AttributeSelection>(*aRefsIter);
919 aSel->updateInHistory();
921 // update the selection list attributes if any
922 aRefs = (*anObj)->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
923 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
924 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
925 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
926 for(int a = aSel->size() - 1; a >= 0; a--) {
927 std::shared_ptr<Model_AttributeSelection> aSelAttr =
928 std::dynamic_pointer_cast<Model_AttributeSelection>(aSel->value(a));
930 aSelAttr->updateInHistory();