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 FeaturePtr aFeatureIter = anObjs->firstFeature();
180 std::set<FeaturePtr> aProcessedFeatures; // to avoid processing twice
181 for (; aFeatureIter.get(); aFeatureIter = anObjs->nextFeature(aFeatureIter)) {
182 updateFeature(aFeatureIter, aProcessedFeatures);
185 if (isAutomaticChanged) myIsAutomatic = false;
186 myIsExecuted = false;
188 // flush to update display
189 static Events_Loop* aLoop = Events_Loop::loop();
190 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
191 aLoop->flush(EVENT_DISP);
195 void Model_Update::updateFeature(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed)
197 // check all features this feature depended on (recursive call of updateFeature)
198 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
200 if (theProcessed.find(theFeature) != theProcessed.end())
202 theProcessed.insert(theFeature);
203 if (theFeature->isDisabled())
206 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
207 // If automatice update is not needed and feature attributes were not updated right now,
208 // do not execute it and do not update arguments.
209 if (!myIsAutomatic && myJustUpdated.find(theFeature) == myJustUpdated.end() && !aCompos.get()) {
210 // execute will be performed later, but some features may have not-result
211 // presentations, so call update for them (like coincidence in the sketcher)
212 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
213 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
217 // Update selection and parameters attributes first, before sub-features analysis (sketch plane).
218 updateArguments(theFeature);
220 // composite feature must be executed after sub-features execution
222 // number of subs can be changed in execution: like fillet
223 for(int a = 0; a < aCompos->numberOfSubs(); a++) {
224 FeaturePtr aSub = aCompos->subFeature(a);
225 updateFeature(aSub, theProcessed);
228 // this checking must be after the composite feature sub-elements processing:
229 // composite feature status may depend on it's subelements
230 if (theFeature->data()->execState() == ModelAPI_StateInvalidArgument) {
231 theFeature->eraseResults();
232 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
236 bool aJustUpdated = myJustUpdated.find(theFeature) != myJustUpdated.end();
238 if (myIsAutomatic && theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
241 // execute feature if it must be updated
242 if (theFeature->isPreviewNeeded() || myIsFinish) {
244 std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures()) {
245 ModelAPI_ExecState aState = theFeature->data()->execState();
246 if (aFactory->validate(theFeature)) {
247 executeFeature(theFeature);
249 theFeature->eraseResults();
250 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
253 } else { // preview is not needed => make state Done
254 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated) {
255 theFeature->data()->execState(ModelAPI_StateDone);
256 if (aJustUpdated) // store that it must be updated on finish
257 myJustUpdated.insert(theFeature);
262 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
264 // make updated and redisplay all results
265 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
266 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
267 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
268 for (; aRIter != aResults.cend(); aRIter++) {
269 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
270 aRes->data()->execState(theState);
271 if (theFeature->data()->updateID() > aRes->data()->updateID()) {
272 aRes->data()->setUpdateID(theFeature->data()->updateID());
274 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
276 // to redisplay "presentable" feature (for ex. distance constraint)
277 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
278 theFeature->data()->execState(theState);
281 /// Updates the state by the referenced object: if something bad with it, set state for this one
282 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
285 ModelAPI_ExecState aRefState = theTarget->data()->execState();
286 if (aRefState == ModelAPI_StateMustBeUpdated) {
287 if (theCurrent == ModelAPI_StateDone)
288 return ModelAPI_StateMustBeUpdated;
289 } else if (aRefState != ModelAPI_StateDone) {
290 return ModelAPI_StateInvalidArgument;
296 void Model_Update::updateArguments(FeaturePtr theFeature) {
297 // perform this method also for disabled features: to make "not done" state for
298 // featuers referenced to the active and modified features
300 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
302 ModelAPI_ExecState aState = theFeature->data()->execState();
303 if (aState == ModelAPI_StateExecFailed) { // try again failed feature: issue 577
304 aState = ModelAPI_StateMustBeUpdated;
306 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
307 aState = ModelAPI_StateMustBeUpdated;
308 // check the parameters state
310 std::list<AttributePtr> aDoubles =
311 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
312 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
313 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
314 AttributeDoublePtr aDouble =
315 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
316 if (aDouble.get() && !aDouble->text().empty()) {
317 if (myIsParamUpdated) {
318 ModelAPI_AttributeEvalMessage::send(aDouble, this);
320 if (aDouble->expressionInvalid()) {
321 aState = ModelAPI_StateInvalidArgument;
327 std::list<AttributePtr> anAttributes =
328 theFeature->data()->attributes(GeomDataAPI_Point::typeId());
329 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
330 for(; anIter != anAttributes.end(); anIter++) {
331 AttributePointPtr aPointAttribute =
332 std::dynamic_pointer_cast<GeomDataAPI_Point>(*anIter);
333 if (aPointAttribute.get()) {
334 if (myIsParamUpdated) {
335 ModelAPI_AttributeEvalMessage::send(aPointAttribute, this);
337 if ((!aPointAttribute->textX().empty() && aPointAttribute->expressionInvalid(0)) ||
338 (!aPointAttribute->textY().empty() && aPointAttribute->expressionInvalid(1)) ||
339 (!aPointAttribute->textZ().empty() && aPointAttribute->expressionInvalid(2)))
340 aState = ModelAPI_StateInvalidArgument;
346 std::list<AttributePtr> anAttributes =
347 theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
348 std::list<AttributePtr>::iterator anIter = anAttributes.begin();
349 for(; anIter != anAttributes.end(); anIter++) {
350 AttributePoint2DPtr aPoint2DAttribute =
351 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
352 if (aPoint2DAttribute.get()) {
353 if (myIsParamUpdated) {
354 ModelAPI_AttributeEvalMessage::send(aPoint2DAttribute, this);
356 if ((!aPoint2DAttribute->textX().empty() && aPoint2DAttribute->expressionInvalid(0)) ||
357 (!aPoint2DAttribute->textY().empty() && aPoint2DAttribute->expressionInvalid(1)))
358 aState = ModelAPI_StateInvalidArgument;
363 //if (aState == ModelAPI_StateDone) {// all referenced objects are ready to be used
364 //std::cout<<"Execute feature "<<theFeature->getKind()<<std::endl;
365 // before execution update the selection attributes if any
366 list<AttributePtr> aRefs =
367 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
368 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
369 for (; aRefsIter != aRefs.end(); aRefsIter++) {
370 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
371 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
372 ObjectPtr aContext = aSel->context();
373 // update argument onlt if the referenced object is changed
374 if (aContext.get() && !aContext->isDisabled() &&
375 aContext->data()->updateID() > theFeature->data()->updateID()) {
376 if (aState == ModelAPI_StateDone)
377 aState = ModelAPI_StateMustBeUpdated;
378 if (!aSel->update()) { // this must be done on execution since it may be long operation
379 if (!aFactory->isNotObligatory(theFeature->getKind(), theFeature->data()->id(aSel)) &&
380 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
381 aState = ModelAPI_StateInvalidArgument;
385 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
386 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
387 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
388 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
389 for(int a = aSel->size() - 1; a >= 0; a--) {
390 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
391 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
393 ObjectPtr aContext = aSelAttr->context();
394 // update argument onlt if the referenced object is changed
395 if (aContext.get() && !aContext->isDisabled() &&
396 aContext->data()->updateID() > theFeature->data()->updateID()) {
397 if (aState == ModelAPI_StateDone)
398 aState = ModelAPI_StateMustBeUpdated;
399 if (!aSelAttr->update()) {
400 if (!aFactory->isNotObligatory(
401 theFeature->getKind(), theFeature->data()->id(aSel)) &&
402 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
403 aState = ModelAPI_StateInvalidArgument;
409 // check all references: if referenced objects are updated, this object also must be updated
410 // also check state of referenced objects: if they are not ready, inherit corresponding state
411 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefsObj;
412 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theFeature->data());
413 aData->referencesToObjects(aRefsObj);
414 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefsObj.begin();
415 for(; aRef != aRefsObj.end(); aRef++) {
416 std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
417 for(; aRefObj != aRef->second.end(); aRefObj++) {
418 // if reference is null, it may mean that this reference is to other document
419 // the does not supported by RefList: parameters may be recomputed
420 if (!aRefObj->get() && theFeature->firstResult().get() &&
421 theFeature->firstResult()->groupName() == ModelAPI_ResultParameter::group()) {
422 if (aState == ModelAPI_StateDone)
423 aState = ModelAPI_StateMustBeUpdated;
424 } else if (myJustUpdated.find(*aRefObj) != myJustUpdated.end() ||
425 (aRefObj->get() && (*aRefObj)->data()->updateID() > theFeature->data()->updateID())) {
426 if (aState == ModelAPI_StateDone)
427 aState = ModelAPI_StateMustBeUpdated;
429 aState = stateByReference(*aRefObj, aState);
432 // composites sub-elements
433 CompositeFeaturePtr aCompos = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
434 // composite feature must be executed after sub-features execution
436 // number of subs can be changed in execution: like fillet
437 for(int a = 0; a < aCompos->numberOfSubs(); a++) {
438 FeaturePtr aSub = aCompos->subFeature(a);
439 if (myJustUpdated.find(aSub) != myJustUpdated.end() ||
440 (aSub.get() && aSub->data()->updateID() > theFeature->data()->updateID())) {
441 if (aState == ModelAPI_StateDone)
442 aState = ModelAPI_StateMustBeUpdated;
448 if (aState != ModelAPI_StateDone)
449 theFeature->data()->execState(aState);
452 void Model_Update::executeFeature(FeaturePtr theFeature)
454 // execute in try-catch to avoid internal problems of the feature
455 ModelAPI_ExecState aState = ModelAPI_StateDone;
456 theFeature->data()->execState(ModelAPI_StateDone);
458 theFeature->execute();
459 myJustUpdated.erase(theFeature);
460 if (theFeature->data()->execState() != ModelAPI_StateDone) {
461 aState = ModelAPI_StateExecFailed;
463 aState = ModelAPI_StateDone;
464 myWaitForFinish.insert(theFeature);
467 aState = ModelAPI_StateExecFailed;
469 "Feature " + theFeature->getKind() + " has failed during the execution");
471 if (aState != ModelAPI_StateDone) {
472 theFeature->eraseResults();
474 theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
475 redisplayWithResults(theFeature, aState);