]> Creatis software - cpPlugins.git/blob - plugins/cpPluginsIO/MeshWriter.cxx
...
[cpPlugins.git] / plugins / cpPluginsIO / MeshWriter.cxx
1 #include <cpPluginsIO/MeshWriter.h>
2 #include <cpPlugins/Mesh.h>
3
4 #include <cstring>
5
6 #include <vtkPolyDataWriter.h>
7 #include <vtkSTLWriter.h>
8
9 // -------------------------------------------------------------------------
10 cpPluginsIO::MeshWriter::
11 MeshWriter( )
12   : Superclass( )
13 {
14   this->_AddInput( "Input" );
15   this->m_Parameters.Clear( );
16   this->m_Parameters.ConfigureAsSaveFileName( "FileName" );
17   this->m_Parameters.SetAcceptedFileExtensions(
18     "FileName",
19     "Mesh files (*.stl *.vtk)"
20     );
21 }
22
23 // -------------------------------------------------------------------------
24 cpPluginsIO::MeshWriter::
25 ~MeshWriter( )
26 {
27 }
28
29 // -------------------------------------------------------------------------
30 void cpPluginsIO::MeshWriter::
31 _GenerateData( )
32 {
33   auto mesh = this->GetInputData( "Input" )->GetVTK< vtkPolyData >( );
34   if( mesh == NULL )
35     this->_Error( "No suitable input." );
36
37   // Get file extension
38   auto fname = this->m_Parameters.GetSaveFileName( "FileName" );
39   std::istringstream fname_str( fname );
40   std::string token, ext;
41   while( std::getline( fname_str, token, '.' ) )
42     ext = token;
43   std::transform( ext.begin( ), ext.end( ), ext.begin( ), tolower );
44
45   // Real read
46   if( ext == "stl" )
47   {
48     vtkSTLWriter* stlw = this->_CreateVTK< vtkSTLWriter >( );
49     stlw->SetInputData( mesh );
50     stlw->SetFileName( fname.c_str( ) );
51     stlw->Update( );
52     if( stlw->GetErrorCode( ) != 0 )
53       this->_Error( "Someting wrong happened." );
54   }
55   else if( ext == "vtk" )
56   {
57     vtkPolyDataWriter* pdw = this->_CreateVTK< vtkPolyDataWriter >( );
58     pdw->SetInputData( mesh );
59     pdw->SetFileName( fname.c_str( ) );
60     pdw->Update( );
61     if( pdw->GetErrorCode( ) != 0 )
62       this->_Error( "Someting wrong happened." );
63   }
64   else
65     this->_Error( "Input file format not recognized." );
66 }
67
68 // eof - $RCSfile$