]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/IO/MeshReader.cxx
Widget integration (step 1/6)
[cpPlugins.git] / lib / cpPlugins / Plugins / IO / MeshReader.cxx
1 #include "MeshReader.h"
2 #include <cpPlugins/Interface/Mesh.h>
3
4 #include <vtkPolyData.h>
5 #include <vtkPolyDataReader.h>
6
7 #ifdef cpPlugins_Interface_QT4
8 #include <QFileDialog>
9 #endif // cpPlugins_Interface_QT4
10
11 // -------------------------------------------------------------------------
12 bool cpPlugins::IO::MeshReader::
13 ExecConfigurationDialog( QWidget* parent )
14 {
15   bool r = false;
16
17 #ifdef cpPlugins_Interface_QT4
18
19   // Show dialog and check if it was accepted
20   QFileDialog dialog( parent );
21   dialog.setFileMode( QFileDialog::ExistingFile );
22   dialog.setDirectory( QFileDialog::tr( "." ) );
23   dialog.setNameFilter( QFileDialog::tr( "All files (*)" ) );
24   if( dialog.exec( ) )
25   {
26     this->m_Parameters = this->m_DefaultParameters;
27     QStringList names = dialog.selectedFiles( );
28     this->m_Parameters.AddValueToStringList(
29       "FileNames", names[ 0 ].toStdString( )
30       );
31
32     /* TODO
33        this->m_Parameters.SetValueAsString( "PixelType", "float" );
34        this->m_Parameters.SetValueAsUint( "Dimension", 3 );
35     */
36
37     r = true;
38
39   } // fi
40
41 #endif // cpPlugins_Interface_QT4
42
43   return( r );
44 }
45
46 // -------------------------------------------------------------------------
47 cpPlugins::IO::MeshReader::
48 MeshReader( )
49   : Superclass( ),
50     m_Reader( NULL )
51 {
52   this->m_ClassName = "cpPlugins::IO::MeshReader";
53   this->m_ClassCategory = "MeshReader";
54
55   this->SetNumberOfOutputs( 1 );
56   this->_MakeOutput< cpPlugins::Interface::Mesh >( 0 );
57
58   using namespace cpPlugins::Interface;
59   this->m_DefaultParameters.Configure( Parameters::String, "FileName" );
60   this->m_DefaultParameters.Configure( Parameters::String, "PixelType" );
61   this->m_DefaultParameters.Configure( Parameters::Uint, "Dimension" );
62   this->m_DefaultParameters.SetValueAsString( "PixelType", "float" );
63   this->m_DefaultParameters.SetValueAsUint( "Dimension", 3 );
64   this->m_Parameters = this->m_DefaultParameters;
65 }
66
67 // -------------------------------------------------------------------------
68 cpPlugins::IO::MeshReader::
69 ~MeshReader( )
70 {
71   if( this->m_Reader != NULL )
72     this->m_Reader = NULL;
73 }
74
75 // -------------------------------------------------------------------------
76 std::string cpPlugins::IO::MeshReader::
77 _GenerateData( )
78 {
79   using namespace cpPlugins::Interface;
80   Parameters::TUint dim = this->m_Parameters.GetValueAsUint( "Dimension" );
81   std::string r = "MeshReader: Mesh dimension not supported.";
82   if( dim == 2 )
83     r = this->_GD0< 2 >( );
84   else if( dim == 3 )
85     r = this->_GD0< 3 >( );
86   return( r );
87 }
88
89 // -------------------------------------------------------------------------
90 template< unsigned int D >
91 std::string cpPlugins::IO::MeshReader::
92 _GD0( )
93 {
94   using namespace cpPlugins::Interface;
95   Parameters::TString pt = this->m_Parameters.GetValueAsString( "PixelType" );
96   std::string r = "MeshReader: Mesh pixel type not supported";
97   if( pt == "float" )       r = this->_GD1< float, D >( );
98   else if( pt == "double" ) r = this->_GD1< double, D >( );
99   return( r );
100 }
101
102 // -------------------------------------------------------------------------
103 template< class P, unsigned int D >
104 std::string cpPlugins::IO::MeshReader::
105 _GD1( )
106 {
107   // Get filename
108   using namespace cpPlugins::Interface;
109   Parameters::TString fname =
110     this->m_Parameters.GetValueAsString( "FileName" );
111
112   if( this->m_Reader != NULL )
113     this->m_Reader->Delete( );
114
115   vtkPolyDataReader* pdr = vtkPolyDataReader::New( );
116   this->m_Reader = pdr;
117   pdr->SetFileName( fname.c_str( ) );
118   pdr->Update( );
119
120   cpPlugins::Interface::Mesh* out =
121     this->GetOutput< cpPlugins::Interface::Mesh >( 0 );
122   if( out != NULL )
123     out->SetVTKMesh( pdr->GetOutput( ) );
124   else
125     return( "MeshReader: output not correctly created." );
126   return( "" );
127 }
128
129 // eof - $RCSfile$