Salome HOME
Fixed problem with too many executions of extrusion wit hexternal sketch edges
[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
46   Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild automatically",
47                                    Config_Prop::Bool, "false");
48   isAutomatic = Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
49 }
50
51 void Model_Update::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
52 {
53   static Events_Loop* aLoop = Events_Loop::loop();
54   static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
55   static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
56   static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
57   static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
58   static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
59   static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
60   bool isAutomaticChanged = false;
61   if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
62     isAutomatic = 
63       Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
64   } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
65     if (isAutomatic == false) {
66       isAutomaticChanged = true;
67       isAutomatic = true;
68     }
69   } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent) {
70     boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
71         boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
72     if (theMessage->eventID() == kCreatedEvent) {
73       myJustCreatedOrUpdated.clear();
74     }
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   } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
80     myJustCreatedOrUpdated.clear();
81     return;
82   }
83
84   if (isExecuted)
85     return;  // nothing to do: it is executed now
86
87   //Events_LongOp::start(this);
88   isExecuted = true;
89   list<boost::shared_ptr<ModelAPI_Document> > aDocs;
90   boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
91       boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
92   if (aMsg) myInitial = aMsg->objects();
93   else {
94     myInitial.clear();
95     // on change flag all documents must be updated
96     if (isAutomatic) {
97       aDocs = ModelAPI_Session::get()->allOpenedDocuments();
98     }
99   }
100   // collect all documents involved into the update process
101   set<boost::shared_ptr<ModelAPI_Object> >::iterator aFIter = myInitial.begin();
102   for (; aFIter != myInitial.end(); aFIter++) {
103     aDocs.push_back((*aFIter)->document());
104   }
105   // iterate all features of features-documents to update them (including hidden)
106   list<boost::shared_ptr<ModelAPI_Document> >::iterator aDIter = aDocs.begin();
107   for (; aDIter != aDocs.end(); aDIter++) {
108     int aNbFeatures = (*aDIter)->size(ModelAPI_Feature::group(), true);
109     for (int aFIndex = 0; aFIndex < aNbFeatures; aFIndex++) {
110       FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(
111           (*aDIter)->object(ModelAPI_Feature::group(), aFIndex, true));
112       if (aFeature)
113         updateFeature(aFeature);
114     }
115   }
116   myUpdated.clear();
117   if (theMessage->eventID() == kUpdatedEvent) {
118     // flush updates without execution now (updates are caused by this process)
119     aLoop->flush(kUpdatedEvent);
120   }
121   // flush to update display
122   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
123   aLoop->flush(EVENT_DISP);
124   //Events_LongOp::end(this);
125   if (isAutomaticChanged) isAutomatic = false;
126   isExecuted = false;
127 }
128
129 bool Model_Update::updateFeature(FeaturePtr theFeature)
130 {
131   // check it is already processed
132   if (myUpdated.find(theFeature) != myUpdated.end())
133     return myUpdated[theFeature];
134   // check all features this feature depended on (recursive call of updateFeature)
135   bool aMustbeUpdated = myInitial.find(theFeature) != myInitial.end();
136   if (theFeature) {  // only real feature contains references to other objects
137     if (theFeature->data()->mustBeUpdated()) aMustbeUpdated = true;
138
139     // composite feature must be executed after sub-features execution
140     CompositeFeaturePtr aComposite = 
141       boost::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
142     if (aComposite) {
143       int aSubsNum = aComposite->numberOfSubs();
144       for(int a = 0; a < aSubsNum; a++) {
145         if (updateFeature(aComposite->subFeature(a)))
146           aMustbeUpdated = true;
147       }
148     }
149     // check all references: if referenced objects are updated, this object also must be updated
150     std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
151     boost::shared_ptr<Model_Data> aData = 
152       boost::dynamic_pointer_cast<Model_Data>(theFeature->data());
153     aData->referencesToObjects(aRefs);
154     std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefs.begin();
155     for(; aRef != aRefs.end(); aRef++) {
156       std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
157       for(; aRefObj != aRef->second.end(); aRefObj++) {
158         if (updateObject(*aRefObj)) {
159           aMustbeUpdated = true;
160         }
161       }
162     }
163
164     // execute feature if it must be updated
165     if (aMustbeUpdated) {
166
167       if (boost::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures() ||
168           !theFeature->isPersistentResult()) {
169         ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
170         if (aFactory->validate(theFeature)) {
171           if (isAutomatic || (myJustCreatedOrUpdated.find(theFeature) != myJustCreatedOrUpdated.end()) ||
172             !theFeature->isPersistentResult() /* execute quick, not persistent results */) 
173           {
174             // before execution update the selection attributes if any
175             list<AttributePtr> aRefs = 
176               theFeature->data()->attributes(ModelAPI_AttributeSelection::type());
177             list<AttributePtr>::iterator aRefsIter = aRefs.begin();
178             for (; aRefsIter != aRefs.end(); aRefsIter++) {
179               boost::shared_ptr<ModelAPI_AttributeSelection> aSel =
180                 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
181               aSel->update(); // this must be done on execution since it may be long operation
182             }
183             aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::type());
184             for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
185               boost::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
186                 boost::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
187               for(int a = aSel->size() - 1; a >= 0; a--) {
188                 aSel->value(a)->update();
189               }
190             }
191             // execute in try-catch to avoid internal problems of the feature
192             try {
193               theFeature->execute();
194             } catch(...) {
195               Events_Error::send(
196                 "Feature " + theFeature->getKind() + " has failed during the execution");
197               theFeature->eraseResults();
198             }
199             theFeature->data()->mustBeUpdated(false);
200             const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
201             std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
202             for (; aRIter != aResults.cend(); aRIter++) {
203               boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
204               aRes->data()->mustBeUpdated(false);
205             }
206           } else {
207             theFeature->data()->mustBeUpdated(true);
208             const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
209             std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
210             for (; aRIter != aResults.cend(); aRIter++) {
211               boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
212               aRes->data()->mustBeUpdated(true);
213             }
214             aMustbeUpdated = false;
215           }
216         } else {
217           theFeature->eraseResults();
218         }
219       }
220       // redisplay all results
221       static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
222       const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
223       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
224       for (; aRIter != aResults.cend(); aRIter++) {
225         boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
226         myUpdated[aRes] = true;
227         ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
228       }
229       // to redisplay "presentable" feature (for ex. distance constraint)
230       ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
231     } else {  // returns also true is results were updated: for sketch that refers to sub-features but results of sub-features were changed
232       const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
233       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
234       for (; aRIter != aResults.cend(); aRIter++) {
235         if (myInitial.find(*aRIter) != myInitial.end()) {
236           aMustbeUpdated = true;
237           break;
238         }
239       }
240     }
241   }
242   myUpdated[theFeature] = aMustbeUpdated;
243   return aMustbeUpdated;
244 }
245
246 bool Model_Update::updateObject(boost::shared_ptr<ModelAPI_Object> theObject)
247 {
248   if (myUpdated.find(theObject) != myUpdated.end())
249     return myUpdated[theObject];  // already processed
250   return myInitial.find(theObject) != myInitial.end();
251
252   /* remove algorithm for update of all features by dependencies tree
253   if (!theObject)
254     return false;
255   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
256   if (aFeature) {  // for feature just call update Feature
257     return updateFeature(aFeature);
258   }
259   // check general object, possible just a result
260   if (myUpdated.find(theObject) != myUpdated.end())
261     return myUpdated[theObject];  // already processed
262   // check the feature of this object must be executed
263   ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
264   if (aResult) {
265     FeaturePtr aResFeature = aResult->document()->feature(aResult);
266     if (aResFeature) {
267       return updateFeature(aResFeature);
268     }
269   }
270   if (myInitial.find(theObject) != myInitial.end())
271     return true;
272   return false;  // nothing is known
273   */
274 }