Salome HOME
f728bb60e23a9861bbbd65daa9df0e37c6df15aa
[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       // to redisplay "presentable" feature (for ex. distance constraint)
104       ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
105     } else { // returns also true is results were updated: for sketch that refers to sub-features but results of sub-features were changed
106       const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
107       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
108       for(; aRIter != aResults.cend(); aRIter++) {
109         if (myInitial.find(*aRIter) != myInitial.end()) {
110           aMustbeUpdated = true;
111           break;
112         }
113       }
114     }
115   }
116   myUpdated[theFeature] = aMustbeUpdated;
117   return aMustbeUpdated;
118 }
119
120 bool Model_Update::updateObject(boost::shared_ptr<ModelAPI_Object> theObject)
121 {
122   if (!theObject) 
123     return false;
124   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
125   if (aFeature) { // for feature just call update Feature
126     return updateFeature(aFeature);
127   }
128   // check general object, possible just a result
129   if (myUpdated.find(theObject) != myUpdated.end())
130     return myUpdated[theObject]; // already processed
131   // check the feature of this object must be executed
132   ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
133   if (aResult) {
134     FeaturePtr aResFeature = aResult->document()->feature(aResult);
135     if (aResFeature) {
136       return updateFeature(aResFeature);
137     }
138   }
139   if (myInitial.find(theObject) != myInitial.end())
140     return true;
141   return false; // nothing is known
142 }