Back to index

salome-med  6.5.0
med2vtk.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include<string>
00024 #include<deque>
00025 
00026 #include "MEDMEM_Exception.hxx"
00027 #include "MEDMEM_define.hxx"
00028 
00029 #include "MEDMEM_MedFileBrowser.hxx"
00030 #include "MEDMEM_Mesh.hxx"
00031 #include "MEDMEM_Grid.hxx"
00032 #include "MEDMEM_Family.hxx"
00033 #include "MEDMEM_Support.hxx"
00034 #include "MEDMEM_Field.hxx"
00035 #include "MEDMEM_VtkMedDriver.hxx"
00036 
00037 using namespace std;
00038 using namespace MEDMEM;
00039 static void usage(char * name)
00040 {
00041   cout << "  " << name << " <input med file> <output vtk file> " <<endl ;
00042   cout << "    " << "(the two file name are mandatory)" << endl ;
00043   exit(-1);
00044 }
00045 
00046 int main (int argc, char ** argv)
00047 {
00048   if (argc != 3) usage(argv[0]);
00049   
00050   string filenameIN = argv[1] ;
00051   string filenameOUT = argv[2] ;
00052   
00053   try {
00055     // we read all meshes and fields in filenameIN //
00057     MEDFILEBROWSER myMed(filenameIN) ;
00058     vector< FIELD_* > fields;
00059     vector< GMESH* > meshes;
00060 
00061     // read all meshes
00063 
00064     cout << "Read all meshes "  ;
00065     int NumberOfMeshes = myMed.getNumberOfMeshes() ;
00066     cout << "( "<<NumberOfMeshes << " ) :" << endl ;
00067     vector<string> MeshName = myMed.getMeshNames() ;
00068     for (int i=0; i<NumberOfMeshes; i++) {
00069       GMESH* mesh = myMed.isStructuredMesh( MeshName[i] ) ? (GMESH*) new GRID : (GMESH*) new MESH;
00070       mesh->addDriver(MED_DRIVER, filenameIN, MeshName[i] );
00071       mesh->read();
00072       cout << "  - Mesh "<<i+1<<", named "<<MeshName[i]<<" is read !" << endl;
00073       meshes.push_back( mesh );
00074     }
00075 
00076     // read all fields
00078 
00079     cout << "Read all fields " ;
00080     int NumberOfFields = myMed.getNumberOfFields() ;
00081     cout << "( "<<NumberOfFields << " ) :" << endl;
00082     vector<string> FieldName = myMed.getFieldNames() ;
00083     for (int i=0; i<NumberOfFields; i++) {
00084       vector<DT_IT_> FieldIteration = myMed.getFieldIteration(FieldName[i]) ;
00085       cout << "  - Field "<<i+1<<", named "<<FieldName[i] << " :" << endl ;
00086       int NumberOfIteration = FieldIteration.size() ;
00087       cout << "    Number of iteration pair : "<< NumberOfIteration << endl;
00088       for (int j=0; j<NumberOfIteration; j++) {
00089         FIELD_ * myField = 0;
00090         switch( myMed.getFieldType( FieldName[j] ))
00091         {
00092         case MED_REEL64: myField = new FIELD<double>; break;
00093         case MED_INT32:  
00094         case MED_INT64:  myField = new FIELD<int>; break;
00095         default:
00096           cout << "Unknown value type - skipped" << endl;
00097           continue;
00098         }
00099         myField->setIterationNumber( FieldIteration[j].dt );
00100         myField->setOrderNumber( FieldIteration[j].it );
00101         myField->addDriver( MED_DRIVER, filenameIN, FieldName[i]);
00102         myField->read() ;
00103         cout << "    * Iteration "<<FieldIteration[j].dt<<" and  order number "<<FieldIteration[j].it<<" ) is read !" << endl;
00104         fields.push_back( myField );
00105         string meshName = myMed.getMeshName( FieldName[j] );
00106         for ( unsigned m = 0; m < meshes.size(); ++m)
00107           if ( meshes[m]->getName() == meshName )
00108             myField->getSupport()->setMesh( meshes[m] );
00109       }
00110     }
00111 
00113     // we write all in VTK file filenameOUT //
00115     if ( !fields.empty() )
00116     {
00117       vector< const FIELD_* > constFields( fields.begin(), fields.end() );
00118       VTK_MED_DRIVER medDriver( filenameOUT, constFields );
00119       medDriver.write();
00120     }
00121     else if ( !meshes.empty() )
00122     {
00123       VTK_MESH_DRIVER meshDriver( filenameOUT, meshes[0] );
00124       meshDriver.write() ;
00125     }
00126 
00127     for ( unsigned i = 0; i < meshes.size(); ++ i)
00128       meshes[i]->removeReference();
00129     for ( unsigned i = 0; i < fields.size(); ++ i)
00130       fields[i]->removeReference();
00131   } 
00132   catch (MEDEXCEPTION& ex){
00133     cout << ex.what() << endl ;
00134   }
00135 
00136 }