]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/WorkspaceIO.cxx
...
[cpPlugins.git] / lib / cpPlugins / WorkspaceIO.cxx
index c23852a8ff72675c3dae95e48102bf12dc9c55be..f7b25ca9f129ed26721bb7661b4ffdcf73728d66 100644 (file)
@@ -5,6 +5,8 @@
 std::string cpPlugins::Workspace::
 LoadWorkspace( const std::string& fname )
 {
+  if( this->m_Interface == NULL )
+    return( "cpPlugins::Workspace: No valid plugins interface" );
   tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument( );
   doc->LoadFile( fname.c_str( ) );
   tinyxml2::XMLElement* root = doc->RootElement( );
@@ -14,6 +16,38 @@ LoadWorkspace( const std::string& fname )
     return( "cpPlugins::Workspace: No valid workspace" );
   std::stringstream err;
 
+  // Load plugins
+  auto loaded_plugins = this->m_Interface->GetPlugins( );
+  tinyxml2::XMLElement* plugins = root->FirstChildElement( "plugins" );
+  std::string plugins_errors = "";
+  while( plugins != NULL )
+  {
+    tinyxml2::XMLElement* plugin = plugins->FirstChildElement( "plugin" );
+    while( plugin != NULL )
+    {
+      std::string name = plugin->Attribute( "name" );
+      if( loaded_plugins.find( name ) == loaded_plugins.end( ) )
+      {
+        try
+        {
+          this->m_Interface->LoadPlugin( name );
+        }
+        catch( std::exception& err )
+        {
+          plugins_errors += err.what( ) + std::string( "\n" );
+
+        } // yrt
+
+      } // fi
+      plugin = plugin->NextSiblingElement( "plugin" );
+
+    } // elihw
+    plugins = plugins->NextSiblingElement( "plugins" );
+
+  } // elihw
+  if( plugins_errors != "" )
+    return( std::string( "cpPlugins::Workspace: " ) + plugins_errors );
+  
   // Read filters
   tinyxml2::XMLElement* filter = root->FirstChildElement( "filter" );
   while( filter != NULL )
@@ -107,6 +141,7 @@ SaveWorkspace( const std::string& fname ) const
   std::stringstream err;
   tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument( );
   tinyxml2::XMLElement* root = doc->NewElement( "cpPlugins_Workspace" );
+  std::set< std::string > used_plugins;
 
   // Save vertices
   auto vIt = this->m_Graph->BeginVertices( );
@@ -116,6 +151,8 @@ SaveWorkspace( const std::string& fname ) const
     auto data = dynamic_cast< DataObject* >( vIt->second.GetPointer( ) );
     if( filter != NULL )
     {
+      used_plugins.insert( this->m_Interface->GetPluginName( filter ) );
+
       tinyxml2::XMLElement* e = doc->NewElement( "filter" );
       e->SetAttribute( "category", filter->GetClassCategory( ) );
       e->SetAttribute( "class", filter->GetClassName( ) );
@@ -134,6 +171,17 @@ SaveWorkspace( const std::string& fname ) const
 
   } // rof
 
+  // Save used plugins
+  tinyxml2::XMLElement* plugins = doc->NewElement( "plugins" );
+  for( auto pIt = used_plugins.begin( ); pIt != used_plugins.end( ); ++pIt )
+  {
+    tinyxml2::XMLElement* e = doc->NewElement( "plugin" );
+    e->SetAttribute( "name", pIt->c_str( ) );
+    plugins->LinkEndChild( e );
+
+  } // rof
+  root->LinkEndChild( plugins );
+
   // Save connections
   auto mIt = this->m_Graph->BeginEdgesRows( );
   for( ; mIt != this->m_Graph->EndEdgesRows( ); ++mIt )