Salome HOME
Porting to Python 3
[plugins/ghs3dprlplugin.git] / src / tools / mesh2facespoints.py
1 #!/usr/bin/env python
2 #  -*- coding: utf-8 -*-
3 # Copyright (C) 2007-2016  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 """
22 these file is using in GHS3DPRL Plugin
23 to convert output files .mesh of Tepal V2
24 to output files files .faces and .points of Tepal V1
25 assume compatibility GHS3DPRL Plugin Tepal V1 => Tepal V2
26 example of use (when Tepal V2):
27   facespoints2mesh.py GHS3DPRL
28   tepal2med --casename=GHS3DPRL --number=12 --medname=DOMAIN --launchtepal=no
29   mesh2facespoints.py DOMAIN
30 """
31
32 import codecs
33 import fileinput
34 import glob
35 import os
36 import shutil
37 import sys
38
39
40 file_m = 'GHS3DPRL'
41 if len(sys.argv) == 2:
42     file_m = sys.argv[1]
43
44
45 def find_cr_or_not(fs, tag):
46     """find number after tag with cr or sp"""
47     for line in fs:
48         if tag + '\n' in line:
49             # print tag+'<cr>'
50             res = fs.readline()
51             break
52         if tag + ' ' in line:
53             # print 'vertices<sp>'
54             res = line.split()[1]
55             break
56     res = res.strip("\t\n ")
57     print(tag + ' ' + res)
58     return res
59
60
61 def m2fp(f1):
62     """convert .mesh file to .points and .faces and .noboite"""
63     print('\nconversion ' + f1 + ' to .points and .faces and .noboite')
64     # fs=open(f1, 'r')
65     # fs=fileinput.FileInput(f1,mode='r') #mode not in v2.4.4
66     fs = fileinput.FileInput(f1)
67     shortname = os.path.splitext(f1)[0]
68     f2 = shortname + '.points'
69     print('creating', f2)
70     with codecs.open(f2, 'w') as fp:
71         nb = find_cr_or_not(fs, 'Vertices')
72         np = nb  # for .noboite
73         fp.write(nb + '\n')
74         for _ in range(int(nb)):
75             fp.write(fs.readline())
76
77     f2 = shortname + '.faces'
78     print('creating', f2)
79     with codecs.open(f2, 'w') as ff:
80         nb = find_cr_or_not(fs, 'Triangles')
81         ff.write(nb + ' 0\n')
82         for _ in range(int(nb)):
83             ff.write('3 ' + fs.readline().strip('\t\n ') + ' 0 0 0\n')
84
85     ne = find_cr_or_not(fs, 'Tetrahedra')
86     f3 = shortname + '.noboite'
87     with codecs.open(f3, 'w') as fb:
88         npfixe = "0"
89         fb.write(ne + ' ' + np + ' ' + npfixe + ' 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n')
90         for _ in range(int(ne)):
91             lig = fs.readline().strip('\t\n ')
92             lig = lig.split()
93             fb.write(lig[0] + " " + lig[1] + " " + lig[2] + " " + lig[3] + " ")
94         fb.write('\n')
95         fs.close()
96         fs = fileinput.FileInput(shortname + '.points')
97         nb = fs.readline()  # eqal to np
98         for _ in range(int(nb)):
99             lig = fs.readline().strip('\t\n ')
100             lig = lig.split()
101             fb.write(lig[0] + " " + lig[1] + " " + lig[2] + " ")
102         fb.write('\n0\n')  # subnumber
103         fs.close()
104
105
106 def rename_tepal_v1(f1, imax):
107     """rename files as version v1 of tepal expect"""
108     shortname = os.path.splitext(f1)[0]
109     fs = os.path.splitext(shortname)
110     i = int(fs[1].strip('.'))
111     ff = fs[0] + '.' + str(imax) + '.' + str(i).zfill(len(str(imax)))
112     # noboite en ".32.02.noboite!"
113     f2 = shortname + '.points'
114     f3 = ff + os.path.splitext(f2)[1]
115     print(f2, '->', f3)
116     shutil.move(f2, f3)
117     f2 = shortname + '.faces'
118     f3 = ff + os.path.splitext(f2)[1]
119     print(f2, '->', f3)
120     shutil.move(f2, f3)
121     f2 = shortname + '.noboite'
122     f3 = ff + os.path.splitext(f2)[1]
123     print(f2, '->', f3)
124     shutil.move(f2, f3)
125     f2 = shortname + '.glo'
126     f3 = ff + os.path.splitext(f2)[1]
127     print(f2, '->', f3)
128     shutil.move(f2, f3)
129     f2 = shortname + '.msg'
130     f3 = ff + os.path.splitext(f2)[1]
131     print(f2, '->', f3)
132     shutil.move(f2, f3)
133
134
135 def my_test(a):
136     return int(os.path.basename(a).split('.')[1])
137
138
139 def cmp(a, b):
140     return (a > b) - (a < b)
141
142 f0 = file_m + '.?????.mesh'
143 # print f0
144 fics = glob.glob(f0)
145 fics.sort(lambda a, b: cmp(my_test(b), my_test(a)))  # tri ordre decroissant
146 print('conversion of files:\n', fics)
147
148 imax = len(fics)
149 for f in fics:
150     m2fp(f)
151     rename_tepal_v1(f, imax)