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 <ModelAPI_ResultBody.h>
42 #include <ModelAPI_ResultPart.h>
43 #include <ModelAPI_ResultConstruction.h>
44 #include <GeomAPI_Shape.h>
45 #include <GeomDataAPI_Point.h>
46 #include <GeomDataAPI_Point2D.h>
47 #include <Events_Loop.h>
48 #include <Events_LongOp.h>
49 #include <Events_InfoMessage.h>
50 #include <Config_PropManager.h>
52 Model_Update MY_UPDATER_INSTANCE; /// the only one instance initialized on load of the library
55 Model_Update::Model_Update()
57 Events_Loop* aLoop = Events_Loop::loop();
58 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
59 aLoop->registerListener(this, kChangedEvent);
60 static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
61 aLoop->registerListener(this, kCreatedEvent);
62 static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
63 aLoop->registerListener(this, kUpdatedEvent);
64 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
65 aLoop->registerListener(this, kOpFinishEvent);
66 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
67 aLoop->registerListener(this, kOpAbortEvent);
68 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
69 aLoop->registerListener(this, kOpStartEvent);
70 static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
71 aLoop->registerListener(this, kStabilityEvent);
72 static const Events_ID kPreviewBlockedEvent = aLoop->eventByName(EVENT_PREVIEW_BLOCKED);
73 aLoop->registerListener(this, kPreviewBlockedEvent);
74 static const Events_ID kPreviewRequestedEvent = aLoop->eventByName(EVENT_PREVIEW_REQUESTED);
75 aLoop->registerListener(this, kPreviewRequestedEvent);
76 static const Events_ID kReorderEvent = aLoop->eventByName(EVENT_ORDER_UPDATED);
77 aLoop->registerListener(this, kReorderEvent);
78 static const Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
79 aLoop->registerListener(this, kUpdatedSel);
80 static const Events_ID kAutoRecomp = aLoop->eventByName(EVENT_AUTOMATIC_RECOMPUTATION_ENABLE);
81 aLoop->registerListener(this, kAutoRecomp);
83 // Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
84 myIsParamUpdated = false;
86 myIsProcessed = false;
87 myIsPreviewBlocked = false;
88 myUpdateBlocked = false;
91 bool Model_Update::addModified(FeaturePtr theFeature, FeaturePtr theReason) {
93 if (!theFeature->data()->isValid())
94 return false; // delete an extrusion created on the sketch
97 bool isNotExecuted = theFeature->isPersistentResult() &&
98 !std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures();
100 redisplayWithResults(theFeature, ModelAPI_StateNothing, false); // redisplay even not executed
101 if (!theReason.get()) // no reason => no construction reason
103 if (myNotPersistentRefs.find(theFeature) == myNotPersistentRefs.end()) {
104 myNotPersistentRefs[theFeature].insert(theReason);
106 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
107 aNewSet.insert(theReason);
108 myNotPersistentRefs[theFeature] = aNewSet;
113 // update arguments for "apply button" state change
114 if ((!theFeature->isPreviewNeeded() && !myIsFinish) || myIsPreviewBlocked) {
116 myProcessOnFinish[theFeature].insert(theReason);
117 else if (myProcessOnFinish.find(theFeature) == myProcessOnFinish.end())
118 myProcessOnFinish[theFeature] = std::set<std::shared_ptr<ModelAPI_Feature> >();
120 std::cout<<"*** Add process on finish "<<theFeature->name()<<std::endl;
122 // keeps the currently updated features to avoid infinitive cycling here: where feature on
123 // "updateArguments" sends "updated" (in selection attribute) and goes here again
124 static std::set<FeaturePtr> aCurrentlyUpdated;
125 if (aCurrentlyUpdated.find(theFeature) == aCurrentlyUpdated.end()) {
126 aCurrentlyUpdated.insert(theFeature);
127 updateArguments(theFeature);
128 aCurrentlyUpdated.erase(theFeature);
130 // make it without conditions otherwise the apply button may have a bad state
131 theFeature->data()->execState(ModelAPI_StateDone);
132 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
133 // need to be validated to update the "Apply" state if not previewed
134 aFactory->validate(theFeature);
136 // to redisplay split's arguments presentation, even result is not computed
137 if (!theFeature->isPreviewNeeded()) {
138 static Events_Loop* aLoop = Events_Loop::loop();
139 static const Events_ID kRedisplayEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
140 ModelAPI_EventCreator::get()->sendUpdated(theFeature, kRedisplayEvent);
141 aLoop->flush(kRedisplayEvent);
144 if (!myIsPreviewBlocked)
147 if (myModified.find(theFeature) != myModified.end()) {
148 if (theReason.get()) {
150 //std::cout<<"*** Add already modified "
151 // <<theFeature->name()<<" reason "<<theReason->name()<<std::endl;
153 myModified[theFeature].insert(theReason);
157 // do not add the disabled, but possibly the sub-elements are not disabled
158 bool aIsDisabled = theFeature->isDisabled();
160 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
161 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated ||
162 theFeature->data()->execState() == ModelAPI_StateInvalidArgument || // issue 1519
163 theFeature->data()->execState() == ModelAPI_StateExecFailed) {
164 // do not forget that in this case all were the reasons
165 aNewSet.insert(theFeature);
168 aNewSet.insert(theReason);
170 myModified[theFeature] = aNewSet;
172 if (theReason.get()) {
173 //std::cout<<"*** Add modified "<<theFeature->name()
174 // <<" reason "<<theReason->name()<<std::endl;
176 //std::cout<<"*** Add modified "<<theFeature->name()<<std::endl;
179 } else { // will be updated during the finish of the operation, or when it becomes enabled
180 if (theFeature->data()->execState() == ModelAPI_StateDone ||
181 theFeature->data()->execState() == ModelAPI_StateExecFailed) // fix issue 1819
182 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
184 return true; // do not need iteration deeply if it is already marked as modified or so
186 //std::cout<<"*** Set modified state "<<theFeature->name()<<std::endl;
189 // clear processed and fill modified recursively
190 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
191 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
192 for(; aRefIter != aRefs.cend(); aRefIter++) {
193 if ((*aRefIter)->isArgument()) {
194 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
195 if (aReferenced.get()) {
196 addModified(aReferenced, theFeature);
200 // process also results
201 std::list<ResultPtr> allResults; // list of this feature and results
202 ModelAPI_Tools::allResults(theFeature, allResults);
203 std::list<ResultPtr>::iterator aRes = allResults.begin();
204 for(; aRes != allResults.end(); aRes++) {
205 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*aRes)->data()->refsToMe();
206 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
207 for(; aRefIter != aRefs.cend(); aRefIter++) {
208 if ((*aRefIter)->isArgument()) {
209 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
210 if (aReferenced.get()) {
211 addModified(aReferenced, theFeature);
217 // also add part feature that contains this feature to the modified
218 if (theFeature->document()->kind() != "PartSet") {
219 FeaturePtr aPart = ModelAPI_Tools::findPartFeature(
220 ModelAPI_Session::get()->moduleDocument(), theFeature->document());
222 addModified(aPart, theFeature);
227 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
229 static Events_Loop* aLoop = Events_Loop::loop();
230 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
231 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
232 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
233 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
234 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
235 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
236 static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
237 static const Events_ID kPreviewBlockedEvent = aLoop->eventByName(EVENT_PREVIEW_BLOCKED);
238 static const Events_ID kPreviewRequestedEvent = aLoop->eventByName(EVENT_PREVIEW_REQUESTED);
239 static const Events_ID kReorderEvent = aLoop->eventByName(EVENT_ORDER_UPDATED);
240 static const Events_ID kRedisplayEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
241 static const Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
244 std::cout<<"****** Event "<<theMessage->eventID().eventText()<<std::endl;
246 // check the automatic update flag on any event
247 bool aNewAutomaticState = ModelAPI_Session::get()->isAutoUpdateBlocked();
248 if (myUpdateBlocked != aNewAutomaticState) {
249 myUpdateBlocked = aNewAutomaticState;
250 if (!myUpdateBlocked) { // process all modified features, even if preview is blocked
251 bool aPreviewBlockedState = myIsPreviewBlocked; // to update the selected arguments
252 myIsPreviewBlocked = false;
253 // iterate everything and add features in state "MustBeUpdated" into modified
254 std::list<std::shared_ptr<ModelAPI_Document> > allDocs =
255 ModelAPI_Session::get()->allOpenedDocuments();
256 std::list<std::shared_ptr<ModelAPI_Document> >::iterator aDoc = allDocs.begin();
257 for(; aDoc != allDocs.end(); aDoc++) {
258 std::list<std::shared_ptr<ModelAPI_Feature> > allFeats = (*aDoc)->allFeatures();
259 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator aFeat = allFeats.begin();
260 for(; aFeat != allFeats.end(); aFeat++) {
261 if ((*aFeat)->data()->isValid() &&
262 (*aFeat)->data()->execState() == ModelAPI_StateMustBeUpdated) {
263 addModified(*aFeat, FeaturePtr());
268 myIsPreviewBlocked = myIsPreviewBlocked;
272 if (theMessage->eventID() == kStabilityEvent) {
273 updateStability(theMessage->sender());
276 if (theMessage->eventID() == kPreviewBlockedEvent) {
277 myIsPreviewBlocked = true;
280 if (theMessage->eventID() == kPreviewRequestedEvent) {
281 if (myIsPreviewBlocked) {
282 bool anUpdateState = myUpdateBlocked;
283 myUpdateBlocked = false;
284 myIsPreviewBlocked = false;
286 myIsPreviewBlocked = true;
287 myUpdateBlocked = anUpdateState;
291 if (theMessage->eventID() == kUpdatedSel) {
292 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
293 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
294 updateSelection(aMsg->objects());
296 // creation is added to "update" to avoid recomputation twice:
297 // on create and immediately after on update
298 if (theMessage->eventID() == kCreatedEvent) {
299 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
300 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
301 const std::set<ObjectPtr>& anObjs = aMsg->objects();
302 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
303 for(; anObjIter != anObjs.cend(); anObjIter++) {
304 if (std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures()) {
305 if ((*anObjIter)->groupName() == ModelAPI_Feature::group()) {
306 // results creation means enabling, not update
307 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kUpdatedEvent);
309 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kRedisplayEvent);
315 if (theMessage->eventID() == kUpdatedEvent) {
316 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
317 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
318 const std::set<ObjectPtr>& anObjs = aMsg->objects();
319 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
320 bool aSomeModified = false; // check that features not changed: only redisplay is needed
321 for(; anObjIter != anObjs.cend(); anObjIter++) {
322 if (!(*anObjIter)->data()->isValid())
325 std::cout<<">>> in event updated "<<(*anObjIter)->groupName()<<
326 " "<<(*anObjIter)->data()->name()<<std::endl;
328 if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
329 myIsParamUpdated = true;
331 // on undo/redo, abort do not update persistent features
332 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
333 if (anUpdated.get()) {
334 if (addModified(anUpdated, FeaturePtr()))
335 aSomeModified = true;
336 if (myUpdateBlocked) { // execute this feature anyway to show the current result
337 /*if (!anUpdated->isStable() && anUpdated->results().size() && (
338 anUpdated->firstResult()->groupName() == ModelAPI_ResultBody::group() ||
339 anUpdated->firstResult()->groupName() == ModelAPI_ResultPart::group())) {
340 if (aFactory->validate(anUpdated)) {
341 executeFeature(anUpdated);
342 redisplayWithResults(anUpdated, ModelAPI_StateNothing, false);
343 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
344 aLoop->flush(EVENT_DISP);
349 // process the updated result as update of features that refers to this result
350 const std::set<std::shared_ptr<ModelAPI_Attribute> >&
351 aRefs = (*anObjIter)->data()->refsToMe();
352 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
354 ResultPtr aReasonResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIter);
355 if (aReasonResult.get())
356 aReason = (*anObjIter)->document()->feature(aReasonResult);
357 for(; aRefIter != aRefs.cend(); aRefIter++) {
358 if (!(*aRefIter)->owner()->data()->isValid())
360 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
361 if (anUpdated.get()) {
362 if (addModified(anUpdated, aReason))
363 aSomeModified = true;
368 // this event is for solver update, not here, do not react immediately
372 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
373 theMessage->eventID() == kOpStartEvent) {
374 myIsPreviewBlocked = false;
376 if (theMessage->eventID() == kOpFinishEvent) {// if update is blocked, skip
378 // add features that wait for finish as modified
379 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >::
380 iterator aFeature = myProcessOnFinish.begin();
381 for(; aFeature != myProcessOnFinish.end(); aFeature++) {
382 if (aFeature->first->data()->isValid()) {// there may be already removed while wait
383 if (aFeature->second.empty()) {
384 addModified(aFeature->first, FeaturePtr());
387 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aReasons;
388 for(aReasons = aFeature->second.begin(); aReasons != aFeature->second.end(); aReasons++){
389 addModified(aFeature->first, *aReasons);
395 // processed features must be only on finish, so clear anyway (to avoid re-import on load)
396 myProcessOnFinish.clear();
398 // #2156: current must be sketch, left after the macro execution
399 DocumentPtr anActiveDoc = ModelAPI_Session::get()->activeDocument();
401 if (anActiveDoc.get())
402 aCurrent = anActiveDoc->currentFeature(false);
404 if (!(theMessage->eventID() == kOpStartEvent)) {
405 processFeatures(false);
408 if (anActiveDoc.get() && aCurrent.get() && aCurrent->data()->isValid()) {
409 if (anActiveDoc->currentFeature(false) != aCurrent &&
410 ModelAPI_Tools::compositeOwner(anActiveDoc->currentFeature(false)) == aCurrent)
411 anActiveDoc->setCurrentFeature(aCurrent, false); // #2156 make the current feature back
414 // remove all macros before clearing all created
415 std::set<FeaturePtr>::iterator anUpdatedIter = myWaitForFinish.begin();
416 while(anUpdatedIter != myWaitForFinish.end()) {
417 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
418 if (aFeature.get()) {
419 // remove macro on finish
420 if (aFeature->isMacro()) {
421 aFeature->document()->removeFeature(aFeature);
422 myWaitForFinish.erase(aFeature);
424 // to avoid the map update problems on "remove"
425 if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
426 anUpdatedIter = myWaitForFinish.begin();
434 // the redisplay signal should be flushed in order
435 // to erase the feature presentation in the viewer
436 // if should be done after removeFeature() of document,
437 // by this reason, upper processFeatures() do not perform this flush
438 Events_Loop::loop()->flush(kRedisplayEvent);
440 // in the end of transaction everything is updated, so clear the old objects
441 myIsParamUpdated = false;
442 myWaitForFinish.clear();
443 } else if (theMessage->eventID() == kReorderEvent) {
444 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aMsg =
445 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
446 if (aMsg->reordered().get())
447 addModified(aMsg->reordered(), aMsg->reordered()); // to update all attributes
451 void Model_Update::processFeatures(const bool theFlushRedisplay)
453 // perform update of everything if it is not performed right now or any preview is blocked
454 if (!myIsProcessed && !myIsPreviewBlocked) {
455 myIsProcessed = true;
457 std::cout<<"****** Start processing"<<std::endl;
460 while(!myModified.empty()) {
461 processFeature(myModified.begin()->first);
463 myIsProcessed = false;
465 // to update the object browser if something is updated/created during executions
466 static Events_Loop* aLoop = Events_Loop::loop();
467 static const Events_ID kCreatedEvent= aLoop->eventByName(EVENT_OBJECT_CREATED);
468 aLoop->flush(kCreatedEvent);
469 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
470 aLoop->flush(kUpdatedEvent);
472 // flush to update display
473 if (theFlushRedisplay) {
474 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
475 aLoop->flush(EVENT_DISP);
478 std::cout<<"****** End processing"<<std::endl;
484 // collects all the features this feature depends on: reasons
485 static void allReasons(FeaturePtr theFeature, std::set<FeaturePtr>& theReasons) {
486 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aDeps;
487 theFeature->data()->referencesToObjects(aDeps);
488 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > >::iterator
489 anAttrsIter = aDeps.begin();
490 for(; anAttrsIter != aDeps.end(); anAttrsIter++) {
491 if (theFeature->attribute(anAttrsIter->first)->isArgument()) {
492 std::list<std::shared_ptr<ModelAPI_Object> >::iterator aDepIter = anAttrsIter->second.begin();
493 for(; aDepIter != anAttrsIter->second.end(); aDepIter++) {
494 FeaturePtr aDepFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(*aDepIter);
495 if (!aDepFeat.get()) { // so, it depends on the result and process the feature owner of it
496 ResultPtr aDepRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aDepIter);
498 aDepFeat = (*aDepIter)->document()->feature(aDepRes);
501 if (aDepFeat.get() && aDepFeat->data()->isValid()) {
502 theReasons.insert(aDepFeat);
507 if (theFeature->getKind() == "Part") {
508 // part is not depended on its subs directly, but subs must be iterated anyway
509 CompositeFeaturePtr aPart = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
510 int aNum = aPart->numberOfSubs();
511 for(int a = 0; a < aNum; a++) {
512 FeaturePtr aSub = aPart->subFeature(a);
513 if (aSub.get() && aSub->data()->isValid()) {
514 theReasons.insert(aSub);
520 bool Model_Update::processFeature(FeaturePtr theFeature)
522 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
524 if (!theFeature->data()->isValid()) { // deleted feature, just remove from all containers
525 if (myModified.find(theFeature) != myModified.end())
526 myModified.erase(theFeature);
530 if (theFeature->isPersistentResult()) {
531 if (!std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures())
535 if (myProcessed.find(theFeature) == myProcessed.end()) {
536 myProcessed[theFeature] = 0;
538 int aCount = myProcessed[theFeature];
540 // too many repetition of processing (in VS it may crash on 330 with stack overflow)
541 Events_InfoMessage("Model_Update",
542 "Feature '%1' is updated in infinitive loop").arg(theFeature->data()->name()).send();
547 myProcessed[theFeature] = aCount + 1;
550 // check this feature is not yet checked or processed
551 bool aIsModified = myModified.find(theFeature) != myModified.end();
552 if (!aIsModified && myIsFinish) { // get info about the modification for features without preview
553 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
555 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
556 // contains itself, so, we don't know which was the reason and the reason is any
557 aNewSet.insert(theFeature);
558 myModified[theFeature] = aNewSet;
563 std::cout<<"* Process feature "<<theFeature->name()<<std::endl;
566 // update the sketch plane before the sketch sub-elements are recomputed
567 // (otherwise sketch will update plane, modify subs, after executed, but with old subs edges)
568 if (aIsModified && theFeature->getKind() == "Sketch") {
570 std::cout<<"****** Update sketch args "<<theFeature->name()<<std::endl;
572 AttributeSelectionPtr anExtSel = theFeature->selection("External");
573 if (anExtSel.get()) {
574 ResultPtr aContext = anExtSel->context();
575 if (aContext.get() && aContext->document().get()) {
576 FeaturePtr anExtBase = aContext->document()->feature(aContext);
577 if (anExtBase.get()) {
578 processFeature(anExtBase);
582 std::shared_ptr<GeomAPI_Shape> aShapeBefore = anExtSel->value();
583 if (!aShapeBefore.get() && anExtSel->context()) aShapeBefore = anExtSel->context()->shape();
584 updateArguments(theFeature);
585 std::shared_ptr<GeomAPI_Shape> aShapeAfter = anExtSel->value();
586 if (!aShapeAfter.get() && anExtSel->context()) aShapeAfter = anExtSel->context()->shape();
587 // if selected plane is changed, try to re-take external references of all subs of the sketch
588 if (aShapeBefore.get() && !aShapeBefore->isEqual(aShapeAfter)) {
589 std::set<FeaturePtr> aWholeR;
590 allReasons(theFeature, aWholeR);
591 std::set<FeaturePtr>::iterator aRIter = aWholeR.begin();
592 for(; aRIter != aWholeR.end(); aRIter++) {
593 if ((*aRIter)->data()->selection("External"))
594 (*aRIter)->attributeChanged("External");
599 if (!aIsModified) { // no modification is needed
603 // evaluate parameter before the sub-elements update:
604 // it updates dependencies on evaluation (#1085)
605 if (theFeature->getKind() == "Parameter") {
606 theFeature->execute();
609 bool isReferencedInvalid = false;
610 // check all features this feature depended on (recursive call of updateFeature)
611 std::set<FeaturePtr>& aReasons = myModified[theFeature];
612 bool allSubsUsed = aReasons.find(theFeature) != aReasons.end();
614 // add all subs in aReasons and temporary remove "theFeature" to avoid processing itself
615 allReasons(theFeature, aReasons);
616 aReasons.erase(theFeature);
618 // take reasons one by one (they may be added during the feature process
619 // (circle by the radius of sketch)
620 std::set<FeaturePtr> aProcessedReasons;
621 while(!aReasons.empty()) {
622 FeaturePtr aReason = *(aReasons.begin());
624 //cout<<theFeature->name()<<" process next reason "<<aReason->name()<<endl;
626 if (aReason != theFeature && (aReason)->data()->isValid()) {
627 if (processFeature(aReason))
629 // check validity of aReason once again because it may be removed by dependent feature
630 // (e.g. by SketchPlugin_IntersectionPoint)
631 if (!aReason->data()->isValid() ||
632 aReason->data()->execState() == ModelAPI_StateInvalidArgument)
633 isReferencedInvalid = true;
635 // searching for the next not used reason
636 aProcessedReasons.insert(aReason);
637 aReasons.erase(aReason);
639 // restore the modified reasons: they will be used in the update of arguments
640 if (allSubsUsed) { // restore theFeature in this set
641 aProcessedReasons.insert(theFeature);
643 myModified[theFeature] = aProcessedReasons;
645 // do not execute the composite that contains the current
646 bool isPostponedMain = false;
647 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
648 if (theFeature->getKind() == "ExtrusionSketch" && aCompos.get()) {
649 CompositeFeaturePtr aCurrentOwner =
650 ModelAPI_Tools::compositeOwner(theFeature->document()->currentFeature(false));
651 isPostponedMain = aCurrentOwner.get() && aCompos->isSub(aCurrentOwner);
652 } else if (theFeature->getKind() == "Sketch" &&
653 std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures()) {
654 // send event that sketch is prepared to be recomputed
655 static Events_ID anID = Events_Loop::eventByName("SketchPrepared");
656 std::shared_ptr<Events_Message> aMsg(new Events_Message(anID, this));
657 Events_Loop* aLoop = Events_Loop::loop();
658 // in case it is finish operation, flush for the sketch other events (#2450)
659 aLoop->flush(aLoop->eventByName(EVENT_OBJECT_UPDATED));
661 // check that sub-elements of sketch are updated => sketch must be re-processed
662 std::set<FeaturePtr> aWholeR;
663 allReasons(theFeature, aWholeR);
664 std::set<FeaturePtr>::iterator aRIter = aWholeR.begin();
665 for(; aRIter != aWholeR.end(); aRIter++) {
666 if (myModified.find(*aRIter) != myModified.end()) {
667 processFeature(theFeature);
674 std::cout<<"Update args "<<theFeature->name()<<std::endl;
676 // TestImport.py : after arguments are updated, theFeature may be removed
677 if (!theFeature->data()->isValid())
679 // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
680 updateArguments(theFeature);
682 // add this feature to the processed right now to be able remove it from this list on
683 // update signal during this feature execution
684 myModified.erase(theFeature);
685 if (myNotPersistentRefs.find(theFeature) != myNotPersistentRefs.end())
686 myNotPersistentRefs.erase(theFeature);
687 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
688 theFeature->data()->execState(ModelAPI_StateDone);
690 // this checking must be after the composite feature sub-elements processing:
691 // composite feature status may depend on it's sub-elements
692 if ((theFeature->data()->execState() == ModelAPI_StateInvalidArgument || isReferencedInvalid) &&
693 theFeature->getKind() != "Part") {
694 // don't disable Part because it will make disabled all the features
695 // (performance and problems with the current feature)
697 std::cout<<"Invalid args "<<theFeature->name()<<std::endl;
699 theFeature->eraseResults(false);
700 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
701 return true; // so, feature is modified (results are erased)
704 // execute feature if it must be updated
705 ModelAPI_ExecState aState = theFeature->data()->execState();
706 if (aFactory->validate(theFeature)) {
707 if (!isPostponedMain) {
708 bool aDoExecute = true;
709 if (myUpdateBlocked) {
710 if (!theFeature->isStable()) {
712 } else if (theFeature->results().size()) { // execute only not-results features
713 aDoExecute = !(theFeature->firstResult()->groupName() == ModelAPI_ResultBody::group() ||
714 theFeature->firstResult()->groupName() == ModelAPI_ResultPart::group() ||
715 theFeature->getKind() == "Sketch");
717 aDoExecute = aState != ModelAPI_StateInvalidArgument;
721 executeFeature(theFeature);
723 // store information that this feature must be executed later
724 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
729 std::cout<<"Feature is not valid, erase results "<<theFeature->name()<<std::endl;
731 theFeature->eraseResults(false);
732 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
737 void Model_Update::redisplayWithResults(
738 FeaturePtr theFeature, const ModelAPI_ExecState theState, bool theUpdateState)
740 // make updated and redisplay all results
741 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
743 std::list<ResultPtr> allResults;
744 ModelAPI_Tools::allResults(theFeature, allResults);
745 std::list<ResultPtr>::iterator aRIter = allResults.begin();
746 for (; aRIter != allResults.cend(); aRIter++) {
747 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
748 if (!aRes->isDisabled()) {
749 // update state only for enabled results
750 // (Placement Result Part may make the original Part Result as invalid)
752 aRes->data()->execState(theState);
754 if (theFeature->data()->updateID() > aRes->data()->updateID()) {
755 aRes->data()->setUpdateID(theFeature->data()->updateID());
757 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
759 // to redisplay "presentable" feature (for ex. distance constraint)
760 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
762 theFeature->data()->execState(theState);
765 /// Updates the state by the referenced object: if something bad with it, set state for this one
766 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
769 ModelAPI_ExecState aRefState = theTarget->data()->execState();
770 if (aRefState == ModelAPI_StateMustBeUpdated) {
771 if (theCurrent == ModelAPI_StateDone)
772 return ModelAPI_StateMustBeUpdated;
773 } else if (aRefState != ModelAPI_StateDone) {
774 return ModelAPI_StateInvalidArgument;
780 void Model_Update::updateArguments(FeaturePtr theFeature) {
781 // perform this method also for disabled features: to make "not done" state for
782 // features referenced to the active and modified features
783 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
785 ModelAPI_ExecState aState = theFeature->data()->execState();
786 if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
787 aState = ModelAPI_StateMustBeUpdated;
789 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
790 aState = ModelAPI_StateMustBeUpdated;
791 // check the parameters state
794 std::list<AttributePtr> anAttrinbutes =
795 theFeature->data()->attributes(ModelAPI_AttributeInteger::typeId());
796 std::list<AttributePtr>::iterator anIter = anAttrinbutes.begin();
797 for(; anIter != anAttrinbutes.end(); anIter++) {
798 AttributeIntegerPtr anAttribute =
799 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(*anIter);
800 if (anAttribute.get() && !anAttribute->text().empty()) {
801 if (myIsParamUpdated) {
802 ModelAPI_AttributeEvalMessage::send(anAttribute, this);
804 if (anAttribute->expressionInvalid()) {
805 aState = ModelAPI_StateInvalidArgument;
812 std::list<AttributePtr> aDoubles =
813 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
814 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
815 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
816 AttributeDoublePtr aDouble =
817 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
818 if (aDouble.get() && !aDouble->text().empty()) {
819 if (myIsParamUpdated) {
820 ModelAPI_AttributeEvalMessage::send(aDouble, this);
822 if (aDouble->expressionInvalid()) {
823 aState = ModelAPI_StateInvalidArgument;
830 std::list<AttributePtr> anAttributes =
831 theFeature->data()->attributes(GeomDataAPI_Point::typeId());
832 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
833 for(; anIter != anAttributes.end(); anIter++) {
834 AttributePointPtr aPointAttribute =
835 std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
836 if (aPointAttribute.get() && (!aPointAttribute->textX().empty() ||
837 !aPointAttribute->textY().empty() || !aPointAttribute->textZ().empty())) {
838 if (myIsParamUpdated) {
839 ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
841 if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
842 (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
843 (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
844 aState = ModelAPI_StateInvalidArgument;
850 std::list<AttributePtr> anAttributes =
851 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
852 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
853 for(; anIter != anAttributes.end(); anIter++) {
854 AttributePoint2DPtr aPoint2DAttribute =
855 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
856 if (aPoint2DAttribute.get()) {
857 if (myIsParamUpdated && (!aPoint2DAttribute->textX().empty() ||
858 !aPoint2DAttribute->textY().empty())) {
859 ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
861 if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
862 (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
863 aState = ModelAPI_StateInvalidArgument;
867 // update the selection attributes if any
868 std::list<AttributePtr> aRefs =
869 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
870 std::list<AttributePtr>::iterator aRefsIter = aRefs.begin();
871 for (; aRefsIter != aRefs.end(); aRefsIter++) {
872 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
873 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
874 ObjectPtr aContext = aSel->context();
875 // update argument only if the referenced object is ready to use
876 if (aContext.get() && !aContext->isDisabled()) {
877 if (isReason(theFeature, aContext)) {
878 if (!aSel->update()) { // this must be done on execution since it may be long operation
879 bool isObligatory = !aFactory->isNotObligatory(
880 theFeature->getKind(), theFeature->data()->id(aSel)) &&
881 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
883 aState = ModelAPI_StateInvalidArgument;
886 } else if (aContext.get()) {
887 // here it may be not obligatory, but if the reference is wrong, it should not be correct
888 bool isObligatory = aFactory->isCase(theFeature, theFeature->data()->id(aSel));
890 aState = ModelAPI_StateInvalidArgument;
893 // update the selection list attributes if any
894 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
895 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
896 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
897 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
898 for(int a = aSel->size() - 1; a >= 0; a--) {
899 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
900 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
902 ObjectPtr aContext = aSelAttr->context();
903 // update argument only if the referenced object is ready to use
904 if (aContext.get() && !aContext->isDisabled() && !aSelAttr->isInvalid()) {
905 if (isReason(theFeature, aContext)) {
906 if (!aSelAttr->update()) {
907 bool isObligatory = !aFactory->isNotObligatory(
908 theFeature->getKind(), theFeature->data()->id(aSel)) &&
909 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
911 aState = ModelAPI_StateInvalidArgument;
914 } else if (aContext.get() || aSelAttr->isInvalid()) {
915 // here it may be not obligatory, but if the reference is wrong, it should not be correct
916 bool isObligatory = aFactory->isCase(theFeature, theFeature->data()->id(aSel));
918 aState = ModelAPI_StateInvalidArgument;
924 if (aState != ModelAPI_StateDone)
925 theFeature->data()->execState(aState);
928 bool Model_Update::isReason(std::shared_ptr<ModelAPI_Feature>& theFeature,
929 std::shared_ptr<ModelAPI_Object> theReason)
931 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
932 ::iterator aReasonsIt = myModified.find(theFeature);
933 if (aReasonsIt != myModified.end()) {
934 if (aReasonsIt->second.find(theFeature) != aReasonsIt->second.end())
935 return true; // any is reason if it contains itself
936 FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
937 if (!aReasFeat.get()) { // try to get feature of this result
938 ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
940 aReasFeat = theReason->document()->feature(aReasRes);
942 if (aReasonsIt->second.find(aReasFeat) != aReasonsIt->second.end())
945 // another try: postponed modification by not-persistences
946 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
947 ::iterator aNotPersist = myNotPersistentRefs.find(theFeature);
948 if (aNotPersist != myNotPersistentRefs.end()) {
949 FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
950 if (!aReasFeat.get()) { // try to get feature of this result
951 ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
953 aReasFeat = theReason->document()->feature(aReasRes);
955 if (aNotPersist->second.find(aReasFeat) != aNotPersist->second.end())
959 // this case only for not-previewed items update state, nothing is changed in args for it
963 void Model_Update::executeFeature(FeaturePtr theFeature)
966 std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
968 // execute in try-catch to avoid internal problems of the feature
969 ModelAPI_ExecState aState = ModelAPI_StateDone;
970 theFeature->data()->execState(ModelAPI_StateDone);
972 theFeature->execute();
973 if (theFeature->data()->execState() != ModelAPI_StateDone) {
974 aState = ModelAPI_StateExecFailed;
976 aState = ModelAPI_StateDone;
979 aState = ModelAPI_StateExecFailed;
980 Events_InfoMessage("Model_Update",
981 "Feature %1 has failed during the execution").arg(theFeature->getKind()).send();
983 // The macro feature has to be deleted in any case even its execution is failed
984 myWaitForFinish.insert(theFeature);
985 if (aState != ModelAPI_StateDone) {
986 theFeature->eraseResults(false);
988 theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
989 redisplayWithResults(theFeature, aState);
992 // it is called on GUI edit of feature start
994 void Model_Update::updateStability(void* theSender)
996 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
998 bool added = false; // object may be was crated
999 ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theSender);
1000 if (aSender && aSender->document()) {
1001 FeaturePtr aFeatureSender =
1002 std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
1003 if (aFeatureSender.get()) {
1004 Model_Objects* aDocObjects =
1005 std::dynamic_pointer_cast<Model_Document>(aSender->document())->objects();
1007 //aDocObjects->synchronizeBackRefs();
1008 // remove or add all concealment refs from this feature
1009 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
1010 aSender->data()->referencesToObjects(aRefs);
1011 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
1012 aRefIt = aRefs.begin();
1013 for(; aRefIt != aRefs.end(); aRefIt++) {
1014 if (!aFactory->isConcealed(aFeatureSender->getKind(), aRefIt->first))
1015 // take into account only concealed references
1016 // (do not remove the sketch constraint and the edge on constraint edit)
1018 std::list<ObjectPtr>& aRefFeaturesList = aRefIt->second;
1019 std::list<ObjectPtr>::iterator aReferenced = aRefFeaturesList.begin();
1020 for(; aReferenced != aRefFeaturesList.end(); aReferenced++) {
1021 // stability is only on results: feature to feature reference mean nested
1022 // features, that will remove nesting references
1023 if (aReferenced->get() && (*aReferenced)->data()->isValid() &&
1024 (*aReferenced)->groupName() != ModelAPI_Feature::group()) {
1025 std::shared_ptr<Model_Data> aData =
1026 std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
1027 if (aFeatureSender->isStable()) {
1028 aData->addBackReference(aFeatureSender, aRefIt->first);
1030 aData->removeBackReference(aFeatureSender, aRefIt->first);
1031 added = true; // remove of concealment may be caused creation of some result
1040 static Events_Loop* aLoop = Events_Loop::loop();
1041 static Events_ID kEventCreated = aLoop->eventByName(EVENT_OBJECT_CREATED);
1042 aLoop->flush(kEventCreated);
1048 void Model_Update::updateSelection(const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects)
1050 std::set<std::shared_ptr<ModelAPI_Object> >::iterator anObj = theObjects.begin();
1051 for(; anObj != theObjects.end(); anObj++) {
1052 std::list<AttributePtr> aRefs =
1053 (*anObj)->data()->attributes(ModelAPI_AttributeSelection::typeId());
1054 std::list<AttributePtr>::iterator aRefsIter = aRefs.begin();
1055 for (; aRefsIter != aRefs.end(); aRefsIter++) {
1056 std::shared_ptr<Model_AttributeSelection> aSel =
1057 std::dynamic_pointer_cast<Model_AttributeSelection>(*aRefsIter);
1058 aSel->updateInHistory();
1060 // update the selection list attributes if any
1061 aRefs = (*anObj)->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1062 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
1063 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
1064 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
1065 for(int a = aSel->size() - 1; a >= 0; a--) {
1066 std::shared_ptr<Model_AttributeSelection> aSelAttr =
1067 std::dynamic_pointer_cast<Model_AttributeSelection>(aSel->value(a));
1069 aSelAttr->updateInHistory();