]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/IO/MeshReader.cxx
397b644ef10b402d267a5e0fadd98a7571e239fd
[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     QStringList names = dialog.selectedFiles( );
27     this->m_Parameters->SetString( "FileName", names[ 0 ].toStdString( ) );
28     this->m_Parameters->SetSelectedChoice( "PixelType", "float" );
29     this->m_Parameters->SetUint( "Dimension", 3 );
30
31     r = true;
32
33   } // fi
34
35 #endif // cpPlugins_Interface_QT4
36
37   return( r );
38 }
39
40 // -------------------------------------------------------------------------
41 cpPlugins::IO::MeshReader::
42 MeshReader( )
43   : Superclass( )
44 {
45   this->SetNumberOfOutputs( 1 );
46   this->_MakeOutput< cpPlugins::Interface::Mesh >( 0 );
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 >( 0 );
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$