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
114 std::set<ObjectPtr>::iterator aCreatedIter = myJustCreated.begin();
115 for(; aCreatedIter != myJustCreated.end(); aCreatedIter++) {
116 FeaturePtr aFeature =
117 std::dynamic_pointer_cast<ModelAPI_Feature>(*aCreatedIter);
118 if (aFeature.get()) {
119 // execute not-previewed feature on "apply"
120 if (!aFeature->isPreviewNeeded() && (myJustCreated.find(aFeature) != myJustCreated.end() ||
121 myJustUpdated.find(aFeature) != myJustUpdated.end())) {
122 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
123 if (aFactory->validate(aFeature)) {
124 executeFeature(aFeature);
127 // remove macro on apply
128 if (aFeature->isMacro()) {
129 aFeature->document()->removeFeature(aFeature);
133 myJustCreated.clear();
134 myJustUpdated.clear();
138 void Model_Update::processOperation(const bool theTotalUpdate, const bool theFinish)
141 // the hardcode (DBC asked): hide the sketch referenced by extrusion on apply
142 std::set<std::shared_ptr<ModelAPI_Object> >::iterator aFIter;
143 for(aFIter = myJustCreated.begin(); aFIter != myJustCreated.end(); aFIter++)
145 FeaturePtr aF = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
146 if (aF && aF->data().get() && aF->getKind() == "Extrusion") {
147 AttributeSelectionListPtr aBase = aF->selectionList("base");
149 for(int a = aBase->size() - 1; a >= 0; a--) {
150 ResultPtr aSketchRes = aBase->value(a)->context();
152 aSketchRes->setDisplayed(false);
159 // perform update of everything if needed
163 bool isAutomaticChanged = false;
165 if (theTotalUpdate && !myIsAutomatic) { // Apply button now works as "Rebuild"
166 isAutomaticChanged = true;
167 myIsAutomatic = true;
170 updateInDoc(ModelAPI_Session::get()->moduleDocument());
172 if (isAutomaticChanged) myIsAutomatic = false;
173 myIsExecuted = false;
175 // flush to update display
176 static Events_Loop* aLoop = Events_Loop::loop();
177 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
178 aLoop->flush(EVENT_DISP);
182 void Model_Update::updateInDoc(std::shared_ptr<ModelAPI_Document> theDoc)
184 std::set<FeaturePtr> alreadyProcessed; // features that are processed before others
185 // all features one by one
186 Model_Objects* anObjs = std::dynamic_pointer_cast<Model_Document>(theDoc)->objects();
188 FeaturePtr aFeatureIter = anObjs->firstFeature();
189 for (; aFeatureIter.get(); aFeatureIter = anObjs->nextFeature(aFeatureIter)) {
190 if (aFeatureIter && alreadyProcessed.find(aFeatureIter) == alreadyProcessed.end()) {
191 // update selection and parameters attributes first, before sub-features analysis (sketch plane)
192 updateArguments(aFeatureIter);
193 // composite feature must be executed after sub-features execution
194 CompositeFeaturePtr aComposite =
195 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeatureIter);
197 // number of subs can be changed in execution: like fillet
198 for(int a = 0; a < aComposite->numberOfSubs(); a++) {
199 FeaturePtr aSub = aComposite->subFeature(a);
200 updateArguments(aSub);
202 alreadyProcessed.insert(aSub);
206 updateFeature(aFeatureIter);
207 // update the document results recursively
208 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeatureIter->results();
209 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
210 for (; aRIter != aResults.cend(); aRIter++) {
211 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRIter);
213 if (!aPart->isDisabled() && aPart->isActivated()) {
214 updateInDoc(aPart->partDoc());
222 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
224 // make updated and redisplay all results
225 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
226 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
227 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
228 for (; aRIter != aResults.cend(); aRIter++) {
229 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
230 aRes->data()->execState(theState);
231 myJustUpdated.insert(aRes);
232 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
234 // to redisplay "presentable" feature (for ex. distance constraint)
235 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
236 theFeature->data()->execState(theState);
239 /// Updates the state by the referenced object: if something bad with it, set state for this one
240 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
243 ModelAPI_ExecState aRefState = theTarget->data()->execState();
244 if (aRefState == ModelAPI_StateMustBeUpdated) {
245 return ModelAPI_StateMustBeUpdated;
246 } else if (aRefState != ModelAPI_StateDone) {
247 return ModelAPI_StateInvalidArgument;
253 void Model_Update::updateArguments(FeaturePtr theFeature) {
254 // perform this method also for disabled features: to make "not done" state for
255 // featuers referenced to the active and modified features
257 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
259 bool aJustUpdated = false;
260 ModelAPI_ExecState aState = theFeature->data()->execState();
261 if (aState == ModelAPI_StateInvalidArgument) // a chance to be corrected
262 aState = ModelAPI_StateMustBeUpdated;
263 // check the parameters: values can be changed
264 /* parameters evaluator now does this
265 std::list<AttributePtr> aDoubles =
266 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
267 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
268 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
269 AttributeDoublePtr aDouble =
270 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
271 if (aDouble.get() && !aDouble->text().empty()) {
273 if (ModelAPI_Tools::findVariable(aDouble->text(), aNewVal)) {
274 if (aNewVal != aDouble->value()) {
275 aDouble->setValue(aNewVal);
279 aState = ModelAPI_StateInvalidArgument;
285 //if (aState == ModelAPI_StateDone) {// all referenced objects are ready to be used
286 //std::cout<<"Execute feature "<<theFeature->getKind()<<std::endl;
287 // before execution update the selection attributes if any
288 list<AttributePtr> aRefs =
289 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
290 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
291 for (; aRefsIter != aRefs.end(); aRefsIter++) {
292 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
293 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
294 ObjectPtr aContext = aSel->context();
295 // update argument onlt if the referenced object is changed
296 if (aContext.get() && isUpdated(aContext) && !aContext->isDisabled()) {
297 if (aState == ModelAPI_StateDone)
298 aState = ModelAPI_StateMustBeUpdated;
299 if (!aSel->update()) { // this must be done on execution since it may be long operation
300 if (!aFactory->isNotObligatory(theFeature->getKind(), theFeature->data()->id(aSel)) &&
301 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
302 aState = ModelAPI_StateInvalidArgument;
306 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
307 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
308 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
309 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
310 for(int a = aSel->size() - 1; a >= 0; a--) {
311 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
312 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
314 ObjectPtr aContext = aSelAttr->context();
315 // update argument onlt if the referenced object is changed
316 if (aContext.get() && isUpdated(aContext) && !aContext->isDisabled()) {
317 if (aState == ModelAPI_StateDone)
318 aState = ModelAPI_StateMustBeUpdated;
319 if (!aSelAttr->update()) {
320 if (!aFactory->isNotObligatory(
321 theFeature->getKind(), theFeature->data()->id(aSel)) &&
322 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
323 aState = ModelAPI_StateInvalidArgument;
330 if (aJustUpdated && myJustCreated.find(theFeature) == myJustCreated.end())
331 myJustUpdated.insert(theFeature);
332 if (aState != ModelAPI_StateDone)
333 theFeature->data()->execState(aState);
336 void Model_Update::updateFeature(FeaturePtr theFeature)
338 // check all features this feature depended on (recursive call of updateFeature)
339 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
341 if (theFeature->isDisabled() || // nothing to do with disabled feature
342 theFeature->data()->execState() == ModelAPI_StateInvalidArgument)
344 bool aJustUpdated = false;
347 if (myIsAutomatic && theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
350 ModelAPI_ExecState aState = ModelAPI_StateDone;
352 // check all references: if referenced objects are updated, this object also must be updated
353 // also check state of referenced objects: if they are not ready, inherit corresponding state
354 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
355 std::shared_ptr<Model_Data> aData =
356 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
357 aData->referencesToObjects(aRefs);
358 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefs.begin();
359 for(; aRef != aRefs.end(); aRef++) {
360 std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
361 for(; aRefObj != aRef->second.end(); aRefObj++) {
362 if (myJustCreated.find(*aRefObj) != myJustCreated.end() ||
363 myJustUpdated.find(*aRefObj) != myJustUpdated.end()) {
366 aState = stateByReference(*aRefObj, aState);
370 // some arguments were changed, so, this feature must be updated
371 if (myJustCreated.find(theFeature) != myJustCreated.end()) {
375 if (myJustUpdated.find(theFeature) == myJustUpdated.end())
376 myJustUpdated.insert(theFeature);
378 aJustUpdated = myJustUpdated.find(theFeature) != myJustUpdated.end();
381 //std::cout<<"Update feature "<<theFeature->getKind()<<" must be updated = "<<aMustbeUpdated<<std::endl;
382 // execute feature if it must be updated
384 if ((std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures() ||
385 !theFeature->isPersistentResult()) && theFeature->isPreviewNeeded()) {
386 if (aFactory->validate(theFeature)) {
387 if (myIsAutomatic || !theFeature->isPersistentResult() /* execute quick, not persistent results */
388 || (isUpdated(theFeature) &&
389 theFeature == theFeature->document()->currentFeature(false))) // currently edited
391 if (aState == ModelAPI_StateDone || aState == ModelAPI_StateMustBeUpdated) {
392 executeFeature(theFeature);
394 } else { // must be updatet, but not updated yet
395 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
396 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
397 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
398 for (; aRIter != aResults.cend(); aRIter++) {
399 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
400 aRes->data()->execState(ModelAPI_StateMustBeUpdated);
404 theFeature->eraseResults();
405 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
407 } else { // for automatically updated features (on abort, etc) it is necessary to redisplay anyway
408 redisplayWithResults(theFeature, ModelAPI_StateNothing);
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();