1 // Copyright (C) 2007-2012 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
23 #include "MEDMEM_define.hxx"
24 #include "MEDMEM_Support.hxx"
25 #include "MEDMEM_Mesh.hxx"
26 #include "MEDMEM_Field.hxx"
28 #include "MEDMEM_EnsightMedDriver.hxx"
29 #include "MEDMEM_EnsightFieldDriver.hxx"
30 #include "MEDMEM_EnsightMeshDriver.hxx"
33 //using namespace MEDMEM;
34 using namespace MED_EN;
35 using namespace MEDMEM_ENSIGHT;
37 // ================================================================================
39 // ================================================================================
42 ENSIGHT_MED_DRIVER::ENSIGHT_MED_DRIVER():_CaseFileDriver_User()
46 ENSIGHT_MED_DRIVER::ENSIGHT_MED_DRIVER(const string & fileName,
47 med_mode_acces accessMode):
48 _CaseFileDriver_User(fileName,accessMode)
52 ENSIGHT_MED_DRIVER::ENSIGHT_MED_DRIVER(const ENSIGHT_MED_DRIVER & driver):
53 _CaseFileDriver_User(driver)
57 void ENSIGHT_MED_DRIVER::openConst() const
59 const char * LOC ="ENSIGHT_MED_DRIVER::open() : ";
62 if ( _fileName.empty() )
64 ( LOCALIZED( STRING(LOC) << "_fileName is empty, "
65 "please set a correct fileName before calling open()"));
67 if (!canOpenFile( _fileName, getAccessMode() ))
69 ( LOCALIZED( STRING(LOC) << "Can not open main Ensight file " << _fileName));
74 void ENSIGHT_MED_DRIVER::open()
78 void ENSIGHT_MED_DRIVER::close()
82 ENSIGHT_MED_DRIVER::~ENSIGHT_MED_DRIVER()
84 MESSAGE_MED("ENSIGHT_MED_DRIVER::~ENSIGHT_MED_DRIVER() has been destroyed");
87 // ================================================================================
89 // ================================================================================
91 ENSIGHT_MED_WRONLY_DRIVER::ENSIGHT_MED_WRONLY_DRIVER() : ENSIGHT_MED_DRIVER(), _fields(0)
95 ENSIGHT_MED_WRONLY_DRIVER::ENSIGHT_MED_WRONLY_DRIVER(const string & fileName,
96 const vector< const FIELD_* >& fields)
97 : ENSIGHT_MED_DRIVER(fileName, MED_EN::WRONLY ), _fields( fields )
101 ENSIGHT_MED_WRONLY_DRIVER::ENSIGHT_MED_WRONLY_DRIVER(const ENSIGHT_MED_WRONLY_DRIVER & driver)
102 : ENSIGHT_MED_DRIVER(driver), _fields( driver._fields )
106 ENSIGHT_MED_WRONLY_DRIVER::~ENSIGHT_MED_WRONLY_DRIVER()
110 GENDRIVER * ENSIGHT_MED_WRONLY_DRIVER::copy() const
112 return new ENSIGHT_MED_WRONLY_DRIVER(*this) ;
115 void ENSIGHT_MED_WRONLY_DRIVER::read() throw (MEDEXCEPTION)
117 throw MEDEXCEPTION("ENSIGHT_MED_WRONLY_DRIVER::read : Can't read with a WRONLY driver !");
120 void ENSIGHT_MED_WRONLY_DRIVER::write() const throw (MEDEXCEPTION)
122 const char * LOC = "ENSIGHT_MED_WRONLY_DRIVER::write() : ";
125 openConst(); // check if can open the case file
127 _CaseFileDriver caseFile( getCaseFileName(), this );
129 map< const GMESH*, vector< const FIELD_* > > mesh2fields;
130 for (unsigned i = 0; i < _fields.size(); ++i )
131 mesh2fields[ _fields[i]->getSupport()->getMesh() ].push_back( _fields[i] );
133 // Create drivers for all meshes and fields
134 // and add them to be written to Case file
136 list< GENDRIVER* > drivers;
137 ENSIGHT_MESH_WRONLY_DRIVER * meshDriver;
138 ENSIGHT_FIELD_WRONLY_DRIVER * fieldDriver;
140 map< const GMESH*, vector< const FIELD_* > >::iterator m_ff = mesh2fields.begin();
141 for (; m_ff != mesh2fields.end(); ++m_ff )
143 const GMESH * mesh = m_ff->first ;
144 meshDriver = new ENSIGHT_MESH_WRONLY_DRIVER( _fileName, mesh );
145 caseFile.addMesh( meshDriver );
146 drivers.push_back( meshDriver );
148 // all fields on this mesh
149 const vector< const FIELD_*> & fields = m_ff->second;
150 for (unsigned j=0; j<fields.size(); j++)
152 fieldDriver = new ENSIGHT_FIELD_WRONLY_DRIVER( _fileName, fields[j] );
153 caseFile.addField( fieldDriver );
154 drivers.push_back( fieldDriver );
161 caseFile.write();// necessary data is passed to the drivers by this method
164 list< GENDRIVER* >::iterator drv = drivers.begin();
165 for ( ; drv != drivers.end(); ++drv ) {
166 GENDRIVER* driver = *drv;
174 // ================================================================================
176 // ================================================================================
178 ENSIGHT_MED_RDONLY_DRIVER::ENSIGHT_MED_RDONLY_DRIVER() :
179 ENSIGHT_MED_DRIVER(), _fields(0), _isFileStructRead(false)
183 ENSIGHT_MED_RDONLY_DRIVER::ENSIGHT_MED_RDONLY_DRIVER(const string & fileName,
184 vector< FIELD_* >& fields)
185 : ENSIGHT_MED_DRIVER( fileName, MED_EN::RDONLY), _fields( & fields ), _isFileStructRead(false)
189 ENSIGHT_MED_RDONLY_DRIVER::ENSIGHT_MED_RDONLY_DRIVER(const ENSIGHT_MED_RDONLY_DRIVER & driver)
190 : ENSIGHT_MED_DRIVER(driver),
191 _fields( driver._fields ),
192 _isFileStructRead(driver._isFileStructRead)
196 ENSIGHT_MED_RDONLY_DRIVER::~ENSIGHT_MED_RDONLY_DRIVER()
200 GENDRIVER * ENSIGHT_MED_RDONLY_DRIVER::copy() const
202 return new ENSIGHT_MED_RDONLY_DRIVER(*this) ;
205 void ENSIGHT_MED_RDONLY_DRIVER::write() const throw (MEDEXCEPTION)
207 throw MEDEXCEPTION("ENSIGHT_MED_RDONLY_DRIVER::write : Can't write with a RDONLY driver !");
210 void ENSIGHT_MED_RDONLY_DRIVER::read()
212 const char * LOC = "ENSIGHT_MED_RDONLY_DRIVER::read() : " ;
215 if ( _isFileStructRead )
217 for ( unsigned i = 0; i < _fields->size(); ++i )
219 const GMESH* mesh = _fields->at(i)->getSupport()->getMesh();
220 if ( mesh->getNumberOfNodes() < 1 )
221 const_cast<GMESH*>( mesh )->read( getId() );
222 _fields->at(i)->read( getId() );
227 open(); // check if can open the case file
229 _CaseFileDriver caseFile( getCaseFileName(), this );
234 int nbOfFields = caseFile.getNbVariables();
235 if ( nbOfFields < 1 )
238 int nbOfMeshes = caseFile.getNbMeshes();
239 vector<MESH*> meshes;
240 for ( int i = 1; i <= nbOfMeshes; ++i )
242 MESH* mesh = new MESH;
243 ENSIGHT_MESH_RDONLY_DRIVER meshDriver(_fileName, mesh, i);
244 caseFile.setDataFileName( i, &meshDriver );
245 int drv = mesh->addDriver( meshDriver );
247 meshes.push_back( mesh );
250 for ( int i = 1; i <= nbOfFields; i++ )
252 int nbSteps = caseFile.getNbVarSteps( i );
253 for ( int step = 1; step <= nbSteps; ++step )
255 FIELD_* field = new FIELD<double>();
256 ENSIGHT_FIELD_RDONLY_DRIVER fieldDriver( _fileName, field );
257 /*int meshIndex =*/ caseFile.setDataFileName( i, step, &fieldDriver );
260 _fields->push_back( field );
266 //================================================================================
268 * \brief Create all meshes and fields but not read them
270 //================================================================================
272 void ENSIGHT_MED_RDONLY_DRIVER::readFileStruct()
274 const char * LOC = "ENSIGHT_MED_RDONLY_DRIVER::read() : " ;
277 open(); // check if can open the case file
279 _CaseFileDriver caseFile( getCaseFileName(), this );
283 int nbOfMeshes = caseFile.getNbMeshes();
284 vector<MESH*> meshes( nbOfMeshes+1, (MESH*)0 );
285 for ( int i = 1; i <= nbOfMeshes; ++i )
287 MESH* mesh = meshes[ i ] = new MESH;
288 ENSIGHT_MESH_RDONLY_DRIVER meshDriver(_fileName, mesh, i);
289 caseFile.setDataFileName( i, &meshDriver ); // retrieve mesh name
290 setId( mesh->addDriver( meshDriver ));
292 _isFileStructRead = true;
295 int nbOfFields = caseFile.getNbVariables();
296 for ( int i = 1; i <= nbOfFields; i++ )
298 int nbSteps = caseFile.getNbVarSteps( i );
299 for ( int step = 1; step <= nbSteps; ++step )
301 FIELD<double>* field = new FIELD<double>();
302 ENSIGHT_FIELD_RDONLY_DRIVER fieldDriver( _fileName, field, step );
303 int meshIndex = caseFile.setDataFileName( i, step, &fieldDriver ); // retrieve field name
304 field->addDriver( fieldDriver );
305 if ( const SUPPORT* sup = field->getSupport() ) {
306 if ( meshIndex > nbOfMeshes || !meshes[ meshIndex ])
308 ((SUPPORT*) sup)->setMesh( meshes[ meshIndex ]);
310 _fields->push_back( field );