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 + "'");
51 Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
54 return FeaturePtr(); // return nothing
57 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::rootDocument()
59 return boost::shared_ptr<ModelAPI_Document>(
60 Model_Application::getApplication()->getDocument("root"));
63 bool Model_PluginManager::hasRootDocument()
65 return Model_Application::getApplication()->hasDocument("root");
68 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::currentDocument()
70 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
71 myCurrentDoc = rootDocument();
75 void Model_PluginManager::setCurrentDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
77 myCurrentDoc = theDoc;
78 static Events_Message aMsg(Events_Loop::eventByName("CurrentDocumentChanged"));
79 Events_Loop::loop()->send(aMsg);
82 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::copy(
83 boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
85 // create a new document
86 boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
87 Model_Application::getApplication()->getDocument(theID));
88 // make a copy of all labels
89 TDF_Label aSourceRoot =
90 boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main().Father();
91 TDF_Label aTargetRoot = aNew->document()->Main().Father();
92 Handle(TDF_DataSet) aDS = new TDF_DataSet;
93 aDS->AddLabel(aSourceRoot);
94 TDF_ClosureTool::Closure(aDS);
95 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
96 aRT->SetRelocation(aSourceRoot, aTargetRoot);
97 TDF_CopyTool::Copy(aDS, aRT);
99 aNew->synchronizeFeatures();
103 Model_PluginManager::Model_PluginManager()
105 myPluginsInfoLoaded = false;
106 myCheckTransactions = true;
107 ModelAPI_PluginManager::setPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
108 // register the configuration reading listener
109 Events_Loop* aLoop = Events_Loop::loop();
110 static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
111 aLoop->registerListener(this, FeatureEvent);
112 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
113 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
114 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
117 void Model_PluginManager::processEvent(const Events_Message* theMessage)
119 static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
120 if (theMessage->eventID() == FeatureEvent) {
121 const Config_FeatureMessage* aMsg =
122 dynamic_cast<const Config_FeatureMessage*>(theMessage);
124 // proccess the plugin info, load plugin
125 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
126 myPlugins[aMsg->id()] = aMsg->pluginLibrary();
129 // plugins information was started to load, so, it will be loaded
130 myPluginsInfoLoaded = true;
131 } else { // create/update/delete
132 if (myCheckTransactions && !rootDocument()->isOperation())
133 Events_Error::send("Modification of data structure outside of the transaction");
137 void Model_PluginManager::LoadPluginsInfo()
139 if (myPluginsInfoLoaded) // nothing to do
142 // Read plugins information from XML files
143 Config_ModuleReader aXMLReader("FeatureRegisterEvent");
144 aXMLReader.readAll();
147 void Model_PluginManager::registerPlugin(ModelAPI_Plugin* thePlugin)
149 myPluginObjs[myCurrentPluginName] = thePlugin;
152 ModelAPI_ValidatorsFactory* Model_PluginManager::validators()
154 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;