]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Update.cpp
Salome HOME
Adaptation to a new ModelAPI architecture
[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_OBJECT_UPDATED));
21 }
22
23 void Model_Update::processEvent(const Events_Message* theMessage)
24 {
25   const ModelAPI_ObjectUpdatedMessage* aMsg = 
26     dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
27   myInitial = aMsg->objects();
28   // collect all documents involved into the update
29   set<boost::shared_ptr<ModelAPI_Document> > aDocs;
30   set<boost::shared_ptr<ModelAPI_Object> >::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(ModelAPI_Feature::group());
38     for(int aFIndex = 0; aFIndex < aNbFeatures; aFIndex++) {
39       boost::shared_ptr<ModelAPI_Object> aFeature = boost::dynamic_pointer_cast<ModelAPI_Object>
40         ((*aDIter)->object(ModelAPI_Feature::group(), aFIndex));
41       if (aFeature)
42         updateObject(aFeature);
43     }
44   }
45   myUpdated.clear();
46   // flush
47   static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
48   Events_Loop::loop()->flush(EVENT_DISP);
49 }
50
51 bool Model_Update::updateObject(boost::shared_ptr<ModelAPI_Object> theObject)
52 {
53   // check it is already processed
54   if (myUpdated.find(theObject) != myUpdated.end())
55     return myUpdated[theObject];
56   // check all features this feature depended on (recursive call of updateFeature)
57   bool anExecute = myInitial.find(theObject) != myInitial.end();
58   bool aMustbeUpdated = myInitial.find(theObject) != myInitial.end();
59   FeaturePtr aRealFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
60   if (aRealFeature) { // only real feature contains references to other objects
61     // references
62     list<boost::shared_ptr<ModelAPI_Attribute> > aRefs = 
63       theObject->data()->attributes(ModelAPI_AttributeReference::type());
64     list<boost::shared_ptr<ModelAPI_Attribute> >::iterator aRefsIter = aRefs.begin();
65     for(; aRefsIter != aRefs.end(); aRefsIter++) {
66       boost::shared_ptr<ModelAPI_Object> aSub =
67         boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(*aRefsIter)->value();
68       if (aSub && aSub != theObject && updateObject(aSub))
69         aMustbeUpdated = true;
70     }
71     // lists of references
72     aRefs = theObject->data()->attributes(ModelAPI_AttributeRefList::type());
73     for(aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
74       list<ObjectPtr> aListRef = 
75         boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*aRefsIter)->list();
76       list<ObjectPtr>::iterator aListIter = aListRef.begin();
77       for(; aListIter != aListRef.end(); aListIter++) {
78         boost::shared_ptr<ModelAPI_Object> aSub = *aListIter;
79         if (aSub && updateObject(aSub))
80           aMustbeUpdated = true;
81       }
82     }
83     // execute feature if it must be updated
84     anExecute = aMustbeUpdated || anExecute;
85     if (anExecute) {
86       aRealFeature->execute();
87       static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
88       ModelAPI_EventCreator::get()->sendUpdated(theObject, EVENT_DISP);
89     }
90   }
91   myUpdated[theObject] = anExecute;
92   return anExecute;
93 }