]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_DriverFactory.cxx
Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MEDMEM / MEDMEM_DriverFactory.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "MEDMEM_DriverFactory.hxx"
23 #include "MEDMEM_MedMedDriver.hxx"
24 #include "MEDMEM_MedMeshDriver.hxx"
25 #include "MEDMEM_Mesh.hxx"
26 #include "MEDMEM_GibiMeshDriver.hxx"
27 #include "MEDMEM_PorflowMeshDriver.hxx"
28 #include "MEDMEM_VtkMeshDriver.hxx"
29 #include "MEDMEM_VtkMedDriver.hxx"
30 #include "MEDMEM_EnsightFieldDriver.hxx"
31 #include "MEDMEM_EnsightMeshDriver.hxx"
32 #include "MEDMEM_EnsightMedDriver.hxx"
33
34 #include "MEDMEM_Exception.hxx"
35
36 #include "MEDMEM_MedVersion.hxx"
37 #include "MEDMEM_Compatibility21_22.hxx"
38 #include "MEDMEM_MedMedDriver21.hxx"
39 #include "MEDMEM_MedMedDriver22.hxx"
40 #include "MEDMEM_MedMeshDriver21.hxx"
41 #include "MEDMEM_MedMeshDriver22.hxx"
42
43 using namespace MEDMEM;
44 using namespace MED_EN;
45
46 template<>
47 void MEDMEM::fill<-1,0x3>(double *a, const double *b)
48 {
49 }
50
51 template<>
52 bool MEDMEM::compare<-1>(const double *a, const double *b)
53 {
54   return false;
55 }
56
57 MED_EN::medFileVersion DRIVERFACTORY::globalMedFileVersionForWriting = MED_EN::V22;
58
59 medFileVersion DRIVERFACTORY::getMedFileVersionForWriting()
60 {
61   return DRIVERFACTORY::globalMedFileVersionForWriting;
62 }
63
64 void DRIVERFACTORY::setMedFileVersionForWriting(medFileVersion version)
65 {
66   DRIVERFACTORY::globalMedFileVersionForWriting = version;
67 }
68
69 driverTypes DRIVERFACTORY::deduceDriverTypeFromFileName(const std::string & fileName)
70 {
71   string extension(fileName);
72   string::size_type pos=extension.rfind('.');
73   if(pos==string::npos)
74     return NO_DRIVER;
75   extension.erase(0,pos+1);
76   if(extension=="med")
77     return MED_DRIVER;
78   if(extension=="sauve" || extension=="sauv")
79     return GIBI_DRIVER;
80   if(extension=="cnc" || extension=="inp" || extension=="xyz")
81     return PORFLOW_DRIVER;
82   if(extension=="vtk")
83     return VTK_DRIVER;
84   if(extension=="case")
85     return ENSIGHT_DRIVER;
86   return NO_DRIVER;
87 }
88
89 GENDRIVER *DRIVERFACTORY::buildDriverForMesh(driverTypes driverType,
90                                              const std::string & fileName,
91                                              MESH *mesh,
92                                              const string & driverName,
93                                              med_mode_acces access)
94 {
95   GENDRIVER *ret;
96   switch(driverType)
97     {
98     case MED_DRIVER : {
99       switch(access)
100         {
101         case RDONLY : {
102           ret = new MED_MESH_RDONLY_DRIVER(fileName, mesh);
103           ret->setMeshName(driverName);
104           return ret;
105         }
106         case WRONLY : {
107           ret = new MED_MESH_WRONLY_DRIVER(fileName, mesh, access);
108           ret->setMeshName(driverName);
109           return ret;
110         }
111         case RDWR : {
112           ret = new MED_MESH_RDWR_DRIVER(fileName, mesh);
113           ret->setMeshName(driverName);
114           return ret;
115         }
116         default:
117           throw MED_EXCEPTION ("access type has not been properly specified to the method");
118         }
119       break;
120     }
121
122     case GIBI_DRIVER : {
123       switch(access)
124         {
125         case RDONLY : {
126           ret=new GIBI_MESH_RDONLY_DRIVER(fileName,mesh);
127           return ret;
128         }
129         case RDWR :
130         case WRONLY :{
131           throw MED_EXCEPTION ("access mode other than MED_LECT has been specified with the GIBI_DRIVER type which is not allowed because GIBI_DRIVER is only a read access driver");
132         }
133         default:
134           throw MED_EXCEPTION ("access type has not been properly specified to the method");
135         }
136       break;
137     }
138
139     case PORFLOW_DRIVER : {
140       switch(access)
141         {
142         case RDONLY : {
143           ret=new PORFLOW_MESH_RDONLY_DRIVER(fileName,mesh);
144           return ret;
145         }
146         case RDWR :
147         case WRONLY : {
148           throw MED_EXCEPTION ("access mode other than MED_LECT has been specified with the PORFLOW_DRIVER type which is not allowed because PORFLOW_DRIVER is only a read access driver");
149         }
150         default:
151           throw MED_EXCEPTION ("access type has not been properly specified to the method");
152         }
153       break;
154     }
155
156     case ENSIGHT_DRIVER : {
157       switch(access)
158         {
159         case RDONLY : {
160           ret=new ENSIGHT_MESH_RDONLY_DRIVER(fileName,mesh);
161           return ret;
162         }
163         case WRONLY : {
164           ret=new ENSIGHT_MESH_WRONLY_DRIVER(fileName,mesh);
165           return ret;
166         }
167         case RDWR : {
168           throw MED_EXCEPTION ("not yet implemented");
169           return ret;
170         }
171         default:
172           throw MED_EXCEPTION ("access type has not been properly specified to the method");
173         }
174       break;
175     }
176
177     case VTK_DRIVER : {
178       switch(access)
179         {
180         case RDONLY : {
181           throw MED_EXCEPTION ("access mode other than MED_ECRI or MED_REMPT has been specified with the VTK_DRIVER type which is not allowed because VTK_DRIVER is only a write access driver");
182         }
183         case RDWR :
184         case WRONLY : {
185           ret=new VTK_MESH_DRIVER(fileName,mesh);
186           return ret;
187         }
188
189         default:
190           throw MED_EXCEPTION ("access type has not been properly specified to the method");
191         }
192       break;
193     }
194
195     case NO_DRIVER : {
196       throw MED_EXCEPTION ("NO_DRIVER has been specified to the method 1 which is not allowed");
197     }
198     default:
199       throw MED_EXCEPTION ("other driver than MED_DRIVER GIBI_DRIVER PORFLOW_DRIVER and VT_DRIVER has been specified to the method which is not allowed");
200     }
201 }
202
203 GENDRIVER *DRIVERFACTORY::buildDriverForMed(driverTypes driverType,
204                                             const std::string & fileName,
205                                             MED *med, med_mode_acces access)
206 {
207   GENDRIVER *ret;
208
209   switch(driverType)
210     {
211     case MED_DRIVER : {
212       switch(access)
213         {
214         case RDONLY : {
215           ret=new MED_MED_RDONLY_DRIVER(fileName,med);
216           break ;
217         }
218         case WRONLY : {
219           ret=new MED_MED_WRONLY_DRIVER(fileName,med);
220           break ;
221         }
222         case RDWR : {
223           ret=new MED_MED_RDWR_DRIVER(fileName,med);
224           break ;
225         }
226         default:
227           throw MED_EXCEPTION ("access type has not been properly specified to the method");
228         }
229       break;
230     }
231
232     case VTK_DRIVER : {
233       switch(access)
234         {
235         case RDONLY : {
236           throw MED_EXCEPTION ("access mode other than MED_ECRI or MED_REMPT has been specified with the VTK_DRIVER type which is not allowed because VTK_DRIVER is only a write access driver");
237         }
238         case WRONLY : {
239           ret=new VTK_MED_DRIVER(fileName,med);
240           break ;
241         }
242         case RDWR : {
243           ret=new VTK_MED_DRIVER(fileName,med);
244           break ;
245         }
246         default:
247           throw MED_EXCEPTION ("access type has not been properly specified to the method");
248         }
249       break;
250     }
251
252     case ENSIGHT_DRIVER : {
253       switch(access)
254         {
255         case RDONLY : {
256           ret=new ENSIGHT_MED_RDONLY_DRIVER(fileName,med);
257           break ;
258         }
259         case WRONLY : {
260           ret=new ENSIGHT_MED_WRONLY_DRIVER(fileName,med);
261           break ;
262         }
263         case RDWR : {
264           throw MED_EXCEPTION ("not yet implemented");
265           break ;
266         }
267         default:
268           throw MED_EXCEPTION ("access type has not been properly specified to the method");
269         }
270       break;
271     }
272
273     case GIBI_DRIVER : {
274       throw MED_EXCEPTION ("GIBI_DRIVER has been specified to the method which is not allowed because there is no GIBI driver for the MED object");
275       break;
276     }
277
278     case PORFLOW_DRIVER : {
279       throw MED_EXCEPTION ("PORFLOW_DRIVER has been specified to the method which is not allowed because there is no PORFLOW driver for the MED object");
280       break;
281     }
282
283     case NO_DRIVER : {
284       throw MED_EXCEPTION ("NO_DRIVER has been specified to the method 2 which is not allowed");
285       break;
286     }
287     default:
288       throw MED_EXCEPTION ("NO_DRIVER has been specified to the method 3 which is not allowed");
289     }
290   return ret;
291 }
292
293 GENDRIVER * DRIVERFACTORY::buildMedDriverFromFile(const string & fileName,
294                                                   MED * const ptrMed,
295                                                   MED_EN::med_mode_acces access)
296 {
297   medFileVersion version;
298
299   try
300     {
301       version = getMedFileVersion(fileName);
302     }
303     catch (MEDEXCEPTION & ex)
304     {
305       version = DRIVERFACTORY::globalMedFileVersionForWriting;
306     }
307
308   MESSAGE_MED("buildMedDriverFromFile version of the file " << version);
309
310   GENDRIVER * driver;
311
312   switch(access)
313     {
314     case RDONLY : {
315       if (version == V21)
316         driver = new MED_MED_RDONLY_DRIVER21(fileName,ptrMed);
317       else if (version == V22)
318         driver = new MED_MED_RDONLY_DRIVER22(fileName,ptrMed);
319       return driver;
320     }
321     case WRONLY : {
322       if (version == V21)
323         driver = new MED_MED_WRONLY_DRIVER21(fileName,ptrMed);
324       else if (version == V22)
325         driver = new MED_MED_WRONLY_DRIVER22(fileName,ptrMed);
326       return driver;
327     }
328     case RDWR : {
329       if (version == V21)
330         driver = new MED_MED_RDWR_DRIVER21(fileName,ptrMed);
331       else if (version == V22)
332         driver = new MED_MED_RDWR_DRIVER22(fileName,ptrMed);
333       return driver;
334     }
335     default:
336       throw MED_EXCEPTION ("access type has not been properly specified to the method");
337     }
338 }
339
340 GENDRIVER * DRIVERFACTORY::buildMeshDriverFromFile(const string & fileName,
341                                                    MESH * ptrMesh,
342                                                    MED_EN::med_mode_acces access)
343 {
344   medFileVersion version;
345
346   try
347     {
348       version = getMedFileVersion(fileName);
349     }
350   catch (MEDEXCEPTION & ex)
351     {
352       version = DRIVERFACTORY::globalMedFileVersionForWriting;
353     }
354
355   MESSAGE_MED("buildMeshDriverFromFile version of the file " << version);
356
357   GENDRIVER * driver;
358
359   switch(access)
360     {
361     case RDONLY : {
362       if (version == V21)
363         driver = new MED_MESH_RDONLY_DRIVER21(fileName,ptrMesh);
364       else if (version == V22)
365         driver = new MED_MESH_RDONLY_DRIVER22(fileName,ptrMesh);
366       return driver;
367     }
368     case WRONLY : {
369       if (version == V21)
370         driver = new MED_MESH_WRONLY_DRIVER21(fileName,ptrMesh);
371       else if (version == V22)
372                                 driver = new MED_MESH_WRONLY_DRIVER22(fileName,ptrMesh,access);
373       return driver;
374     }
375     case RDWR : {
376       if (version == V21)
377         driver = new MED_MESH_RDWR_DRIVER21(fileName,ptrMesh);
378       else if (version == V22)
379         driver = new MED_MESH_RDWR_DRIVER22(fileName,ptrMesh);
380       return driver;
381     }
382     default:
383       throw MED_EXCEPTION ("access type has not been properly specified to the method");
384     }
385 }
386
387 GENDRIVER * DRIVERFACTORY::buildConcreteMedDriverForMesh(const std::string & fileName,
388                                                          MESH *ptrMesh,const string &  driverName,
389                                                          MED_EN::med_mode_acces access,
390                                                          MED_EN::medFileVersion version)
391 {
392   GENDRIVER * driver;
393
394   MESSAGE_MED("buildConcreteMedDriverForMesh version of the file " << version);
395
396   switch(access)
397     {
398     case RDONLY : {
399       if (version == V21)
400         driver = new MED_MESH_RDONLY_DRIVER21(fileName,ptrMesh);
401       else if (version == V22)
402         driver = new MED_MESH_RDONLY_DRIVER22(fileName,ptrMesh);
403       driver->setMeshName(driverName);
404       return driver;
405     }
406     case WRONLY : {
407       if (version == V21)
408         driver = new MED_MESH_WRONLY_DRIVER21(fileName,ptrMesh);
409       else if (version == V22)
410         driver = new MED_MESH_WRONLY_DRIVER22(fileName,ptrMesh);
411       driver->setMeshName(driverName);
412       return driver;
413     }
414     case RDWR : {
415       if (version == V21)
416         driver = new MED_MESH_RDWR_DRIVER21(fileName,ptrMesh);
417       else if (version == V22)
418         driver = new MED_MESH_RDWR_DRIVER22(fileName,ptrMesh);
419       driver->setMeshName(driverName);
420       return driver;
421     }
422     default:
423       throw MED_EXCEPTION ("access type has not been properly specified to the method");
424     }
425 }