Salome HOME
MEDMEM suppression
[modules/med.git] / src / MEDMEMBinTest / med2vtk.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include<string>
24 #include<deque>
25
26 #include "MEDMEM_Exception.hxx"
27 #include "MEDMEM_define.hxx"
28
29 #include "MEDMEM_MedFileBrowser.hxx"
30 #include "MEDMEM_Mesh.hxx"
31 #include "MEDMEM_Grid.hxx"
32 #include "MEDMEM_Family.hxx"
33 #include "MEDMEM_Support.hxx"
34 #include "MEDMEM_Field.hxx"
35 #include "MEDMEM_VtkMedDriver.hxx"
36
37 using namespace std;
38 using namespace MEDMEM;
39 static void usage(char * name)
40 {
41   string prog = name;
42 #ifdef WIN32
43   int pos = prog.rfind( '\\' );
44 #else
45   int pos = prog.rfind( '/' );
46 #endif
47   if ( pos >= 0 )
48     prog = prog.substr( pos+1 );
49   cout << "  " << prog.c_str() << " <input med file> <output vtk file> " << endl ;
50   cout << "  " << "Note: both file names are mandatory" << endl ;
51   exit(-1);
52 }
53
54 int main (int argc, char ** argv)
55 {
56   if (argc != 3) usage(argv[0]);
57   
58   string filenameIN = argv[1] ;
59   string filenameOUT = argv[2] ;
60   
61   try {
62     /////////////////////////////////////////////////
63     // we read all meshes and fields in filenameIN //
64     /////////////////////////////////////////////////
65     MEDFILEBROWSER myMed(filenameIN) ;
66     vector< FIELD_* > fields;
67     vector< GMESH* > meshes;
68
69     // read all meshes
70     ////////////////////
71
72     cout << "Read all meshes "  ;
73     int NumberOfMeshes = myMed.getNumberOfMeshes() ;
74     cout << "( "<<NumberOfMeshes << " ) :" << endl ;
75     vector<string> MeshName = myMed.getMeshNames() ;
76     for (int i=0; i<NumberOfMeshes; i++) {
77       GMESH* mesh = myMed.isStructuredMesh( MeshName[i] ) ? (GMESH*) new GRID : (GMESH*) new MESH;
78       mesh->addDriver(MED_DRIVER, filenameIN, MeshName[i] );
79       mesh->read();
80       cout << "  - Mesh "<<i+1<<", named "<<MeshName[i]<<" is read !" << endl;
81       meshes.push_back( mesh );
82     }
83
84     // read all fields
85     ////////////////////
86
87     cout << "Read all fields " ;
88     int NumberOfFields = myMed.getNumberOfFields() ;
89     cout << "( "<<NumberOfFields << " ) :" << endl;
90     vector<string> FieldName = myMed.getFieldNames() ;
91     for (int i=0; i<NumberOfFields; i++) {
92       vector<DT_IT_> FieldIteration = myMed.getFieldIteration(FieldName[i]) ;
93       cout << "  - Field "<<i+1<<", named "<<FieldName[i] << " :" << endl ;
94       int NumberOfIteration = FieldIteration.size() ;
95       cout << "    Number of iteration pair : "<< NumberOfIteration << endl;
96       for (int j=0; j<NumberOfIteration; j++) {
97         FIELD_ * myField = 0;
98         switch( myMed.getFieldType( FieldName[j] ))
99         {
100         case MED_REEL64: myField = new FIELD<double>; break;
101         case MED_INT32:  
102         case MED_INT64:  myField = new FIELD<int>; break;
103         default:
104           cout << "Unknown value type - skipped" << endl;
105           continue;
106         }
107         myField->setIterationNumber( FieldIteration[j].dt );
108         myField->setOrderNumber( FieldIteration[j].it );
109         myField->addDriver( MED_DRIVER, filenameIN, FieldName[i]);
110         myField->read() ;
111         cout << "    * Iteration "<<FieldIteration[j].dt<<" and  order number "<<FieldIteration[j].it<<" ) is read !" << endl;
112         fields.push_back( myField );
113         string meshName = myMed.getMeshName( FieldName[j] );
114         for ( unsigned m = 0; m < meshes.size(); ++m)
115           if ( meshes[m]->getName() == meshName )
116             myField->getSupport()->setMesh( meshes[m] );
117       }
118     }
119
120     //////////////////////////////////////////
121     // we write all in VTK file filenameOUT //
122     /////////////////////////////////////////
123     if ( !fields.empty() )
124     {
125       vector< const FIELD_* > constFields( fields.begin(), fields.end() );
126       VTK_MED_DRIVER medDriver( filenameOUT, constFields );
127       medDriver.write();
128     }
129     else if ( !meshes.empty() )
130     {
131       VTK_MESH_DRIVER meshDriver( filenameOUT, meshes[0] );
132       meshDriver.write() ;
133     }
134
135     for ( unsigned i = 0; i < meshes.size(); ++ i)
136       meshes[i]->removeReference();
137     for ( unsigned i = 0; i < fields.size(); ++ i)
138       fields[i]->removeReference();
139   } 
140   catch (MEDEXCEPTION& ex){
141     cout << ex.what() << endl ;
142   }
143
144 }