1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <Model_Update.h>
22 #include <Model_Document.h>
23 #include <Model_Data.h>
24 #include <Model_Objects.h>
25 #include <Model_AttributeSelection.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_Events.h>
30 #include <ModelAPI_AttributeReference.h>
31 #include <ModelAPI_AttributeRefList.h>
32 #include <ModelAPI_AttributeRefAttr.h>
33 #include <ModelAPI_AttributeSelection.h>
34 #include <ModelAPI_AttributeSelectionList.h>
35 #include <ModelAPI_Result.h>
36 #include <ModelAPI_ResultPart.h>
37 #include <ModelAPI_Validator.h>
38 #include <ModelAPI_CompositeFeature.h>
39 #include <ModelAPI_Session.h>
40 #include <ModelAPI_Tools.h>
41 #include <GeomAPI_Shape.h>
42 #include <GeomDataAPI_Point.h>
43 #include <GeomDataAPI_Point2D.h>
44 #include <Events_Loop.h>
45 #include <Events_LongOp.h>
46 #include <Events_InfoMessage.h>
47 #include <Config_PropManager.h>
49 Model_Update MY_UPDATER_INSTANCE; /// the only one instance initialized on load of the library
52 Model_Update::Model_Update()
54 Events_Loop* aLoop = Events_Loop::loop();
55 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
56 aLoop->registerListener(this, kChangedEvent);
57 static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
58 aLoop->registerListener(this, kCreatedEvent);
59 static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
60 aLoop->registerListener(this, kUpdatedEvent);
61 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
62 aLoop->registerListener(this, kOpFinishEvent);
63 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
64 aLoop->registerListener(this, kOpAbortEvent);
65 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
66 aLoop->registerListener(this, kOpStartEvent);
67 static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
68 aLoop->registerListener(this, kStabilityEvent);
69 static const Events_ID kPreviewBlockedEvent = aLoop->eventByName(EVENT_PREVIEW_BLOCKED);
70 aLoop->registerListener(this, kPreviewBlockedEvent);
71 static const Events_ID kPreviewRequestedEvent = aLoop->eventByName(EVENT_PREVIEW_REQUESTED);
72 aLoop->registerListener(this, kPreviewRequestedEvent);
73 static const Events_ID kReorderEvent = aLoop->eventByName(EVENT_ORDER_UPDATED);
74 aLoop->registerListener(this, kReorderEvent);
75 static const Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
76 aLoop->registerListener(this, kUpdatedSel);
78 // Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
79 myIsParamUpdated = false;
81 myIsProcessed = false;
82 myIsPreviewBlocked = false;
83 myUpdateBlocked = false;
86 bool Model_Update::addModified(FeaturePtr theFeature, FeaturePtr theReason) {
88 if (!theFeature->data()->isValid())
89 return false; // delete an extrusion created on the sketch
92 bool isNotExecuted = theFeature->isPersistentResult() &&
93 !std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures();
95 redisplayWithResults(theFeature, ModelAPI_StateNothing, false); // redisplay even not executed
96 if (!theReason.get()) // no reason => no construction reason
98 if (myNotPersistentRefs.find(theFeature) == myNotPersistentRefs.end()) {
99 myNotPersistentRefs[theFeature].insert(theReason);
101 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
102 aNewSet.insert(theReason);
103 myNotPersistentRefs[theFeature] = aNewSet;
108 // update arguments for "apply button" state change
109 if ((!theFeature->isPreviewNeeded() && !myIsFinish) || myIsPreviewBlocked || myUpdateBlocked) {
111 myProcessOnFinish[theFeature].insert(theReason);
112 else if (myProcessOnFinish.find(theFeature) == myProcessOnFinish.end())
113 myProcessOnFinish[theFeature] = std::set<std::shared_ptr<ModelAPI_Feature> >();
115 std::cout<<"*** Add process on finish "<<theFeature->name()<<std::endl;
117 // keeps the currently updated features to avoid infinitive cycling here: where feature on
118 // "updateArguments" sends "updated" (in selection attribute) and goes here again
119 static std::set<FeaturePtr> aCurrentlyUpdated;
120 if (aCurrentlyUpdated.find(theFeature) == aCurrentlyUpdated.end()) {
121 aCurrentlyUpdated.insert(theFeature);
122 updateArguments(theFeature);
123 aCurrentlyUpdated.erase(theFeature);
125 // make it without conditions otherwise the apply button may have a bad state
126 theFeature->data()->execState(ModelAPI_StateDone);
127 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
128 // need to be validated to update the "Apply" state if not previewed
129 aFactory->validate(theFeature);
131 // to redisplay split's arguments presentation, even result is not computed
132 if (!theFeature->isPreviewNeeded()) {
133 static Events_Loop* aLoop = Events_Loop::loop();
134 static const Events_ID kRedisplayEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
135 ModelAPI_EventCreator::get()->sendUpdated(theFeature, kRedisplayEvent);
136 aLoop->flush(kRedisplayEvent);
139 if (!myIsPreviewBlocked && !myUpdateBlocked)
142 if (myModified.find(theFeature) != myModified.end()) {
143 if (theReason.get()) {
145 //std::cout<<"*** Add already modified "
146 // <<theFeature->name()<<" reason "<<theReason->name()<<std::endl;
148 myModified[theFeature].insert(theReason);
152 // do not add the disabled, but possibly the sub-elements are not disabled
153 bool aIsDisabled = theFeature->isDisabled();
155 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
156 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated ||
157 theFeature->data()->execState() == ModelAPI_StateInvalidArgument || // issue 1519
158 theFeature->data()->execState() == ModelAPI_StateExecFailed) {
159 // do not forget that in this case all were the reasons
160 aNewSet.insert(theFeature);
163 aNewSet.insert(theReason);
165 myModified[theFeature] = aNewSet;
167 if (theReason.get()) {
168 //std::cout<<"*** Add modified "<<theFeature->name()
169 // <<" reason "<<theReason->name()<<std::endl;
171 //std::cout<<"*** Add modified "<<theFeature->name()<<std::endl;
174 } else { // will be updated during the finish of the operation, or when it becomes enabled
175 if (theFeature->data()->execState() == ModelAPI_StateDone ||
176 theFeature->data()->execState() == ModelAPI_StateExecFailed) // fix issue 1819
177 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
179 return true; // do not need iteration deeply if it is already marked as modified or so
181 //std::cout<<"*** Set modified state "<<theFeature->name()<<std::endl;
184 // clear processed and fill modified recursively
185 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
186 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
187 for(; aRefIter != aRefs.cend(); aRefIter++) {
188 if ((*aRefIter)->isArgument()) {
189 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
190 if (aReferenced.get()) {
191 addModified(aReferenced, theFeature);
195 // proccess also results
196 std::list<ResultPtr> allResults; // list of this feature and results
197 ModelAPI_Tools::allResults(theFeature, allResults);
198 std::list<ResultPtr>::iterator aRes = allResults.begin();
199 for(; aRes != allResults.end(); aRes++) {
200 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*aRes)->data()->refsToMe();
201 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
202 for(; aRefIter != aRefs.cend(); aRefIter++) {
203 if ((*aRefIter)->isArgument()) {
204 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
205 if (aReferenced.get()) {
206 addModified(aReferenced, theFeature);
212 // also add part feature that contains this feature to the modified
213 if (theFeature->document()->kind() != "PartSet") {
214 FeaturePtr aPart = ModelAPI_Tools::findPartFeature(
215 ModelAPI_Session::get()->moduleDocument(), theFeature->document());
217 addModified(aPart, theFeature);
222 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
224 static Events_Loop* aLoop = Events_Loop::loop();
225 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
226 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
227 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
228 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
229 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
230 static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
231 static const Events_ID kPreviewBlockedEvent = aLoop->eventByName(EVENT_PREVIEW_BLOCKED);
232 static const Events_ID kPreviewRequestedEvent = aLoop->eventByName(EVENT_PREVIEW_REQUESTED);
233 static const Events_ID kReorderEvent = aLoop->eventByName(EVENT_ORDER_UPDATED);
234 static const Events_ID kRedisplayEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
235 static const Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
236 static const Events_ID kAutomaticOff = aLoop->eventByName(EVENT_AUTOMATIC_RECOMPUTATION_DISABLE);
237 static const Events_ID kAutomaticOn = aLoop->eventByName(EVENT_AUTOMATIC_RECOMPUTATION_ENABLE);
240 std::cout<<"****** Event "<<theMessage->eventID().eventText()<<std::endl;
242 if (theMessage->eventID() == kStabilityEvent) {
243 updateStability(theMessage->sender());
246 if (theMessage->eventID() == kPreviewBlockedEvent) {
247 myIsPreviewBlocked = true;
250 if (theMessage->eventID() == kPreviewRequestedEvent) {
251 if (myIsPreviewBlocked) {
252 bool anUpdateState = myUpdateBlocked;
253 myUpdateBlocked = false;
254 myIsPreviewBlocked = false;
256 myIsPreviewBlocked = true;
257 myUpdateBlocked = anUpdateState;
261 if (theMessage->eventID() == kAutomaticOff) {
262 myUpdateBlocked = true;
265 if (theMessage->eventID() == kAutomaticOn) {
266 myUpdateBlocked = false; // then process all modified features, even if preview is blocked
267 bool aPreviewBlockedState = myIsPreviewBlocked; // to update the selected arguments
268 myIsPreviewBlocked = false;
270 myIsPreviewBlocked = myIsPreviewBlocked;
273 if (theMessage->eventID() == kUpdatedSel) {
274 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
275 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
276 updateSelection(aMsg->objects());
278 // creation is added to "update" to avoid recomputation twice:
279 // on create and immediately after on update
280 if (theMessage->eventID() == kCreatedEvent) {
281 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
282 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
283 const std::set<ObjectPtr>& anObjs = aMsg->objects();
284 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
285 for(; anObjIter != anObjs.cend(); anObjIter++) {
286 if (std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures()) {
287 if ((*anObjIter)->groupName() == ModelAPI_Feature::group()) {
288 // results creation means enabling, not update
289 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kUpdatedEvent);
291 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kRedisplayEvent);
297 if (theMessage->eventID() == kUpdatedEvent) {
298 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
299 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
300 const std::set<ObjectPtr>& anObjs = aMsg->objects();
301 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
302 bool aSomeModified = false; // check that features not changed: only redisplay is needed
303 for(; anObjIter != anObjs.cend(); anObjIter++) {
304 if (!(*anObjIter)->data()->isValid())
307 std::cout<<">>> in event updated "<<(*anObjIter)->groupName()<<
308 " "<<(*anObjIter)->data()->name()<<std::endl;
310 if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
311 myIsParamUpdated = true;
313 // on undo/redo, abort do not update persisten features
314 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
315 if (anUpdated.get()) {
316 if (addModified(anUpdated, FeaturePtr()))
317 aSomeModified = true;
319 // process the updated result as update of features that refers to this result
320 const std::set<std::shared_ptr<ModelAPI_Attribute> >&
321 aRefs = (*anObjIter)->data()->refsToMe();
322 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
324 ResultPtr aReasonResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIter);
325 if (aReasonResult.get())
326 aReason = (*anObjIter)->document()->feature(aReasonResult);
327 for(; aRefIter != aRefs.cend(); aRefIter++) {
328 if (!(*aRefIter)->owner()->data()->isValid())
330 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
331 if (anUpdated.get()) {
332 if (addModified(anUpdated, aReason))
333 aSomeModified = true;
338 // this event is for solver update, not here, do not react immediately
342 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
343 theMessage->eventID() == kOpStartEvent) {
344 myIsPreviewBlocked = false;
346 if (theMessage->eventID() == kOpFinishEvent && !myUpdateBlocked) {// if update is blocked, skip
348 // add features that wait for finish as modified
349 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >::
350 iterator aFeature = myProcessOnFinish.begin();
351 for(; aFeature != myProcessOnFinish.end(); aFeature++) {
352 if (aFeature->first->data()->isValid()) {// there may be already removed while wait
353 if (aFeature->second.empty()) {
354 addModified(aFeature->first, FeaturePtr());
357 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aReasons;
358 for(aReasons = aFeature->second.begin(); aReasons != aFeature->second.end(); aReasons++){
359 addModified(aFeature->first, *aReasons);
365 // processed features must be only on finish, so clear anyway (to avoid reimport on load)
366 if (!myUpdateBlocked) {
367 myProcessOnFinish.clear();
369 // #2156: current must be sketch, left after the macro execution
370 DocumentPtr anActiveDoc = ModelAPI_Session::get()->activeDocument();
372 if (anActiveDoc.get())
373 aCurrent = anActiveDoc->currentFeature(false);
375 if (!(theMessage->eventID() == kOpStartEvent)) {
376 processFeatures(false);
379 if (anActiveDoc.get() && aCurrent.get() && aCurrent->data()->isValid()) {
380 if (anActiveDoc->currentFeature(false) != aCurrent)
381 anActiveDoc->setCurrentFeature(aCurrent, false); // #2156 make the current feature back
386 // remove all macros before clearing all created
387 std::set<FeaturePtr>::iterator anUpdatedIter = myWaitForFinish.begin();
388 while(anUpdatedIter != myWaitForFinish.end()) {
389 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
390 if (aFeature.get()) {
391 // remove macro on finish
392 if (aFeature->isMacro()) {
393 aFeature->document()->removeFeature(aFeature);
394 myWaitForFinish.erase(aFeature);
396 // to avoid the map update problems on "remove"
397 if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
398 anUpdatedIter = myWaitForFinish.begin();
406 // the redisplay signal should be flushed in order
407 // to erase the feature presentation in the viewer
408 // if should be done after removeFeature() of document,
409 // by this reason, upper processFeatures() do not perform this flush
410 Events_Loop::loop()->flush(kRedisplayEvent);
412 // in the end of transaction everything is updated, so clear the old objects
413 myIsParamUpdated = false;
414 myWaitForFinish.clear();
415 } else if (theMessage->eventID() == kReorderEvent) {
416 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aMsg =
417 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
418 if (aMsg->reordered().get())
419 addModified(aMsg->reordered(), aMsg->reordered()); // to update all attributes
423 void Model_Update::processFeatures(const bool theFlushRedisplay)
425 // perform update of everything if it is not performed right now or any preview is blocked
426 if (!myIsProcessed && !myIsPreviewBlocked && !myUpdateBlocked) {
427 myIsProcessed = true;
429 std::cout<<"****** Start processing"<<std::endl;
432 while(!myModified.empty()) {
433 processFeature(myModified.begin()->first);
435 myIsProcessed = false;
437 // to update the object browser if something is updated/created during executions
438 static Events_Loop* aLoop = Events_Loop::loop();
439 static const Events_ID kCreatedEvent= aLoop->eventByName(EVENT_OBJECT_CREATED);
440 aLoop->flush(kCreatedEvent);
441 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
442 aLoop->flush(kUpdatedEvent);
444 // flush to update display
445 if (theFlushRedisplay) {
446 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
447 aLoop->flush(EVENT_DISP);
450 std::cout<<"****** End processing"<<std::endl;
456 // collects all the feautres this feature depends on: reasons
457 static void allReasons(FeaturePtr theFeature, std::set<FeaturePtr>& theReasons) {
458 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aDeps;
459 theFeature->data()->referencesToObjects(aDeps);
460 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > >::iterator
461 anAttrsIter = aDeps.begin();
462 for(; anAttrsIter != aDeps.end(); anAttrsIter++) {
463 if (theFeature->attribute(anAttrsIter->first)->isArgument()) {
464 std::list<std::shared_ptr<ModelAPI_Object> >::iterator aDepIter = anAttrsIter->second.begin();
465 for(; aDepIter != anAttrsIter->second.end(); aDepIter++) {
466 FeaturePtr aDepFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(*aDepIter);
467 if (!aDepFeat.get()) { // so, it depends on the result and process the feature owner of it
468 ResultPtr aDepRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aDepIter);
470 aDepFeat = (*aDepIter)->document()->feature(aDepRes);
473 if (aDepFeat.get() && aDepFeat->data()->isValid()) {
474 theReasons.insert(aDepFeat);
479 if (theFeature->getKind() == "Part") {
480 // part is not depended on its subs directly, but subs must be iterated anyway
481 CompositeFeaturePtr aPart = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
482 int aNum = aPart->numberOfSubs();
483 for(int a = 0; a < aNum; a++) {
484 FeaturePtr aSub = aPart->subFeature(a);
485 if (aSub.get() && aSub->data()->isValid()) {
486 theReasons.insert(aSub);
492 bool Model_Update::processFeature(FeaturePtr theFeature)
494 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
496 if (!theFeature->data()->isValid()) { // deleted feature, just remove from all containers
497 if (myModified.find(theFeature) != myModified.end())
498 myModified.erase(theFeature);
502 if (theFeature->isPersistentResult()) {
503 if (!std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures())
507 if (myProcessed.find(theFeature) == myProcessed.end()) {
508 myProcessed[theFeature] = 0;
510 int aCount = myProcessed[theFeature];
512 // too many repetition of processing (in VS it may crash on 330 with stack overflow)
513 Events_InfoMessage("Model_Update",
514 "Feature '%1' is updated in infinitive loop").arg(theFeature->data()->name()).send();
519 myProcessed[theFeature] = aCount + 1;
522 // check this feature is not yet checked or processed
523 bool aIsModified = myModified.find(theFeature) != myModified.end();
524 if (!aIsModified && myIsFinish) { // get info about the modification for features without preview
525 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
527 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
528 // contains itself, so, we don't know which was the reason and the reason is any
529 aNewSet.insert(theFeature);
530 myModified[theFeature] = aNewSet;
535 std::cout<<"* Process feature "<<theFeature->name()<<std::endl;
538 // update the sketch plane before the sketch sub-elements are recomputed
539 // (otherwise sketch will update plane, modify subs, after executed, but with old subs edges)
540 if (aIsModified && theFeature->getKind() == "Sketch") {
542 std::cout<<"****** Update sketch args "<<theFeature->name()<<std::endl;
544 AttributeSelectionPtr anExtSel = theFeature->selection("External");
545 if (anExtSel.get()) {
546 ResultPtr aContext = anExtSel->context();
547 if (aContext.get() && aContext->document().get()) {
548 FeaturePtr anExtBase = aContext->document()->feature(aContext);
549 if (anExtBase.get()) {
550 processFeature(anExtBase);
554 std::shared_ptr<GeomAPI_Shape> aShapeBefore = anExtSel->value();
555 if (!aShapeBefore.get() && anExtSel->context()) aShapeBefore = anExtSel->context()->shape();
556 updateArguments(theFeature);
557 std::shared_ptr<GeomAPI_Shape> aShapeAfter = anExtSel->value();
558 if (!aShapeAfter.get() && anExtSel->context()) aShapeAfter = anExtSel->context()->shape();
559 // if selected plane is changed, try to re-take external references of all subs of the sketch
560 if (aShapeBefore.get() && !aShapeBefore->isEqual(aShapeAfter)) {
561 std::set<FeaturePtr> aWholeR;
562 allReasons(theFeature, aWholeR);
563 std::set<FeaturePtr>::iterator aRIter = aWholeR.begin();
564 for(; aRIter != aWholeR.end(); aRIter++) {
565 if ((*aRIter)->data()->selection("External"))
566 (*aRIter)->attributeChanged("External");
571 if (!aIsModified) { // no modification is needed
575 // evaluate parameter before the sub-elements update:
576 // it updates dependencies on evaluation (#1085)
577 if (theFeature->getKind() == "Parameter") {
578 theFeature->execute();
581 bool isReferencedInvalid = false;
582 // check all features this feature depended on (recursive call of updateFeature)
583 std::set<FeaturePtr>& aReasons = myModified[theFeature];
584 bool allSubsUsed = aReasons.find(theFeature) != aReasons.end();
586 // add all subs in aReasons and temporary remove "theFeature" to avoid processing itself
587 allReasons(theFeature, aReasons);
588 aReasons.erase(theFeature);
590 // take reasons one by one (they may be added during the feature process
591 // (circle by the radius of sketch)
592 std::set<FeaturePtr> aProcessedReasons;
593 while(!aReasons.empty()) {
594 FeaturePtr aReason = *(aReasons.begin());
596 //cout<<theFeature->name()<<" process next reason "<<aReason->name()<<endl;
598 if (aReason != theFeature && (aReason)->data()->isValid()) {
599 if (processFeature(aReason))
601 // check validity of aReason once again because it may be removed by dependent feature
602 // (e.g. by SketchPlugin_IntersectionPoint)
603 if (!aReason->data()->isValid() ||
604 aReason->data()->execState() == ModelAPI_StateInvalidArgument)
605 isReferencedInvalid = true;
607 // searching for the next not used reason
608 aProcessedReasons.insert(aReason);
609 aReasons.erase(aReason);
611 // restore the modified reasons: they will be used in the update of arguments
612 if (allSubsUsed) { // restore theFeature in this set
613 aProcessedReasons.insert(theFeature);
615 myModified[theFeature] = aProcessedReasons;
617 // do not execute the composite that contains the current
618 bool isPostponedMain = false;
619 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
620 if (theFeature->getKind() == "ExtrusionSketch" && aCompos.get()) {
621 CompositeFeaturePtr aCurrentOwner =
622 ModelAPI_Tools::compositeOwner(theFeature->document()->currentFeature(false));
623 isPostponedMain = aCurrentOwner.get() && aCompos->isSub(aCurrentOwner);
624 } else if (theFeature->getKind() == "Sketch" &&
625 std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures()) {
626 // send event that sketch is prepared to be recomputed
627 static Events_ID anID = Events_Loop::eventByName("SketchPrepared");
628 std::shared_ptr<Events_Message> aMsg(new Events_Message(anID, this));
629 Events_Loop* aLoop = Events_Loop::loop();
630 // in case it is finish operation, flush for the sketch other events (#2450)
631 aLoop->flush(aLoop->eventByName(EVENT_OBJECT_UPDATED));
633 // check that sub-elements of sketch are updated => sketch must be re-processed
634 std::set<FeaturePtr> aWholeR;
635 allReasons(theFeature, aWholeR);
636 std::set<FeaturePtr>::iterator aRIter = aWholeR.begin();
637 for(; aRIter != aWholeR.end(); aRIter++) {
638 if (myModified.find(*aRIter) != myModified.end()) {
639 processFeature(theFeature);
646 std::cout<<"Update args "<<theFeature->name()<<std::endl;
648 // TestImport.py : after arguments are updated, theFeature may be removed
649 if (!theFeature->data()->isValid())
651 // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
652 updateArguments(theFeature);
654 // add this feature to the processed right now to be able remove it from this list on
655 // update signal during this feature execution
656 myModified.erase(theFeature);
657 if (myNotPersistentRefs.find(theFeature) != myNotPersistentRefs.end())
658 myNotPersistentRefs.erase(theFeature);
659 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
660 theFeature->data()->execState(ModelAPI_StateDone);
662 // this checking must be after the composite feature sub-elements processing:
663 // composite feature status may depend on it's subelements
664 if ((theFeature->data()->execState() == ModelAPI_StateInvalidArgument || isReferencedInvalid) &&
665 theFeature->getKind() != "Part") {
666 // don't disable Part because it will make disabled all the features
667 // (performance and problems with the current feature)
669 std::cout<<"Invalid args "<<theFeature->name()<<std::endl;
671 theFeature->eraseResults(false);
672 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
673 return true; // so, feature is modified (results are erased)
676 // execute feature if it must be updated
677 ModelAPI_ExecState aState = theFeature->data()->execState();
678 if (aFactory->validate(theFeature)) {
679 if (!isPostponedMain) {
680 executeFeature(theFeature);
684 std::cout<<"Feature is not valid, erase results "<<theFeature->name()<<std::endl;
686 theFeature->eraseResults(false);
687 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
692 void Model_Update::redisplayWithResults(
693 FeaturePtr theFeature, const ModelAPI_ExecState theState, bool theUpdateState)
695 // make updated and redisplay all results
696 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
698 std::list<ResultPtr> allResults;
699 ModelAPI_Tools::allResults(theFeature, allResults);
700 std::list<ResultPtr>::iterator aRIter = allResults.begin();
701 for (; aRIter != allResults.cend(); aRIter++) {
702 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
703 if (!aRes->isDisabled()) {
704 // update state only for enabled results
705 // (Placement Result Part may make the original Part Result as invalid)
707 aRes->data()->execState(theState);
709 if (theFeature->data()->updateID() > aRes->data()->updateID()) {
710 aRes->data()->setUpdateID(theFeature->data()->updateID());
712 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
714 // to redisplay "presentable" feature (for ex. distance constraint)
715 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
717 theFeature->data()->execState(theState);
720 /// Updates the state by the referenced object: if something bad with it, set state for this one
721 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
724 ModelAPI_ExecState aRefState = theTarget->data()->execState();
725 if (aRefState == ModelAPI_StateMustBeUpdated) {
726 if (theCurrent == ModelAPI_StateDone)
727 return ModelAPI_StateMustBeUpdated;
728 } else if (aRefState != ModelAPI_StateDone) {
729 return ModelAPI_StateInvalidArgument;
735 void Model_Update::updateArguments(FeaturePtr theFeature) {
736 // perform this method also for disabled features: to make "not done" state for
737 // features referenced to the active and modified features
738 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
740 ModelAPI_ExecState aState = theFeature->data()->execState();
741 if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
742 aState = ModelAPI_StateMustBeUpdated;
744 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
745 aState = ModelAPI_StateMustBeUpdated;
746 // check the parameters state
749 std::list<AttributePtr> anAttrinbutes =
750 theFeature->data()->attributes(ModelAPI_AttributeInteger::typeId());
751 std::list<AttributePtr>::iterator anIter = anAttrinbutes.begin();
752 for(; anIter != anAttrinbutes.end(); anIter++) {
753 AttributeIntegerPtr anAttribute =
754 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(*anIter);
755 if (anAttribute.get() && !anAttribute->text().empty()) {
756 if (myIsParamUpdated) {
757 ModelAPI_AttributeEvalMessage::send(anAttribute, this);
759 if (anAttribute->expressionInvalid()) {
760 aState = ModelAPI_StateInvalidArgument;
767 std::list<AttributePtr> aDoubles =
768 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
769 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
770 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
771 AttributeDoublePtr aDouble =
772 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
773 if (aDouble.get() && !aDouble->text().empty()) {
774 if (myIsParamUpdated) {
775 ModelAPI_AttributeEvalMessage::send(aDouble, this);
777 if (aDouble->expressionInvalid()) {
778 aState = ModelAPI_StateInvalidArgument;
785 std::list<AttributePtr> anAttributes =
786 theFeature->data()->attributes(GeomDataAPI_Point::typeId());
787 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
788 for(; anIter != anAttributes.end(); anIter++) {
789 AttributePointPtr aPointAttribute =
790 std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
791 if (aPointAttribute.get() && (!aPointAttribute->textX().empty() ||
792 !aPointAttribute->textY().empty() || !aPointAttribute->textZ().empty())) {
793 if (myIsParamUpdated) {
794 ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
796 if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
797 (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
798 (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
799 aState = ModelAPI_StateInvalidArgument;
805 std::list<AttributePtr> anAttributes =
806 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
807 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
808 for(; anIter != anAttributes.end(); anIter++) {
809 AttributePoint2DPtr aPoint2DAttribute =
810 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
811 if (aPoint2DAttribute.get()) {
812 if (myIsParamUpdated && (!aPoint2DAttribute->textX().empty() ||
813 !aPoint2DAttribute->textY().empty())) {
814 ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
816 if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
817 (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
818 aState = ModelAPI_StateInvalidArgument;
822 // update the selection attributes if any
823 std::list<AttributePtr> aRefs =
824 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
825 std::list<AttributePtr>::iterator aRefsIter = aRefs.begin();
826 for (; aRefsIter != aRefs.end(); aRefsIter++) {
827 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
828 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
829 ObjectPtr aContext = aSel->context();
830 // update argument only if the referenced object is ready to use
831 if (aContext.get() && !aContext->isDisabled()) {
832 if (isReason(theFeature, aContext)) {
833 if (!aSel->update()) { // this must be done on execution since it may be long operation
834 bool isObligatory = !aFactory->isNotObligatory(
835 theFeature->getKind(), theFeature->data()->id(aSel)) &&
836 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
838 aState = ModelAPI_StateInvalidArgument;
841 } else if (aContext.get()) {
842 // here it may be not obligatory, but if the reference is wrong, it should not be correct
843 bool isObligatory = aFactory->isCase(theFeature, theFeature->data()->id(aSel));
845 aState = ModelAPI_StateInvalidArgument;
848 // update the selection list attributes if any
849 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
850 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
851 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
852 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
853 for(int a = aSel->size() - 1; a >= 0; a--) {
854 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
855 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
857 ObjectPtr aContext = aSelAttr->context();
858 // update argument only if the referenced object is ready to use
859 if (aContext.get() && !aContext->isDisabled() && !aSelAttr->isInvalid()) {
860 if (isReason(theFeature, aContext)) {
861 if (!aSelAttr->update()) {
862 bool isObligatory = !aFactory->isNotObligatory(
863 theFeature->getKind(), theFeature->data()->id(aSel)) &&
864 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
866 aState = ModelAPI_StateInvalidArgument;
869 } else if (aContext.get() || aSelAttr->isInvalid()) {
870 // here it may be not obligatory, but if the reference is wrong, it should not be correct
871 bool isObligatory = aFactory->isCase(theFeature, theFeature->data()->id(aSel));
873 aState = ModelAPI_StateInvalidArgument;
879 if (aState != ModelAPI_StateDone)
880 theFeature->data()->execState(aState);
883 bool Model_Update::isReason(std::shared_ptr<ModelAPI_Feature>& theFeature,
884 std::shared_ptr<ModelAPI_Object> theReason)
886 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
887 ::iterator aReasonsIt = myModified.find(theFeature);
888 if (aReasonsIt != myModified.end()) {
889 if (aReasonsIt->second.find(theFeature) != aReasonsIt->second.end())
890 return true; // any is reason if it contains itself
891 FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
892 if (!aReasFeat.get()) { // try to get feature of this result
893 ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
895 aReasFeat = theReason->document()->feature(aReasRes);
897 if (aReasonsIt->second.find(aReasFeat) != aReasonsIt->second.end())
900 // another try: postponed modification by not-persistences
901 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
902 ::iterator aNotPersist = myNotPersistentRefs.find(theFeature);
903 if (aNotPersist != myNotPersistentRefs.end()) {
904 FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
905 if (!aReasFeat.get()) { // try to get feature of this result
906 ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
908 aReasFeat = theReason->document()->feature(aReasRes);
910 if (aNotPersist->second.find(aReasFeat) != aNotPersist->second.end())
914 // this case only for not-previewed items update state, nothing is changed in args for it
918 void Model_Update::executeFeature(FeaturePtr theFeature)
921 std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
923 // execute in try-catch to avoid internal problems of the feature
924 ModelAPI_ExecState aState = ModelAPI_StateDone;
925 theFeature->data()->execState(ModelAPI_StateDone);
927 theFeature->execute();
928 if (theFeature->data()->execState() != ModelAPI_StateDone) {
929 aState = ModelAPI_StateExecFailed;
931 aState = ModelAPI_StateDone;
934 aState = ModelAPI_StateExecFailed;
935 Events_InfoMessage("Model_Update",
936 "Feature %1 has failed during the execution").arg(theFeature->getKind()).send();
938 // The macro feature has to be deleted in any case even its execution is failed
939 myWaitForFinish.insert(theFeature);
940 if (aState != ModelAPI_StateDone) {
941 theFeature->eraseResults(false);
943 theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
944 redisplayWithResults(theFeature, aState);
947 void Model_Update::updateStability(void* theSender)
949 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
951 bool added = false; // object may be was crated
952 ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theSender);
953 if (aSender && aSender->document()) {
954 FeaturePtr aFeatureSender =
955 std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
956 if (aFeatureSender.get()) {
957 Model_Objects* aDocObjects =
958 std::dynamic_pointer_cast<Model_Document>(aSender->document())->objects();
960 //aDocObjects->synchronizeBackRefs();
961 // remove or add all concealment refs from this feature
962 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
963 aSender->data()->referencesToObjects(aRefs);
964 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
965 aRefIt = aRefs.begin();
966 for(; aRefIt != aRefs.end(); aRefIt++) {
967 if (!aFactory->isConcealed(aFeatureSender->getKind(), aRefIt->first))
968 // take into account only concealed references
969 // (do not remove the sketch constraint and the edge on constraint edit)
971 std::list<ObjectPtr>& aRefFeaturesList = aRefIt->second;
972 std::list<ObjectPtr>::iterator aReferenced = aRefFeaturesList.begin();
973 for(; aReferenced != aRefFeaturesList.end(); aReferenced++) {
974 // stability is only on results: feature to feature reference mean nested
975 // features, that will remove nesting references
976 if (aReferenced->get() && (*aReferenced)->data()->isValid() &&
977 (*aReferenced)->groupName() != ModelAPI_Feature::group()) {
978 std::shared_ptr<Model_Data> aData =
979 std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
980 if (aFeatureSender->isStable()) {
981 aData->addBackReference(aFeatureSender, aRefIt->first);
983 aData->removeBackReference(aFeatureSender, aRefIt->first);
984 added = true; // remove of concealment may be caused creation of some result
993 static Events_Loop* aLoop = Events_Loop::loop();
994 static Events_ID kEventCreated = aLoop->eventByName(EVENT_OBJECT_CREATED);
995 aLoop->flush(kEventCreated);
1000 void Model_Update::updateSelection(const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects)
1002 std::set<std::shared_ptr<ModelAPI_Object> >::iterator anObj = theObjects.begin();
1003 for(; anObj != theObjects.end(); anObj++) {
1004 std::list<AttributePtr> aRefs =
1005 (*anObj)->data()->attributes(ModelAPI_AttributeSelection::typeId());
1006 std::list<AttributePtr>::iterator aRefsIter = aRefs.begin();
1007 for (; aRefsIter != aRefs.end(); aRefsIter++) {
1008 std::shared_ptr<Model_AttributeSelection> aSel =
1009 std::dynamic_pointer_cast<Model_AttributeSelection>(*aRefsIter);
1010 aSel->updateInHistory();
1012 // update the selection list attributes if any
1013 aRefs = (*anObj)->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1014 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
1015 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
1016 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
1017 for(int a = aSel->size() - 1; a >= 0; a--) {
1018 std::shared_ptr<Model_AttributeSelection> aSelAttr =
1019 std::dynamic_pointer_cast<Model_AttributeSelection>(aSel->value(a));
1021 aSelAttr->updateInHistory();