Back to index

salome-med  6.5.0
med2ensight.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_Mesh.hxx"
00030 #include "MEDMEM_Grid.hxx"
00031 #include "MEDMEM_Family.hxx"
00032 #include "MEDMEM_Support.hxx"
00033 #include "MEDMEM_Field.hxx"
00034 #include "MEDMEM_EnsightMedDriver.hxx"
00035 #include "MEDMEM_MedFileBrowser.hxx"
00036 #include "MEDMEM_EnsightMeshDriver.hxx"
00037 
00038 using namespace std;
00039 using namespace MEDMEM;
00040 
00041 static void usage(char * name)
00042 {
00043   cout << " ERROR ABOUT SYNTAX " << endl ;
00044   cout << "  " << name << " <input med file> <output ensight file> " << endl ;
00045   exit(-1);
00046 }
00047 
00048 int main (int argc, char ** argv) {
00049 
00050   string filenameIN ;
00051   string filenameOUT;
00052   
00053   if ( argc == 3 ) {
00054     filenameIN  = argv[1] ;
00055     filenameOUT = argv[2] ;
00056     cout << "-> reading all into the Med file " << filenameIN << " and writing all into the Ensight file " << filenameOUT <<  endl ;
00057 
00058     MEDFILEBROWSER myMed(filenameIN) ;
00059 
00060     vector< FIELD_* > fields;
00061     vector< GMESH* > meshes;
00062 
00063     cout << "-> Read all meshes "  ;
00064     int NumberOfMeshes = myMed.getNumberOfMeshes() ;
00065     cout << "( "<<NumberOfMeshes << " ) :" << endl ;
00066     vector<string> MeshName = myMed.getMeshNames() ;
00067     for (int i=0; i<NumberOfMeshes; i++) {
00068       GMESH* mesh = myMed.isStructuredMesh( MeshName[i] ) ? (GMESH*) new GRID : (GMESH*) new MESH;
00069       mesh->addDriver(MED_DRIVER, filenameIN, MeshName[i], MED_EN::RDONLY);
00070       mesh->read();
00071       cout << "-> Mesh "<<i+1<<", named "<<MeshName[i]<<" is read !" << endl;
00072       meshes.push_back( mesh );
00073     }
00074 
00075     cout << "-> Read all fields " ;
00076     int NumberOfFields = myMed.getNumberOfFields() ;
00077     cout << "( "<<NumberOfFields << " ) :" << endl;
00078     vector<string> FieldName = myMed.getFieldNames() ;
00079     for (int i=0; i<NumberOfFields; i++) {
00080       vector<DT_IT_> FieldIteration = myMed.getFieldIteration(FieldName[i]) ;
00081       cout << "-> Field "<<i+1<<", named "<<FieldName[i] << " :" << endl ;
00082       int NumberOfIteration = FieldIteration.size() ;
00083       cout << "    Number of iteration pair : "<< NumberOfIteration << endl;
00084       for (int j=0; j<NumberOfIteration; j++) {
00085         FIELD_ * myField = 0;
00086         switch( myMed.getFieldType( FieldName[i] ))
00087         {
00088         case MED_REEL64: myField = new FIELD<double>; break;
00089         case MED_INT32:  
00090         case MED_INT64:  myField = new FIELD<int>; break;
00091         default:
00092           cout << "Unknown value type - skipped" << endl;
00093           continue;
00094         }
00095         myField->setIterationNumber( FieldIteration[j].dt );
00096         myField->setOrderNumber( FieldIteration[j].it );
00097         myField->addDriver( MED_DRIVER, filenameIN, FieldName[i], MED_EN::RDONLY);
00098         myField->read() ;
00099         cout << "    * Iteration "<<FieldIteration[j].dt<<" and  order number "<<FieldIteration[j].it<<" is read !" << endl;
00100         fields.push_back( myField );
00101         // set mesh to myField
00102         string meshName = myField->getSupport()->getMeshName();
00103         for ( unsigned i = 0; i < meshes.size(); ++ i)
00104           if ( meshes[i]->getName() == meshName )
00105             myField->getSupport()->setMesh( meshes[i] );
00106       }
00107     }
00108 
00109     if ( !fields.empty() )
00110     {
00111       vector< const FIELD_* > constFields( fields.begin(), fields.end() );
00112       ENSIGHT_MED_WRONLY_DRIVER medDriver( filenameOUT, constFields );
00113       medDriver.write();
00114     }
00115     else if ( !meshes.empty() )
00116     {
00117       ENSIGHT_MESH_WRONLY_DRIVER meshDriver( filenameOUT, meshes[0] );
00118       meshDriver.write() ;
00119     }
00120 
00121     for ( unsigned i = 0; i < meshes.size(); ++ i)
00122       meshes[i]->removeReference();
00123     for ( unsigned i = 0; i < fields.size(); ++ i)
00124       fields[i]->removeReference();
00125   }
00126   else usage(argv[0]);
00127 }