1 // File: Model_Update.cxx
2 // Created: 25 Jun 2014
3 // Author: Mikhail PONIKAROV
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>
18 Model_Update MY_INSTANCE; /// the only one instance initialized on load of the library
20 Model_Update::Model_Update()
22 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
23 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
26 void Model_Update::processEvent(const Events_Message* theMessage)
28 if (isExecuted) return; // nothing to do: it is executed now
29 Events_LongOp::start(this);
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());
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));
48 updateFeature(aFeature);
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);
59 bool Model_Update::updateFeature(FeaturePtr theFeature)
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
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;
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;
91 // execute feature if it must be updated
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);
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;
114 myUpdated[theFeature] = aMustbeUpdated;
115 return aMustbeUpdated;
118 bool Model_Update::updateObject(boost::shared_ptr<ModelAPI_Object> theObject)
122 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
123 if (aFeature) { // for feature just call update Feature
124 return updateFeature(aFeature);
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);
132 FeaturePtr aResFeature = aResult->document()->feature(aResult);
134 return updateFeature(aResFeature);
137 if (myInitial.find(theObject) != myInitial.end())
139 return false; // nothing is known