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 <Events_Loop.h>
27 #include <Events_LongOp.h>
28 #include <Events_Error.h>
29 #include <Config_PropManager.h>
33 Model_Update MY_UPDATER_INSTANCE; /// the only one instance initialized on load of the library
35 Model_Update::Model_Update()
37 Events_Loop* aLoop = Events_Loop::loop();
38 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
39 aLoop->registerListener(this, kChangedEvent);
40 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
41 aLoop->registerListener(this, kRebuildEvent);
42 static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
43 aLoop->registerListener(this, kCreatedEvent);
44 static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
45 aLoop->registerListener(this, kUpdatedEvent);
46 static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
47 aLoop->registerListener(this, kMovedEvent);
48 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
49 aLoop->registerListener(this, kOpFinishEvent);
50 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
51 aLoop->registerListener(this, kOpAbortEvent);
52 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
53 aLoop->registerListener(this, kOpStartEvent);
55 Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild immediately",
56 Config_Prop::Boolean, "false");
58 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
61 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
63 static Events_Loop* aLoop = Events_Loop::loop();
64 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
65 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
66 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
67 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
68 static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
69 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
70 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
71 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
72 bool isOperationChanged = false;
73 if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
75 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
76 if (aPropVal != myIsAutomatic) { // something is changed
77 myIsAutomatic = aPropVal;
78 if (myIsAutomatic) // higher level of automatization => to rebuild
79 processOperation(false);
81 } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
82 processOperation(true);
83 } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent ||
84 theMessage->eventID() == kMovedEvent) {
85 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
86 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
87 const std::set<ObjectPtr>& anObjs = aMsg->objects();
88 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
89 bool isOnlyResults = true; // check that only results were changed: only redisplay is needed
90 for(; anObjIter != anObjs.cend(); anObjIter++) {
91 if (!std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIter).get()) {
92 isOnlyResults = false;
94 // created objects are always must be up to date (python box feature)
95 // and updated not in internal uptation chain
96 if (theMessage->eventID() == kCreatedEvent) {
97 myJustCreated.insert(*anObjIter);
98 } else if (myJustCreated.find(*anObjIter) == myJustCreated.end()) { // moved and updated
99 myJustUpdated.insert(*anObjIter);
102 // this event is for solver update, not here, do not react immideately
103 if (!isOnlyResults && !(theMessage->eventID() == kMovedEvent))
104 processOperation(false);
105 } else if (theMessage->eventID() == kOpStartEvent) {
106 // we don't need the update only on operation start (caused problems in PartSet_Listener::processEvent)
107 isOperationChanged = true;
108 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
109 processOperation(true, theMessage->eventID() == kOpFinishEvent);
110 isOperationChanged = true;
112 if (isOperationChanged) {
113 // remove all macros before clearing all created and execute all not-previewed
114 std::set<ObjectPtr>::iterator aCreatedIter = myJustCreated.begin();
115 std::set<ObjectPtr>::iterator anUpdatedIter = myJustUpdated.begin();
116 for(; aCreatedIter != myJustCreated.end() || anUpdatedIter != myJustUpdated.end();
117 aCreatedIter == myJustCreated.end() ? anUpdatedIter++ : aCreatedIter++) {
118 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*
119 (aCreatedIter == myJustCreated.end() ? anUpdatedIter : aCreatedIter));
120 if (aFeature.get()) {
121 // execute not-previewed feature on "apply"
122 if (!aFeature->isPreviewNeeded() && (myJustCreated.find(aFeature) != myJustCreated.end() ||
123 myJustUpdated.find(aFeature) != myJustUpdated.end())) {
124 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
125 if (aFactory->validate(aFeature)) {
126 executeFeature(aFeature);
129 // remove macro on apply
130 if (aFeature->isMacro()) {
131 aFeature->document()->removeFeature(aFeature);
135 myJustCreated.clear();
136 myJustUpdated.clear();
140 void Model_Update::processOperation(const bool theTotalUpdate, const bool theFinish)
143 // the hardcode (DBC asked): hide the sketch referenced by extrusion on apply
144 std::set<std::shared_ptr<ModelAPI_Object> >::iterator aFIter;
145 for(aFIter = myJustCreated.begin(); aFIter != myJustCreated.end(); aFIter++)
147 FeaturePtr aF = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
148 if (aF && aF->data()->isValid() && aF->getKind() == "Extrusion") {
149 AttributeSelectionListPtr aBase = aF->selectionList("base");
151 for(int a = aBase->size() - 1; a >= 0; a--) {
152 ResultPtr aSketchRes = aBase->value(a)->context();
154 aSketchRes->setDisplayed(false);
161 // perform update of everything if needed
165 bool isAutomaticChanged = false;
167 if (theTotalUpdate && !myIsAutomatic) { // Apply button now works as "Rebuild"
168 isAutomaticChanged = true;
169 myIsAutomatic = true;
172 updateInDoc(ModelAPI_Session::get()->moduleDocument());
174 if (isAutomaticChanged) myIsAutomatic = false;
175 myIsExecuted = false;
177 // flush to update display
178 static Events_Loop* aLoop = Events_Loop::loop();
179 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
180 aLoop->flush(EVENT_DISP);
184 void Model_Update::updateInDoc(std::shared_ptr<ModelAPI_Document> theDoc)
186 std::set<FeaturePtr> alreadyProcessed; // features that are processed before others
187 // all features one by one
188 Model_Objects* anObjs = std::dynamic_pointer_cast<Model_Document>(theDoc)->objects();
190 FeaturePtr aFeatureIter = anObjs->firstFeature();
191 for (; aFeatureIter.get(); aFeatureIter = anObjs->nextFeature(aFeatureIter)) {
192 if (aFeatureIter && alreadyProcessed.find(aFeatureIter) == alreadyProcessed.end()) {
193 // update selection and parameters attributes first, before sub-features analysis (sketch plane)
194 updateArguments(aFeatureIter);
195 // composite feature must be executed after sub-features execution
196 CompositeFeaturePtr aComposite =
197 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeatureIter);
199 // number of subs can be changed in execution: like fillet
200 for(int a = 0; a < aComposite->numberOfSubs(); a++) {
201 FeaturePtr aSub = aComposite->subFeature(a);
202 updateArguments(aSub);
204 alreadyProcessed.insert(aSub);
208 updateFeature(aFeatureIter);
209 // update the document results recursively
210 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeatureIter->results();
211 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
212 for (; aRIter != aResults.cend(); aRIter++) {
213 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRIter);
215 if (!aPart->isDisabled() && aPart->isActivated()) {
216 updateInDoc(aPart->partDoc());
224 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
226 // make updated and redisplay all results
227 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
228 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
229 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
230 for (; aRIter != aResults.cend(); aRIter++) {
231 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
232 aRes->data()->execState(theState);
233 myJustUpdated.insert(aRes);
234 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
236 // to redisplay "presentable" feature (for ex. distance constraint)
237 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
238 theFeature->data()->execState(theState);
241 /// Updates the state by the referenced object: if something bad with it, set state for this one
242 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
245 ModelAPI_ExecState aRefState = theTarget->data()->execState();
246 if (aRefState == ModelAPI_StateMustBeUpdated) {
247 return ModelAPI_StateMustBeUpdated;
248 } else if (aRefState != ModelAPI_StateDone) {
249 return ModelAPI_StateInvalidArgument;
255 void Model_Update::updateArguments(FeaturePtr theFeature) {
256 // perform this method also for disabled features: to make "not done" state for
257 // featuers referenced to the active and modified features
259 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
261 bool aJustUpdated = false;
262 ModelAPI_ExecState aState = theFeature->data()->execState();
263 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
264 aState = ModelAPI_StateMustBeUpdated;
265 // check the parameters state
266 std::list<AttributePtr> aDoubles =
267 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
268 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
269 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
270 AttributeDoublePtr aDouble =
271 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
272 if (aDouble.get() && !aDouble->text().empty()) {
273 if (aDouble->expressionInvalid()) {
274 aState = ModelAPI_StateInvalidArgument;
279 //if (aState == ModelAPI_StateDone) {// all referenced objects are ready to be used
280 //std::cout<<"Execute feature "<<theFeature->getKind()<<std::endl;
281 // before execution update the selection attributes if any
282 list<AttributePtr> aRefs =
283 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
284 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
285 for (; aRefsIter != aRefs.end(); aRefsIter++) {
286 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
287 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
288 ObjectPtr aContext = aSel->context();
289 // update argument onlt if the referenced object is changed
290 if (aContext.get() && isUpdated(aContext) && !aContext->isDisabled()) {
291 if (aState == ModelAPI_StateDone)
292 aState = ModelAPI_StateMustBeUpdated;
293 if (!aSel->update()) { // this must be done on execution since it may be long operation
294 if (!aFactory->isNotObligatory(theFeature->getKind(), theFeature->data()->id(aSel)) &&
295 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
296 aState = ModelAPI_StateInvalidArgument;
300 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
301 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
302 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
303 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
304 for(int a = aSel->size() - 1; a >= 0; a--) {
305 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
306 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
308 ObjectPtr aContext = aSelAttr->context();
309 // update argument onlt if the referenced object is changed
310 if (aContext.get() && isUpdated(aContext) && !aContext->isDisabled()) {
311 if (aState == ModelAPI_StateDone)
312 aState = ModelAPI_StateMustBeUpdated;
313 if (!aSelAttr->update()) {
314 if (!aFactory->isNotObligatory(
315 theFeature->getKind(), theFeature->data()->id(aSel)) &&
316 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
317 aState = ModelAPI_StateInvalidArgument;
324 if (aJustUpdated && myJustCreated.find(theFeature) == myJustCreated.end())
325 myJustUpdated.insert(theFeature);
326 if (aState != ModelAPI_StateDone)
327 theFeature->data()->execState(aState);
330 void Model_Update::updateFeature(FeaturePtr theFeature)
332 // check all features this feature depended on (recursive call of updateFeature)
333 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
335 if (theFeature->isDisabled() || // nothing to do with disabled feature
336 theFeature->data()->execState() == ModelAPI_StateInvalidArgument)
338 bool aJustUpdated = false;
341 if (myIsAutomatic && theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
344 ModelAPI_ExecState aState = ModelAPI_StateDone;
346 // check all references: if referenced objects are updated, this object also must be updated
347 // also check state of referenced objects: if they are not ready, inherit corresponding state
348 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
349 std::shared_ptr<Model_Data> aData =
350 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
351 aData->referencesToObjects(aRefs);
352 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefs.begin();
353 for(; aRef != aRefs.end(); aRef++) {
354 std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
355 for(; aRefObj != aRef->second.end(); aRefObj++) {
356 if (myJustCreated.find(*aRefObj) != myJustCreated.end() ||
357 myJustUpdated.find(*aRefObj) != myJustUpdated.end()) {
360 aState = stateByReference(*aRefObj, aState);
364 // some arguments were changed, so, this feature must be updated
365 if (myJustCreated.find(theFeature) != myJustCreated.end()) {
369 if (myJustUpdated.find(theFeature) == myJustUpdated.end())
370 myJustUpdated.insert(theFeature);
372 aJustUpdated = myJustUpdated.find(theFeature) != myJustUpdated.end();
375 //std::cout<<"Update feature "<<theFeature->getKind()<<" must be updated = "<<aMustbeUpdated<<std::endl;
376 // execute feature if it must be updated
378 if (theFeature->isPreviewNeeded()) {
379 if (std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures() ||
380 !theFeature->isPersistentResult()) {
381 if (aFactory->validate(theFeature)) {
382 if (myIsAutomatic || !theFeature->isPersistentResult() /* execute quick, not persistent results */
383 || (isUpdated(theFeature) &&
384 (theFeature == theFeature->document()->currentFeature(false) ||
385 theFeature->document()->currentFeature(false)->isMacro()))) // currently edited
387 if (aState == ModelAPI_StateDone || aState == ModelAPI_StateMustBeUpdated) {
388 executeFeature(theFeature);
390 } else { // must be updatet, but not updated yet
391 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
392 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
393 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
394 for (; aRIter != aResults.cend(); aRIter++) {
395 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
396 aRes->data()->execState(ModelAPI_StateMustBeUpdated);
400 theFeature->eraseResults();
401 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
403 } else { // for automatically updated features (on abort, etc) it is necessary to redisplay anyway
404 redisplayWithResults(theFeature, ModelAPI_StateNothing);
406 } else { // preview is not needed => make state Done
407 if (theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
408 theFeature->data()->execState(ModelAPI_StateDone);
414 void Model_Update::executeFeature(FeaturePtr theFeature)
416 // execute in try-catch to avoid internal problems of the feature
417 ModelAPI_ExecState aState = ModelAPI_StateDone;
418 theFeature->data()->execState(ModelAPI_StateDone);
420 theFeature->execute();
421 if (theFeature->data()->execState() != ModelAPI_StateDone) {
422 aState = ModelAPI_StateExecFailed;
424 aState = ModelAPI_StateDone;
427 aState = ModelAPI_StateExecFailed;
429 "Feature " + theFeature->getKind() + " has failed during the execution");
431 if (aState != ModelAPI_StateDone) {
432 theFeature->eraseResults();
434 redisplayWithResults(theFeature, aState);
437 bool Model_Update::isUpdated(const ObjectPtr& theObj)
439 return myJustCreated.find(theObj) != myJustCreated.end() ||
440 myJustUpdated.find(theObj) != myJustUpdated.end();