]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Update.cpp
Salome HOME
b6a4a9553860f80e52cb95204d788390132c45a0
[modules/shaper.git] / src / Model / Model_Update.cpp
1 // File:        Model_Update.cxx
2 // Created:     25 Jun 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_Update.h>
6 #include <Model_Document.h>
7 #include <Model_Data.h>
8 #include <ModelAPI_Feature.h>
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_Events.h>
12 #include <ModelAPI_AttributeReference.h>
13 #include <ModelAPI_AttributeRefList.h>
14 #include <ModelAPI_AttributeRefAttr.h>
15 #include <ModelAPI_AttributeSelection.h>
16 #include <ModelAPI_AttributeSelectionList.h>
17 #include <ModelAPI_Result.h>
18 #include <ModelAPI_Validator.h>
19 #include <ModelAPI_CompositeFeature.h>
20 #include <ModelAPI_Session.h>
21 #include <Events_Loop.h>
22 #include <Events_LongOp.h>
23 #include <Events_Error.h>
24 #include <Config_PropManager.h>
25
26 using namespace std;
27
28 Model_Update MY_UPDATER_INSTANCE;  /// the only one instance initialized on load of the library
29
30 Model_Update::Model_Update()
31 {
32   Events_Loop* aLoop = Events_Loop::loop();
33   static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
34   aLoop->registerListener(this, kChangedEvent);
35   static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
36   aLoop->registerListener(this, kRebuildEvent);
37   static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
38   aLoop->registerListener(this, kCreatedEvent);
39   static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
40   aLoop->registerListener(this, kUpdatedEvent);
41   static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
42   aLoop->registerListener(this, kOpFinishEvent);
43   static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
44   aLoop->registerListener(this, kOpAbortEvent);
45   static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
46   aLoop->registerListener(this, kOpStartEvent);
47
48   Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild automatically",
49                                    Config_Prop::Bool, "false");
50   isAutomatic = Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
51 }
52
53 void Model_Update::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
54 {
55   static Events_Loop* aLoop = Events_Loop::loop();
56   static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
57   static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
58   static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
59   static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
60   static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
61   static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
62   static const Events_ID kOpStartEvent = aLoop->eventByName("StartOperation");
63   bool isAutomaticChanged = false;
64   if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
65     isAutomatic = 
66       Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
67   } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
68     if (isAutomatic == false) {
69       isAutomaticChanged = true;
70       isAutomatic = true;
71     }
72   } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent) {
73     boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
74         boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
75     const std::set<ObjectPtr>& anObjs = aMsg->objects();
76     std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
77     for(; anObjIter != anObjs.cend(); anObjIter++) {
78       myJustCreatedOrUpdated.insert(*anObjIter);
79     }
80   } else if (theMessage->eventID() == kOpStartEvent) {
81     myJustCreatedOrUpdated.clear();
82     return; // we don't need the update only on operation start (caused problems in PartSet_Listener::processEvent)
83   } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
84     if (isAutomatic == false) { // Apply button now works as "Rebuild"
85       isAutomaticChanged = true;
86       isAutomatic = true;
87     }
88   }
89
90   if (isExecuted)
91     return;  // nothing to do: it is executed now
92
93   //Events_LongOp::start(this);
94   isExecuted = true;
95   std::list<boost::shared_ptr<ModelAPI_Document> > aDocs;
96   boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
97       boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
98   if (aMsg) myInitial = aMsg->objects();
99   else {
100     myInitial.clear();
101     // on change flag all documents must be updated
102     if (isAutomatic) {
103       aDocs = ModelAPI_Session::get()->allOpenedDocuments();
104     }
105   }
106   // collect all documents involved into the update process
107   set<boost::shared_ptr<ModelAPI_Object> >::iterator aFIter = myInitial.begin();
108   for (; aFIter != myInitial.end(); aFIter++) {
109     aDocs.push_back((*aFIter)->document());
110   }
111   // iterate all features of features-documents to update them (including hidden)
112   std::set<boost::shared_ptr<ModelAPI_Document> > alreadyUsed;
113   list<boost::shared_ptr<ModelAPI_Document> >::iterator aDIter = aDocs.begin();
114   for (; aDIter != aDocs.end(); aDIter++) {
115     if (alreadyUsed.find(*aDIter) != alreadyUsed.end())
116       continue;
117     alreadyUsed.insert(*aDIter);
118     int aNbFeatures = (*aDIter)->size(ModelAPI_Feature::group(), true);
119     for (int aFIndex = 0; aFIndex < aNbFeatures; aFIndex++) {
120       FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(
121           (*aDIter)->object(ModelAPI_Feature::group(), aFIndex, true));
122       if (aFeature)
123         updateFeature(aFeature);
124     }
125   }
126   myUpdated.clear();
127   // flush to update display
128   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
129   aLoop->flush(EVENT_DISP);
130   //Events_LongOp::end(this);
131   if (isAutomaticChanged) isAutomatic = false;
132
133   if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
134     myJustCreatedOrUpdated.clear();
135   }
136
137   isExecuted = false;
138 }
139
140 void Model_Update::redisplayWithResults(FeaturePtr theFeature) {
141   // maske updated and redisplay all results
142   static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
143   const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
144   std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
145   for (; aRIter != aResults.cend(); aRIter++) {
146     boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
147     aRes->data()->mustBeUpdated(false);
148     myUpdated[aRes] = true;
149     ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
150   }
151   // to redisplay "presentable" feature (for ex. distance constraint)
152   ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
153   theFeature->data()->mustBeUpdated(false);
154 }
155
156 bool Model_Update::updateFeature(FeaturePtr theFeature)
157 {
158   // check it is already processed
159   if (myUpdated.find(theFeature) != myUpdated.end())
160     return myUpdated[theFeature];
161   // check all features this feature depended on (recursive call of updateFeature)
162   ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
163   bool aMustbeUpdated = myInitial.find(theFeature) != myInitial.end();
164   if (theFeature) {  // only real feature contains references to other objects
165     if (theFeature->data()->mustBeUpdated()) aMustbeUpdated = true;
166
167     // composite feature must be executed after sub-features execution
168     CompositeFeaturePtr aComposite = 
169       boost::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
170     if (aComposite) {
171       int aSubsNum = aComposite->numberOfSubs();
172       for(int a = 0; a < aSubsNum; a++) {
173         if (updateFeature(aComposite->subFeature(a)))
174           aMustbeUpdated = true;
175       }
176       if (aMustbeUpdated) {
177         for(int a = 0; a < aSubsNum; a++) {
178           if (aComposite->subFeature(a) && aFactory->validate(aComposite->subFeature(a)))
179             aComposite->subFeature(a)->execute();
180         }
181       }
182     }
183     // check all references: if referenced objects are updated, this object also must be updated
184     std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
185     boost::shared_ptr<Model_Data> aData = 
186       boost::dynamic_pointer_cast<Model_Data>(theFeature->data());
187     aData->referencesToObjects(aRefs);
188     std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefs.begin();
189     for(; aRef != aRefs.end(); aRef++) {
190       std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
191       for(; aRefObj != aRef->second.end(); aRefObj++) {
192         if (updateObject(*aRefObj)) {
193           aMustbeUpdated = true;
194         }
195       }
196     }
197
198     // execute feature if it must be updated
199     if (aMustbeUpdated) {
200       if (boost::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures() ||
201           !theFeature->isPersistentResult()) {
202         if (aFactory->validate(theFeature)) {
203           if (isAutomatic || (myJustCreatedOrUpdated.find(theFeature) != myJustCreatedOrUpdated.end()) ||
204             !theFeature->isPersistentResult() /* execute quick, not persistent results */) 
205           {
206             // before execution update the selection attributes if any
207             list<AttributePtr> aRefs = 
208               theFeature->data()->attributes(ModelAPI_AttributeSelection::type());
209             list<AttributePtr>::iterator aRefsIter = aRefs.begin();
210             for (; aRefsIter != aRefs.end(); aRefsIter++) {
211               boost::shared_ptr<ModelAPI_AttributeSelection> aSel =
212                 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
213               aSel->update(); // this must be done on execution since it may be long operation
214             }
215             aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::type());
216             for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
217               boost::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
218                 boost::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
219               for(int a = aSel->size() - 1; a >= 0; a--) {
220                   aSel->value(a)->update();
221               }
222             }
223             // execute in try-catch to avoid internal problems of the feature
224             try {
225               theFeature->execute();
226             } catch(...) {
227               Events_Error::send(
228                 "Feature " + theFeature->getKind() + " has failed during the execution");
229               theFeature->eraseResults();
230             }
231             redisplayWithResults(theFeature);
232           } else { // must be updatet, but not updated yet
233             theFeature->data()->mustBeUpdated(true);
234             const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
235             std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
236             for (; aRIter != aResults.cend(); aRIter++) {
237               boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
238               aRes->data()->mustBeUpdated(true);
239             }
240           }
241         } else {
242           theFeature->eraseResults();
243           redisplayWithResults(theFeature); // result also must be updated
244         }
245       } else { // for automatically updated features (on abort, etc) it is necessary to redisplay anyway
246         redisplayWithResults(theFeature);
247       }
248     } else {  // returns also true is results were updated: for sketch that refers to sub-features but results of sub-features were changed
249       const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
250       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
251       for (; aRIter != aResults.cend(); aRIter++) {
252         if (myInitial.find(*aRIter) != myInitial.end()) {
253           aMustbeUpdated = true;
254           break;
255         }
256       }
257     }
258   }
259   myUpdated[theFeature] = aMustbeUpdated;
260   return aMustbeUpdated;
261 }
262
263 bool Model_Update::updateObject(boost::shared_ptr<ModelAPI_Object> theObject, const bool theCyclic)
264 {
265   if (myUpdated.find(theObject) != myUpdated.end())
266     return myUpdated[theObject];  // already processed
267
268   /*
269   if (theCyclic) { // algorithm for update of all features by dependencies tree
270     if (!theObject)
271       return false;
272     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
273     if (aFeature) {  // for feature just call update Feature
274       return updateFeature(aFeature);
275     }
276     // check general object, possible just a result
277     if (myUpdated.find(theObject) != myUpdated.end())
278       return myUpdated[theObject];  // already processed
279     // check the feature of this object must be executed
280     ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
281     if (aResult) {
282       FeaturePtr aResFeature = aResult->document()->feature(aResult);
283       if (aResFeature) {
284         return updateFeature(aResFeature);
285       }
286     }
287   }
288   */
289   return myInitial.find(theObject) != myInitial.end();
290 }