]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/WorkspaceIO.cxx
...
[cpPlugins.git] / lib / cpPlugins / Interface / WorkspaceIO.cxx
1 #include <cpPlugins/Interface/Workspace.h>
2 #include <third_party/tinyxml/tinyxml.h>
3
4 // -------------------------------------------------------------------------
5 std::string cpPlugins::Interface::Workspace::
6 LoadWorkspace( const std::string& fname )
7 {
8   TiXmlDocument* doc = new TiXmlDocument( fname.c_str( ) );
9   doc->LoadFile( );
10   TiXmlElement* root = doc->RootElement( );
11   if( root == NULL )
12     return( "cpPlugins::Interface::Workspace: No valid file" );
13   if( std::string( root->Value( ) ) != "cpPlugins_Workspace" )
14     return( "cpPlugins::Interface::Workspace: No valid workspace" );
15   std::stringstream err;
16
17   // Read filters
18   TiXmlElement* filter = root->FirstChildElement( "filter" );
19   while( filter != NULL )
20   {
21     const char* class_value = filter->Attribute( "class" );
22     const char* name_value = filter->Attribute( "name" );
23     float viewX = float( 0 ), viewY = float( 0 );
24     filter->QueryFloatAttribute( "ViewX", &viewX );
25     filter->QueryFloatAttribute( "ViewY", &viewY );
26     if( class_value != NULL && name_value != NULL )
27     {
28       if( this->CreateFilter( class_value, name_value ) )
29       {
30         TFilter* new_filter = this->GetFilter( name_value );
31         new_filter->SetViewCoords( viewX, viewY );
32
33         // Read parameters
34         TParameters* parameters = new_filter->GetParameters( );
35         parameters->FromXML( filter );
36       }
37       else
38         err
39           << "No valid class \"" << class_value << "\" with name \""
40           << name_value << "\"" << std::endl;
41     }
42     else
43       err << "Incomplete data." << std::endl;
44     filter = filter->NextSiblingElement( "filter" );
45
46   } // elihw
47
48   // Read connections
49   TiXmlElement* connection = root->FirstChildElement( "connection" );
50   while( connection != NULL )
51   {
52     TiXmlElement* orig = connection->FirstChildElement( "origin" );
53     TiXmlElement* dest = connection->FirstChildElement( "destination" );
54     if( orig != NULL && dest != NULL )
55     {
56       const char* orig_filter = orig->Attribute( "filter" );
57       const char* dest_filter = dest->Attribute( "filter" );
58       const char* orig_name = orig->Attribute( "name" );
59       const char* dest_name = dest->Attribute( "name" );
60       if(
61         orig_filter != NULL && dest_filter != NULL &&
62         orig_name != NULL && dest_name != NULL
63         )
64         this->Connect( orig_filter, dest_filter, orig_name, dest_name );
65
66     } // fi
67     connection = connection->NextSiblingElement( "connection" );
68
69   } // elihw
70
71   // Read exposed inputs
72   TiXmlElement* port = root->FirstChildElement( "exposed_input_port" );
73   while( port != NULL )
74   {
75     this->ExposeInputPort(
76       port->Attribute( "port_name" ),
77       port->Attribute( "filter" ),
78       port->Attribute( "filter_port_name" )
79       );
80     port = port->NextSiblingElement( "exposed_input_port" );
81
82   } // elihw
83
84   // Read exposed outputs
85   port = root->FirstChildElement( "exposed_output_port" );
86   while( port != NULL )
87   {
88     this->ExposeOutputPort(
89       port->Attribute( "port_name" ),
90       port->Attribute( "filter" ),
91       port->Attribute( "filter_port_name" )
92       );
93     port = port->NextSiblingElement( "exposed_output_port" );
94
95   } // elihw
96
97   // Finish and return
98   delete doc;
99   return( err.str( ) );
100 }
101
102 // -------------------------------------------------------------------------
103 std::string cpPlugins::Interface::Workspace::
104 SaveWorkspace( const std::string& fname ) const
105 {
106   std::stringstream err;
107   TiXmlDocument* doc = new TiXmlDocument( );
108   TiXmlElement* root = new TiXmlElement( "cpPlugins_Workspace" );
109
110   // Save vertices
111   auto vIt = this->m_Graph->BeginVertices( );
112   for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
113   {
114     auto filter = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) );
115     auto data = dynamic_cast< TData* >( vIt->second.GetPointer( ) );
116     if( filter != NULL )
117     {
118       TiXmlElement* e = new TiXmlElement( "filter" );
119       e->SetAttribute( "class", filter->GetClassName( ) );
120       e->SetAttribute( "name", vIt->first.c_str( ) );
121       e->SetAttribute( "ViewX", filter->GetViewX( ) );
122       e->SetAttribute( "ViewY", filter->GetViewY( ) );
123
124       const TParameters* params = filter->GetParameters( );
125       params->ToXML( e );
126
127       root->LinkEndChild( e );
128     }
129     else if( data != NULL )
130     {
131       // TODO
132     } // fi
133
134   } // rof
135
136   // Save connections
137   auto mIt = this->m_Graph->BeginEdgesRows( );
138   for( ; mIt != this->m_Graph->EndEdgesRows( ); ++mIt )
139   {
140     auto rIt = mIt->second.begin( );
141     for( ; rIt != mIt->second.end( ); ++rIt )
142     {
143       auto eIt = rIt->second.begin( );
144       for( ; eIt != rIt->second.end( ); ++eIt )
145       {
146         TiXmlElement* conn = new TiXmlElement( "connection" );
147         TiXmlElement* orig = new TiXmlElement( "origin" );
148         TiXmlElement* dest = new TiXmlElement( "destination" );
149         orig->SetAttribute( "filter", mIt->first.c_str( ) );
150         orig->SetAttribute( "name", eIt->first.c_str( ) );
151         dest->SetAttribute( "filter", rIt->first.c_str( ) );
152         dest->SetAttribute( "name", eIt->second.c_str( ) );
153
154         conn->LinkEndChild( orig );
155         conn->LinkEndChild( dest );
156         root->LinkEndChild( conn );
157
158       } // rof
159
160     } // rof
161
162   } // rof
163
164   // Save exposed ports
165   auto eipIt = this->m_ExposedInputPorts.begin( );
166   for( ; eipIt != this->m_ExposedInputPorts.end( ); ++eipIt )
167   {
168     TiXmlElement* port = new TiXmlElement( "exposed_input_port" );
169     port->SetAttribute( "port_name", eipIt->first.c_str( ) );
170     port->SetAttribute( "filter", eipIt->second.first.c_str( ) );
171     port->SetAttribute( "filter_port_name", eipIt->second.second.c_str( ) );
172     root->LinkEndChild( port );
173
174   } // rof
175
176   auto eopIt = this->m_ExposedOutputPorts.begin( );
177   for( ; eopIt != this->m_ExposedOutputPorts.end( ); ++eopIt )
178   {
179     TiXmlElement* port = new TiXmlElement( "exposed_output_port" );
180     port->SetAttribute( "port_name", eopIt->first.c_str( ) );
181     port->SetAttribute( "filter", eopIt->second.first.c_str( ) );
182     port->SetAttribute( "filter_port_name", eopIt->second.second.c_str( ) );
183     root->LinkEndChild( port );
184
185   } // rof
186
187   // Physical write and return
188   doc->LinkEndChild( root );
189   doc->SaveFile( fname.c_str( ) );
190   delete doc;
191   return( err.str( ) );
192 }
193
194 // eof - $RCSfile$