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_ValidatorMessage.h>
17 #include <Config_ModuleReader.h>
19 #include <TDF_CopyTool.hxx>
20 #include <TDF_DataSet.hxx>
21 #include <TDF_RelocationTable.hxx>
22 #include <TDF_ClosureTool.hxx>
26 static Model_PluginManager* myImpl = new Model_PluginManager();
28 FeaturePtr Model_PluginManager::createFeature(string theFeatureID)
31 return myImpl->createFeature(theFeatureID);
34 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
35 myCurrentPluginName = myPlugins[theFeatureID];
36 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
37 // load plugin library if not yet done
38 Config_ModuleReader::loadLibrary(myCurrentPluginName);
40 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
41 FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
44 string("Can not initialize feature '") + theFeatureID + "' in plugin '"
45 + myCurrentPluginName + "'");
49 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
52 Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
55 return FeaturePtr(); // return nothing
58 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::rootDocument()
60 return boost::shared_ptr<ModelAPI_Document>(
61 Model_Application::getApplication()->getDocument("root"));
64 bool Model_PluginManager::hasRootDocument()
66 return Model_Application::getApplication()->hasDocument("root");
69 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::currentDocument()
71 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
72 myCurrentDoc = rootDocument();
76 void Model_PluginManager::setCurrentDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
78 myCurrentDoc = theDoc;
79 static Events_Message aMsg(Events_Loop::eventByName("CurrentDocumentChanged"));
80 Events_Loop::loop()->send(aMsg);
83 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::copy(
84 boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
86 // create a new document
87 boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
88 Model_Application::getApplication()->getDocument(theID));
89 // make a copy of all labels
90 TDF_Label aSourceRoot = boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
92 TDF_Label aTargetRoot = aNew->document()->Main().Father();
93 Handle(TDF_DataSet) aDS = new TDF_DataSet;
94 aDS->AddLabel(aSourceRoot);
95 TDF_ClosureTool::Closure(aDS);
96 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
97 aRT->SetRelocation(aSourceRoot, aTargetRoot);
98 TDF_CopyTool::Copy(aDS, aRT);
100 aNew->synchronizeFeatures();
104 Model_PluginManager::Model_PluginManager()
106 myPluginsInfoLoaded = false;
107 myCheckTransactions = true;
108 ModelAPI_PluginManager::setPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
109 // register the configuration reading listener
110 Events_Loop* aLoop = Events_Loop::loop();
111 static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
112 aLoop->registerListener(this, kFeatureEvent);
113 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
114 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
115 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
116 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
119 void Model_PluginManager::processEvent(const Events_Message* theMessage)
121 static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
122 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
123 if (theMessage->eventID() == kFeatureEvent) {
124 const Config_FeatureMessage* aMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
126 // proccess the plugin info, load plugin
127 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
128 myPlugins[aMsg->id()] = aMsg->pluginLibrary();
131 // plugins information was started to load, so, it will be loaded
132 myPluginsInfoLoaded = true;
133 } else if (theMessage->eventID() == kValidatorEvent) {
134 const Config_ValidatorMessage* aMsg = dynamic_cast<const Config_ValidatorMessage*>(theMessage);
136 if (aMsg->attributeId().empty()) { // feature validator
137 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
138 } else { // attribute validator
139 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
143 } else { // create/update/delete
144 if (myCheckTransactions && !rootDocument()->isOperation())
145 Events_Error::send("Modification of data structure outside of the transaction");
149 void Model_PluginManager::LoadPluginsInfo()
151 if (myPluginsInfoLoaded) // nothing to do
154 // Read plugins information from XML files
155 Config_ModuleReader aXMLReader("FeatureRegisterEvent");
156 aXMLReader.readAll();
159 void Model_PluginManager::registerPlugin(ModelAPI_Plugin* thePlugin)
161 myPluginObjs[myCurrentPluginName] = thePlugin;
164 ModelAPI_ValidatorsFactory* Model_PluginManager::validators()
166 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;