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 kRebuildEvent = aLoop->eventByName("Rebuild");
44 aLoop->registerListener(this, kRebuildEvent);
45 static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
46 aLoop->registerListener(this, kCreatedEvent);
47 static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
48 aLoop->registerListener(this, kUpdatedEvent);
49 static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
50 aLoop->registerListener(this, kMovedEvent);
51 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
52 aLoop->registerListener(this, kOpFinishEvent);
53 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
54 aLoop->registerListener(this, kOpAbortEvent);
55 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
56 aLoop->registerListener(this, kOpStartEvent);
58 Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild immediately",
59 Config_Prop::Boolean, "false");
61 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
62 myIsParamUpdated = false;
66 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
68 static Events_Loop* aLoop = Events_Loop::loop();
69 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
70 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
71 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
72 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
73 static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
74 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
75 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
76 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
77 if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
79 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
80 if (aPropVal != myIsAutomatic) { // something is changed
81 myIsAutomatic = aPropVal;
82 if (myIsAutomatic) // higher level of automatization => to rebuild
83 processOperation(false);
85 } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
86 processOperation(true);
87 } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent ||
88 theMessage->eventID() == kMovedEvent) {
89 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
90 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
91 const std::set<ObjectPtr>& anObjs = aMsg->objects();
92 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
93 bool isOnlyResults = true; // check that only results were changed: only redisplay is needed
94 for(; anObjIter != anObjs.cend(); anObjIter++) {
95 if (!std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIter).get()) {
96 isOnlyResults = false;
98 if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
99 myIsParamUpdated = true;
101 // created objects are always must be up to date (python box feature)
102 // and updated not in internal uptation chain
103 myJustUpdated.insert(*anObjIter);
105 if ((*anObjIter)->data() && (*anObjIter)->data()->isValid()) {
106 std::cout<<"Add updated "<<(*anObjIter)->groupName()<<" "
107 <<(*anObjIter)->data()->name()<<std::endl;
111 // this event is for solver update, not here, do not react immideately
112 if (!isOnlyResults && !(theMessage->eventID() == kMovedEvent))
113 processOperation(false);
114 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
115 theMessage->eventID() == kOpStartEvent) {
116 myIsParamUpdated = false;
118 if (!(theMessage->eventID() == kOpStartEvent)) {
120 processOperation(true, theMessage->eventID() == kOpFinishEvent);
123 // remove all macros before clearing all created
124 std::set<ObjectPtr>::iterator anUpdatedIter = myWaitForFinish.begin();
125 while(anUpdatedIter != myWaitForFinish.end()) {
126 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
127 if (aFeature.get()) {
128 // remove macro on finish
129 if (aFeature->isMacro()) {
130 aFeature->document()->removeFeature(aFeature);
131 myWaitForFinish.erase(aFeature);
133 // to avoid the map update problems on "remove"
134 if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
135 anUpdatedIter = myWaitForFinish.begin();
143 // in the end of transaction everything is updated, so clear the old objects (the only one
144 // place where results are cleared)
145 myJustUpdated.clear();
146 myWaitForFinish.clear();
150 void Model_Update::processOperation(const bool theTotalUpdate, const bool theFinish)
153 // the hardcode (DBC asked): hide the sketch referenced by extrusion on apply
154 std::set<std::shared_ptr<ModelAPI_Object> >::iterator aFIter;
155 for(aFIter = myWaitForFinish.begin(); aFIter != myWaitForFinish.end(); aFIter++)
157 FeaturePtr aF = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
158 if (aF && aF->data()->isValid() && aF->getKind() == "Extrusion") {
159 AttributeSelectionListPtr aBase = aF->selectionList("base");
161 for(int a = aBase->size() - 1; a >= 0; a--) {
162 ResultPtr aSketchRes = aBase->value(a)->context();
164 aSketchRes->setDisplayed(false);
171 // perform update of everything if needed
174 std::cout<<"****** Start processing"<<std::endl;
178 bool isAutomaticChanged = false;
180 if (theTotalUpdate && !myIsAutomatic) { // Apply button now works as "Rebuild"
181 isAutomaticChanged = true;
182 myIsAutomatic = true;
185 // iterate all features in the root document to update each
186 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
187 Model_Objects* anObjs = std::dynamic_pointer_cast<Model_Document>(aRootDoc)->objects();
189 // two cycles: parameters are first to process
190 FeaturePtr aFeatureIter = anObjs->firstFeature();
191 std::set<FeaturePtr> aProcessedFeatures; // to avoid processing twice
192 for (; aFeatureIter.get(); aFeatureIter = anObjs->nextFeature(aFeatureIter)) {
193 if (aFeatureIter->groupName() == "Parameter")
194 updateFeature(aFeatureIter, aProcessedFeatures);
196 aFeatureIter = anObjs->firstFeature();
197 for (; aFeatureIter.get(); aFeatureIter = anObjs->nextFeature(aFeatureIter)) {
198 if (aFeatureIter->groupName() != "Parameter")
199 updateFeature(aFeatureIter, aProcessedFeatures);
202 if (isAutomaticChanged) myIsAutomatic = false;
203 myIsExecuted = false;
205 // flush to update display
206 static Events_Loop* aLoop = Events_Loop::loop();
207 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
208 aLoop->flush(EVENT_DISP);
210 std::cout<<"****** End processing"<<std::endl;
215 void Model_Update::updateFeature(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed)
217 // check all features this feature depended on (recursive call of updateFeature)
218 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
220 if (theProcessed.find(theFeature) != theProcessed.end())
222 theProcessed.insert(theFeature);
223 if (theFeature->isDisabled())
227 std::cout<<"Update Feature "<<theFeature->name()<<std::endl;
229 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
230 // If automatice update is not needed and feature attributes were not updated right now,
231 // do not execute it and do not update arguments.
232 if (!myIsAutomatic && myJustUpdated.find(theFeature) == myJustUpdated.end() && !aCompos.get()) {
233 // execute will be performed later, but some features may have not-result
234 // presentations, so call update for them (like coincidence in the sketcher)
235 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
236 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
240 // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
241 updateArguments(theFeature);
243 // composite feature must be executed after sub-features execution
245 // two cycles: parameters must be processed first
246 for(int a = 0; a < aCompos->numberOfSubs(); a++) {
247 FeaturePtr aSub = aCompos->subFeature(a);
248 if (aSub->groupName() == "Parameter")
249 updateFeature(aSub, theProcessed);
251 // number of subs can be changed in execution: like fillet
252 for(int a = 0; a < aCompos->numberOfSubs(); a++) {
253 FeaturePtr aSub = aCompos->subFeature(a);
254 if (aSub->groupName() != "Parameter")
255 updateFeature(aSub, theProcessed);
258 // this checking must be after the composite feature sub-elements processing:
259 // composite feature status may depend on it's subelements
260 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
261 theFeature->eraseResults();
262 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
266 bool aJustUpdated = myJustUpdated.find(theFeature) != myJustUpdated.end();
268 if (myIsAutomatic && theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
271 // On abort, undo or redo execute is not needed: results in document are updated automatically
272 // But redisplay is needed: results are updated, must be also updated in the viewer.
274 !std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures()) {
275 if (!theFeature->isPersistentResult()) { // not persistent must be re-executed on abort, etc.
276 ModelAPI_ExecState aState = theFeature->data()->execState();
277 if (aFactory->validate(theFeature)) {
278 executeFeature(theFeature);
280 theFeature->eraseResults();
281 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
284 redisplayWithResults(theFeature, ModelAPI_StateNothing);
285 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) { // it is done (in the tree)
286 theFeature->data()->execState(ModelAPI_StateDone);
292 // execute feature if it must be updated
293 if (theFeature->isPreviewNeeded() || myIsFinish) {
295 ModelAPI_ExecState aState = theFeature->data()->execState();
296 if (aFactory->validate(theFeature)) {
298 std::cout<<"Execute Feature "<<theFeature->name()<<std::endl;
300 executeFeature(theFeature);
302 theFeature->eraseResults();
303 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
306 } else { // preview is not needed => make state Done
307 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
308 theFeature->data()->execState(ModelAPI_StateDone);
309 if (aJustUpdated) // store that it must be updated on finish
310 myJustUpdated.insert(theFeature);
315 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
317 // make updated and redisplay all results
318 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
319 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
320 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
321 for (; aRIter != aResults.cend(); aRIter++) {
322 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
323 aRes->data()->execState(theState);
324 if (theFeature->data()->updateID() > aRes->data()->updateID()) {
325 aRes->data()->setUpdateID(theFeature->data()->updateID());
327 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
329 // to redisplay "presentable" feature (for ex. distance constraint)
330 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
331 theFeature->data()->execState(theState);
334 /// Updates the state by the referenced object: if something bad with it, set state for this one
335 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
338 ModelAPI_ExecState aRefState = theTarget->data()->execState();
339 if (aRefState == ModelAPI_StateMustBeUpdated) {
340 if (theCurrent == ModelAPI_StateDone)
341 return ModelAPI_StateMustBeUpdated;
342 } else if (aRefState != ModelAPI_StateDone) {
343 return ModelAPI_StateInvalidArgument;
349 void Model_Update::updateArguments(FeaturePtr theFeature) {
350 // perform this method also for disabled features: to make "not done" state for
351 // featuers referenced to the active and modified features
353 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
355 ModelAPI_ExecState aState = theFeature->data()->execState();
356 if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
357 aState = ModelAPI_StateMustBeUpdated;
359 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
360 aState = ModelAPI_StateMustBeUpdated;
361 // check the parameters state
363 std::list<AttributePtr> aDoubles =
364 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
365 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
366 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
367 AttributeDoublePtr aDouble =
368 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
369 if (aDouble.get() && !aDouble->text().empty()) {
370 if (myIsParamUpdated) {
371 ModelAPI_AttributeEvalMessage::send(aDouble, this);
373 if (aDouble->expressionInvalid()) {
374 aState = ModelAPI_StateInvalidArgument;
380 std::list<AttributePtr> anAttributes =
381 theFeature->data()->attributes(GeomDataAPI_Point::typeId());
382 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
383 for(; anIter != anAttributes.end(); anIter++) {
384 AttributePointPtr aPointAttribute =
385 std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
386 if (aPointAttribute.get()) {
387 if (myIsParamUpdated) {
388 ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
390 if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
391 (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
392 (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
393 aState = ModelAPI_StateInvalidArgument;
399 std::list<AttributePtr> anAttributes =
400 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
401 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
402 for(; anIter != anAttributes.end(); anIter++) {
403 AttributePoint2DPtr aPoint2DAttribute =
404 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
405 if (aPoint2DAttribute.get()) {
406 if (myIsParamUpdated) {
407 ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
409 if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
410 (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
411 aState = ModelAPI_StateInvalidArgument;
416 //if (aState == ModelAPI_StateDone) {// all referenced objects are ready to be used
417 //std::cout<<"Execute feature "<<theFeature->getKind()<<std::endl;
418 // before execution update the selection attributes if any
419 list<AttributePtr> aRefs =
420 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
421 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
422 for (; aRefsIter != aRefs.end(); aRefsIter++) {
423 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
424 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
425 ObjectPtr aContext = aSel->context();
426 // update argument onlt if the referenced object is changed
427 if (aContext.get() && !aContext->isDisabled() &&
428 (myJustUpdated.find(aContext) != myJustUpdated.end() ||
429 aContext->data()->updateID() > theFeature->data()->updateID())) {
430 if (aState == ModelAPI_StateDone)
431 aState = ModelAPI_StateMustBeUpdated;
432 if (!aSel->update()) { // this must be done on execution since it may be long operation
433 if (!aFactory->isNotObligatory(theFeature->getKind(), theFeature->data()->id(aSel)) &&
434 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
435 aState = ModelAPI_StateInvalidArgument;
439 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
440 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
441 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
442 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
443 for(int a = aSel->size() - 1; a >= 0; a--) {
444 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
445 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
447 ObjectPtr aContext = aSelAttr->context();
448 // update argument onlt if the referenced object is changed
449 if (aContext.get() && !aContext->isDisabled() &&
450 (myJustUpdated.find(aContext) != myJustUpdated.end() ||
451 aContext->data()->updateID() > theFeature->data()->updateID())) {
452 if (aState == ModelAPI_StateDone)
453 aState = ModelAPI_StateMustBeUpdated;
454 if (!aSelAttr->update()) {
455 if (!aFactory->isNotObligatory(
456 theFeature->getKind(), theFeature->data()->id(aSel)) &&
457 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
458 aState = ModelAPI_StateInvalidArgument;
464 // check all references: if referenced objects are updated, this object also must be updated
465 // also check state of referenced objects: if they are not ready, inherit corresponding state
466 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefsObj;
467 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theFeature->data());
468 aData->referencesToObjects(aRefsObj);
469 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefsObj.begin();
470 for(; aRef != aRefsObj.end(); aRef++) {
471 std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
472 for(; aRefObj != aRef->second.end(); aRefObj++) {
473 // if reference is null, it may mean that this reference is to other document
474 // the does not supported by RefList: parameters may be recomputed
475 if (!aRefObj->get() && theFeature->firstResult().get() &&
476 theFeature->firstResult()->groupName() == ModelAPI_ResultParameter::group()) {
477 if (aState == ModelAPI_StateDone)
478 aState = ModelAPI_StateMustBeUpdated;
479 } else if (myJustUpdated.find(*aRefObj) != myJustUpdated.end() ||
480 (aRefObj->get() && (*aRefObj)->data()->updateID() > theFeature->data()->updateID())) {
481 if (aState == ModelAPI_StateDone)
482 aState = ModelAPI_StateMustBeUpdated;
484 aState = stateByReference(*aRefObj, aState);
487 // composites sub-elements
488 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
489 // composite feature must be executed after sub-features execution
491 // number of subs can be changed in execution: like fillet
492 for(int a = 0; a < aCompos->numberOfSubs(); a++) {
493 FeaturePtr aSub = aCompos->subFeature(a);
494 if (myJustUpdated.find(aSub) != myJustUpdated.end() ||
495 (aSub.get() && aSub->data()->updateID() > theFeature->data()->updateID())) {
496 if (aState == ModelAPI_StateDone)
497 aState = ModelAPI_StateMustBeUpdated;
503 if (aState != ModelAPI_StateDone)
504 theFeature->data()->execState(aState);
507 void Model_Update::executeFeature(FeaturePtr theFeature)
509 // execute in try-catch to avoid internal problems of the feature
510 ModelAPI_ExecState aState = ModelAPI_StateDone;
511 theFeature->data()->execState(ModelAPI_StateDone);
513 theFeature->execute();
514 myJustUpdated.erase(theFeature);
515 if (theFeature->data()->execState() != ModelAPI_StateDone) {
516 aState = ModelAPI_StateExecFailed;
518 aState = ModelAPI_StateDone;
519 myWaitForFinish.insert(theFeature);
522 aState = ModelAPI_StateExecFailed;
524 "Feature " + theFeature->getKind() + " has failed during the execution");
526 if (aState != ModelAPI_StateDone) {
527 theFeature->eraseResults();
529 theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
530 redisplayWithResults(theFeature, aState);