1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Update.cxx
4 // Created: 25 Jun 2014
5 // Author: Mikhail PONIKAROV
7 #include <Model_Update.h>
8 #include <Model_Document.h>
9 #include <Model_Data.h>
10 #include <Model_Objects.h>
11 #include <ModelAPI_Feature.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_Document.h>
14 #include <ModelAPI_Events.h>
15 #include <ModelAPI_AttributeReference.h>
16 #include <ModelAPI_AttributeRefList.h>
17 #include <ModelAPI_AttributeRefAttr.h>
18 #include <ModelAPI_AttributeSelection.h>
19 #include <ModelAPI_AttributeSelectionList.h>
20 #include <ModelAPI_Result.h>
21 #include <ModelAPI_ResultPart.h>
22 #include <ModelAPI_Validator.h>
23 #include <ModelAPI_CompositeFeature.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Tools.h>
26 #include <GeomDataAPI_Point.h>
27 #include <GeomDataAPI_Point2D.h>
28 #include <Events_Loop.h>
29 #include <Events_LongOp.h>
30 #include <Events_Error.h>
31 #include <Config_PropManager.h>
35 Model_Update MY_UPDATER_INSTANCE; /// the only one instance initialized on load of the library
38 Model_Update::Model_Update()
40 Events_Loop* aLoop = Events_Loop::loop();
41 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
42 aLoop->registerListener(this, kChangedEvent);
43 static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
44 aLoop->registerListener(this, kCreatedEvent);
45 static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
46 aLoop->registerListener(this, kUpdatedEvent);
47 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
48 aLoop->registerListener(this, kOpFinishEvent);
49 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
50 aLoop->registerListener(this, kOpAbortEvent);
51 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
52 aLoop->registerListener(this, kOpStartEvent);
53 static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
54 aLoop->registerListener(this, kStabilityEvent);
55 static const Events_ID kPreviewBlockedEvent = aLoop->eventByName(EVENT_PREVIEW_BLOCKED);
56 aLoop->registerListener(this, kPreviewBlockedEvent);
57 static const Events_ID kPreviewRequestedEvent = aLoop->eventByName(EVENT_PREVIEW_REQUESTED);
58 aLoop->registerListener(this, kPreviewRequestedEvent);
60 // Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
61 myIsParamUpdated = false;
63 myIsProcessed = false;
64 myIsPreviewBlocked = false;
67 bool Model_Update::addModified(FeaturePtr theFeature, FeaturePtr theReason) {
69 if (!theFeature->data()->isValid())
70 return false; // delete an extrusion created on the sketch
72 if (theFeature->isPersistentResult()) {
73 if (!std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures())
77 // update arguments for "apply button" state change
78 if ((!theFeature->isPreviewNeeded() && !myIsFinish) || myIsPreviewBlocked) {
79 myProcessOnFinish.insert(theFeature);
81 std::cout<<"*** Add process on finish "<<theFeature->name()<<std::endl;
83 // keeps the currently updated features to avoid infinitive cycling here: where feature on
84 // "updateArguments" sends "updated" (in selection attribute) and goes here again
85 static std::set<FeaturePtr> aCurrentlyUpdated;
86 if (aCurrentlyUpdated.find(theFeature) == aCurrentlyUpdated.end()) {
87 aCurrentlyUpdated.insert(theFeature);
88 updateArguments(theFeature);
89 aCurrentlyUpdated.erase(theFeature);
91 // make it without conditions otherwise the apply button may have a bad state
92 theFeature->data()->execState(ModelAPI_StateDone);
93 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
94 aFactory->validate(theFeature); // need to be validated to update the "Apply" state if not previewed
96 if (!myIsPreviewBlocked)
99 if (myModified.find(theFeature) != myModified.end()) {
100 if (theReason.get()) {
102 std::cout<<"*** Add already modified "<<theFeature->name()<<std::endl;
104 myModified[theFeature].insert(theReason);
108 // do not add the disabled, but possibly the sub-elements are not disabled
109 bool aIsDisabled = theFeature->isDisabled();
111 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
112 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
113 // do not forget that in this case all were the reasons
114 aNewSet.insert(theFeature);
117 aNewSet.insert(theReason);
119 myModified[theFeature] = aNewSet;
121 std::cout<<"*** Add modified "<<theFeature->name()<<std::endl;
123 } else { // will be updated during the finish of the operation, or when it becomes enabled
124 if (theFeature->data()->execState() == ModelAPI_StateDone)
125 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
127 return true; // do not need iteration deeply if it is already marked as modified or so
129 std::cout<<"*** Set modified state "<<theFeature->name()<<std::endl;
132 // clear processed and fill modified recursively
133 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = theFeature->data()->refsToMe();
134 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
135 for(; aRefIter != aRefs.cend(); aRefIter++) {
136 if ((*aRefIter)->isArgument()) {
137 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
138 if (aReferenced.get()) {
139 addModified(aReferenced, theFeature);
143 // proccess also results
144 std::list<ResultPtr> allResults; // list of this feature and results
145 ModelAPI_Tools::allResults(theFeature, allResults);
146 std::list<ResultPtr>::iterator aRes = allResults.begin();
147 for(; aRes != allResults.end(); aRes++) {
148 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*aRes)->data()->refsToMe();
149 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
150 for(; aRefIter != aRefs.cend(); aRefIter++) {
151 if ((*aRefIter)->isArgument()) {
152 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
153 if (aReferenced.get()) {
154 addModified(aReferenced, theFeature);
160 // also add part feature that contains this feature to the modified
161 if (theFeature->document()->kind() != "PartSet") {
162 FeaturePtr aPart = ModelAPI_Tools::findPartFeature(
163 ModelAPI_Session::get()->moduleDocument(), theFeature->document());
165 addModified(aPart, theFeature);
170 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
172 static Events_Loop* aLoop = Events_Loop::loop();
173 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
174 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
175 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
176 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
177 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
178 static const Events_ID kStabilityEvent = aLoop->eventByName(EVENT_STABILITY_CHANGED);
179 static const Events_ID kPreviewBlockedEvent = aLoop->eventByName(EVENT_PREVIEW_BLOCKED);
180 static const Events_ID kPreviewRequestedEvent = aLoop->eventByName(EVENT_PREVIEW_REQUESTED);
183 std::cout<<"****** Event "<<theMessage->eventID().eventText()<<std::endl;
185 if (theMessage->eventID() == kStabilityEvent) {
186 updateStability(theMessage->sender());
189 if (theMessage->eventID() == kPreviewBlockedEvent) {
190 myIsPreviewBlocked = true;
193 if (theMessage->eventID() == kPreviewRequestedEvent) {
194 if (myIsPreviewBlocked) {
195 myIsPreviewBlocked = false;
197 myIsPreviewBlocked = true;
201 // creation is added to "update" to avoid recomputation twice: on create and immediately after on update
202 if (theMessage->eventID() == kCreatedEvent) {
203 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
204 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
205 const std::set<ObjectPtr>& anObjs = aMsg->objects();
206 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
207 for(; anObjIter != anObjs.cend(); anObjIter++) {
208 if (std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures())
209 ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, kUpdatedEvent);
213 if (theMessage->eventID() == kUpdatedEvent) {
214 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
215 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
216 const std::set<ObjectPtr>& anObjs = aMsg->objects();
217 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
218 bool aSomeModified = false; // check that features not changed: only redisplay is needed
219 for(; anObjIter != anObjs.cend(); anObjIter++) {
220 if (!(*anObjIter)->data()->isValid())
223 std::cout<<">>> in event updated "<<(*anObjIter)->data()->name()<<std::endl;
225 if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
226 myIsParamUpdated = true;
228 // on undo/redo, abort do not update persisten features
229 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
230 if (anUpdated.get()) {
231 if (addModified(anUpdated, FeaturePtr()))
232 aSomeModified = true;
233 } else { // process the updated result as update of features that refers to this result
234 const std::set<std::shared_ptr<ModelAPI_Attribute> >& aRefs = (*anObjIter)->data()->refsToMe();
235 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
236 for(; aRefIter != aRefs.cend(); aRefIter++) {
237 if (!(*aRefIter)->owner()->data()->isValid())
239 FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
240 if (anUpdated.get()) {
241 if (addModified(anUpdated, FeaturePtr()))
242 aSomeModified = true;
247 // this event is for solver update, not here, do not react immediately
251 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
252 theMessage->eventID() == kOpStartEvent) {
253 myIsPreviewBlocked = false;
255 if (theMessage->eventID() == kOpFinishEvent) {
257 // add features that wait for finish as modified
258 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aFeature = myProcessOnFinish.begin();
259 for(; aFeature != myProcessOnFinish.end(); aFeature++)
260 if ((*aFeature)->data()->isValid()) // there may be already removed wait for features
261 addModified(*aFeature, FeaturePtr());
264 myProcessOnFinish.clear(); // processed features must be only on finish, so clear anyway (to avoid reimport on load)
266 if (!(theMessage->eventID() == kOpStartEvent)) {
267 processFeatures(false);
269 // remove all macros before clearing all created
270 std::set<FeaturePtr>::iterator anUpdatedIter = myWaitForFinish.begin();
271 while(anUpdatedIter != myWaitForFinish.end()) {
272 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
273 if (aFeature.get()) {
274 // remove macro on finish
275 if (aFeature->isMacro()) {
276 aFeature->document()->removeFeature(aFeature);
277 myWaitForFinish.erase(aFeature);
279 // to avoid the map update problems on "remove"
280 if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
281 anUpdatedIter = myWaitForFinish.begin();
289 // the redisplay signal should be flushed in order to erase the feature presentation in the viewer
290 // if should be done after removeFeature() of document,
291 // by this reason, upper processFeatures() do not perform this flush
292 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
294 // in the end of transaction everything is updated, so clear the old objects
295 myIsParamUpdated = false;
296 myWaitForFinish.clear();
300 void Model_Update::processFeatures(const bool theFlushRedisplay)
302 // perform update of everything if it is not performed right now or any preview is blocked
303 if (!myIsProcessed && !myIsPreviewBlocked) {
304 myIsProcessed = true;
306 std::cout<<"****** Start processing"<<std::endl;
309 while(!myModified.empty()) {
310 processFeature(myModified.begin()->first);
312 myIsProcessed = false;
314 // flush updates just before "myModification" increment: to distinguish
315 // updates by "execute" produced by this updater and other updates, coming outside,
316 // which are really important for "processEvent" of this updater
317 static Events_Loop* aLoop = Events_Loop::loop();
318 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
319 aLoop->flush(kUpdatedEvent);
321 // flush to update display
322 if (theFlushRedisplay) {
323 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
324 aLoop->flush(EVENT_DISP);
327 std::cout<<"****** End processing"<<std::endl;
333 bool Model_Update::processFeature(FeaturePtr theFeature)
335 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
337 if (!theFeature->data()->isValid()) { // deleted feature, just remove from all containers
338 if (myModified.find(theFeature) != myModified.end())
339 myModified.erase(theFeature);
343 if (theFeature->isPersistentResult()) {
344 if (!std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures())
348 if (myProcessed.find(theFeature) == myProcessed.end()) {
349 myProcessed[theFeature] = 0;
351 int aCount = myProcessed[theFeature];
352 if (aCount > 100) { // too many repetition of processing (in VS it may crash on 330 with stack overflow)
354 "Feature '" + theFeature->data()->name() + "' is updated in infinitive loop");
357 myProcessed[theFeature] = aCount + 1;
360 // check this feature is not yet checked or processed
361 bool aIsModified = myModified.find(theFeature) != myModified.end();
362 if (!aIsModified && myIsFinish) { // get info about the modification for features without preview
363 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
365 std::set<std::shared_ptr<ModelAPI_Feature> > aNewSet;
366 aNewSet.insert(theFeature); // contains itself, so, we don't know which was the reason and the reason is any
367 myModified[theFeature] = aNewSet;
372 std::cout<<"* Process feature "<<theFeature->name()<<std::endl;
375 // update the sketch plane before the sketch sub-elements are recomputed
376 // (otherwise sketch will update plane, modify subs, after executed, but with old subs edges)
377 if (aIsModified && theFeature->getKind() == "Sketch") {
379 std::cout<<"****** Update sketch args "<<theFeature->name()<<std::endl;
381 AttributeSelectionPtr anExtSel = theFeature->selection("External");
382 if (anExtSel.get()) {
383 ResultPtr aContext = anExtSel->context();
384 if (aContext.get() && aContext->document().get()) {
385 FeaturePtr anExtBase = aContext->document()->feature(aContext);
386 if (anExtBase.get()) {
387 processFeature(anExtBase);
391 updateArguments(theFeature);
394 if (!aIsModified) { // no modification is needed
398 // evaluate parameter before the sub-elements update: it updates dependencies on evaluation (#1085)
399 if (theFeature->getKind() == "Parameter") {
400 theFeature->execute();
403 bool isReferencedInvalid = false;
404 // check all features this feature depended on (recursive call of updateFeature)
405 std::set<std::shared_ptr<ModelAPI_Feature> >& aReasons = myModified[theFeature];
406 if (aReasons.find(theFeature) == aReasons.end()) {
407 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aReasonIter = aReasons.begin();
408 for(; aReasonIter != aReasons.end(); aReasonIter++) {
409 if (*aReasonIter != theFeature) {
410 if (processFeature(*aReasonIter))
412 if ((*aReasonIter)->data()->execState() == ModelAPI_StateInvalidArgument)
413 isReferencedInvalid = true;
416 } else { // check all features this feature depended on because here which one was modified is unknown
417 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aDeps;
418 theFeature->data()->referencesToObjects(aDeps);
419 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > >::iterator
420 anAttrsIter = aDeps.begin();
421 for(; anAttrsIter != aDeps.end(); anAttrsIter++) {
422 if (theFeature->attribute(anAttrsIter->first)->isArgument()) {
423 std::list<std::shared_ptr<ModelAPI_Object> >::iterator aDepIter = anAttrsIter->second.begin();
424 for(; aDepIter != anAttrsIter->second.end(); aDepIter++) {
425 FeaturePtr aDepFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(*aDepIter);
426 if (!aDepFeat.get()) { // so, it depends on the result and process the feature owner of it
427 ResultPtr aDepRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aDepIter);
429 aDepFeat = (*aDepIter)->document()->feature(aDepRes);
432 if (aDepFeat.get()) {
433 if (processFeature(aDepFeat))
435 if (aDepFeat->data()->execState() == ModelAPI_StateInvalidArgument)
436 isReferencedInvalid = true;
441 if (theFeature->getKind() == "Part") { // part is not depended on its subs directly, but subs must be iterated anyway
442 CompositeFeaturePtr aPart = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
443 int aNum = aPart->numberOfSubs();
444 for(int a = 0; a < aNum; a++) {
445 FeaturePtr aSub = aPart->subFeature(a);
447 if (processFeature(aSub))
449 if (aSub->data()->execState() == ModelAPI_StateInvalidArgument)
450 isReferencedInvalid = true;
456 // do not execute the composite that contains the current
457 bool isPostponedMain = false;
458 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
459 if (theFeature->getKind() == "ExtrusionSketch" && aCompos.get()) {
460 CompositeFeaturePtr aCurrentOwner =
461 ModelAPI_Tools::compositeOwner(theFeature->document()->currentFeature(false));
462 isPostponedMain = aCurrentOwner.get() && aCompos->isSub(aCurrentOwner);
466 std::cout<<"Update args "<<theFeature->name()<<std::endl;
468 // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
469 updateArguments(theFeature);
471 // add this feature to the processed right now to be able remove it from this list on
472 // update signal during this feature execution
473 myModified.erase(theFeature);
474 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
475 theFeature->data()->execState(ModelAPI_StateDone);
477 // this checking must be after the composite feature sub-elements processing:
478 // composite feature status may depend on it's subelements
479 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument || isReferencedInvalid) {
481 std::cout<<"Invalid args "<<theFeature->name()<<std::endl;
483 theFeature->eraseResults();
484 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
485 return true; // so, feature is modified (results are erased)
488 // execute feature if it must be updated
489 ModelAPI_ExecState aState = theFeature->data()->execState();
490 if (aFactory->validate(theFeature)) {
491 if (!isPostponedMain) {
492 executeFeature(theFeature);
496 std::cout<<"Feature is not valid, erase results "<<theFeature->name()<<std::endl;
498 theFeature->eraseResults();
499 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
504 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
506 // make updated and redisplay all results
507 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
509 std::list<ResultPtr> allResults;
510 ModelAPI_Tools::allResults(theFeature, allResults);
511 std::list<ResultPtr>::iterator aRIter = allResults.begin();
512 for (; aRIter != allResults.cend(); aRIter++) {
513 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
514 if (!aRes->isDisabled()) {// update state only for enabled results (Placement Result Part may make the original Part Result as invalid)
515 aRes->data()->execState(theState);
517 if (theFeature->data()->updateID() > aRes->data()->updateID()) {
518 aRes->data()->setUpdateID(theFeature->data()->updateID());
520 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
522 // to redisplay "presentable" feature (for ex. distance constraint)
523 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
524 theFeature->data()->execState(theState);
527 /// Updates the state by the referenced object: if something bad with it, set state for this one
528 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
531 ModelAPI_ExecState aRefState = theTarget->data()->execState();
532 if (aRefState == ModelAPI_StateMustBeUpdated) {
533 if (theCurrent == ModelAPI_StateDone)
534 return ModelAPI_StateMustBeUpdated;
535 } else if (aRefState != ModelAPI_StateDone) {
536 return ModelAPI_StateInvalidArgument;
542 void Model_Update::updateArguments(FeaturePtr theFeature) {
543 // perform this method also for disabled features: to make "not done" state for
544 // features referenced to the active and modified features
546 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
548 ModelAPI_ExecState aState = theFeature->data()->execState();
549 if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
550 aState = ModelAPI_StateMustBeUpdated;
552 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
553 aState = ModelAPI_StateMustBeUpdated;
554 // check the parameters state
557 std::list<AttributePtr> anAttrinbutes =
558 theFeature->data()->attributes(ModelAPI_AttributeInteger::typeId());
559 std::list<AttributePtr>::iterator anIter = anAttrinbutes.begin();
560 for(; anIter != anAttrinbutes.end(); anIter++) {
561 AttributeIntegerPtr anAttribute =
562 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(*anIter);
563 if (anAttribute.get() && !anAttribute->text().empty()) {
564 if (myIsParamUpdated) {
565 ModelAPI_AttributeEvalMessage::send(anAttribute, this);
567 if (anAttribute->expressionInvalid()) {
568 aState = ModelAPI_StateInvalidArgument;
575 std::list<AttributePtr> aDoubles =
576 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
577 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
578 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
579 AttributeDoublePtr aDouble =
580 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
581 if (aDouble.get() && !aDouble->text().empty()) {
582 if (myIsParamUpdated) {
583 ModelAPI_AttributeEvalMessage::send(aDouble, this);
585 if (aDouble->expressionInvalid()) {
586 aState = ModelAPI_StateInvalidArgument;
593 std::list<AttributePtr> anAttributes =
594 theFeature->data()->attributes(GeomDataAPI_Point::typeId());
595 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
596 for(; anIter != anAttributes.end(); anIter++) {
597 AttributePointPtr aPointAttribute =
598 std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
599 if (aPointAttribute.get() && (!aPointAttribute->textX().empty() ||
600 !aPointAttribute->textY().empty() || !aPointAttribute->textZ().empty())) {
601 if (myIsParamUpdated) {
602 ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
604 if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
605 (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
606 (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
607 aState = ModelAPI_StateInvalidArgument;
613 std::list<AttributePtr> anAttributes =
614 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
615 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
616 for(; anIter != anAttributes.end(); anIter++) {
617 AttributePoint2DPtr aPoint2DAttribute =
618 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
619 if (aPoint2DAttribute.get()) {
620 if (myIsParamUpdated && (!aPoint2DAttribute->textX().empty() ||
621 !aPoint2DAttribute->textY().empty())) {
622 ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
624 if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
625 (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
626 aState = ModelAPI_StateInvalidArgument;
630 // update the selection attributes if any
631 list<AttributePtr> aRefs =
632 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
633 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
634 for (; aRefsIter != aRefs.end(); aRefsIter++) {
635 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
636 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
637 ObjectPtr aContext = aSel->context();
638 // update argument only if the referenced object is ready to use
639 if (aContext.get() && !aContext->isDisabled()) {
640 if (isReason(theFeature, aContext)) {
641 if (!aSel->update()) { // this must be done on execution since it may be long operation
642 bool isObligatory = !aFactory->isNotObligatory(
643 theFeature->getKind(), theFeature->data()->id(aSel)) &&
644 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
646 aState = ModelAPI_StateInvalidArgument;
649 } else if (aContext.get()) {
650 // here it may be not obligatory, but if the reference is wrong, it should not be correct
651 bool isObligatory = aFactory->isCase(theFeature, theFeature->data()->id(aSel));
653 aState = ModelAPI_StateInvalidArgument;
656 // update the selection list attributes if any
657 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
658 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
659 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
660 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
661 for(int a = aSel->size() - 1; a >= 0; a--) {
662 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
663 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
665 ObjectPtr aContext = aSelAttr->context();
666 // update argument only if the referenced object is ready to use
667 if (aContext.get() && !aContext->isDisabled()) {
668 if (isReason(theFeature, aContext)) {
669 if (!aSelAttr->update()) {
670 bool isObligatory = !aFactory->isNotObligatory(
671 theFeature->getKind(), theFeature->data()->id(aSel)) &&
672 aFactory->isCase(theFeature, theFeature->data()->id(aSel));
674 aState = ModelAPI_StateInvalidArgument;
677 } else if (aContext.get()) {
678 // here it may be not obligatory, but if the reference is wrong, it should not be correct
679 bool isObligatory = aFactory->isCase(theFeature, theFeature->data()->id(aSel));
681 aState = ModelAPI_StateInvalidArgument;
687 if (aState != ModelAPI_StateDone)
688 theFeature->data()->execState(aState);
691 bool Model_Update::isReason(std::shared_ptr<ModelAPI_Feature>& theFeature,
692 std::shared_ptr<ModelAPI_Object> theReason)
694 std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >
695 ::iterator aReasonsIt = myModified.find(theFeature);
696 if (aReasonsIt == myModified.end())
697 return false; // this case only for not-previewed items update state, nothing is changed in args for it
698 if (aReasonsIt->second.find(theFeature) != aReasonsIt->second.end())
699 return true; // any is reason if it contains itself
700 FeaturePtr aReasFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(theReason);
701 if (!aReasFeat.get()) { // try to get feature of this result
702 ResultPtr aReasRes = std::dynamic_pointer_cast<ModelAPI_Result>(theReason);
704 aReasFeat = theReason->document()->feature(aReasRes);
706 return aReasonsIt->second.find(aReasFeat) != aReasonsIt->second.end();
710 void Model_Update::executeFeature(FeaturePtr theFeature)
713 std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
715 // execute in try-catch to avoid internal problems of the feature
716 ModelAPI_ExecState aState = ModelAPI_StateDone;
717 theFeature->data()->execState(ModelAPI_StateDone);
719 theFeature->execute();
720 if (theFeature->data()->execState() != ModelAPI_StateDone) {
721 aState = ModelAPI_StateExecFailed;
723 aState = ModelAPI_StateDone;
726 aState = ModelAPI_StateExecFailed;
728 "Feature " + theFeature->getKind() + " has failed during the execution");
730 // The macro feature has to be deleted in any case even its execution is failed
731 myWaitForFinish.insert(theFeature);
732 if (aState != ModelAPI_StateDone) {
733 theFeature->eraseResults();
735 theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
736 redisplayWithResults(theFeature, aState);
739 void Model_Update::updateStability(void* theSender)
741 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
743 bool added = false; // object may be was crated
744 ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theSender);
745 if (aSender && aSender->document()) {
746 FeaturePtr aFeatureSender =
747 std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
748 if (aFeatureSender.get()) {
749 Model_Objects* aDocObjects =
750 std::dynamic_pointer_cast<Model_Document>(aSender->document())->objects();
752 //aDocObjects->synchronizeBackRefs();
753 // remove or add all concealment refs from this feature
754 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
755 aSender->data()->referencesToObjects(aRefs);
756 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefIt = aRefs.begin();
757 for(; aRefIt != aRefs.end(); aRefIt++) {
758 if (!aFactory->isConcealed(aFeatureSender->getKind(), aRefIt->first))
759 continue; // take into account only concealed references (do not remove the sketch constraint and the edge on constraint edit)
760 std::list<ObjectPtr>& aRefFeaturesList = aRefIt->second;
761 std::list<ObjectPtr>::iterator aReferenced = aRefFeaturesList.begin();
762 for(; aReferenced != aRefFeaturesList.end(); aReferenced++) {
763 // stability is only on results: feature to feature reference mean nested
764 // features, that will remove nesting references
765 if (aReferenced->get() && (*aReferenced)->data()->isValid() &&
766 (*aReferenced)->groupName() != ModelAPI_Feature::group()) {
767 std::shared_ptr<Model_Data> aData =
768 std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
769 if (aFeatureSender->isStable()) {
770 aData->addBackReference(aFeatureSender, aRefIt->first);
772 aData->removeBackReference(aFeatureSender, aRefIt->first);
773 added = true; // remove of concealment may be caused creation of some result
782 static Events_Loop* aLoop = Events_Loop::loop();
783 static Events_ID kEventCreated = aLoop->eventByName(EVENT_OBJECT_CREATED);
784 aLoop->flush(kEventCreated);