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