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 <Model_Events.h>
12 #include <Events_Loop.h>
13 #include <Events_Error.h>
14 #include <Config_FeatureMessage.h>
15 #include <Config_ModuleReader.h>
17 #include <TDF_CopyTool.hxx>
18 #include <TDF_DataSet.hxx>
19 #include <TDF_RelocationTable.hxx>
20 #include <TDF_ClosureTool.hxx>
25 static Model_PluginManager* myImpl = new Model_PluginManager();
27 FeaturePtr Model_PluginManager::createFeature(string theFeatureID)
29 if (this != myImpl) return myImpl->createFeature(theFeatureID);
32 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
33 myCurrentPluginName = myPlugins[theFeatureID];
34 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
35 // load plugin library if not yet done
36 Config_ModuleReader::loadLibrary(myCurrentPluginName);
38 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
40 myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
42 Events_Error::send(string("Can not initialize feature '") + theFeatureID +
43 "' in plugin '" + myCurrentPluginName + "'");
47 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
51 return FeaturePtr(); // return nothing
54 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::rootDocument()
56 return boost::shared_ptr<ModelAPI_Document>(
57 Model_Application::getApplication()->getDocument("root"));
60 bool Model_PluginManager::hasRootDocument()
62 return Model_Application::getApplication()->hasDocument("root");
65 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::currentDocument()
67 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
68 myCurrentDoc = rootDocument();
72 void Model_PluginManager::setCurrentDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
74 myCurrentDoc = theDoc;
77 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::copy(
78 boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
80 // create a new document
81 boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
82 Model_Application::getApplication()->getDocument(theID));
83 // make a copy of all labels
84 TDF_Label aSourceRoot =
85 boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main().Father();
86 TDF_Label aTargetRoot = aNew->document()->Main().Father();
87 Handle(TDF_DataSet) aDS = new TDF_DataSet;
88 aDS->AddLabel(aSourceRoot);
89 TDF_ClosureTool::Closure(aDS);
90 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
91 aRT->SetRelocation(aSourceRoot, aTargetRoot);
92 TDF_CopyTool::Copy(aDS, aRT);
94 aNew->synchronizeFeatures();
98 Model_PluginManager::Model_PluginManager()
100 myPluginsInfoLoaded = false;
101 myCheckTransactions = true;
102 ModelAPI_PluginManager::SetPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
103 // register the configuration reading listener
104 Events_Loop* aLoop = Events_Loop::loop();
105 static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
106 aLoop->registerListener(this, FeatureEvent);
107 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
108 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
109 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
112 void Model_PluginManager::processEvent(const Events_Message* theMessage)
114 static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
115 if (theMessage->eventID() == FeatureEvent) {
116 const Config_FeatureMessage* aMsg =
117 dynamic_cast<const Config_FeatureMessage*>(theMessage);
119 // proccess the plugin info, load plugin
120 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
121 myPlugins[aMsg->id()] = aMsg->pluginLibrary();
124 // plugins information was started to load, so, it will be loaded
125 myPluginsInfoLoaded = true;
126 } else { // create/update/delete
127 if (myCheckTransactions && !rootDocument()->isOperation())
128 Events_Error::send("Modification of data structure outside of the transaction");
132 void Model_PluginManager::LoadPluginsInfo()
134 if (myPluginsInfoLoaded) // nothing to do
137 // Read plugins information from XML files
138 Config_ModuleReader aXMLReader("FeatureRegisterEvent");
139 aXMLReader.readAll();
142 void Model_PluginManager::registerPlugin(ModelAPI_Plugin* thePlugin)
144 myPluginObjs[myCurrentPluginName] = thePlugin;