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 <ModelAPI_Feature.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Document.h>
13 #include <ModelAPI_Events.h>
14 #include <ModelAPI_AttributeReference.h>
15 #include <ModelAPI_AttributeRefList.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeSelection.h>
18 #include <ModelAPI_AttributeSelectionList.h>
19 #include <ModelAPI_Result.h>
20 #include <ModelAPI_Validator.h>
21 #include <ModelAPI_CompositeFeature.h>
22 #include <ModelAPI_Session.h>
23 #include <Events_Loop.h>
24 #include <Events_LongOp.h>
25 #include <Events_Error.h>
26 #include <Config_PropManager.h>
30 Model_Update MY_UPDATER_INSTANCE; /// the only one instance initialized on load of the library
32 Model_Update::Model_Update()
34 Events_Loop* aLoop = Events_Loop::loop();
35 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
36 aLoop->registerListener(this, kChangedEvent);
37 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
38 aLoop->registerListener(this, kRebuildEvent);
39 static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
40 aLoop->registerListener(this, kCreatedEvent);
41 static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
42 aLoop->registerListener(this, kUpdatedEvent);
43 static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
44 aLoop->registerListener(this, kMovedEvent);
45 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
46 aLoop->registerListener(this, kOpFinishEvent);
47 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
48 aLoop->registerListener(this, kOpAbortEvent);
49 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
50 aLoop->registerListener(this, kOpStartEvent);
52 Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild immediately",
53 Config_Prop::Bool, "false");
54 isAutomatic = Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
57 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
59 static Events_Loop* aLoop = Events_Loop::loop();
60 static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
61 static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
62 static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
63 static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
64 static const Events_ID kMovedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
65 static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
66 static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
67 static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
68 bool isAutomaticChanged = false;
69 if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
71 Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
72 } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
73 if (isAutomatic == false) {
74 isAutomaticChanged = true;
77 } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent ||
78 theMessage->eventID() == kMovedEvent) {
79 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
80 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
81 const std::set<ObjectPtr>& anObjs = aMsg->objects();
82 std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
83 for(; anObjIter != anObjs.cend(); anObjIter++) {
84 myJustCreatedOrUpdated.insert(*anObjIter);
85 (*anObjIter)->data()->mustBeUpdated(true); // object must be updated because it was changed
87 if (theMessage->eventID() == kMovedEvent)
88 return; // this event is for solver update, not here
89 } else if (theMessage->eventID() == kOpStartEvent) {
90 myJustCreatedOrUpdated.clear();
91 return; // we don't need the update only on operation start (caused problems in PartSet_Listener::processEvent)
92 } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
93 if (isAutomatic == false) { // Apply button now works as "Rebuild"
94 isAutomaticChanged = true;
97 // the hardcode (DBC asked): hide the sketch referenced by extrusion on apply
98 if (theMessage->eventID() == kOpFinishEvent) {
99 std::set<std::shared_ptr<ModelAPI_Object> >::iterator aFIter;
100 for(aFIter = myJustCreatedOrUpdated.begin(); aFIter != myJustCreatedOrUpdated.end(); aFIter++)
102 FeaturePtr aF = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
103 if (aF && aF->getKind() == "Extrusion") {
104 if (aF->selection("extrusion_face")) {
105 ResultPtr aSketchRes = aF->selection("extrusion_face")->context();
107 static Events_ID HIDE_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TOHIDE);
108 ModelAPI_EventCreator::get()->sendUpdated(aSketchRes, HIDE_DISP);
117 return; // nothing to do: it is executed now
119 //Events_LongOp::start(this);
121 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
122 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
123 if (aMsg) myInitial = aMsg->objects();
127 // iterate all documents: features in Root first, then - subs
128 updateInDoc(ModelAPI_Session::get()->moduleDocument());
131 // flush to update display
132 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
133 aLoop->flush(EVENT_DISP);
134 //Events_LongOp::end(this);
135 if (isAutomaticChanged) isAutomatic = false;
137 if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
138 myJustCreatedOrUpdated.clear();
144 void Model_Update::updateInDoc(std::shared_ptr<ModelAPI_Document> theDoc)
146 // all features one by one
147 int aNbFeatures = theDoc->size(ModelAPI_Feature::group(), true);
148 for (int aFIndex = 0; aFIndex < aNbFeatures; aFIndex++) {
149 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(
150 theDoc->object(ModelAPI_Feature::group(), aFIndex, true));
152 updateFeature(aFeature);
154 // all sub-documents one by one
155 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(theDoc);
157 const std::set<std::string>& aSubs = aDoc->subDocuments();
158 for(std::set<std::string>::iterator aSub = aSubs.begin(); aSub != aSubs.end(); aSub++) {
159 DocumentPtr aSubDoc = theDoc->subDocument(*aSub);
161 updateInDoc(aSubDoc);
167 void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
169 // make updated and redisplay all results
170 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
171 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
172 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
173 for (; aRIter != aResults.cend(); aRIter++) {
174 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
175 aRes->data()->execState(theState);
176 myUpdated[aRes] = true;
177 ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
179 // to redisplay "presentable" feature (for ex. distance constraint)
180 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
181 theFeature->data()->execState(theState);
184 /// Updates the state by the referenced object: if something bad with it, set state for this one
185 ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecState theCurrent)
188 ModelAPI_ExecState aRefState = theTarget->data()->execState();
189 if (aRefState == ModelAPI_StateMustBeUpdated) {
190 return ModelAPI_StateMustBeUpdated;
191 } else if (aRefState != ModelAPI_StateDone) {
192 return ModelAPI_StateInvalidArgument;
198 bool Model_Update::updateFeature(FeaturePtr theFeature)
200 // check it is already processed
201 if (myUpdated.find(theFeature) != myUpdated.end())
202 return myUpdated[theFeature];
203 // check all features this feature depended on (recursive call of updateFeature)
204 ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
205 bool aMustbeUpdated = myInitial.find(theFeature) != myInitial.end();
206 if (theFeature) { // only real feature contains references to other objects
207 if (theFeature->data()->execState() != ModelAPI_StateDone)
208 aMustbeUpdated = true;
210 // composite feature must be executed after sub-features execution
211 CompositeFeaturePtr aComposite =
212 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
214 int aSubsNum = aComposite->numberOfSubs();
215 for(int a = 0; a < aSubsNum; a++) {
216 if (updateFeature(aComposite->subFeature(a)))
217 aMustbeUpdated = true;
220 ModelAPI_ExecState aState = ModelAPI_StateDone;
221 // check all references: if referenced objects are updated, this object also must be updated
222 // also check state of referenced objects: if they are not ready, inherit corresponding state
223 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
224 std::shared_ptr<Model_Data> aData =
225 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
226 aData->referencesToObjects(aRefs);
227 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefs.begin();
228 for(; aRef != aRefs.end(); aRef++) {
229 std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
230 for(; aRefObj != aRef->second.end(); aRefObj++) {
231 if (updateObject(*aRefObj)) {
232 aMustbeUpdated = true;
234 aState = stateByReference(*aRefObj, aState);
238 //std::cout<<"Update feature "<<theFeature->getKind()<<" must be updated = "<<aMustbeUpdated<<std::endl;
239 // execute feature if it must be updated
240 if (aMustbeUpdated) {
241 if (std::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures() ||
242 !theFeature->isPersistentResult()) {
243 if (aFactory->validate(theFeature)) {
245 (myJustCreatedOrUpdated.find(theFeature) != myJustCreatedOrUpdated.end()) ||
246 !theFeature->isPersistentResult() /* execute quick, not persistent results */)
248 if (aState == ModelAPI_StateDone) {// all referenced objects are ready to be used
249 //std::cout<<"Execute feature "<<theFeature->getKind()<<std::endl;
250 // before execution update the selection attributes if any
251 list<AttributePtr> aRefs =
252 theFeature->data()->attributes(ModelAPI_AttributeSelection::type());
253 list<AttributePtr>::iterator aRefsIter = aRefs.begin();
254 for (; aRefsIter != aRefs.end(); aRefsIter++) {
255 std::shared_ptr<ModelAPI_AttributeSelection> aSel =
256 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
257 if (!aSel->update()) { // this must be done on execution since it may be long operation
258 if (!aFactory->isNotObligatory(theFeature->getKind(), theFeature->data()->id(aSel)))
259 aState = ModelAPI_StateInvalidArgument;
262 aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::type());
263 for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
264 std::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
265 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
266 for(int a = aSel->size() - 1; a >= 0; a--) {
267 std::shared_ptr<ModelAPI_AttributeSelection> aSelAttr =
268 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aSel->value(a));
270 if (!aSelAttr->update()) {
271 if (!aFactory->isNotObligatory(
272 theFeature->getKind(), theFeature->data()->id(aSel)))
273 aState = ModelAPI_StateInvalidArgument;
278 // for sketch after update of plane (by update of selection attribute)
279 // but before execute, all sub-elements also must be updated (due to the plane changes)
281 int aSubsNum = aComposite->numberOfSubs();
282 for(int a = 0; a < aSubsNum; a++) {
283 FeaturePtr aSub = aComposite->subFeature(a);
284 bool aWasModified = myUpdated[aSub];
285 myUpdated.erase(myUpdated.find(aSub)); // erase to update for sure (plane may be changed)
286 myInitial.insert(aSub);
288 myUpdated[aSub] = aWasModified; // restore value
290 // re-execute after update: solver may update the previous values, so, shapes must be
292 for(int a = 0; a < aSubsNum; a++) {
293 if (aComposite->subFeature(a) && aFactory->validate(aComposite->subFeature(a)))
294 aComposite->subFeature(a)->execute();
299 // execute in try-catch to avoid internal problems of the feature
300 if (aState == ModelAPI_StateDone) {
301 theFeature->data()->execState(ModelAPI_StateDone);
303 theFeature->execute();
304 if (theFeature->data()->execState() != ModelAPI_StateDone) {
305 aState = ModelAPI_StateExecFailed;
308 aState = ModelAPI_StateExecFailed;
310 "Feature " + theFeature->getKind() + " has failed during the execution");
313 if (aState != ModelAPI_StateDone) {
314 theFeature->eraseResults();
316 redisplayWithResults(theFeature, aState);
317 } else { // must be updatet, but not updated yet
318 theFeature->data()->execState(ModelAPI_StateMustBeUpdated);
319 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
320 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
321 for (; aRIter != aResults.cend(); aRIter++) {
322 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
323 aRes->data()->execState(ModelAPI_StateMustBeUpdated);
327 theFeature->eraseResults();
328 redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
330 } else { // for automatically updated features (on abort, etc) it is necessary to redisplay anyway
331 redisplayWithResults(theFeature, ModelAPI_StateNothing);
334 // returns also true is results were updated: for sketch that
335 // refers to sub-features but results of sub-features were changed
336 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
337 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
338 for (; aRIter != aResults.cend(); aRIter++) {
339 if (myInitial.find(*aRIter) != myInitial.end()) {
340 aMustbeUpdated = true;
346 myUpdated[theFeature] = aMustbeUpdated;
347 return aMustbeUpdated;
350 bool Model_Update::updateObject(std::shared_ptr<ModelAPI_Object> theObject, const bool theCyclic)
352 if (myUpdated.find(theObject) != myUpdated.end())
353 return myUpdated[theObject]; // already processed
356 if (theCyclic) { // algorithm for update of all features by dependencies tree
359 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
360 if (aFeature) { // for feature just call update Feature
361 return updateFeature(aFeature);
363 // check general object, possible just a result
364 if (myUpdated.find(theObject) != myUpdated.end())
365 return myUpdated[theObject]; // already processed
366 // check the feature of this object must be executed
367 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
369 FeaturePtr aResFeature = aResult->document()->feature(aResult);
371 return updateFeature(aResFeature);
376 return myInitial.find(theObject) != myInitial.end();