Salome HOME
Merge branch 'master' of newgeom:newgeom
[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 <ModelAPI_Result.h>
13 #include <Events_Loop.h>
14 #include <Events_LongOp.h>
15
16 using namespace std;
17
18 Model_Update MY_INSTANCE; /// the only one instance initialized on load of the library
19
20 Model_Update::Model_Update()
21 {
22   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
23   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
24 }
25
26 void Model_Update::processEvent(const Events_Message* theMessage)
27 {
28   if (isExecuted) return; // nothing to do: it is executed now
29   Events_LongOp::start(this);
30   isExecuted = true;
31   const ModelAPI_ObjectUpdatedMessage* aMsg = 
32     dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
33   myInitial = aMsg->objects();
34   // collect all documents involved into the update
35   set<boost::shared_ptr<ModelAPI_Document> > aDocs;
36   set<boost::shared_ptr<ModelAPI_Object> >::iterator aFIter = myInitial.begin();
37   for(; aFIter != myInitial.end(); aFIter++) {
38     aDocs.insert((*aFIter)->document());
39   }
40   // iterate all features of features-documents to update them (including hidden)
41   set<boost::shared_ptr<ModelAPI_Document> >::iterator aDIter = aDocs.begin();
42   for(; aDIter != aDocs.end(); aDIter++) {
43     int aNbFeatures = (*aDIter)->size(ModelAPI_Feature::group(), true);
44     for(int aFIndex = 0; aFIndex < aNbFeatures; aFIndex++) {
45       FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>
46         ((*aDIter)->object(ModelAPI_Feature::group(), aFIndex, true));
47       if (aFeature)
48         updateFeature(aFeature);
49     }
50   }
51   myUpdated.clear();
52   // flush
53   static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
54   Events_Loop::loop()->flush(EVENT_DISP);
55   Events_LongOp::end(this);
56   isExecuted = false;
57 }
58
59 bool Model_Update::updateFeature(FeaturePtr theFeature)
60 {
61   // check it is already processed
62   if (myUpdated.find(theFeature) != myUpdated.end())
63     return myUpdated[theFeature];
64   // check all features this feature depended on (recursive call of updateFeature)
65   bool aMustbeUpdated = myInitial.find(theFeature) != myInitial.end();
66   if (theFeature) { // only real feature contains references to other objects
67     // references
68     list<boost::shared_ptr<ModelAPI_Attribute> > aRefs = 
69       theFeature->data()->attributes(ModelAPI_AttributeReference::type());
70     list<boost::shared_ptr<ModelAPI_Attribute> >::iterator aRefsIter = aRefs.begin();
71     for(; aRefsIter != aRefs.end(); aRefsIter++) {
72       boost::shared_ptr<ModelAPI_Object> aSub =
73         boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(*aRefsIter)->value();
74       if (updateObject(aSub)) {
75         aMustbeUpdated = true;
76       }
77     }
78     // lists of references
79     aRefs = theFeature->data()->attributes(ModelAPI_AttributeRefList::type());
80     for(aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
81       list<ObjectPtr> aListRef = 
82         boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*aRefsIter)->list();
83       list<ObjectPtr>::iterator aListIter = aListRef.begin();
84       for(; aListIter != aListRef.end(); aListIter++) {
85         boost::shared_ptr<ModelAPI_Object> aSub = *aListIter;
86         if (updateObject(aSub)) {
87           aMustbeUpdated = true;
88         }
89       }
90     }
91     // execute feature if it must be updated
92     if (aMustbeUpdated) {
93       theFeature->execute();
94       // redisplay all results
95       static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
96       const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
97       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
98       for(; aRIter != aResults.cend(); aRIter++) {
99         boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
100         myUpdated[aRes] = true;
101         ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
102       }
103     } else { // returns also true is results were updated: for sketch that refers to sub-features but results of sub-features were changed
104       const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
105       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
106       for(; aRIter != aResults.cend(); aRIter++) {
107         if (myInitial.find(*aRIter) != myInitial.end()) {
108           aMustbeUpdated = true;
109           break;
110         }
111       }
112     }
113   }
114   myUpdated[theFeature] = aMustbeUpdated;
115   return aMustbeUpdated;
116 }
117
118 bool Model_Update::updateObject(boost::shared_ptr<ModelAPI_Object> theObject)
119 {
120   if (!theObject) 
121     return false;
122   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
123   if (aFeature) { // for feature just call update Feature
124     return updateFeature(aFeature);
125   }
126   // check general object, possible just a result
127   if (myUpdated.find(theObject) != myUpdated.end())
128     return myUpdated[theObject]; // already processed
129   // check the feature of this object must be executed
130   ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
131   if (aResult) {
132     FeaturePtr aResFeature = aResult->document()->feature(aResult);
133     if (aResFeature) {
134       return updateFeature(aResFeature);
135     }
136   }
137   if (myInitial.find(theObject) != myInitial.end())
138     return true;
139   return false; // nothing is known
140 }