1 // File: Model_PluginManager.cxx
2 // Created: 20 Mar 2014
3 // Author: Mikhail PONIKAROV
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>
16 #include <TDF_CopyTool.hxx>
17 #include <TDF_DataSet.hxx>
18 #include <TDF_RelocationTable.hxx>
19 #include <TDF_ClosureTool.hxx>
24 static Model_PluginManager* myImpl = new Model_PluginManager();
26 boost::shared_ptr<ModelAPI_Feature> Model_PluginManager::createFeature(string theFeatureID)
28 if (this != myImpl) return myImpl->createFeature(theFeatureID);
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);
37 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
38 boost::shared_ptr<ModelAPI_Feature> aCreated =
39 myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
41 Events_Error::send(string("Can not initialize feature '") + theFeatureID +
42 "' in plugin '" + myCurrentPluginName + "'");
46 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
50 return boost::shared_ptr<ModelAPI_Feature>(); // return nothing
53 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::rootDocument()
55 return boost::shared_ptr<ModelAPI_Document>(
56 Model_Application::getApplication()->getDocument("root"));
59 bool Model_PluginManager::hasRootDocument()
61 return Model_Application::getApplication()->hasDocument("root");
64 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::currentDocument()
66 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
67 myCurrentDoc = rootDocument();
71 void Model_PluginManager::setCurrentDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
73 myCurrentDoc = theDoc;
76 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::copy(
77 boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
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);
93 aNew->synchronizeFeatures();
97 Model_PluginManager::Model_PluginManager()
99 myPluginsInfoLoaded = false;
100 //TODO(sbh): Implement static method to extract event id [SEID]
101 static Events_ID aFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
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);
109 void Model_PluginManager::processEvent(const Events_Message* theMessage)
111 const Config_FeatureMessage* aMsg =
112 dynamic_cast<const Config_FeatureMessage*>(theMessage);
114 // proccess the plugin info, load plugin
115 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
116 myPlugins[aMsg->id()] = aMsg->pluginLibrary();
119 // plugins information was started to load, so, it will be loaded
120 myPluginsInfoLoaded = true;
123 void Model_PluginManager::LoadPluginsInfo()
125 if (myPluginsInfoLoaded) // nothing to do
128 // Read plugins information from XML files
129 Config_ModuleReader aXMLReader("FeatureRegisterEvent");
130 aXMLReader.readAll();
133 void Model_PluginManager::registerPlugin(ModelAPI_Plugin* thePlugin)
135 myPluginObjs[myCurrentPluginName] = thePlugin;