1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
26 #include "MEDMEM_Exception.hxx"
27 #include "MEDMEM_define.hxx"
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"
38 using namespace MEDMEM;
39 static void usage(char * name)
43 int pos = prog.rfind( '\\' );
45 int pos = prog.rfind( '/' );
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 ;
54 int main (int argc, char ** argv)
56 if (argc != 3) usage(argv[0]);
58 string filenameIN = argv[1] ;
59 string filenameOUT = argv[2] ;
62 /////////////////////////////////////////////////
63 // we read all meshes and fields in filenameIN //
64 /////////////////////////////////////////////////
65 MEDFILEBROWSER myMed(filenameIN) ;
66 vector< FIELD_* > fields;
67 vector< GMESH* > meshes;
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] );
80 cout << " - Mesh "<<i+1<<", named "<<MeshName[i]<<" is read !" << endl;
81 meshes.push_back( mesh );
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++) {
98 switch( myMed.getFieldType( FieldName[j] ))
100 case MED_REEL64: myField = new FIELD<double>; break;
102 case MED_INT64: myField = new FIELD<int>; break;
104 cout << "Unknown value type - skipped" << endl;
107 myField->setIterationNumber( FieldIteration[j].dt );
108 myField->setOrderNumber( FieldIteration[j].it );
109 myField->addDriver( MED_DRIVER, filenameIN, FieldName[i]);
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] );
120 //////////////////////////////////////////
121 // we write all in VTK file filenameOUT //
122 /////////////////////////////////////////
123 if ( !fields.empty() )
125 vector< const FIELD_* > constFields( fields.begin(), fields.end() );
126 VTK_MED_DRIVER medDriver( filenameOUT, constFields );
129 else if ( !meshes.empty() )
131 VTK_MESH_DRIVER meshDriver( filenameOUT, meshes[0] );
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();
140 catch (MEDEXCEPTION& ex){
141 cout << ex.what() << endl ;