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;
85 bool Model_Update::addModified(FeaturePtr theFeature, FeaturePtr theReason) {
87 if (!theFeature->data()->isValid())
88 return false; // delete an extrusion created on the sketch
91 bool isNotExecuted = theFeature->isPersistentResult() &&
92 !std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures();
94 redisplayWithResults(theFeature, ModelAPI_StateNothing, false); // redisplay even not executed
95 if (!theReason.get()) // no reason => no construction reason
97 if (myNotPersistentRefs.find(theFeature) == myNotPersistentRefs.end()) {
98 myNotPersistentRefs[theFeature].insert(theReason);
100 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
101 aNewSet.insert(theReason);
102 myNotPersistentRefs[theFeature] = aNewSet;
107 // update arguments for "apply button" state change
108 if ((!theFeature->isPreviewNeeded() && !myIsFinish) || myIsPreviewBlocked) {
110 myProcessOnFinish[theFeature].insert(theReason);
111 else if (myProcessOnFinish.find(theFeature) == myProcessOnFinish.end())
112 myProcessOnFinish[theFeature] = std::set<std::shared_ptr<ModelAPI_Feature> >();
114 std::cout<<"*** Add process on finish "<<theFeature->name()<<std::endl;
116 // keeps the currently updated features to avoid infinitive cycling here: where feature on
117 // "updateArguments" sends "updated" (in selection attribute) and goes here again
118 static std::set<FeaturePtr> aCurrentlyUpdated;
119 if (aCurrentlyUpdated.find(theFeature) == aCurrentlyUpdated.end()) {
120 aCurrentlyUpdated.insert(theFeature);
121 updateArguments(theFeature);
122 aCurrentlyUpdated.erase(theFeature);
124 // make it without conditions otherwise the apply button may have a bad state
125 theFeature->data()->execState(ModelAPI_StateDone);
126 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
127 // need to be validated to update the "Apply" state if not previewed
128 aFactory->validate(theFeature);
130 // to redisplay split's arguments presentation, even result is not computed
131 if (!theFeature->isPreviewNeeded()) {
132 static Events_Loop* aLoop = Events_Loop::loop();
133 static const Events_ID kRedisplayEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
134 ModelAPI_EventCreator::get()->sendUpdated(theFeature, kRedisplayEvent);
135 aLoop->flush(kRedisplayEvent);
138 if (!myIsPreviewBlocked)
141 if (myModified.find(theFeature) != myModified.end()) {
142 if (theReason.get()) {
144 //std::cout<<"*** Add already modified "
145 // <<theFeature->name()<<" reason "<<theReason->name()<<std::endl;
147 myModified[theFeature].insert(theReason);
151 // do not add the disabled, but possibly the sub-elements are not disabled
152 bool aIsDisabled = theFeature->isDisabled();
154 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
155 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated ||
156 theFeature->data()->execState() == ModelAPI_StateInvalidArgument || // issue 1519
157 theFeature->data()->execState() == ModelAPI_StateExecFailed) {
158 // do not forget that in this case all were the reasons
159 aNewSet.insert(theFeature);
162 aNewSet.insert(theReason);
164 myModified[theFeature] = aNewSet;
166 if (theReason.get()) {
167 //std::cout<<"*** Add modified "<<theFeature->name()
168 // <<" reason "<<theReason->name()<<std::endl;
170 //std::cout<<"*** Add modified "<<theFeature->name()<<std::endl;
173 } else { // will be updated during the finish of the operation, or when it becomes enabled
174 if (theFeature->data()->execState() == ModelAPI_StateDone ||
175 theFeature->data()->execState() == ModelAPI_StateExecFailed) // fix issue 1819
176 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
178 return true; // do not need iteration deeply if it is already marked as modified or so
180 //std::cout<<"*** Set modified state "<<theFeature->name()<<std::endl;
183 // clear processed and fill modified recursively
184 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
185 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
186 for(; aRefIter != aRefs.cend(); aRefIter++) {
187 if ((*aRefIter)->isArgument()) {
188 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
189 if (aReferenced.get()) {
190 addModified(aReferenced, theFeature);
194 // proccess also results
195 std::list<ResultPtr> allResults; // list of this feature and results
196 ModelAPI_Tools::allResults(theFeature, allResults);
197 std::list<ResultPtr>::iterator aRes = allResults.begin();
198 for(; aRes != allResults.end(); aRes++) {
199 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*aRes)->data()->refsToMe();
200 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
201 for(; aRefIter != aRefs.cend(); aRefIter++) {
202 if ((*aRefIter)->isArgument()) {
203 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
204 if (aReferenced.get()) {
205 addModified(aReferenced, theFeature);
211 // also add part feature that contains this feature to the modified
212 if (theFeature->document()->kind() != "PartSet") {
213 FeaturePtr aPart = ModelAPI_Tools::findPartFeature(
214 ModelAPI_Session::get()->moduleDocument(), theFeature->document());
216 addModified(aPart, theFeature);
221 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
223 static Events_Loop* aLoop = Events_Loop::loop();
224 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
225 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
226 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
227 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
228 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
229 static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
230 static const Events_ID kPreviewBlockedEvent = aLoop->eventByName(EVENT_PREVIEW_BLOCKED);
231 static const Events_ID kPreviewRequestedEvent = aLoop->eventByName(EVENT_PREVIEW_REQUESTED);
232 static const Events_ID kReorderEvent = aLoop->eventByName(EVENT_ORDER_UPDATED);
233 static const Events_ID kRedisplayEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
234 static const Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
237 std::cout<<"****** Event "<<theMessage->eventID().eventText()<<std::endl;
239 if (theMessage->eventID() == kStabilityEvent) {
240 updateStability(theMessage->sender());
243 if (theMessage->eventID() == kPreviewBlockedEvent) {
244 myIsPreviewBlocked = true;
247 if (theMessage->eventID() == kPreviewRequestedEvent) {
248 if (myIsPreviewBlocked) {
249 myIsPreviewBlocked = false;
251 myIsPreviewBlocked = true;
255 if (theMessage->eventID() == kUpdatedSel) {
256 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
257 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
258 updateSelection(aMsg->objects());
260 // creation is added to "update" to avoid recomputation twice:
261 // on create and immediately after on update
262 if (theMessage->eventID() == kCreatedEvent) {
263 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
264 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
265 const std::set<ObjectPtr>& anObjs = aMsg->objects();
266 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
267 for(; anObjIter != anObjs.cend(); anObjIter++) {
268 if (std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures()) {
269 if ((*anObjIter)->groupName() == ModelAPI_Feature::group()) {
270 // results creation means enabling, not update
271 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kUpdatedEvent);
273 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kRedisplayEvent);
279 if (theMessage->eventID() == kUpdatedEvent) {
280 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
281 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
282 const std::set<ObjectPtr>& anObjs = aMsg->objects();
283 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
284 bool aSomeModified = false; // check that features not changed: only redisplay is needed
285 for(; anObjIter != anObjs.cend(); anObjIter++) {
286 if (!(*anObjIter)->data()->isValid())
289 std::cout<<">>> in event updated "<<(*anObjIter)->groupName()<<
290 " "<<(*anObjIter)->data()->name()<<std::endl;
292 if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
293 myIsParamUpdated = true;
295 // on undo/redo, abort do not update persisten features
296 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
297 if (anUpdated.get()) {
298 if (addModified(anUpdated, FeaturePtr()))
299 aSomeModified = true;
301 // process the updated result as update of features that refers to this result
302 const std::set<std::shared_ptr<ModelAPI_Attribute> >&
303 aRefs = (*anObjIter)->data()->refsToMe();
304 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
306 ResultPtr aReasonResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIter);
307 if (aReasonResult.get())
308 aReason = (*anObjIter)->document()->feature(aReasonResult);
309 for(; aRefIter != aRefs.cend(); aRefIter++) {
310 if (!(*aRefIter)->owner()->data()->isValid())
312 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
313 if (anUpdated.get()) {
314 if (addModified(anUpdated, aReason))
315 aSomeModified = true;
320 // this event is for solver update, not here, do not react immediately
324 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
325 theMessage->eventID() == kOpStartEvent) {
326 myIsPreviewBlocked = false;
328 if (theMessage->eventID() == kOpFinishEvent) {
330 // add features that wait for finish as modified
331 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >::
332 iterator aFeature = myProcessOnFinish.begin();
333 for(; aFeature != myProcessOnFinish.end(); aFeature++) {
334 if (aFeature->first->data()->isValid()) {// there may be already removed while wait
335 if (aFeature->second.empty()) {
336 addModified(aFeature->first, FeaturePtr());
339 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aReasons;
340 for(aReasons = aFeature->second.begin(); aReasons != aFeature->second.end(); aReasons++) {
341 addModified(aFeature->first, *aReasons);
347 // processed features must be only on finish, so clear anyway (to avoid reimport on load)
348 myProcessOnFinish.clear();
350 // #2156: current must be sketch, left after the macro execution
351 DocumentPtr anActiveDoc = ModelAPI_Session::get()->activeDocument();
353 if (anActiveDoc.get())
354 aCurrent = anActiveDoc->currentFeature(false);
356 if (!(theMessage->eventID() == kOpStartEvent)) {
357 processFeatures(false);
360 if (anActiveDoc.get() && aCurrent.get() && aCurrent->data()->isValid()) {
361 if (anActiveDoc->currentFeature(false) != aCurrent)
362 anActiveDoc->setCurrentFeature(aCurrent, false); // #2156 make the current feature back
366 // remove all macros before clearing all created
367 std::set<FeaturePtr>::iterator anUpdatedIter = myWaitForFinish.begin();
368 while(anUpdatedIter != myWaitForFinish.end()) {
369 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
370 if (aFeature.get()) {
371 // remove macro on finish
372 if (aFeature->isMacro()) {
373 aFeature->document()->removeFeature(aFeature);
374 myWaitForFinish.erase(aFeature);
376 // to avoid the map update problems on "remove"
377 if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
378 anUpdatedIter = myWaitForFinish.begin();
386 // the redisplay signal should be flushed in order
387 // to erase the feature presentation in the viewer
388 // if should be done after removeFeature() of document,
389 // by this reason, upper processFeatures() do not perform this flush
390 Events_Loop::loop()->flush(kRedisplayEvent);
392 // in the end of transaction everything is updated, so clear the old objects
393 myIsParamUpdated = false;
394 myWaitForFinish.clear();
395 } else if (theMessage->eventID() == kReorderEvent) {
396 std::shared_ptr<ModelAPI_OrderUpdatedMessage> aMsg =
397 std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
398 if (aMsg->reordered().get())
399 addModified(aMsg->reordered(), aMsg->reordered()); // to update all attributes
403 void Model_Update::processFeatures(const bool theFlushRedisplay)
405 // perform update of everything if it is not performed right now or any preview is blocked
406 if (!myIsProcessed && !myIsPreviewBlocked) {
407 myIsProcessed = true;
409 std::cout<<"****** Start processing"<<std::endl;
412 while(!myModified.empty()) {
413 processFeature(myModified.begin()->first);
415 myIsProcessed = false;
417 // to update the object browser if something is updated/created during executions
418 static Events_Loop* aLoop = Events_Loop::loop();
419 static const Events_ID kCreatedEvent= aLoop->eventByName(EVENT_OBJECT_CREATED);
420 aLoop->flush(kCreatedEvent);
421 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
422 aLoop->flush(kUpdatedEvent);
424 // flush to update display
425 if (theFlushRedisplay) {
426 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
427 aLoop->flush(EVENT_DISP);
430 std::cout<<"****** End processing"<<std::endl;
436 // collects all the feautres this feature depends on: reasons
437 static void allReasons(FeaturePtr theFeature, std::set<FeaturePtr>& theReasons) {
438 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aDeps;
439 theFeature->data()->referencesToObjects(aDeps);
440 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > >::iterator
441 anAttrsIter = aDeps.begin();
442 for(; anAttrsIter != aDeps.end(); anAttrsIter++) {
443 if (theFeature->attribute(anAttrsIter->first)->isArgument()) {
444 std::list<std::shared_ptr<ModelAPI_Object> >::iterator aDepIter = anAttrsIter->second.begin();
445 for(; aDepIter != anAttrsIter->second.end(); aDepIter++) {
446 FeaturePtr aDepFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(*aDepIter);
447 if (!aDepFeat.get()) { // so, it depends on the result and process the feature owner of it
448 ResultPtr aDepRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aDepIter);
450 aDepFeat = (*aDepIter)->document()->feature(aDepRes);
453 if (aDepFeat.get() && aDepFeat->data()->isValid()) {
454 theReasons.insert(aDepFeat);
459 if (theFeature->getKind() == "Part") {
460 // part is not depended on its subs directly, but subs must be iterated anyway
461 CompositeFeaturePtr aPart = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
462 int aNum = aPart->numberOfSubs();
463 for(int a = 0; a < aNum; a++) {
464 FeaturePtr aSub = aPart->subFeature(a);
465 if (aSub.get() && aSub->data()->isValid()) {
466 theReasons.insert(aSub);
472 bool Model_Update::processFeature(FeaturePtr theFeature)
474 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
476 if (!theFeature->data()->isValid()) { // deleted feature, just remove from all containers
477 if (myModified.find(theFeature) != myModified.end())
478 myModified.erase(theFeature);
482 if (theFeature->isPersistentResult()) {
483 if (!std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures())
487 if (myProcessed.find(theFeature) == myProcessed.end()) {
488 myProcessed[theFeature] = 0;
490 int aCount = myProcessed[theFeature];
492 // too many repetition of processing (in VS it may crash on 330 with stack overflow)
493 Events_InfoMessage("Model_Update",
494 "Feature '%1' is updated in infinitive loop").arg(theFeature->data()->name()).send();
499 myProcessed[theFeature] = aCount + 1;
502 // check this feature is not yet checked or processed
503 bool aIsModified = myModified.find(theFeature) != myModified.end();
504 if (!aIsModified && myIsFinish) { // get info about the modification for features without preview
505 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
507 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
508 // contains itself, so, we don't know which was the reason and the reason is any
509 aNewSet.insert(theFeature);
510 myModified[theFeature] = aNewSet;
515 std::cout<<"* Process feature "<<theFeature->name()<<std::endl;
518 // update the sketch plane before the sketch sub-elements are recomputed
519 // (otherwise sketch will update plane, modify subs, after executed, but with old subs edges)
520 if (aIsModified && theFeature->getKind() == "Sketch") {
522 std::cout<<"****** Update sketch args "<<theFeature->name()<<std::endl;
524 AttributeSelectionPtr anExtSel = theFeature->selection("External");
525 if (anExtSel.get()) {
526 ResultPtr aContext = anExtSel->context();
527 if (aContext.get() && aContext->document().get()) {
528 FeaturePtr anExtBase = aContext->document()->feature(aContext);
529 if (anExtBase.get()) {
530 processFeature(anExtBase);
534 std::shared_ptr<GeomAPI_Shape> aShapeBefore = anExtSel->value();
535 if (!aShapeBefore.get() && anExtSel->context()) aShapeBefore = anExtSel->context()->shape();
536 updateArguments(theFeature);
537 std::shared_ptr<GeomAPI_Shape> aShapeAfter = anExtSel->value();
538 if (!aShapeAfter.get() && anExtSel->context()) aShapeAfter = anExtSel->context()->shape();
539 // if selected plane is changed, try to re-take external references of all subs of the sketch
540 if (aShapeBefore.get() && !aShapeBefore->isEqual(aShapeAfter)) {
541 std::set<FeaturePtr> aWholeR;
542 allReasons(theFeature, aWholeR);
543 std::set<FeaturePtr>::iterator aRIter = aWholeR.begin();
544 for(; aRIter != aWholeR.end(); aRIter++) {
545 if ((*aRIter)->data()->selection("External"))
546 (*aRIter)->attributeChanged("External");
551 if (!aIsModified) { // no modification is needed
555 // evaluate parameter before the sub-elements update:
556 // it updates dependencies on evaluation (#1085)
557 if (theFeature->getKind() == "Parameter") {
558 theFeature->execute();
561 bool isReferencedInvalid = false;
562 // check all features this feature depended on (recursive call of updateFeature)
563 std::set<FeaturePtr>& aReasons = myModified[theFeature];
564 bool allSubsUsed = aReasons.find(theFeature) != aReasons.end();
566 // add all subs in aReasons and temporary remove "theFeature" to avoid processing itself
567 allReasons(theFeature, aReasons);
568 aReasons.erase(theFeature);
570 // take reasons one by one (they may be added during the feature process
571 // (circle by the radius of sketch)
572 std::set<FeaturePtr> aProcessedReasons;
573 while(!aReasons.empty()) {
574 FeaturePtr aReason = *(aReasons.begin());
576 //cout<<theFeature->name()<<" process next reason "<<aReason->name()<<endl;
578 if (aReason != theFeature && (aReason)->data()->isValid()) {
579 if (processFeature(aReason))
581 // check validity of aReason once again because it may be removed by dependent feature
582 // (e.g. by SketchPlugin_IntersectionPoint)
583 if (!aReason->data()->isValid() ||
584 aReason->data()->execState() == ModelAPI_StateInvalidArgument)
585 isReferencedInvalid = true;
587 // searching for the next not used reason
588 aProcessedReasons.insert(aReason);
589 aReasons.erase(aReason);
591 // restore the modified reasons: they will be used in the update of arguments
592 if (allSubsUsed) { // restore theFeature in this set
593 aProcessedReasons.insert(theFeature);
595 myModified[theFeature] = aProcessedReasons;
597 // do not execute the composite that contains the current
598 bool isPostponedMain = false;
599 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
600 if (theFeature->getKind() == "ExtrusionSketch" && aCompos.get()) {
601 CompositeFeaturePtr aCurrentOwner =
602 ModelAPI_Tools::compositeOwner(theFeature->document()->currentFeature(false));
603 isPostponedMain = aCurrentOwner.get() && aCompos->isSub(aCurrentOwner);
604 } else if (theFeature->getKind() == "Sketch" &&
605 std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures()) {
606 // send event that sketch is prepared to be recomputed
607 static Events_ID anID = Events_Loop::eventByName("SketchPrepared");
608 std::shared_ptr<Events_Message> aMsg(new Events_Message(anID, this));
609 Events_Loop* aLoop = Events_Loop::loop();
610 // in case it is finish operation, flush for the sketch other events (#2450)
611 aLoop->flush(aLoop->eventByName(EVENT_OBJECT_UPDATED));
613 // check that sub-elements of sketch are updated => sketch must be re-processed
614 std::set<FeaturePtr> aWholeR;
615 allReasons(theFeature, aWholeR);
616 std::set<FeaturePtr>::iterator aRIter = aWholeR.begin();
617 for(; aRIter != aWholeR.end(); aRIter++) {
618 if (myModified.find(*aRIter) != myModified.end()) {
619 processFeature(theFeature);
626 std::cout<<"Update args "<<theFeature->name()<<std::endl;
628 // TestImport.py : after arguments are updated, theFeature may be removed
629 if (!theFeature->data()->isValid())
631 // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
632 updateArguments(theFeature);
634 // add this feature to the processed right now to be able remove it from this list on
635 // update signal during this feature execution
636 myModified.erase(theFeature);
637 if (myNotPersistentRefs.find(theFeature) != myNotPersistentRefs.end())
638 myNotPersistentRefs.erase(theFeature);
639 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
640 theFeature->data()->execState(ModelAPI_StateDone);
642 // this checking must be after the composite feature sub-elements processing:
643 // composite feature status may depend on it's subelements
644 if ((theFeature->data()->execState() == ModelAPI_StateInvalidArgument || isReferencedInvalid) &&
645 theFeature->getKind() != "Part") {
646 // don't disable Part because it will make disabled all the features
647 // (performance and problems with the current feature)
649 std::cout<<"Invalid args "<<theFeature->name()<<std::endl;
651 theFeature->eraseResults(false);
652 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
653 return true; // so, feature is modified (results are erased)
656 // execute feature if it must be updated
657 ModelAPI_ExecState aState = theFeature->data()->execState();
658 if (aFactory->validate(theFeature)) {
659 if (!isPostponedMain) {
660 executeFeature(theFeature);
664 std::cout<<"Feature is not valid, erase results "<<theFeature->name()<<std::endl;
666 theFeature->eraseResults(false);
667 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
672 void Model_Update::redisplayWithResults(
673 FeaturePtr theFeature, const ModelAPI_ExecState theState, bool theUpdateState)
675 // make updated and redisplay all results
676 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
678 std::list<ResultPtr> allResults;
679 ModelAPI_Tools::allResults(theFeature, allResults);
680 std::list<ResultPtr>::iterator aRIter = allResults.begin();
681 for (; aRIter != allResults.cend(); aRIter++) {
682 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
683 if (!aRes->isDisabled()) {
684 // update state only for enabled results
685 // (Placement Result Part may make the original Part Result as invalid)
687 aRes->data()->execState(theState);
689 if (theFeature->data()->updateID() > aRes->data()->updateID()) {
690 aRes->data()->setUpdateID(theFeature->data()->updateID());
692 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
694 // to redisplay "presentable" feature (for ex. distance constraint)
695 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
697 theFeature->data()->execState(theState);
700 /// Updates the state by the referenced object: if something bad with it, set state for this one
701 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
704 ModelAPI_ExecState aRefState = theTarget->data()->execState();
705 if (aRefState == ModelAPI_StateMustBeUpdated) {
706 if (theCurrent == ModelAPI_StateDone)
707 return ModelAPI_StateMustBeUpdated;
708 } else if (aRefState != ModelAPI_StateDone) {
709 return ModelAPI_StateInvalidArgument;
715 void Model_Update::updateArguments(FeaturePtr theFeature) {
716 // perform this method also for disabled features: to make "not done" state for
717 // features referenced to the active and modified features
718 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
720 ModelAPI_ExecState aState = theFeature->data()->execState();
721 if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
722 aState = ModelAPI_StateMustBeUpdated;
724 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
725 aState = ModelAPI_StateMustBeUpdated;
726 // check the parameters state
729 std::list<AttributePtr> anAttrinbutes =
730 theFeature->data()->attributes(ModelAPI_AttributeInteger::typeId());
731 std::list<AttributePtr>::iterator anIter = anAttrinbutes.begin();
732 for(; anIter != anAttrinbutes.end(); anIter++) {
733 AttributeIntegerPtr anAttribute =
734 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(*anIter);
735 if (anAttribute.get() && !anAttribute->text().empty()) {
736 if (myIsParamUpdated) {
737 ModelAPI_AttributeEvalMessage::send(anAttribute, this);
739 if (anAttribute->expressionInvalid()) {
740 aState = ModelAPI_StateInvalidArgument;
747 std::list<AttributePtr> aDoubles =
748 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
749 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
750 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
751 AttributeDoublePtr aDouble =
752 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
753 if (aDouble.get() && !aDouble->text().empty()) {
754 if (myIsParamUpdated) {
755 ModelAPI_AttributeEvalMessage::send(aDouble, this);
757 if (aDouble->expressionInvalid()) {
758 aState = ModelAPI_StateInvalidArgument;
765 std::list<AttributePtr> anAttributes =
766 theFeature->data()->attributes(GeomDataAPI_Point::typeId());
767 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
768 for(; anIter != anAttributes.end(); anIter++) {
769 AttributePointPtr aPointAttribute =
770 std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
771 if (aPointAttribute.get() && (!aPointAttribute->textX().empty() ||
772 !aPointAttribute->textY().empty() || !aPointAttribute->textZ().empty())) {
773 if (myIsParamUpdated) {
774 ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
776 if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
777 (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
778 (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
779 aState = ModelAPI_StateInvalidArgument;
785 std::list<AttributePtr> anAttributes =
786 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
787 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
788 for(; anIter != anAttributes.end(); anIter++) {
789 AttributePoint2DPtr aPoint2DAttribute =
790 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
791 if (aPoint2DAttribute.get()) {
792 if (myIsParamUpdated && (!aPoint2DAttribute->textX().empty() ||
793 !aPoint2DAttribute->textY().empty())) {
794 ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
796 if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
797 (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
798 aState = ModelAPI_StateInvalidArgument;
802 // update the selection attributes if any
803 std::list<AttributePtr> aRefs =
804 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
805 std::list<AttributePtr>::iterator aRefsIter = aRefs.begin();
806 for (; aRefsIter != aRefs.end(); aRefsIter++) {
807 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
808 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
809 ObjectPtr aContext = aSel->context();
810 // update argument only if the referenced object is ready to use
811 if (aContext.get() && !aContext->isDisabled()) {
812 if (isReason(theFeature, aContext)) {
813 if (!aSel->update()) { // this must be done on execution since it may be long operation
814 bool isObligatory = !aFactory->isNotObligatory(
815 theFeature->getKind(), theFeature->data()->id(aSel)) &&
816 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
818 aState = ModelAPI_StateInvalidArgument;
821 } else if (aContext.get()) {
822 // here it may be not obligatory, but if the reference is wrong, it should not be correct
823 bool isObligatory = aFactory->isCase(theFeature, theFeature->data()->id(aSel));
825 aState = ModelAPI_StateInvalidArgument;
828 // update the selection list attributes if any
829 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
830 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
831 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
832 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
833 for(int a = aSel->size() - 1; a >= 0; a--) {
834 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
835 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
837 ObjectPtr aContext = aSelAttr->context();
838 // update argument only if the referenced object is ready to use
839 if (aContext.get() && !aContext->isDisabled() && !aSelAttr->isInvalid()) {
840 if (isReason(theFeature, aContext)) {
841 if (!aSelAttr->update()) {
842 bool isObligatory = !aFactory->isNotObligatory(
843 theFeature->getKind(), theFeature->data()->id(aSel)) &&
844 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
846 aState = ModelAPI_StateInvalidArgument;
849 } else if (aContext.get() || aSelAttr->isInvalid()) {
850 // here it may be not obligatory, but if the reference is wrong, it should not be correct
851 bool isObligatory = aFactory->isCase(theFeature, theFeature->data()->id(aSel));
853 aState = ModelAPI_StateInvalidArgument;
859 if (aState != ModelAPI_StateDone)
860 theFeature->data()->execState(aState);
863 bool Model_Update::isReason(std::shared_ptr<ModelAPI_Feature>& theFeature,
864 std::shared_ptr<ModelAPI_Object> theReason)
866 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
867 ::iterator aReasonsIt = myModified.find(theFeature);
868 if (aReasonsIt != myModified.end()) {
869 if (aReasonsIt->second.find(theFeature) != aReasonsIt->second.end())
870 return true; // any is reason if it contains itself
871 FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
872 if (!aReasFeat.get()) { // try to get feature of this result
873 ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
875 aReasFeat = theReason->document()->feature(aReasRes);
877 if (aReasonsIt->second.find(aReasFeat) != aReasonsIt->second.end())
880 // another try: postponed modification by not-persistences
881 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
882 ::iterator aNotPersist = myNotPersistentRefs.find(theFeature);
883 if (aNotPersist != myNotPersistentRefs.end()) {
884 FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
885 if (!aReasFeat.get()) { // try to get feature of this result
886 ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
888 aReasFeat = theReason->document()->feature(aReasRes);
890 if (aNotPersist->second.find(aReasFeat) != aNotPersist->second.end())
894 // this case only for not-previewed items update state, nothing is changed in args for it
898 void Model_Update::executeFeature(FeaturePtr theFeature)
901 std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
903 // execute in try-catch to avoid internal problems of the feature
904 ModelAPI_ExecState aState = ModelAPI_StateDone;
905 theFeature->data()->execState(ModelAPI_StateDone);
907 theFeature->execute();
908 if (theFeature->data()->execState() != ModelAPI_StateDone) {
909 aState = ModelAPI_StateExecFailed;
911 aState = ModelAPI_StateDone;
914 aState = ModelAPI_StateExecFailed;
915 Events_InfoMessage("Model_Update",
916 "Feature %1 has failed during the execution").arg(theFeature->getKind()).send();
918 // The macro feature has to be deleted in any case even its execution is failed
919 myWaitForFinish.insert(theFeature);
920 if (aState != ModelAPI_StateDone) {
921 theFeature->eraseResults(false);
923 theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
924 redisplayWithResults(theFeature, aState);
927 void Model_Update::updateStability(void* theSender)
929 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
931 bool added = false; // object may be was crated
932 ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theSender);
933 if (aSender && aSender->document()) {
934 FeaturePtr aFeatureSender =
935 std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
936 if (aFeatureSender.get()) {
937 Model_Objects* aDocObjects =
938 std::dynamic_pointer_cast<Model_Document>(aSender->document())->objects();
940 //aDocObjects->synchronizeBackRefs();
941 // remove or add all concealment refs from this feature
942 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
943 aSender->data()->referencesToObjects(aRefs);
944 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
945 aRefIt = aRefs.begin();
946 for(; aRefIt != aRefs.end(); aRefIt++) {
947 if (!aFactory->isConcealed(aFeatureSender->getKind(), aRefIt->first))
948 // take into account only concealed references
949 // (do not remove the sketch constraint and the edge on constraint edit)
951 std::list<ObjectPtr>& aRefFeaturesList = aRefIt->second;
952 std::list<ObjectPtr>::iterator aReferenced = aRefFeaturesList.begin();
953 for(; aReferenced != aRefFeaturesList.end(); aReferenced++) {
954 // stability is only on results: feature to feature reference mean nested
955 // features, that will remove nesting references
956 if (aReferenced->get() && (*aReferenced)->data()->isValid() &&
957 (*aReferenced)->groupName() != ModelAPI_Feature::group()) {
958 std::shared_ptr<Model_Data> aData =
959 std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
960 if (aFeatureSender->isStable()) {
961 aData->addBackReference(aFeatureSender, aRefIt->first);
963 aData->removeBackReference(aFeatureSender, aRefIt->first);
964 added = true; // remove of concealment may be caused creation of some result
973 static Events_Loop* aLoop = Events_Loop::loop();
974 static Events_ID kEventCreated = aLoop->eventByName(EVENT_OBJECT_CREATED);
975 aLoop->flush(kEventCreated);
980 void Model_Update::updateSelection(const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects)
982 std::set<std::shared_ptr<ModelAPI_Object> >::iterator anObj = theObjects.begin();
983 for(; anObj != theObjects.end(); anObj++) {
984 std::list<AttributePtr> aRefs =
985 (*anObj)->data()->attributes(ModelAPI_AttributeSelection::typeId());
986 std::list<AttributePtr>::iterator aRefsIter = aRefs.begin();
987 for (; aRefsIter != aRefs.end(); aRefsIter++) {
988 std::shared_ptr<Model_AttributeSelection> aSel =
989 std::dynamic_pointer_cast<Model_AttributeSelection>(*aRefsIter);
990 aSel->updateInHistory();
992 // update the selection list attributes if any
993 aRefs = (*anObj)->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
994 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
995 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
996 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
997 for(int a = aSel->size() - 1; a >= 0; a--) {
998 std::shared_ptr<Model_AttributeSelection> aSelAttr =
999 std::dynamic_pointer_cast<Model_AttributeSelection>(aSel->value(a));
1001 aSelAttr->updateInHistory();