]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Session.cpp
Salome HOME
6f334290c900e761b3dcf56414b59b1951691ae4
[modules/shaper.git] / src / Model / Model_Session.cpp
1 // File:        Model_Session.cxx
2 // Created:     20 Mar 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_Session.h>
6 #include <ModelAPI_Feature.h>
7 #include <ModelAPI_Plugin.h>
8 #include <Model_Data.h>
9 #include <Model_Document.h>
10 #include <Model_Application.h>
11 #include <Model_Events.h>
12 #include <Model_Validator.h>
13 #include <Events_Loop.h>
14 #include <Events_Error.h>
15 #include <Config_FeatureMessage.h>
16 #include <Config_ValidatorMessage.h>
17 #include <Config_ModuleReader.h>
18
19 #include <TDF_CopyTool.hxx>
20 #include <TDF_DataSet.hxx>
21 #include <TDF_RelocationTable.hxx>
22 #include <TDF_ClosureTool.hxx>
23
24 using namespace std;
25
26 static Model_Session* myImpl = new Model_Session();
27
28 // t oredirect all calls to the root document
29 #define ROOT_DOC boost::dynamic_pointer_cast<Model_Document>(moduleDocument())
30
31 bool Model_Session::load(const char* theFileName)
32 {
33   return ROOT_DOC->load(theFileName);
34 }
35
36 bool Model_Session::save(const char* theFileName, std::list<std::string>& theResults)
37 {
38   return ROOT_DOC->save(theFileName, theResults);
39 }
40
41 void Model_Session::startOperation()
42 {
43   ROOT_DOC->startOperation();
44 }
45
46 void Model_Session::finishOperation()
47 {
48   ROOT_DOC->finishOperation();
49 }
50
51 void Model_Session::abortOperation()
52 {
53   ROOT_DOC->abortOperation();
54 }
55
56 bool Model_Session::isOperation()
57 {
58   return ROOT_DOC->isOperation();
59 }
60
61 bool Model_Session::isModified()
62 {
63   return ROOT_DOC->isModified();
64 }
65
66 bool Model_Session::canUndo()
67 {
68   return ROOT_DOC->canUndo();
69 }
70
71 void Model_Session::undo()
72 {
73   ROOT_DOC->undo();
74 }
75
76 bool Model_Session::canRedo()
77 {
78   return ROOT_DOC->canRedo();
79 }
80
81 void Model_Session::redo()
82 {
83   ROOT_DOC->redo();
84 }
85
86 FeaturePtr Model_Session::createFeature(string theFeatureID)
87 {
88   if (this != myImpl)
89     return myImpl->createFeature(theFeatureID);
90
91   LoadPluginsInfo();
92   if (myPlugins.find(theFeatureID) != myPlugins.end()) {
93     myCurrentPluginName = myPlugins[theFeatureID];
94     if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
95       // load plugin library if not yet done
96       Config_ModuleReader::loadLibrary(myCurrentPluginName);
97     }
98     if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
99       FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
100       if (!aCreated) {
101         Events_Error::send(
102             string("Can not initialize feature '") + theFeatureID + "' in plugin '"
103                 + myCurrentPluginName + "'");
104       }
105       return aCreated;
106     } else {
107       Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
108     }
109   } else {
110     Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
111   }
112
113   return FeaturePtr();  // return nothing
114 }
115
116 boost::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
117 {
118   return boost::shared_ptr<ModelAPI_Document>(
119       Model_Application::getApplication()->getDocument("root"));
120 }
121
122 bool Model_Session::hasModuleDocument()
123 {
124   return Model_Application::getApplication()->hasDocument("root");
125 }
126
127 boost::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
128 {
129   if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
130     myCurrentDoc = moduleDocument();
131   return myCurrentDoc;
132 }
133
134 void Model_Session::setActiveDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
135 {
136   myCurrentDoc = theDoc;
137   static Events_Message aMsg(Events_Loop::eventByName("CurrentDocumentChanged"));
138   Events_Loop::loop()->send(aMsg);
139 }
140
141 boost::shared_ptr<ModelAPI_Document> Model_Session::copy(
142     boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
143 {
144   // create a new document
145   boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
146       Model_Application::getApplication()->getDocument(theID));
147   // make a copy of all labels
148   TDF_Label aSourceRoot = boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
149       .Father();
150   TDF_Label aTargetRoot = aNew->document()->Main().Father();
151   Handle(TDF_DataSet) aDS = new TDF_DataSet;
152   aDS->AddLabel(aSourceRoot);
153   TDF_ClosureTool::Closure(aDS);
154   Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
155   aRT->SetRelocation(aSourceRoot, aTargetRoot);
156   TDF_CopyTool::Copy(aDS, aRT);
157
158   aNew->synchronizeFeatures();
159   return aNew;
160 }
161
162 Model_Session::Model_Session()
163 {
164   myPluginsInfoLoaded = false;
165   myCheckTransactions = true;
166   ModelAPI_Session::setSession(boost::shared_ptr<ModelAPI_Session>(this));
167   // register the configuration reading listener
168   Events_Loop* aLoop = Events_Loop::loop();
169   static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
170   aLoop->registerListener(this, kFeatureEvent);
171   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
172   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
173   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
174   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
175 }
176
177 void Model_Session::processEvent(const Events_Message* theMessage)
178 {
179   static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
180   static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
181   if (theMessage->eventID() == kFeatureEvent) {
182     const Config_FeatureMessage* aMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
183     if (aMsg) {
184       // proccess the plugin info, load plugin
185       if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
186         myPlugins[aMsg->id()] = aMsg->pluginLibrary();
187       }
188     }
189     // plugins information was started to load, so, it will be loaded
190     myPluginsInfoLoaded = true;
191   } else if (theMessage->eventID() == kValidatorEvent) {
192     const Config_ValidatorMessage* aMsg = dynamic_cast<const Config_ValidatorMessage*>(theMessage);
193     if (aMsg) {
194       if (aMsg->attributeId().empty()) {  // feature validator
195         validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
196       } else {  // attribute validator
197         validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
198                                       aMsg->parameters());
199       }
200     }
201   } else {  // create/update/delete
202     if (myCheckTransactions && !isOperation())
203       Events_Error::send("Modification of data structure outside of the transaction");
204   }
205 }
206
207 void Model_Session::LoadPluginsInfo()
208 {
209   if (myPluginsInfoLoaded)  // nothing to do
210     return;
211
212   // Read plugins information from XML files
213   Config_ModuleReader aXMLReader("FeatureRegisterEvent");
214   aXMLReader.readAll();
215 }
216
217 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
218 {
219   myPluginObjs[myCurrentPluginName] = thePlugin;
220   static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
221   ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
222   Events_Loop::loop()->flush(EVENT_LOAD);
223 }
224
225 ModelAPI_ValidatorsFactory* Model_Session::validators()
226 {
227   static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;
228   return aFactory;
229 }