Back to index

salome-med  6.5.0
ensight2med.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 #include<vector>
00026 
00027 #include "MEDMEM_Exception.hxx"
00028 #include "MEDMEM_define.hxx"
00029 
00030 #include "MEDMEM_Mesh.hxx"
00031 #include "MEDMEM_Family.hxx"
00032 #include "MEDMEM_Support.hxx"
00033 #include "MEDMEM_Field.hxx"
00034 #include "MEDMEM_EnsightMedDriver.hxx"
00035 #include "MEDMEM_EnsightFieldDriver.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 ensight file> <output med file> [I - to read into MED_INT32 fields]"
00045        << endl ;
00046   exit(-1);
00047 }
00048 
00049 // ensight2med toto.case toto.med => input : toto.case => output : toto.med....
00050 
00051 int main (int argc, char ** argv) {
00052 
00053   string filenameIN ;
00054   string filenameOUT;
00055  
00056   if ( argc == 3 ) {
00057     filenameIN  = argv[1] ;
00058     filenameOUT = argv[2] ;
00059     cout << " reading all into the Ensight file " << filenameIN << " and writing all into the Med file " << filenameOUT <<  endl ;
00060 
00061     vector< FIELD_* > fields;
00062     vector< const GMESH* > meshes;
00063 
00064     // Read
00065 
00066     ENSIGHT_MED_RDONLY_DRIVER medDriver(filenameIN, fields);
00067     medDriver.open();
00068     medDriver.read();
00069     medDriver.close();
00070     if ( !fields.empty() )
00071     {
00072       set<const GMESH*> uniqueMeshes;
00073       for ( unsigned i = 0; i < fields.size(); ++i )
00074         uniqueMeshes.insert( fields[i]->getSupport()->getMesh() );
00075       meshes.assign( uniqueMeshes.begin(), uniqueMeshes.end() );
00076     }
00077     else
00078     {
00079       // no fields but only meshes in the file
00080       MESH* mesh = 0;
00081       int meshIndex = 1;
00082       do
00083       {
00084         mesh = new MESH;
00085         meshes.push_back( mesh );
00086         ENSIGHT_MESH_RDONLY_DRIVER meshDriver( filenameIN, mesh, meshIndex++ );
00087         meshDriver.open();
00088         meshDriver.read();
00089         meshDriver.close();
00090       } while ( mesh->getNumberOfNodes() > 0 );
00091       meshes.back()->removeReference();
00092       meshes.pop_back();
00093     }
00094 
00095     // Write
00096 
00097     for ( unsigned i = 0; i < meshes.size(); ++i )
00098     {
00099       meshes[i]->write(MED_DRIVER,filenameOUT);
00100     }
00101     for ( unsigned i = 0; i < fields.size(); ++i )
00102     {
00103       fields[i]->write(MED_DRIVER,filenameOUT);
00104       fields[i]->removeReference();
00105     }
00106     for ( unsigned i = 0; i < meshes.size(); ++i )
00107       meshes[i]->removeReference();
00108   }
00109   else if ( argc == 4 && strncmp(argv[3], "I", 1 )==0 )
00110   {
00111     // we read all variables into INT32 fields
00112     // (we need such fields for test_operation_fieldint)
00113 
00114     filenameIN  = argv[1] ;
00115     filenameOUT = argv[2] ;
00116 
00117     vector< FIELD_* > fields;
00118 
00119     // Read
00120 
00121     ENSIGHT_MED_RDONLY_DRIVER medDriver(filenameIN, fields);
00122     medDriver.open();
00123     medDriver.readFileStruct();
00124     medDriver.close();
00125 
00126     if ( fields.empty() ) {
00127       cout << "No fileds found in EnSight file " << filenameIN << endl;
00128       return -1;
00129     }
00130     // read-write the mesh
00131     const GMESH* mesh = fields[0]->getSupport()->getMesh();
00132     const_cast<GMESH*>( mesh )->read();
00133     mesh->write(MED_DRIVER, filenameOUT );
00134 
00135     // read-write fields
00136     for ( unsigned i = 0; i < fields.size(); ++i )
00137     {
00138       for ( int timeStep = 1; ; ++timeStep )
00139       {
00140         FIELD<int> *intF=new FIELD<int>;
00141         intF->setName( fields[i]->getName() );
00142         try
00143         {
00144           ENSIGHT_FIELD_RDONLY_DRIVER fieldDriver( filenameIN, intF, timeStep );
00145           fieldDriver.open();
00146           fieldDriver.read();
00147           fieldDriver.close();
00148           // replace zero values as theses fields are used for division
00149           int nbVals = intF->getValueLength();
00150           int* values = const_cast<int*>(intF->getValue());
00151           while ( nbVals-- ) {
00152             if ( values[nbVals]==0 )
00153               values[nbVals]= nbVals%5 + 1;
00154           }
00155         }
00156         catch ( const MEDEXCEPTION& ex)
00157         {
00158           intF->removeReference();
00159           intF=0;
00160           break;
00161         }
00162         intF->write(MED_DRIVER,filenameOUT);
00163         intF->removeReference();
00164       }
00165       fields[i]->removeReference();
00166     }
00167     mesh->removeReference();
00168   }
00169   else usage(argv[0]);
00170 
00171 }
00172 
00173