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
37 Model_Update::Model_Update()
39 Events_Loop* aLoop = Events_Loop::loop();
40 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
41 aLoop->registerListener(this, kChangedEvent);
42 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
43 aLoop->registerListener(this, kRebuildEvent);
44 static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
45 aLoop->registerListener(this, kCreatedEvent);
46 static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
47 aLoop->registerListener(this, kUpdatedEvent);
48 static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
49 aLoop->registerListener(this, kMovedEvent);
50 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
51 aLoop->registerListener(this, kOpFinishEvent);
52 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
53 aLoop->registerListener(this, kOpAbortEvent);
54 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
55 aLoop->registerListener(this, kOpStartEvent);
57 Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild immediately",
58 Config_Prop::Boolean, "false");
60 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
61 myIsParamUpdated = false;
65 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
67 static Events_Loop* aLoop = Events_Loop::loop();
68 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
69 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
70 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
71 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
72 static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
73 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
74 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
75 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
76 if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
78 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
79 if (aPropVal != myIsAutomatic) { // something is changed
80 myIsAutomatic = aPropVal;
81 if (myIsAutomatic) // higher level of automatization => to rebuild
82 processOperation(false);
84 } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
85 processOperation(true);
86 } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent ||
87 theMessage->eventID() == kMovedEvent) {
88 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
89 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
90 const std::set<ObjectPtr>& anObjs = aMsg->objects();
91 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
92 bool isOnlyResults = true; // check that only results were changed: only redisplay is needed
93 for(; anObjIter != anObjs.cend(); anObjIter++) {
94 if (!std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIter).get()) {
95 isOnlyResults = false;
97 if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
98 myIsParamUpdated = true;
100 // created objects are always must be up to date (python box feature)
101 // and updated not in internal uptation chain
102 myJustUpdated.insert(*anObjIter);
104 // this event is for solver update, not here, do not react immideately
105 if (!isOnlyResults && !(theMessage->eventID() == kMovedEvent))
106 processOperation(false);
107 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent ||
108 theMessage->eventID() == kOpStartEvent) {
109 myIsParamUpdated = false;
111 if (!(theMessage->eventID() == kOpStartEvent)) {
113 processOperation(true, theMessage->eventID() == kOpFinishEvent);
116 // remove all macros before clearing all created
117 std::set<ObjectPtr>::iterator anUpdatedIter = myWaitForFinish.begin();
118 while(anUpdatedIter != myWaitForFinish.end()) {
119 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anUpdatedIter);
120 if (aFeature.get()) {
121 // remove macro on finish
122 if (aFeature->isMacro()) {
123 aFeature->document()->removeFeature(aFeature);
124 myWaitForFinish.erase(aFeature);
126 // to avoid the map update problems on "remove"
127 if (myWaitForFinish.find(aFeature) == myWaitForFinish.end()) {
128 anUpdatedIter = myWaitForFinish.begin();
136 // in the end of transaction everything is updated, so clear the old objects (the only one
137 // place where results are cleared)
138 myJustUpdated.clear();
139 myWaitForFinish.clear();
143 void Model_Update::processOperation(const bool theTotalUpdate, const bool theFinish)
146 // the hardcode (DBC asked): hide the sketch referenced by extrusion on apply
147 std::set<std::shared_ptr<ModelAPI_Object> >::iterator aFIter;
148 for(aFIter = myWaitForFinish.begin(); aFIter != myWaitForFinish.end(); aFIter++)
150 FeaturePtr aF = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
151 if (aF && aF->data()->isValid() && aF->getKind() == "Extrusion") {
152 AttributeSelectionListPtr aBase = aF->selectionList("base");
154 for(int a = aBase->size() - 1; a >= 0; a--) {
155 ResultPtr aSketchRes = aBase->value(a)->context();
157 aSketchRes->setDisplayed(false);
164 // perform update of everything if needed
168 bool isAutomaticChanged = false;
170 if (theTotalUpdate && !myIsAutomatic) { // Apply button now works as "Rebuild"
171 isAutomaticChanged = true;
172 myIsAutomatic = true;
175 // iterate all features in the root document to update each
176 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
177 Model_Objects* anObjs = std::dynamic_pointer_cast<Model_Document>(aRootDoc)->objects();
179 // two cycles: parameters are first to process
180 FeaturePtr aFeatureIter = anObjs->firstFeature();
181 std::set<FeaturePtr> aProcessedFeatures; // to avoid processing twice
182 for (; aFeatureIter.get(); aFeatureIter = anObjs->nextFeature(aFeatureIter)) {
183 if (aFeatureIter->groupName() == "Parameter")
184 updateFeature(aFeatureIter, aProcessedFeatures);
186 aFeatureIter = anObjs->firstFeature();
187 for (; aFeatureIter.get(); aFeatureIter = anObjs->nextFeature(aFeatureIter)) {
188 if (aFeatureIter->groupName() != "Parameter")
189 updateFeature(aFeatureIter, aProcessedFeatures);
192 if (isAutomaticChanged) myIsAutomatic = false;
193 myIsExecuted = false;
195 // flush to update display
196 static Events_Loop* aLoop = Events_Loop::loop();
197 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
198 aLoop->flush(EVENT_DISP);
202 void Model_Update::updateFeature(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed)
204 // check all features this feature depended on (recursive call of updateFeature)
205 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
207 if (theProcessed.find(theFeature) != theProcessed.end())
209 theProcessed.insert(theFeature);
210 if (theFeature->isDisabled())
213 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
214 // If automatice update is not needed and feature attributes were not updated right now,
215 // do not execute it and do not update arguments.
216 if (!myIsAutomatic && myJustUpdated.find(theFeature) == myJustUpdated.end() && !aCompos.get()) {
217 // execute will be performed later, but some features may have not-result
218 // presentations, so call update for them (like coincidence in the sketcher)
219 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
220 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
224 // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
225 updateArguments(theFeature);
227 // composite feature must be executed after sub-features execution
229 // two cycles: parameters must be processed first
230 for(int a = 0; a < aCompos->numberOfSubs(); a++) {
231 FeaturePtr aSub = aCompos->subFeature(a);
232 if (aSub->groupName() == "Parameter")
233 updateFeature(aSub, theProcessed);
235 // number of subs can be changed in execution: like fillet
236 for(int a = 0; a < aCompos->numberOfSubs(); a++) {
237 FeaturePtr aSub = aCompos->subFeature(a);
238 if (aSub->groupName() != "Parameter")
239 updateFeature(aSub, theProcessed);
242 // this checking must be after the composite feature sub-elements processing:
243 // composite feature status may depend on it's subelements
244 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
245 theFeature->eraseResults();
246 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
250 bool aJustUpdated = myJustUpdated.find(theFeature) != myJustUpdated.end();
252 if (myIsAutomatic && theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
255 // On abort, undo or redo execute is not needed: results in document are updated automatically
256 // But redisplay is needed: results are updated, must be also updated in the viewer.
258 !std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures()) {
259 if (!theFeature->isPersistentResult()) { // not persistent must be re-executed on abort, etc.
260 ModelAPI_ExecState aState = theFeature->data()->execState();
261 if (aFactory->validate(theFeature)) {
262 executeFeature(theFeature);
264 theFeature->eraseResults();
265 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
268 redisplayWithResults(theFeature, ModelAPI_StateNothing);
269 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) { // it is done (in the tree)
270 theFeature->data()->execState(ModelAPI_StateDone);
276 // execute feature if it must be updated
277 if (theFeature->isPreviewNeeded() || myIsFinish) {
279 ModelAPI_ExecState aState = theFeature->data()->execState();
280 if (aFactory->validate(theFeature)) {
281 executeFeature(theFeature);
283 theFeature->eraseResults();
284 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
287 } else { // preview is not needed => make state Done
288 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
289 theFeature->data()->execState(ModelAPI_StateDone);
290 if (aJustUpdated) // store that it must be updated on finish
291 myJustUpdated.insert(theFeature);
296 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
298 // make updated and redisplay all results
299 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
300 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
301 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
302 for (; aRIter != aResults.cend(); aRIter++) {
303 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
304 aRes->data()->execState(theState);
305 if (theFeature->data()->updateID() > aRes->data()->updateID()) {
306 aRes->data()->setUpdateID(theFeature->data()->updateID());
308 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
310 // to redisplay "presentable" feature (for ex. distance constraint)
311 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
312 theFeature->data()->execState(theState);
315 /// Updates the state by the referenced object: if something bad with it, set state for this one
316 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
319 ModelAPI_ExecState aRefState = theTarget->data()->execState();
320 if (aRefState == ModelAPI_StateMustBeUpdated) {
321 if (theCurrent == ModelAPI_StateDone)
322 return ModelAPI_StateMustBeUpdated;
323 } else if (aRefState != ModelAPI_StateDone) {
324 return ModelAPI_StateInvalidArgument;
330 void Model_Update::updateArguments(FeaturePtr theFeature) {
331 // perform this method also for disabled features: to make "not done" state for
332 // featuers referenced to the active and modified features
334 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
336 ModelAPI_ExecState aState = theFeature->data()->execState();
337 if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
338 aState = ModelAPI_StateMustBeUpdated;
340 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
341 aState = ModelAPI_StateMustBeUpdated;
342 // check the parameters state
344 std::list<AttributePtr> aDoubles =
345 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
346 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
347 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
348 AttributeDoublePtr aDouble =
349 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
350 if (aDouble.get() && !aDouble->text().empty()) {
351 if (myIsParamUpdated) {
352 ModelAPI_AttributeEvalMessage::send(aDouble, this);
354 if (aDouble->expressionInvalid()) {
355 aState = ModelAPI_StateInvalidArgument;
361 std::list<AttributePtr> anAttributes =
362 theFeature->data()->attributes(GeomDataAPI_Point::typeId());
363 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
364 for(; anIter != anAttributes.end(); anIter++) {
365 AttributePointPtr aPointAttribute =
366 std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
367 if (aPointAttribute.get()) {
368 if (myIsParamUpdated) {
369 ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
371 if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
372 (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
373 (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
374 aState = ModelAPI_StateInvalidArgument;
380 std::list<AttributePtr> anAttributes =
381 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
382 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
383 for(; anIter != anAttributes.end(); anIter++) {
384 AttributePoint2DPtr aPoint2DAttribute =
385 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
386 if (aPoint2DAttribute.get()) {
387 if (myIsParamUpdated) {
388 ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
390 if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
391 (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
392 aState = ModelAPI_StateInvalidArgument;
397 //if (aState == ModelAPI_StateDone) {// all referenced objects are ready to be used
398 //std::cout<<"Execute feature "<<theFeature->getKind()<<std::endl;
399 // before execution update the selection attributes if any
400 list<AttributePtr> aRefs =
401 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
402 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
403 for (; aRefsIter != aRefs.end(); aRefsIter++) {
404 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
405 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
406 ObjectPtr aContext = aSel->context();
407 // update argument onlt if the referenced object is changed
408 if (aContext.get() && !aContext->isDisabled() &&
409 aContext->data()->updateID() > theFeature->data()->updateID()) {
410 if (aState == ModelAPI_StateDone)
411 aState = ModelAPI_StateMustBeUpdated;
412 if (!aSel->update()) { // this must be done on execution since it may be long operation
413 if (!aFactory->isNotObligatory(theFeature->getKind(), theFeature->data()->id(aSel)) &&
414 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
415 aState = ModelAPI_StateInvalidArgument;
419 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
420 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
421 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
422 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
423 for(int a = aSel->size() - 1; a >= 0; a--) {
424 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
425 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
427 ObjectPtr aContext = aSelAttr->context();
428 // update argument onlt if the referenced object is changed
429 if (aContext.get() && !aContext->isDisabled() &&
430 aContext->data()->updateID() > theFeature->data()->updateID()) {
431 if (aState == ModelAPI_StateDone)
432 aState = ModelAPI_StateMustBeUpdated;
433 if (!aSelAttr->update()) {
434 if (!aFactory->isNotObligatory(
435 theFeature->getKind(), theFeature->data()->id(aSel)) &&
436 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
437 aState = ModelAPI_StateInvalidArgument;
443 // check all references: if referenced objects are updated, this object also must be updated
444 // also check state of referenced objects: if they are not ready, inherit corresponding state
445 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefsObj;
446 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theFeature->data());
447 aData->referencesToObjects(aRefsObj);
448 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefsObj.begin();
449 for(; aRef != aRefsObj.end(); aRef++) {
450 std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
451 for(; aRefObj != aRef->second.end(); aRefObj++) {
452 // if reference is null, it may mean that this reference is to other document
453 // the does not supported by RefList: parameters may be recomputed
454 if (!aRefObj->get() && theFeature->firstResult().get() &&
455 theFeature->firstResult()->groupName() == ModelAPI_ResultParameter::group()) {
456 if (aState == ModelAPI_StateDone)
457 aState = ModelAPI_StateMustBeUpdated;
458 } else if (myJustUpdated.find(*aRefObj) != myJustUpdated.end() ||
459 (aRefObj->get() && (*aRefObj)->data()->updateID() > theFeature->data()->updateID())) {
460 if (aState == ModelAPI_StateDone)
461 aState = ModelAPI_StateMustBeUpdated;
463 aState = stateByReference(*aRefObj, aState);
466 // composites sub-elements
467 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
468 // composite feature must be executed after sub-features execution
470 // number of subs can be changed in execution: like fillet
471 for(int a = 0; a < aCompos->numberOfSubs(); a++) {
472 FeaturePtr aSub = aCompos->subFeature(a);
473 if (myJustUpdated.find(aSub) != myJustUpdated.end() ||
474 (aSub.get() && aSub->data()->updateID() > theFeature->data()->updateID())) {
475 if (aState == ModelAPI_StateDone)
476 aState = ModelAPI_StateMustBeUpdated;
482 if (aState != ModelAPI_StateDone)
483 theFeature->data()->execState(aState);
486 void Model_Update::executeFeature(FeaturePtr theFeature)
488 // execute in try-catch to avoid internal problems of the feature
489 ModelAPI_ExecState aState = ModelAPI_StateDone;
490 theFeature->data()->execState(ModelAPI_StateDone);
492 theFeature->execute();
493 myJustUpdated.erase(theFeature);
494 if (theFeature->data()->execState() != ModelAPI_StateDone) {
495 aState = ModelAPI_StateExecFailed;
497 aState = ModelAPI_StateDone;
498 myWaitForFinish.insert(theFeature);
501 aState = ModelAPI_StateExecFailed;
503 "Feature " + theFeature->getKind() + " has failed during the execution");
505 if (aState != ModelAPI_StateDone) {
506 theFeature->eraseResults();
508 theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
509 redisplayWithResults(theFeature, aState);