Salome HOME
Debug of flush operation
[modules/shaper.git] / src / Model / Model_PluginManager.cpp
index 75cc0430c17ea114ed9f8b2f42c09ae8b2ceeda3..05f1586e678f1c1b5ba1df48cd2b668ee71926d8 100644 (file)
@@ -8,15 +8,23 @@
 #include <Model_Data.h>
 #include <Model_Document.h>
 #include <Model_Application.h>
-#include <Event_Loop.h>
+#include <Model_Events.h>
+#include <Events_Loop.h>
+#include <Events_Error.h>
 #include <Config_FeatureMessage.h>
 #include <Config_ModuleReader.h>
 
+#include <TDF_CopyTool.hxx>
+#include <TDF_DataSet.hxx>
+#include <TDF_RelocationTable.hxx>
+#include <TDF_ClosureTool.hxx>
+
+
 using namespace std;
 
 static Model_PluginManager* myImpl = new Model_PluginManager();
 
-shared_ptr<ModelAPI_Feature> Model_PluginManager::createFeature(string theFeatureID)
+boost::shared_ptr<ModelAPI_Feature> Model_PluginManager::createFeature(string theFeatureID)
 {
   if (this != myImpl) return myImpl->createFeature(theFeatureID);
 
@@ -25,21 +33,27 @@ shared_ptr<ModelAPI_Feature> Model_PluginManager::createFeature(string theFeatur
     myCurrentPluginName = myPlugins[theFeatureID];
     if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
       // load plugin library if not yet done
-      loadLibrary(myCurrentPluginName);
+      Config_ModuleReader::loadLibrary(myCurrentPluginName);
     }
     if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
-      std::shared_ptr<ModelAPI_Feature> aCreated = 
+      boost::shared_ptr<ModelAPI_Feature> aCreated = 
         myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
+      if (!aCreated) {
+        Events_Error::send(string("Can not initialize feature '") + theFeatureID +
+          "' in plugin '" + myCurrentPluginName + "'");
+      }
       return aCreated;
+    } else {
+      Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
     }
   }
 
-  return std::shared_ptr<ModelAPI_Feature>(); // return nothing
+  return boost::shared_ptr<ModelAPI_Feature>(); // return nothing
 }
 
-std::shared_ptr<ModelAPI_Document> Model_PluginManager::rootDocument()
+boost::shared_ptr<ModelAPI_Document> Model_PluginManager::rootDocument()
 {
-  return std::shared_ptr<ModelAPI_Document>(
+  return boost::shared_ptr<ModelAPI_Document>(
     Model_Application::getApplication()->getDocument("root"));
 }
 
@@ -48,42 +62,70 @@ bool Model_PluginManager::hasRootDocument()
   return Model_Application::getApplication()->hasDocument("root");
 }
 
-shared_ptr<ModelAPI_Document> Model_PluginManager::currentDocument()
+boost::shared_ptr<ModelAPI_Document> Model_PluginManager::currentDocument()
 {
-  if (!myCurrentDoc)
+  if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
     myCurrentDoc = rootDocument();
   return myCurrentDoc;
 }
 
-void Model_PluginManager::setCurrentDocument(shared_ptr<ModelAPI_Document> theDoc)
+void Model_PluginManager::setCurrentDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
 {
   myCurrentDoc = theDoc;
 }
 
+boost::shared_ptr<ModelAPI_Document> Model_PluginManager::copy(
+  boost::shared_ptr<ModelAPI_Document> theSource, std::string theID) 
+{
+  // create a new document
+  boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
+    Model_Application::getApplication()->getDocument(theID));
+  // make a copy of all labels
+  TDF_Label aSourceRoot = 
+    boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main().Father();
+  TDF_Label aTargetRoot = aNew->document()->Main().Father();
+  Handle(TDF_DataSet) aDS = new TDF_DataSet;
+  aDS->AddLabel(aSourceRoot);
+  TDF_ClosureTool::Closure(aDS);
+  Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
+  aRT->SetRelocation(aSourceRoot, aTargetRoot);
+  TDF_CopyTool::Copy(aDS, aRT);
+
+  aNew->synchronizeFeatures();
+  return aNew;
+}
+
 Model_PluginManager::Model_PluginManager()
 {
   myPluginsInfoLoaded = false;
-  //TODO(sbh): Implement static method to extract event id [SEID]
-  static Event_ID aFeatureEvent = Event_Loop::eventByName("FeatureRegisterEvent");
-
-  ModelAPI_PluginManager::SetPluginManager(std::shared_ptr<ModelAPI_PluginManager>(this));
+  ModelAPI_PluginManager::SetPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
   // register the configuration reading listener
-  Event_Loop* aLoop = Event_Loop::loop();
-  aLoop->registerListener(this, aFeatureEvent);
+  Events_Loop* aLoop = Events_Loop::loop();
+  static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
+  aLoop->registerListener(this, FeatureEvent);
+  aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
+  aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
+  aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
 }
 
-void Model_PluginManager::processEvent(const Event_Message* theMessage)
+void Model_PluginManager::processEvent(const Events_Message* theMessage)
 {
-  const Config_FeatureMessage* aMsg =
-    dynamic_cast<const Config_FeatureMessage*>(theMessage);
-  if (aMsg) {
-    // proccess the plugin info, load plugin
-    if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
-      myPlugins[aMsg->id()] = aMsg->pluginLibrary();
+  static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
+  if (theMessage->eventID() == FeatureEvent) {
+    const Config_FeatureMessage* aMsg =
+      dynamic_cast<const Config_FeatureMessage*>(theMessage);
+    if (aMsg) {
+      // proccess the plugin info, load plugin
+      if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
+        myPlugins[aMsg->id()] = aMsg->pluginLibrary();
+      }
     }
+    // plugins information was started to load, so, it will be loaded
+    myPluginsInfoLoaded = true;
+  } else { // create/update/delete
+    if (!rootDocument()->isOperation())
+      Events_Error::send("Modification of data structure outside of the transaction");
   }
-  // plugins information was started to load, so, it will be loaded
-  myPluginsInfoLoaded = true;
 }
 
 void Model_PluginManager::LoadPluginsInfo()
@@ -93,7 +135,6 @@ void Model_PluginManager::LoadPluginsInfo()
 
   // Read plugins information from XML files
   Config_ModuleReader aXMLReader("FeatureRegisterEvent");
-  aXMLReader.setAutoImport(true);
   aXMLReader.readAll();
 }