]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Update.cpp
Salome HOME
Moved the model messages from Model to ModelAPI and removed dependencies on Model
[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 <ModelAPI_Feature.h>
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_Document.h>
9 #include <ModelAPI_Events.h>
10 #include <ModelAPI_AttributeReference.h>
11 #include <ModelAPI_AttributeRefList.h>
12 #include <Events_Loop.h>
13
14 using namespace std;
15
16 Model_Update MY_INSTANCE; /// the only one instance initialized on load of the library
17
18 Model_Update::Model_Update()
19 {
20   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
21 }
22
23 void Model_Update::processEvent(const Events_Message* theMessage)
24 {
25   const ModelAPI_FeatureUpdatedMessage* aMsg = 
26     dynamic_cast<const ModelAPI_FeatureUpdatedMessage*>(theMessage);
27   myInitial = aMsg->features();
28   // collect all documents involved into the update
29   set<boost::shared_ptr<ModelAPI_Document> > aDocs;
30   set<boost::shared_ptr<ModelAPI_Feature> >::iterator aFIter = myInitial.begin();
31   for(; aFIter != myInitial.end(); aFIter++) {
32     aDocs.insert((*aFIter)->document());
33   }
34   // iterate all features of features-documents to update them
35   set<boost::shared_ptr<ModelAPI_Document> >::iterator aDIter = aDocs.begin();
36   for(; aDIter != aDocs.end(); aDIter++) {
37     int aNbFeatures = (*aDIter)->size(FEATURES_GROUP);
38     for(int aFIndex = 0; aFIndex < aNbFeatures; aFIndex++) {
39       boost::shared_ptr<ModelAPI_Feature> aFeature = (*aDIter)->feature(FEATURES_GROUP, aFIndex);
40       if (aFeature)
41         updateFeature(aFeature);
42     }
43   }
44   myUpdated.clear();
45 }
46
47 bool Model_Update::updateFeature(boost::shared_ptr<ModelAPI_Feature> theFeature)
48 {
49   // check it is already processed
50   if (myUpdated.find(theFeature) != myUpdated.end())
51     return myUpdated[theFeature];
52   // check all features this feature depended on (recursive call of updateFeature)
53   bool aMustbeUpdated = myInitial.find(theFeature) != myInitial.end();
54   // references
55   list<boost::shared_ptr<ModelAPI_Attribute> > aRefs = 
56     theFeature->data()->attributes(ModelAPI_AttributeReference::type());
57   list<boost::shared_ptr<ModelAPI_Attribute> >::iterator aRefsIter = aRefs.begin();
58   for(; aRefsIter != aRefs.end(); aRefsIter++) {
59     boost::shared_ptr<ModelAPI_Feature> aSub =
60       boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(*aRefsIter)->value();
61     if (aSub && aSub != theFeature && updateFeature(aSub))
62       aMustbeUpdated = true;
63   }
64   // lists of references
65   aRefs = theFeature->data()->attributes(ModelAPI_AttributeRefList::type());
66   for(aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
67     list<FeaturePtr> aListRef = 
68       boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*aRefsIter)->list();
69     list<FeaturePtr>::iterator aListIter = aListRef.begin();
70     for(; aListIter != aListRef.end(); aListIter++) {
71       boost::shared_ptr<ModelAPI_Feature> aSub = *aListIter;
72       if (aSub && updateFeature(aSub))
73         aMustbeUpdated = true;
74     }
75   }
76   // execute feature if it must be updated
77   bool anExecute = aMustbeUpdated || myInitial.find(theFeature) != myInitial.end();
78   if (anExecute) {
79     theFeature->execute();
80   }
81   myUpdated[theFeature] = anExecute;
82   return anExecute;
83 }