Salome HOME
22612: [CEA 1189] sauv2med should not change faces orientation
[modules/med.git] / src / MEDLoader / Swig / medutilities.py
1 # -*- coding: iso-8859-1 -*-
2 # --
3 # Copyright (C) 2009-2014  CEA/DEN, EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 # Author : Erwan ADAM (CEA), Anthony GEAY (CEA)
22 # --
23
24 from MEDLoader import *
25
26 def my_remove(f):
27     from os import remove
28     try:
29         remove(f)
30     except OSError:
31         pass
32     return
33
34 def convert(file_in, driver_in, driver_out, format=1, file_out=None):
35     #
36     print file_in
37     #
38     if file_out is None:
39         file_out = file_in
40         if driver_out == "GIBI":
41             file_out += ".sauv"
42         elif driver_out == "MED":
43             file_out += ".med"
44         else:
45             msg = "Driver out %s is unknown"%(driver_out)
46             raise NotImplementedError(msg)
47         pass
48     print file_out
49     #
50     if driver_in == "GIBI":
51         sr = SauvReader.New(file_in)
52         mfd= sr.loadInMEDFileDS( format )
53         pass
54     elif driver_in == "MED":
55         mfd = MEDFileData(file_in)
56         pass
57     else:
58         raise NotImplementedError("Driver in %s is unknown"%(driver_in))
59     #
60     my_remove(file_out)
61     #
62     if driver_out == "GIBI":
63         sw=SauvWriter.New()
64         sw.setMEDFileDS(mfd,0);#0 ?
65         sw.write(file_out)
66         #
67         mesh = mfd.getMeshes()[0]
68         mesh_dim = mesh.getSpaceDimension()
69         if mesh_dim >= 3:
70             from sys import platform
71             if platform in ["win32"]:
72                 f = open(file_out)
73                 content = f.read()
74                 f.close()
75                 content = content.replace("IFOUR  -1", "IFOUR   2")
76                 content = content.replace("IFOMOD  -1", "IFOMOD   2")
77                 f = open(file_out, "w")
78                 f.write(content)
79                 f.close()
80             else:
81                 cmd  = "sed"
82                 cmd += ' -e "s/IFOUR  -1/IFOUR   2/g"'
83                 cmd += ' -e "s/IFOMOD  -1/IFOMOD   2/g"'
84                 # cmd += ' -e "s/IECHO   1/IECHO   0/g"'
85                 cmd += ' %s > .dummy'%(file_out)
86                 cmd += ' && '
87                 cmd += ' mv -f .dummy %s'%(file_out)
88                 from os import system
89                 system(cmd)
90                 pass
91             pass
92         #
93         if format == 0:
94             from castemlauncher import CastemLauncher
95             dgibi_stream  = "\n"
96             dgibi_stream += "OPTI REST FORMAT '%s' ;\n"%(file_out)
97             dgibi_stream += "REST FORMAT;\n"
98             file_out = file_out.replace('__format__', '')
99             dgibi_stream += "OPTI SAUV '%s' ;\n"%(file_out)
100             dgibi_stream += "SAUV ;\n"
101             cl = CastemLauncher(dgibi_stream)
102             cl.addTmpFiles(file_out+'__format__', "UTILNOTI", "UTILPROC")
103             cl.run()
104             pass
105         return
106     elif driver_out == "MED":
107         mfd.write(file_out,2)
108         return
109     else:
110         raise NotImplementedError("Driver in %s is unknown"%(driver_in))
111
112 def sauv2med(*argv):
113     keep2DOri = ( "--keep2DOri" in argv )
114     for arg in argv:
115         if not arg.startswith("--keep"):
116             convert(arg, "GIBI", "MED", format = keep2DOri)
117         pass
118     return
119
120 def med2sauv(*argv):
121     argv = list(argv)
122     format = 1
123     for arg in argv[:]:
124         if arg.find('--format') == 0:
125             argv.remove(arg)
126             try:
127                 value = arg.split("=")[1]
128             except IndexError:
129                 usage(1)
130                 pass
131             try:
132                 value = int(value)
133             except ValueError:
134                 usage(1)
135                 pass
136             format = value
137             pass
138         pass
139     for arg in argv:
140         convert(arg, "MED", "GIBI", format)
141         pass
142     return