Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / Model / Model_PluginManager.cpp
1 // File:        Model_PluginManager.cxx
2 // Created:     20 Mar 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_PluginManager.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 <Events_Loop.h>
12 #include <Events_Error.h>
13 #include <Config_FeatureMessage.h>
14 #include <Config_ModuleReader.h>
15
16 #include <TDF_CopyTool.hxx>
17 #include <TDF_DataSet.hxx>
18 #include <TDF_RelocationTable.hxx>
19 #include <TDF_ClosureTool.hxx>
20
21
22 using namespace std;
23
24 static Model_PluginManager* myImpl = new Model_PluginManager();
25
26 boost::shared_ptr<ModelAPI_Feature> Model_PluginManager::createFeature(string theFeatureID)
27 {
28   if (this != myImpl) return myImpl->createFeature(theFeatureID);
29
30   LoadPluginsInfo();
31   if (myPlugins.find(theFeatureID) != myPlugins.end()) {
32     myCurrentPluginName = myPlugins[theFeatureID];
33     if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
34       // load plugin library if not yet done
35       Config_ModuleReader::loadLibrary(myCurrentPluginName);
36     }
37     if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
38       boost::shared_ptr<ModelAPI_Feature> aCreated = 
39         myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
40       if (!aCreated) {
41         Events_Error::send(string("Can not initialize feature '") + theFeatureID +
42           "' in plugin '" + myCurrentPluginName + "'");
43       }
44       return aCreated;
45     } else {
46       Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
47     }
48   }
49
50   return boost::shared_ptr<ModelAPI_Feature>(); // return nothing
51 }
52
53 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::rootDocument()
54 {
55   return boost::shared_ptr<ModelAPI_Document>(
56     Model_Application::getApplication()->getDocument("root"));
57 }
58
59 bool Model_PluginManager::hasRootDocument()
60 {
61   return Model_Application::getApplication()->hasDocument("root");
62 }
63
64 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::currentDocument()
65 {
66   if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
67     myCurrentDoc = rootDocument();
68   return myCurrentDoc;
69 }
70
71 void Model_PluginManager::setCurrentDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
72 {
73   myCurrentDoc = theDoc;
74 }
75
76 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::copy(
77   boost::shared_ptr<ModelAPI_Document> theSource, std::string theID) 
78 {
79   // create a new document
80   boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
81     Model_Application::getApplication()->getDocument(theID));
82   // make a copy of all labels
83   TDF_Label aSourceRoot = 
84     boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main().Father();
85   TDF_Label aTargetRoot = aNew->document()->Main().Father();
86   Handle(TDF_DataSet) aDS = new TDF_DataSet;
87   aDS->AddLabel(aSourceRoot);
88   TDF_ClosureTool::Closure(aDS);
89   Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
90   aRT->SetRelocation(aSourceRoot, aTargetRoot);
91   TDF_CopyTool::Copy(aDS, aRT);
92
93   aNew->synchronizeFeatures();
94   return aNew;
95 }
96
97 Model_PluginManager::Model_PluginManager()
98 {
99   myPluginsInfoLoaded = false;
100   //TODO(sbh): Implement static method to extract event id [SEID]
101   static Events_ID aFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
102
103   ModelAPI_PluginManager::SetPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
104   // register the configuration reading listener
105   Events_Loop* aLoop = Events_Loop::loop();
106   aLoop->registerListener(this, aFeatureEvent);
107 }
108
109 void Model_PluginManager::processEvent(const Events_Message* theMessage)
110 {
111   const Config_FeatureMessage* aMsg =
112     dynamic_cast<const Config_FeatureMessage*>(theMessage);
113   if (aMsg) {
114     // proccess the plugin info, load plugin
115     if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
116       myPlugins[aMsg->id()] = aMsg->pluginLibrary();
117     }
118   }
119   // plugins information was started to load, so, it will be loaded
120   myPluginsInfoLoaded = true;
121 }
122
123 void Model_PluginManager::LoadPluginsInfo()
124 {
125   if (myPluginsInfoLoaded) // nothing to do
126     return;
127
128   // Read plugins information from XML files
129   Config_ModuleReader aXMLReader("FeatureRegisterEvent");
130   aXMLReader.readAll();
131 }
132
133 void Model_PluginManager::registerPlugin(ModelAPI_Plugin* thePlugin)
134 {
135   myPluginObjs[myCurrentPluginName] = thePlugin;
136 }