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
27 #include "MEDMEM_Exception.hxx"
28 #include "MEDMEM_define.hxx"
30 #include "MEDMEM_Mesh.hxx"
31 #include "MEDMEM_Family.hxx"
32 #include "MEDMEM_Support.hxx"
33 #include "MEDMEM_Field.hxx"
34 #include "MEDMEM_EnsightMedDriver.hxx"
35 #include "MEDMEM_EnsightFieldDriver.hxx"
36 #include "MEDMEM_EnsightMeshDriver.hxx"
39 using namespace MEDMEM;
41 static void usage(char * name)
43 cout << " ERROR ABOUT SYNTAX " << endl ;
44 cout << " " << name << " <input ensight file> <output med file> [I - to read into MED_INT32 fields]"
49 // ensight2med toto.case toto.med => input : toto.case => output : toto.med....
51 int main (int argc, char ** argv) {
57 filenameIN = argv[1] ;
58 filenameOUT = argv[2] ;
59 cout << " reading all into the Ensight file " << filenameIN << " and writing all into the Med file " << filenameOUT << endl ;
61 vector< FIELD_* > fields;
62 vector< const GMESH* > meshes;
66 ENSIGHT_MED_RDONLY_DRIVER medDriver(filenameIN, fields);
70 if ( !fields.empty() )
72 set<const GMESH*> uniqueMeshes;
73 for ( unsigned i = 0; i < fields.size(); ++i )
74 uniqueMeshes.insert( fields[i]->getSupport()->getMesh() );
75 meshes.assign( uniqueMeshes.begin(), uniqueMeshes.end() );
79 // no fields but only meshes in the file
85 meshes.push_back( mesh );
86 ENSIGHT_MESH_RDONLY_DRIVER meshDriver( filenameIN, mesh, meshIndex++ );
90 } while ( mesh->getNumberOfNodes() > 0 );
91 meshes.back()->removeReference();
97 for ( unsigned i = 0; i < meshes.size(); ++i )
99 meshes[i]->write(MED_DRIVER,filenameOUT);
101 for ( unsigned i = 0; i < fields.size(); ++i )
103 fields[i]->write(MED_DRIVER,filenameOUT);
104 fields[i]->removeReference();
106 for ( unsigned i = 0; i < meshes.size(); ++i )
107 meshes[i]->removeReference();
109 else if ( argc == 4 && strncmp(argv[3], "I", 1 )==0 )
111 // we read all variables into INT32 fields
112 // (we need such fields for test_operation_fieldint)
114 filenameIN = argv[1] ;
115 filenameOUT = argv[2] ;
117 vector< FIELD_* > fields;
121 ENSIGHT_MED_RDONLY_DRIVER medDriver(filenameIN, fields);
123 medDriver.readFileStruct();
126 if ( fields.empty() ) {
127 cout << "No fileds found in EnSight file " << filenameIN << endl;
130 // read-write the mesh
131 const GMESH* mesh = fields[0]->getSupport()->getMesh();
132 const_cast<GMESH*>( mesh )->read();
133 mesh->write(MED_DRIVER, filenameOUT );
136 for ( unsigned i = 0; i < fields.size(); ++i )
138 for ( int timeStep = 1; ; ++timeStep )
140 FIELD<int> *intF=new FIELD<int>;
141 intF->setName( fields[i]->getName() );
144 ENSIGHT_FIELD_RDONLY_DRIVER fieldDriver( filenameIN, intF, timeStep );
148 // replace zero values as theses fields are used for division
149 int nbVals = intF->getValueLength();
150 int* values = const_cast<int*>(intF->getValue());
152 if ( values[nbVals]==0 )
153 values[nbVals]= nbVals%5 + 1;
156 catch ( const MEDEXCEPTION& ex)
158 intF->removeReference();
162 intF->write(MED_DRIVER,filenameOUT);
163 intF->removeReference();
165 fields[i]->removeReference();
167 mesh->removeReference();