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 for(; anObjIter != anObjs.cend(); anObjIter++) {
90 // created objects are always must be up to date (python box feature)
91 // and updated not in internal uptation chain
92 if (theMessage->eventID() == kCreatedEvent) {
93 myJustCreated.insert(*anObjIter);
94 } else if (myJustCreated.find(*anObjIter) == myJustCreated.end()) { // moved and updated
95 myJustUpdated.insert(*anObjIter);
98 // this event is for solver update, not here, do not react immideately
99 if (!(theMessage->eventID() == kMovedEvent))
100 processOperation(false);
101 } else if (theMessage->eventID() == kOpStartEvent) {
102 // we don't need the update only on operation start (caused problems in PartSet_Listener::processEvent)
103 isOperationChanged = true;
104 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
105 processOperation(true);
106 isOperationChanged = true;
108 if (isOperationChanged) {
109 // remove all macros before clearing all created
110 std::set<ObjectPtr>::iterator aCreatedIter = myJustCreated.begin();
111 for(; aCreatedIter != myJustCreated.end(); aCreatedIter++) {
112 FeaturePtr aFeature =
113 std::dynamic_pointer_cast<ModelAPI_Feature>(*aCreatedIter);
114 if (aFeature.get()) {
115 // execute not-previewed feature on "apply"
116 if (!aFeature->isPreviewNeeded() && (myJustCreated.find(aFeature) != myJustCreated.end() ||
117 myJustUpdated.find(aFeature) != myJustUpdated.end())) {
118 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
119 if (aFactory->validate(aFeature)) {
120 executeFeature(aFeature);
123 // remove macro on apply
124 if (aFeature->isMacro()) {
125 aFeature->document()->removeFeature(aFeature);
129 myJustCreated.clear();
130 myJustUpdated.clear();
134 void Model_Update::processOperation(const bool theTotalUpdate)
136 // the hardcode (DBC asked): hide the sketch referenced by extrusion on apply
137 std::set<std::shared_ptr<ModelAPI_Object> >::iterator aFIter;
138 for(aFIter = myJustCreated.begin(); aFIter != myJustCreated.end(); aFIter++)
140 FeaturePtr aF = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
141 if (aF && aF->data().get() && aF->getKind() == "Extrusion") {
142 AttributeSelectionListPtr aBase = aF->selectionList("base");
144 for(int a = aBase->size() - 1; a >= 0; a--) {
145 ResultPtr aSketchRes = aBase->value(a)->context();
147 aSketchRes->setDisplayed(false);
153 // perform update of everything if needed
157 bool isAutomaticChanged = false;
159 if (theTotalUpdate && !myIsAutomatic) { // Apply button now works as "Rebuild"
160 isAutomaticChanged = true;
161 myIsAutomatic = true;
164 updateInDoc(ModelAPI_Session::get()->moduleDocument());
166 if (isAutomaticChanged) myIsAutomatic = false;
167 myIsExecuted = false;
169 // flush to update display
170 static Events_Loop* aLoop = Events_Loop::loop();
171 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
172 aLoop->flush(EVENT_DISP);
176 void Model_Update::updateInDoc(std::shared_ptr<ModelAPI_Document> theDoc)
178 std::set<FeaturePtr> alreadyProcessed; // features that are processed before others
179 // all features one by one
180 Model_Objects* anObjs = std::dynamic_pointer_cast<Model_Document>(theDoc)->objects();
182 FeaturePtr aFeatureIter = anObjs->firstFeature();
183 for (; aFeatureIter.get(); aFeatureIter = anObjs->nextFeature(aFeatureIter)) {
184 if (aFeatureIter && alreadyProcessed.find(aFeatureIter) == alreadyProcessed.end()) {
185 // update selection and parameters attributes first, before sub-features analysis (sketch plane)
186 updateArguments(aFeatureIter);
187 // composite feature must be executed after sub-features execution
188 CompositeFeaturePtr aComposite =
189 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeatureIter);
191 // number of subs can be changed in execution: like fillet
192 for(int a = 0; a < aComposite->numberOfSubs(); a++) {
193 FeaturePtr aSub = aComposite->subFeature(a);
195 alreadyProcessed.insert(aSub);
199 updateFeature(aFeatureIter);
200 // update the document results recursively
201 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeatureIter->results();
202 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
203 for (; aRIter != aResults.cend(); aRIter++) {
204 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRIter);
206 if (!aPart->isDisabled() && aPart->isActivated()) {
207 updateInDoc(aPart->partDoc());
215 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
217 // make updated and redisplay all results
218 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
219 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
220 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
221 for (; aRIter != aResults.cend(); aRIter++) {
222 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
223 aRes->data()->execState(theState);
224 myJustUpdated.insert(aRes);
225 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
227 // to redisplay "presentable" feature (for ex. distance constraint)
228 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
229 theFeature->data()->execState(theState);
232 /// Updates the state by the referenced object: if something bad with it, set state for this one
233 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
236 ModelAPI_ExecState aRefState = theTarget->data()->execState();
237 if (aRefState == ModelAPI_StateMustBeUpdated) {
238 return ModelAPI_StateMustBeUpdated;
239 } else if (aRefState != ModelAPI_StateDone) {
240 return ModelAPI_StateInvalidArgument;
246 void Model_Update::updateArguments(FeaturePtr theFeature) {
247 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
249 if (theFeature->isDisabled()) // nothing to do with disabled feature
251 bool aJustUpdated = false;
252 ModelAPI_ExecState aState = ModelAPI_StateDone;
253 // check the parameters: values can be changed
254 std::list<AttributePtr> aDoubles =
255 theFeature->data()->attributes(ModelAPI_AttributeDouble::typeId());
256 std::list<AttributePtr>::iterator aDoubleIter = aDoubles.begin();
257 for(; aDoubleIter != aDoubles.end(); aDoubleIter++) {
258 AttributeDoublePtr aDouble =
259 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(*aDoubleIter);
260 if (aDouble.get() && !aDouble->text().empty()) {
262 if (ModelAPI_Tools::findVariable(aDouble->text(), aNewVal)) {
263 if (aNewVal != aDouble->value()) {
264 aDouble->setValue(aNewVal);
268 aState = ModelAPI_StateInvalidArgument;
273 if (aState == ModelAPI_StateDone) {// all referenced objects are ready to be used
274 //std::cout<<"Execute feature "<<theFeature->getKind()<<std::endl;
275 // before execution update the selection attributes if any
276 list<AttributePtr> aRefs =
277 theFeature->data()->attributes(ModelAPI_AttributeSelection::typeId());
278 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
279 for (; aRefsIter != aRefs.end(); aRefsIter++) {
280 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
281 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
282 if (!aSel->update()) { // this must be done on execution since it may be long operation
283 if (!aFactory->isNotObligatory(theFeature->getKind(), theFeature->data()->id(aSel)) &&
284 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
285 aState = ModelAPI_StateInvalidArgument;
288 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
289 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
290 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
291 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
292 for(int a = aSel->size() - 1; a >= 0; a--) {
293 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
294 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
296 if (!aSelAttr->update()) {
297 if (!aFactory->isNotObligatory(
298 theFeature->getKind(), theFeature->data()->id(aSel)) &&
299 aFactory->isCase(theFeature, theFeature->data()->id(aSel)))
300 aState = ModelAPI_StateInvalidArgument;
306 if (aJustUpdated && myJustCreated.find(theFeature) == myJustCreated.end())
307 myJustUpdated.insert(theFeature);
308 if (aState != ModelAPI_StateDone)
309 theFeature->data()->execState(aState);
312 void Model_Update::updateFeature(FeaturePtr theFeature)
314 // check all features this feature depended on (recursive call of updateFeature)
315 static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
317 if (theFeature->isDisabled()) // nothing to do with disabled feature
319 bool aJustUpdated = false;
322 if (theFeature->data()->execState() != ModelAPI_StateDone)
325 ModelAPI_ExecState aState = ModelAPI_StateDone;
327 // check all references: if referenced objects are updated, this object also must be updated
328 // also check state of referenced objects: if they are not ready, inherit corresponding state
329 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
330 std::shared_ptr<Model_Data> aData =
331 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
332 aData->referencesToObjects(aRefs);
333 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefs.begin();
334 for(; aRef != aRefs.end(); aRef++) {
335 std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
336 for(; aRefObj != aRef->second.end(); aRefObj++) {
337 if (myJustCreated.find(*aRefObj) != myJustCreated.end() ||
338 myJustUpdated.find(*aRefObj) != myJustUpdated.end()) {
341 aState = stateByReference(*aRefObj, aState);
345 // some arguments were changed, so, this feature must be updated
346 if (myJustCreated.find(theFeature) != myJustCreated.end()) {
350 if (myJustUpdated.find(theFeature) == myJustUpdated.end())
351 myJustUpdated.insert(theFeature);
353 aJustUpdated = myJustUpdated.find(theFeature) != myJustUpdated.end();
356 //std::cout<<"Update feature "<<theFeature->getKind()<<" must be updated = "<<aMustbeUpdated<<std::endl;
357 // execute feature if it must be updated
359 if ((std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures() ||
360 !theFeature->isPersistentResult()) && theFeature->isPreviewNeeded()) {
361 if (aFactory->validate(theFeature)) {
363 (myJustCreated.find(theFeature) != myJustCreated.end() ||
364 (myJustUpdated.find(theFeature) != myJustUpdated.end() &&
365 theFeature == theFeature->document()->currentFeature(false)) || // currently edited
366 !theFeature->isPersistentResult() /* execute quick, not persistent results */))
368 if (aState == ModelAPI_StateDone || aState == ModelAPI_StateMustBeUpdated) {
369 executeFeature(theFeature);
371 } else { // must be updatet, but not updated yet
372 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
373 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
374 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
375 for (; aRIter != aResults.cend(); aRIter++) {
376 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
377 aRes->data()->execState(ModelAPI_StateMustBeUpdated);
381 theFeature->eraseResults();
382 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
384 } else { // for automatically updated features (on abort, etc) it is necessary to redisplay anyway
385 redisplayWithResults(theFeature, ModelAPI_StateNothing);
391 void Model_Update::executeFeature(FeaturePtr theFeature)
393 // execute in try-catch to avoid internal problems of the feature
394 ModelAPI_ExecState aState = ModelAPI_StateDone;
395 theFeature->data()->execState(ModelAPI_StateDone);
397 theFeature->execute();
398 if (theFeature->data()->execState() != ModelAPI_StateDone) {
399 aState = ModelAPI_StateExecFailed;
401 aState = ModelAPI_StateDone;
404 aState = ModelAPI_StateExecFailed;
406 "Feature " + theFeature->getKind() + " has failed during the execution");
408 if (aState != ModelAPI_StateDone) {
409 theFeature->eraseResults();
411 redisplayWithResults(theFeature, aState);