]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Session.cpp
Salome HOME
Patch by Renaud: a test plugin written in python
[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 // TEST
25 #include <Python.h>
26
27 using namespace std;
28
29 static Model_Session* myImpl = new Model_Session();
30
31 // t oredirect all calls to the root document
32 #define ROOT_DOC boost::dynamic_pointer_cast<Model_Document>(moduleDocument())
33
34 bool Model_Session::load(const char* theFileName)
35 {
36   return ROOT_DOC->load(theFileName);
37 }
38
39 bool Model_Session::save(const char* theFileName, std::list<std::string>& theResults)
40 {
41   return ROOT_DOC->save(theFileName, theResults);
42 }
43
44 void Model_Session::startOperation()
45 {
46   ROOT_DOC->startOperation();
47 }
48
49 void Model_Session::finishOperation()
50 {
51   ROOT_DOC->finishOperation();
52   static boost::shared_ptr<Events_Message> aFinishMsg
53     (new Events_Message(Events_Loop::eventByName("FinishOperation")));
54   Events_Loop::loop()->send(aFinishMsg);
55 }
56
57 void Model_Session::abortOperation()
58 {
59   ROOT_DOC->abortOperation();
60   static boost::shared_ptr<Events_Message> anAbortMsg
61     (new Events_Message(Events_Loop::eventByName("AbortOperation")));
62   Events_Loop::loop()->send(anAbortMsg);
63 }
64
65 bool Model_Session::isOperation()
66 {
67   return ROOT_DOC->isOperation();
68 }
69
70 bool Model_Session::isModified()
71 {
72   return ROOT_DOC->isModified();
73 }
74
75 bool Model_Session::canUndo()
76 {
77   return ROOT_DOC->canUndo();
78 }
79
80 void Model_Session::undo()
81 {
82   ROOT_DOC->undo();
83 }
84
85 bool Model_Session::canRedo()
86 {
87   return ROOT_DOC->canRedo();
88 }
89
90 void Model_Session::redo()
91 {
92   ROOT_DOC->redo();
93 }
94
95 FeaturePtr Model_Session::createFeature(string theFeatureID)
96 {
97   if (this != myImpl) {
98     return myImpl->createFeature(theFeatureID);
99   }
100
101   LoadPluginsInfo();
102   if (myPlugins.find(theFeatureID) != myPlugins.end()) {
103     std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
104     if (!aPlugin.second.empty() && aPlugin.second != activeDocument()->kind()) {
105       Events_Error::send(
106           string("Feature '") + theFeatureID + "' can not be created in document '"
107               + aPlugin.second + "' by the XML definition");
108       return FeaturePtr();
109     }
110     myCurrentPluginName = aPlugin.first;
111     if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
112       // load plugin library if not yet done
113       //TODO: Get info from Config about python libraries
114       if (myCurrentPluginName.compare(string("PythonFeaturesPlugin")) == 0) {
115         Py_Initialize();
116         PyObject* module = PyImport_ImportModule(myCurrentPluginName.c_str());
117         assert(module != NULL);
118       } else {
119         Config_ModuleReader::loadLibrary(myCurrentPluginName);
120       }
121     }
122     if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
123       FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
124       if (!aCreated) {
125         Events_Error::send(
126             string("Can not initialize feature '") + theFeatureID + "' in plugin '"
127                 + myCurrentPluginName + "'");
128       }
129       return aCreated;
130     } else {
131       Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
132     }
133   } else {
134     Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
135   }
136
137   return FeaturePtr();  // return nothing
138 }
139
140 boost::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
141 {
142   return boost::shared_ptr<ModelAPI_Document>(
143       Model_Application::getApplication()->getDocument("root"));
144 }
145
146 bool Model_Session::hasModuleDocument()
147 {
148   return Model_Application::getApplication()->hasDocument("root");
149 }
150
151 boost::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
152 {
153   if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
154     myCurrentDoc = moduleDocument();
155   return myCurrentDoc;
156 }
157
158 void Model_Session::setActiveDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
159 {
160   if (myCurrentDoc != theDoc) {
161     myCurrentDoc = theDoc;
162     static boost::shared_ptr<Events_Message> aMsg(new Events_Message(Events_Loop::eventByName("CurrentDocumentChanged")));
163     Events_Loop::loop()->send(aMsg);
164   }
165 }
166
167 std::list<boost::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
168 {
169   list<boost::shared_ptr<ModelAPI_Document> > aResult;
170   aResult.push_back(moduleDocument());
171   // add subs recursively
172   list<boost::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
173   for(; aDoc != aResult.end(); aDoc++) {
174     DocumentPtr anAPIDoc = *aDoc;
175     boost::shared_ptr<Model_Document> aDoc = boost::dynamic_pointer_cast<Model_Document>(anAPIDoc);
176     if (aDoc) {
177       std::set<std::string>::const_iterator aSubIter = aDoc->subDocuments().cbegin();
178       for(; aSubIter != aDoc->subDocuments().cend(); aSubIter++) {
179         if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
180           aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
181         }
182       }
183     }
184   }
185   return aResult;
186 }
187
188 boost::shared_ptr<ModelAPI_Document> Model_Session::copy(
189     boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
190 {
191   // create a new document
192   boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
193       Model_Application::getApplication()->getDocument(theID));
194   // make a copy of all labels
195   TDF_Label aSourceRoot = boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
196       .Father();
197   TDF_Label aTargetRoot = aNew->document()->Main().Father();
198   Handle(TDF_DataSet) aDS = new TDF_DataSet;
199   aDS->AddLabel(aSourceRoot);
200   TDF_ClosureTool::Closure(aDS);
201   Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
202   aRT->SetRelocation(aSourceRoot, aTargetRoot);
203   TDF_CopyTool::Copy(aDS, aRT);
204
205   aNew->synchronizeFeatures();
206   return aNew;
207 }
208
209 Model_Session::Model_Session()
210 {
211   myPluginsInfoLoaded = false;
212   myCheckTransactions = true;
213   ModelAPI_Session::setSession(boost::shared_ptr<ModelAPI_Session>(this));
214   // register the configuration reading listener
215   Events_Loop* aLoop = Events_Loop::loop();
216   static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
217   aLoop->registerListener(this, kFeatureEvent);
218   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
219   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
220   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
221   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
222 }
223
224 void Model_Session::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
225 {
226   static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
227   static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
228   if (theMessage->eventID() == kFeatureEvent) {
229     const boost::shared_ptr<Config_FeatureMessage> aMsg = 
230       boost::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
231     if (aMsg) {
232       // proccess the plugin info, load plugin
233       if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
234         myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
235           aMsg->pluginLibrary(), aMsg->documentKind());
236       }
237     }
238     // plugins information was started to load, so, it will be loaded
239     myPluginsInfoLoaded = true;
240   } else if (theMessage->eventID() == kValidatorEvent) {
241     boost::shared_ptr<Config_ValidatorMessage> aMsg = 
242       boost::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
243     if (aMsg) {
244       if (aMsg->attributeId().empty()) {  // feature validator
245         validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
246       } else {  // attribute validator
247         validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
248                                       aMsg->parameters());
249       }
250     }
251   } else {  // create/update/delete
252     if (myCheckTransactions && !isOperation())
253       Events_Error::send("Modification of data structure outside of the transaction");
254   }
255 }
256
257 void Model_Session::LoadPluginsInfo()
258 {
259   if (myPluginsInfoLoaded)  // nothing to do
260     return;
261
262   // Read plugins information from XML files
263   Config_ModuleReader aXMLReader("FeatureRegisterEvent");
264   aXMLReader.readAll();
265 }
266
267 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
268 {
269   myPluginObjs[myCurrentPluginName] = thePlugin;
270   static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
271   ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
272   Events_Loop::loop()->flush(EVENT_LOAD);
273 }
274
275 ModelAPI_ValidatorsFactory* Model_Session::validators()
276 {
277   static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;
278   return aFactory;
279 }