]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_DriverFactory.cxx
Salome HOME
Join modifications from branch CEAFor_V3_2_0
[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 driverTypes DRIVERFACTORY::deduceDriverTypeFromFileName(const std::string & fileName)
65 {
66   string extension(fileName);
67   unsigned int pos=extension.rfind('.');
68   if(pos==string::npos)
69     return NO_DRIVER;
70   extension.erase(0,pos+1);
71   if(extension=="med")
72     return MED_DRIVER;
73   if(extension=="sauve" || extension=="sauv")
74     return GIBI_DRIVER;
75   if(extension=="cnc" || extension=="inp" || extension=="xyz")
76     return PORFLOW_DRIVER;
77   if(extension=="vtk")
78     return VTK_DRIVER;
79   return NO_DRIVER;
80 }
81
82 GENDRIVER *DRIVERFACTORY::buildDriverForMesh(driverTypes driverType,
83                                              const std::string & fileName,
84                                              MESH *mesh,
85                                              const string & driverName,
86                                              med_mode_acces access)
87 {
88   GENDRIVER *ret;
89   switch(driverType)
90     {
91     case MED_DRIVER : {
92       switch(access)
93         {
94         case MED_LECT : {
95           ret = new MED_MESH_RDONLY_DRIVER(fileName, mesh);
96           ret->setMeshName(driverName);
97           return ret;
98         }
99         case MED_ECRI : {
100           ret = new MED_MESH_WRONLY_DRIVER(fileName, mesh);
101           ret->setMeshName(driverName);
102           return ret;
103         }
104         case MED_REMP : {
105           ret = new MED_MESH_RDWR_DRIVER(fileName, mesh);
106           ret->setMeshName(driverName);
107           return ret;
108         }
109         default:
110           throw MED_EXCEPTION ("access type has not been properly specified to the method");
111         }
112       break;
113     }
114
115     case GIBI_DRIVER : {
116       switch(access)
117         {
118         case MED_LECT : {
119           ret=new GIBI_MESH_RDONLY_DRIVER(fileName,mesh);
120           return ret;
121         }
122         case MED_ECRI : {
123           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");
124         }
125         case MED_REMP : {
126           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");
127         }
128         default:
129           throw MED_EXCEPTION ("access type has not been properly specified to the method");
130         }
131       break;
132     }
133
134     case PORFLOW_DRIVER : {
135       switch(access)
136         {
137         case MED_LECT : {
138           ret=new PORFLOW_MESH_RDONLY_DRIVER(fileName,mesh);
139           return ret;
140         }
141         case MED_ECRI : {
142           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");
143         }
144         case MED_REMP : {
145           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");
146         }
147         default:
148           throw MED_EXCEPTION ("access type has not been properly specified to the method");
149         }
150       break;
151     }
152
153     case VTK_DRIVER : {
154       switch(access)
155         {
156         case MED_LECT : {
157           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");
158         }
159         case MED_ECRI : {
160           ret=new VTK_MESH_DRIVER(fileName,mesh);
161           return ret;
162         }
163         case MED_REMP : {
164           ret=new VTK_MESH_DRIVER(fileName,mesh);
165           return ret;
166         }
167         default:
168           throw MED_EXCEPTION ("access type has not been properly specified to the method");
169         }
170       break;
171     }
172
173     case NO_DRIVER : {
174       throw MED_EXCEPTION ("NO_DRIVER has been specified to the method which is not allowed");
175     }
176     default:
177       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");
178     }
179 }
180
181 GENDRIVER *DRIVERFACTORY::buildDriverForMed(driverTypes driverType,
182                                             const std::string & fileName,
183                                             MED *med, med_mode_acces access)
184 {
185   GENDRIVER *ret;
186
187   switch(driverType)
188     {
189     case MED_DRIVER : {
190       switch(access)
191         {
192         case MED_LECT : {
193           ret=new MED_MED_RDONLY_DRIVER(fileName,med);
194           break ;
195         }
196         case MED_ECRI : {
197           ret=new MED_MED_WRONLY_DRIVER(fileName,med);
198           break ;
199         }
200         case MED_REMP : {
201           ret=new MED_MED_RDWR_DRIVER(fileName,med);
202           break ;
203         }
204         default:
205           throw MED_EXCEPTION ("access type has not been properly specified to the method");
206         }
207       break;
208     }
209
210     case VTK_DRIVER : {
211       switch(access)
212         {
213         case MED_LECT : {
214           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");
215         }
216         case MED_ECRI : {
217           ret=new VTK_MED_DRIVER(fileName,med);
218           break ;
219         }
220         case MED_REMP : {
221           ret=new VTK_MED_DRIVER(fileName,med);
222           break ;
223         }
224         default:
225           throw MED_EXCEPTION ("access type has not been properly specified to the method");
226         }
227       break;
228     }
229
230     case GIBI_DRIVER : {
231       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");
232       break;
233     }
234
235     case PORFLOW_DRIVER : {
236       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");
237       break;
238     }
239
240     case NO_DRIVER : {
241       throw MED_EXCEPTION ("NO_DRIVER has been specified to the method which is not allowed");
242       break;
243     }
244     default:
245       throw MED_EXCEPTION ("NO_DRIVER has been specified to the method which is not allowed");
246     }
247   return ret;
248 }
249
250 GENDRIVER * DRIVERFACTORY::buildMedDriverFromFile(const string & fileName,
251                                                   MED * const ptrMed,
252                                                   MED_EN::med_mode_acces access)
253 {
254   medFileVersion version;
255
256   try
257     {
258       version = getMedFileVersion(fileName);
259     }
260   catch (MEDEXCEPTION & ex)
261     {
262       version = DRIVERFACTORY::globalMedFileVersionForWriting;
263     }
264
265   MESSAGE("buildMedDriverFromFile version of the file " << version);
266
267   GENDRIVER * driver;
268
269   switch(access)
270     {
271     case MED_LECT : {
272       if (version == V21)
273         driver = new MED_MED_RDONLY_DRIVER21(fileName,ptrMed);
274       else if (version == V22)
275         driver = new MED_MED_RDONLY_DRIVER22(fileName,ptrMed);
276       return driver;
277     }
278     case MED_ECRI : {
279       if (version == V21)
280         driver = new MED_MED_WRONLY_DRIVER21(fileName,ptrMed);
281       else if (version == V22)
282         driver = new MED_MED_WRONLY_DRIVER22(fileName,ptrMed);
283       return driver;
284     }
285     case MED_REMP : {
286       if (version == V21)
287         driver = new MED_MED_RDWR_DRIVER21(fileName,ptrMed);
288       else if (version == V22)
289         driver = new MED_MED_RDWR_DRIVER22(fileName,ptrMed);
290       return driver;
291     }
292     default:
293       throw MED_EXCEPTION ("access type has not been properly specified to the method");
294     }
295 }
296
297 GENDRIVER * DRIVERFACTORY::buildMeshDriverFromFile(const string & fileName,
298                                                    MESH * ptrMesh,
299                                                    MED_EN::med_mode_acces access)
300 {
301   medFileVersion version;
302
303   try
304     {
305       version = getMedFileVersion(fileName);
306     }
307   catch (MEDEXCEPTION & ex)
308     {
309       version = DRIVERFACTORY::globalMedFileVersionForWriting;
310     }
311
312   MESSAGE("buildMeshDriverFromFile version of the file " << version);
313
314   GENDRIVER * driver;
315
316   switch(access)
317     {
318     case MED_LECT : {
319       if (version == V21)
320         driver = new MED_MESH_RDONLY_DRIVER21(fileName,ptrMesh);
321       else if (version == V22)
322         driver = new MED_MESH_RDONLY_DRIVER22(fileName,ptrMesh);
323       return driver;
324     }
325     case MED_ECRI : {
326       if (version == V21)
327         driver = new MED_MESH_WRONLY_DRIVER21(fileName,ptrMesh);
328       else if (version == V22)
329         driver = new MED_MESH_WRONLY_DRIVER22(fileName,ptrMesh);
330       return driver;
331     }
332     case MED_REMP : {
333       if (version == V21)
334         driver = new MED_MESH_RDWR_DRIVER21(fileName,ptrMesh);
335       else if (version == V22)
336         driver = new MED_MESH_RDWR_DRIVER22(fileName,ptrMesh);
337       return driver;
338     }
339     default:
340       throw MED_EXCEPTION ("access type has not been properly specified to the method");
341     }
342 }
343
344 GENDRIVER * DRIVERFACTORY::buildConcreteMedDriverForMesh(const std::string & fileName,
345                                                          MESH *ptrMesh,const string &  driverName,
346                                                          MED_EN::med_mode_acces access,
347                                                          MED_EN::medFileVersion version)
348 {
349   GENDRIVER * driver;
350
351   MESSAGE("buildConcreteMedDriverForMesh version of the file " << version);
352
353   switch(access)
354     {
355     case MED_LECT : {
356       if (version == V21)
357         driver = new MED_MESH_RDONLY_DRIVER21(fileName,ptrMesh);
358       else if (version == V22)
359         driver = new MED_MESH_RDONLY_DRIVER22(fileName,ptrMesh);
360       driver->setMeshName(driverName);
361       return driver;
362     }
363     case MED_ECRI : {
364       if (version == V21)
365         driver = new MED_MESH_WRONLY_DRIVER21(fileName,ptrMesh);
366       else if (version == V22)
367         driver = new MED_MESH_WRONLY_DRIVER22(fileName,ptrMesh);
368       driver->setMeshName(driverName);
369       return driver;
370     }
371     case MED_REMP : {
372       if (version == V21)
373         driver = new MED_MESH_RDWR_DRIVER21(fileName,ptrMesh);
374       else if (version == V22)
375         driver = new MED_MESH_RDWR_DRIVER22(fileName,ptrMesh);
376       driver->setMeshName(driverName);
377       return driver;
378     }
379     default:
380       throw MED_EXCEPTION ("access type has not been properly specified to the method");
381     }
382 }