Salome HOME
Merge from V6_main 01/04/2013
[modules/med.git] / src / MEDMEM / MEDMEM_DriverFactory.cxx
1 // Copyright (C) 2007-2013  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
23 #include "MEDMEM_DriverFactory.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
33 #include "MEDMEM_Exception.hxx"
34
35 #include "MEDMEM_MedVersion.hxx"
36 #include "MEDMEM_MedMeshDriver.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 bool DRIVERFACTORY::globalVtkBinaryFormatForWriting = false;
53
54 bool DRIVERFACTORY::getVtkBinaryFormatForWriting()
55 {
56   return globalVtkBinaryFormatForWriting;
57 }
58
59 void DRIVERFACTORY::setVtkBinaryFormatForWriting(bool isBinary)
60 {
61   globalVtkBinaryFormatForWriting = isBinary;
62 }
63
64 driverTypes DRIVERFACTORY::deduceDriverTypeFromFileName(const std::string & fileName)
65 {
66   string extension(fileName);
67   string::size_type 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   if(extension=="case")
80     return ENSIGHT_DRIVER;
81   return NO_DRIVER;
82 }
83
84 GENDRIVER *DRIVERFACTORY::buildDriverForMesh(driverTypes driverType,
85                                              const std::string & fileName,
86                                              GMESH *mesh,
87                                              const string & driverName,
88                                              med_mode_acces access)
89 {
90   GENDRIVER *ret;
91   switch(driverType)
92     {
93     case MED_DRIVER : {
94       switch(access)
95         {
96         case RDONLY : {
97           ret = new MED_MESH_RDONLY_DRIVER(fileName, mesh);
98           ret->setMeshName(driverName);
99           return ret;
100         }
101         case WRONLY : {
102           ret = new MED_MESH_WRONLY_DRIVER(fileName, mesh, access);
103           ret->setMeshName(driverName);
104           return ret;
105         }
106         case RDWR : {
107           ret = new MED_MESH_RDWR_DRIVER(fileName, mesh);
108           ret->setMeshName(driverName);
109           return ret;
110         }
111         default:
112           throw MED_EXCEPTION ("access type has not been properly specified to the method");
113         }
114       break;
115     }
116
117     case GIBI_DRIVER : {
118       if ( mesh->getIsAGrid() )
119         throw MED_EXCEPTION("GIBI file can contain unstructured mesh only, not a GRID");
120       switch(access)
121         {
122         case RDONLY : {
123           ret=new GIBI_MESH_RDONLY_DRIVER(fileName,(MESH*)mesh);
124           return ret;
125         }
126         case RDWR :
127           ret=new GIBI_MESH_RDWR_DRIVER(fileName,(MESH*)mesh);
128           return ret;
129           
130         case WRONLY :{
131           ret=new GIBI_MESH_WRONLY_DRIVER(fileName,(MESH*)mesh);
132           return ret;
133         }
134         default:
135           throw MED_EXCEPTION ("access type has not been properly specified to the method");
136         }
137       break;
138     }
139
140     case PORFLOW_DRIVER : {
141       if ( mesh->getIsAGrid() )
142         throw MED_EXCEPTION("PORFLOW file can contain unstructured mesh only, not a GRID");
143       switch(access)
144         {
145         case RDONLY : {
146           ret=new PORFLOW_MESH_RDONLY_DRIVER(fileName,(MESH*)mesh);
147           return ret;
148         }
149         case RDWR :
150         case WRONLY : {
151           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");
152         }
153         default:
154           throw MED_EXCEPTION ("access type has not been properly specified to the method");
155         }
156       break;
157     }
158
159     case ENSIGHT_DRIVER : {
160       if ( mesh->getIsAGrid() )
161         throw MED_EXCEPTION("EnSight driver reads unstructured mesh, not a GRID");
162       switch(access)
163         {
164         case RDONLY : {
165           ret=new ENSIGHT_MESH_RDONLY_DRIVER(fileName,(MESH*)mesh);
166           return ret;
167         }
168         case WRONLY : {
169           ret=new ENSIGHT_MESH_WRONLY_DRIVER(fileName,(MESH*)mesh);
170           return ret;
171         }
172         case RDWR : {
173           throw MED_EXCEPTION ("not yet implemented");
174           return ret;
175         }
176         default:
177           throw MED_EXCEPTION ("access type has not been properly specified to the method");
178         }
179       break;
180     }
181
182     case VTK_DRIVER : {
183       switch(access)
184         {
185         case RDONLY : {
186           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");
187         }
188         case RDWR :
189         case WRONLY : {
190           ret=new VTK_MESH_DRIVER(fileName,mesh);
191           return ret;
192         }
193
194         default:
195           throw MED_EXCEPTION ("access type has not been properly specified to the method");
196         }
197       break;
198     }
199
200     case NO_DRIVER : {
201       throw MED_EXCEPTION ("NO_DRIVER has been specified to the method 1 which is not allowed");
202     }
203     default:
204       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");
205     }
206 }
207
208 GENDRIVER * DRIVERFACTORY::buildMeshDriverFromFile(const string &         fileName,
209                                                    GMESH *                ptrMesh,
210                                                    MED_EN::med_mode_acces access)
211 {
212   medFileVersion version = MED_EN::V22;
213
214   try
215     {
216       version = getMedFileVersion(fileName);
217     }
218   catch (MEDEXCEPTION & ex)
219     {
220     }
221
222   MESSAGE_MED("buildMeshDriverFromFile version of the file " << version);
223
224   if (version == MED_EN::V21)
225     throw MED_EXCEPTION ("med-2.1 files are no more supported");
226
227   GENDRIVER * driver=0;
228
229   switch(access)
230     {
231     case RDONLY : {
232       driver = new MED_MESH_RDONLY_DRIVER(fileName,ptrMesh);
233       return driver;
234     }
235     case WRONLY : {
236       driver = new MED_MESH_WRONLY_DRIVER(fileName,ptrMesh,access);
237       return driver;
238     }
239     case RDWR : {
240       driver = new MED_MESH_RDWR_DRIVER(fileName,ptrMesh);
241       return driver;
242     }
243     default:
244       throw MED_EXCEPTION ("access type has not been properly specified to the method");
245     }
246   return driver;
247 }
248
249 GENDRIVER * DRIVERFACTORY::buildConcreteMedDriverForMesh(const std::string &    fileName,
250                                                          GMESH *                ptrMesh,
251                                                          const string &         driverName,
252                                                          MED_EN::med_mode_acces access,
253                                                          MED_EN::medFileVersion version)
254 {
255   GENDRIVER * driver=0;
256
257   MESSAGE_MED("buildConcreteMedDriverForMesh version of the file " << version);
258
259   if (version == MED_EN::V21)
260     throw MED_EXCEPTION ("med-2.1 files are no more supported");
261
262   switch(access)
263     {
264     case RDONLY : {
265       driver = new MED_MESH_RDONLY_DRIVER(fileName,ptrMesh);
266       driver->setMeshName(driverName);
267       return driver;
268     }
269     case WRONLY : {
270       driver = new MED_MESH_WRONLY_DRIVER(fileName,ptrMesh);
271       driver->setMeshName(driverName);
272       return driver;
273     }
274     case RDWR : {
275       driver = new MED_MESH_RDWR_DRIVER(fileName,ptrMesh);
276       driver->setMeshName(driverName);
277       return driver;
278     }
279     default:
280       throw MED_EXCEPTION ("access type has not been properly specified to the method");
281     }
282   return driver;
283 }