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 <Model_Validator.h>
13 #include <Events_Loop.h>
14 #include <Events_Error.h>
15 #include <Config_FeatureMessage.h>
16 #include <Config_ModuleReader.h>
18 #include <TDF_CopyTool.hxx>
19 #include <TDF_DataSet.hxx>
20 #include <TDF_RelocationTable.hxx>
21 #include <TDF_ClosureTool.hxx>
26 static Model_PluginManager* myImpl = new Model_PluginManager();
28 FeaturePtr Model_PluginManager::createFeature(string theFeatureID)
30 if (this != myImpl) return myImpl->createFeature(theFeatureID);
33 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
34 myCurrentPluginName = myPlugins[theFeatureID];
35 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
36 // load plugin library if not yet done
37 Config_ModuleReader::loadLibrary(myCurrentPluginName);
39 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
41 myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
43 Events_Error::send(string("Can not initialize feature '") + theFeatureID +
44 "' in plugin '" + myCurrentPluginName + "'");
48 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
52 return FeaturePtr(); // return nothing
55 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::rootDocument()
57 return boost::shared_ptr<ModelAPI_Document>(
58 Model_Application::getApplication()->getDocument("root"));
61 bool Model_PluginManager::hasRootDocument()
63 return Model_Application::getApplication()->hasDocument("root");
66 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::currentDocument()
68 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
69 myCurrentDoc = rootDocument();
73 void Model_PluginManager::setCurrentDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
75 myCurrentDoc = theDoc;
78 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::copy(
79 boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
81 // create a new document
82 boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
83 Model_Application::getApplication()->getDocument(theID));
84 // make a copy of all labels
85 TDF_Label aSourceRoot =
86 boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main().Father();
87 TDF_Label aTargetRoot = aNew->document()->Main().Father();
88 Handle(TDF_DataSet) aDS = new TDF_DataSet;
89 aDS->AddLabel(aSourceRoot);
90 TDF_ClosureTool::Closure(aDS);
91 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
92 aRT->SetRelocation(aSourceRoot, aTargetRoot);
93 TDF_CopyTool::Copy(aDS, aRT);
95 aNew->synchronizeFeatures();
99 Model_PluginManager::Model_PluginManager()
101 myPluginsInfoLoaded = false;
102 myCheckTransactions = true;
103 ModelAPI_PluginManager::setPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
104 // register the configuration reading listener
105 Events_Loop* aLoop = Events_Loop::loop();
106 static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
107 aLoop->registerListener(this, FeatureEvent);
108 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
109 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
110 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
113 void Model_PluginManager::processEvent(const Events_Message* theMessage)
115 static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
116 if (theMessage->eventID() == FeatureEvent) {
117 const Config_FeatureMessage* aMsg =
118 dynamic_cast<const Config_FeatureMessage*>(theMessage);
120 // proccess the plugin info, load plugin
121 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
122 myPlugins[aMsg->id()] = aMsg->pluginLibrary();
125 // plugins information was started to load, so, it will be loaded
126 myPluginsInfoLoaded = true;
127 } else { // create/update/delete
128 if (myCheckTransactions && !rootDocument()->isOperation())
129 Events_Error::send("Modification of data structure outside of the transaction");
133 void Model_PluginManager::LoadPluginsInfo()
135 if (myPluginsInfoLoaded) // nothing to do
138 // Read plugins information from XML files
139 Config_ModuleReader aXMLReader("FeatureRegisterEvent");
140 aXMLReader.readAll();
143 void Model_PluginManager::registerPlugin(ModelAPI_Plugin* thePlugin)
145 myPluginObjs[myCurrentPluginName] = thePlugin;
148 ModelAPI_ValidatorsFactory* Model_PluginManager::validators()
150 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;