Salome HOME
Join modifications from branch V3_1_0_For_CCRT
[modules/med.git] / src / MEDMEM / MEDMEM_DriverFactory.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/
19 //
20 #include "MEDMEM_DriverFactory.hxx"
21 #include "MEDMEM_MedMedDriver.hxx"
22 #include "MEDMEM_MedMeshDriver.hxx"
23 #include "MEDMEM_Mesh.hxx"
24 #include "MEDMEM_GibiMeshDriver.hxx"
25 #include "MEDMEM_PorflowMeshDriver.hxx"
26 #include "MEDMEM_VtkMeshDriver.hxx"
27 #include "MEDMEM_VtkMedDriver.hxx"
28
29 #include "MEDMEM_Exception.hxx"
30
31 #include "MEDMEM_MedVersion.hxx"
32 #include "MEDMEM_Compatibility21_22.hxx"
33 #include "MEDMEM_MedMedDriver21.hxx"
34 #include "MEDMEM_MedMedDriver22.hxx"
35 #include "MEDMEM_MedMeshDriver21.hxx"
36 #include "MEDMEM_MedMeshDriver22.hxx"
37
38 using namespace MEDMEM;
39 using namespace MED_EN;
40
41 template<>
42 void MEDMEM::fill<-1,0x3>(double *a, const double *b)
43 {
44 }
45
46 template<>
47 bool MEDMEM::compare<-1>(const double *a, const double *b)
48 {
49   return false;
50 }
51
52 MED_EN::medFileVersion DRIVERFACTORY::globalMedFileVersionForWriting = MED_EN::V22;
53
54 medFileVersion DRIVERFACTORY::getMedFileVersionForWriting()
55 {
56   return DRIVERFACTORY::globalMedFileVersionForWriting;
57 }
58
59 void DRIVERFACTORY::setMedFileVersionForWriting(medFileVersion version)
60 {
61   DRIVERFACTORY::globalMedFileVersionForWriting = version;
62 }
63
64 GENDRIVER *DRIVERFACTORY::buildDriverForMesh(driverTypes driverType,
65                                              const std::string & fileName,
66                                              MESH *mesh,
67                                              const string & driverName,
68                                              med_mode_acces access)
69 {
70   GENDRIVER *ret;
71   switch(driverType)
72     {
73     case MED_DRIVER : {
74       switch(access)
75         {
76         case MED_LECT : {
77           ret = new MED_MESH_RDONLY_DRIVER(fileName, mesh);
78           ret->setMeshName(driverName);
79           return ret;
80         }
81         case MED_ECRI : {
82           ret = new MED_MESH_WRONLY_DRIVER(fileName, mesh);
83           ret->setMeshName(driverName);
84           return ret;
85         }
86         case MED_REMP : {
87           ret = new MED_MESH_RDWR_DRIVER(fileName, mesh);
88           ret->setMeshName(driverName);
89           return ret;
90         }
91         default:
92           throw MED_EXCEPTION ("access type has not been properly specified to the method");
93         }
94       break;
95     }
96
97     case GIBI_DRIVER : {
98       switch(access)
99         {
100         case MED_LECT : {
101           ret=new GIBI_MESH_RDONLY_DRIVER(fileName,mesh);
102           return ret;
103         }
104         case MED_ECRI : {
105           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");
106         }
107         case MED_REMP : {
108           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");
109         }
110         default:
111           throw MED_EXCEPTION ("access type has not been properly specified to the method");
112         }
113       break;
114     }
115
116     case PORFLOW_DRIVER : {
117       switch(access)
118         {
119         case MED_LECT : {
120           ret=new PORFLOW_MESH_RDONLY_DRIVER(fileName,mesh);
121           return ret;
122         }
123         case MED_ECRI : {
124           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");
125         }
126         case MED_REMP : {
127           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");
128         }
129         default:
130           throw MED_EXCEPTION ("access type has not been properly specified to the method");
131         }
132       break;
133     }
134
135     case VTK_DRIVER : {
136       switch(access)
137         {
138         case MED_LECT : {
139           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");
140         }
141         case MED_ECRI : {
142           ret=new VTK_MESH_DRIVER(fileName,mesh);
143           return ret;
144         }
145         case MED_REMP : {
146           ret=new VTK_MESH_DRIVER(fileName,mesh);
147           return ret;
148         }
149         default:
150           throw MED_EXCEPTION ("access type has not been properly specified to the method");
151         }
152       break;
153     }
154
155     case NO_DRIVER : {
156       throw MED_EXCEPTION ("NO_DRIVER has been specified to the method which is not allowed");
157     }
158     default:
159       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");
160     }
161 }
162
163 GENDRIVER *DRIVERFACTORY::buildDriverForMed(driverTypes driverType,
164                                             const std::string & fileName,
165                                             MED *med, med_mode_acces access)
166 {
167   GENDRIVER *ret;
168
169   switch(driverType)
170     {
171     case MED_DRIVER : {
172       switch(access)
173         {
174         case MED_LECT : {
175           ret=new MED_MED_RDONLY_DRIVER(fileName,med);
176           break ;
177         }
178         case MED_ECRI : {
179           ret=new MED_MED_WRONLY_DRIVER(fileName,med);
180           break ;
181         }
182         case MED_REMP : {
183           ret=new MED_MED_RDONLY_DRIVER(fileName,med);
184           break ;
185         }
186         default:
187           throw MED_EXCEPTION ("access type has not been properly specified to the method");
188         }
189       break;
190     }
191
192     case VTK_DRIVER : {
193       switch(access)
194         {
195         case MED_LECT : {
196           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");
197         }
198         case MED_ECRI : {
199           ret=new VTK_MED_DRIVER(fileName,med);
200           break ;
201         }
202         case MED_REMP : {
203           ret=new VTK_MED_DRIVER(fileName,med);
204           break ;
205         }
206         default:
207           throw MED_EXCEPTION ("access type has not been properly specified to the method");
208         }
209       break;
210     }
211
212     case GIBI_DRIVER : {
213       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");
214       break;
215     }
216
217     case PORFLOW_DRIVER : {
218       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");
219       break;
220     }
221
222     case NO_DRIVER : {
223       throw MED_EXCEPTION ("NO_DRIVER has been specified to the method which is not allowed");
224       break;
225     }
226     default:
227       throw MED_EXCEPTION ("NO_DRIVER has been specified to the method which is not allowed");
228     }
229   return ret;
230 }
231
232 GENDRIVER * DRIVERFACTORY::buildMedDriverFromFile(const string & fileName,
233                                                   MED * const ptrMed,
234                                                   MED_EN::med_mode_acces access)
235 {
236   medFileVersion version;
237
238   try
239     {
240       version = getMedFileVersion(fileName);
241     }
242   catch (MEDEXCEPTION & ex)
243     {
244       version = DRIVERFACTORY::globalMedFileVersionForWriting;
245     }
246
247   MESSAGE("buildMedDriverFromFile version of the file " << version);
248
249   GENDRIVER * driver;
250
251   switch(access)
252     {
253     case MED_LECT : {
254       if (version == V21)
255         driver = new MED_MED_RDONLY_DRIVER21(fileName,ptrMed);
256       else if (version == V22)
257         driver = new MED_MED_RDONLY_DRIVER22(fileName,ptrMed);
258       return driver;
259     }
260     case MED_ECRI : {
261       if (version == V21)
262         driver = new MED_MED_WRONLY_DRIVER21(fileName,ptrMed);
263       else if (version == V22)
264         driver = new MED_MED_WRONLY_DRIVER22(fileName,ptrMed);
265       return driver;
266     }
267     case MED_REMP : {
268       if (version == V21)
269         driver = new MED_MED_RDWR_DRIVER21(fileName,ptrMed);
270       else if (version == V22)
271         driver = new MED_MED_RDWR_DRIVER22(fileName,ptrMed);
272       return driver;
273     }
274     default:
275       throw MED_EXCEPTION ("access type has not been properly specified to the method");
276     }
277 }
278
279 GENDRIVER * DRIVERFACTORY::buildMeshDriverFromFile(const string & fileName,
280                                                    MESH * ptrMesh,
281                                                    MED_EN::med_mode_acces access)
282 {
283   medFileVersion version;
284
285   try
286     {
287       version = getMedFileVersion(fileName);
288     }
289   catch (MEDEXCEPTION & ex)
290     {
291       version = DRIVERFACTORY::globalMedFileVersionForWriting;
292     }
293
294   MESSAGE("buildMeshDriverFromFile version of the file " << version);
295
296   GENDRIVER * driver;
297
298   switch(access)
299     {
300     case MED_LECT : {
301       if (version == V21)
302         driver = new MED_MESH_RDONLY_DRIVER21(fileName,ptrMesh);
303       else if (version == V22)
304         driver = new MED_MESH_RDONLY_DRIVER22(fileName,ptrMesh);
305       return driver;
306     }
307     case MED_ECRI : {
308       if (version == V21)
309         driver = new MED_MESH_WRONLY_DRIVER21(fileName,ptrMesh);
310       else if (version == V22)
311         driver = new MED_MESH_WRONLY_DRIVER22(fileName,ptrMesh);
312       return driver;
313     }
314     case MED_REMP : {
315       if (version == V21)
316         driver = new MED_MESH_RDWR_DRIVER21(fileName,ptrMesh);
317       else if (version == V22)
318         driver = new MED_MESH_RDWR_DRIVER22(fileName,ptrMesh);
319       return driver;
320     }
321     default:
322       throw MED_EXCEPTION ("access type has not been properly specified to the method");
323     }
324 }
325
326 GENDRIVER * DRIVERFACTORY::buildConcreteMedDriverForMesh(const std::string & fileName,
327                                                          MESH *ptrMesh,const string &  driverName,
328                                                          MED_EN::med_mode_acces access,
329                                                          MED_EN::medFileVersion version)
330 {
331   GENDRIVER * driver;
332
333   MESSAGE("buildConcreteMedDriverForMesh version of the file " << version);
334
335   switch(access)
336     {
337     case MED_LECT : {
338       if (version == V21)
339         driver = new MED_MESH_RDONLY_DRIVER21(fileName,ptrMesh);
340       else if (version == V22)
341         driver = new MED_MESH_RDONLY_DRIVER22(fileName,ptrMesh);
342       driver->setMeshName(driverName);
343       return driver;
344     }
345     case MED_ECRI : {
346       if (version == V21)
347         driver = new MED_MESH_WRONLY_DRIVER21(fileName,ptrMesh);
348       else if (version == V22)
349         driver = new MED_MESH_WRONLY_DRIVER22(fileName,ptrMesh);
350       driver->setMeshName(driverName);
351       return driver;
352     }
353     case MED_REMP : {
354       if (version == V21)
355         driver = new MED_MESH_RDWR_DRIVER21(fileName,ptrMesh);
356       else if (version == V22)
357         driver = new MED_MESH_RDWR_DRIVER22(fileName,ptrMesh);
358       driver->setMeshName(driverName);
359       return driver;
360     }
361     default:
362       throw MED_EXCEPTION ("access type has not been properly specified to the method");
363     }
364 }