]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/IO/MeshReader.cxx
f8c3b84483a96a6b79be61bf9a11eb056fddcd48
[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 cpPlugins::IO::MeshReader::
13 DialogResult cpPlugins::IO::MeshReader::
14 ExecConfigurationDialog( QWidget* parent )
15 {
16   DialogResult r = Self::DialogResult_Cancel;
17
18 #ifdef cpPlugins_Interface_QT4
19
20   // Show dialog and check if it was accepted
21   QFileDialog dialog( parent );
22   dialog.setFileMode( QFileDialog::ExistingFile );
23   dialog.setDirectory( QFileDialog::tr( "." ) );
24   dialog.setNameFilter( QFileDialog::tr( "All files (*)" ) );
25   if( dialog.exec( ) )
26   {
27     QStringList names = dialog.selectedFiles( );
28     this->m_Parameters->SetString( "FileName", names[ 0 ].toStdString( ) );
29     this->m_Parameters->SetSelectedChoice( "PixelType", "float" );
30     this->m_Parameters->SetUint( "Dimension", 3 );
31
32     r = Self::DialogResult_NoModal;
33
34   } // fi
35
36 #endif // cpPlugins_Interface_QT4
37
38   return( r );
39 }
40
41 // -------------------------------------------------------------------------
42 cpPlugins::IO::MeshReader::
43 MeshReader( )
44   : Superclass( )
45 {
46   this->_MakeOutput< cpPlugins::Interface::Mesh >( "Output" );
47
48   std::vector< TParameters::TString > valid_types;
49   valid_types.push_back( "float" );
50   valid_types.push_back( "double" );
51   this->m_Parameters->ConfigureAsString( "FileName", "" );
52   this->m_Parameters->ConfigureAsChoices( "PixelType", valid_types );
53   this->m_Parameters->ConfigureAsUint( "Dimension", 3 );
54 }
55
56 // -------------------------------------------------------------------------
57 cpPlugins::IO::MeshReader::
58 ~MeshReader( )
59 {
60 }
61
62 // -------------------------------------------------------------------------
63 std::string cpPlugins::IO::MeshReader::
64 _GenerateData( )
65 {
66   using namespace cpPlugins::Interface;
67   Parameters::TUint dim = this->m_Parameters->GetUint( "Dimension" );
68   std::string r = "MeshReader: Mesh dimension not supported.";
69   if( dim == 2 )
70     r = this->_GD0< 2 >( );
71   else if( dim == 3 )
72     r = this->_GD0< 3 >( );
73   return( r );
74 }
75
76 // -------------------------------------------------------------------------
77 template< unsigned int D >
78 std::string cpPlugins::IO::MeshReader::
79 _GD0( )
80 {
81   using namespace cpPlugins::Interface;
82   Parameters::TString pt =
83     this->m_Parameters->GetSelectedChoice( "PixelType" );
84   std::string r = "MeshReader: Mesh pixel type not supported";
85   if( pt == "float" )       r = this->_GD1< float, D >( );
86   else if( pt == "double" ) r = this->_GD1< double, D >( );
87   return( r );
88 }
89
90 // -------------------------------------------------------------------------
91 template< class P, unsigned int D >
92 std::string cpPlugins::IO::MeshReader::
93 _GD1( )
94 {
95   // Get filename
96   std::string fname = this->m_Parameters->GetString( "FileName" );
97
98   vtkPolyDataReader* pdr = this->_CreateVTK< vtkPolyDataReader >( );
99   pdr->SetFileName( fname.c_str( ) );
100   pdr->Update( );
101
102   cpPlugins::Interface::Mesh* out =
103     this->GetOutput< cpPlugins::Interface::Mesh >( "Output" );
104   if( out != NULL )
105     out->SetVTK( pdr->GetOutput( ) );
106   else
107     return( "MeshReader: output not correctly created." );
108   return( "" );
109 }
110
111 // eof - $RCSfile$