#
# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
#
-
"""
these file is using in GHS3DPRL Plugin
to convert output files .mesh of Tepal V2
mesh2facespoints.py DOMAIN
"""
+import codecs
+import fileinput
+import glob
import os
+import shutil
import sys
-import glob
-import fileinput
-import string
-
-file_m='GHS3DPRL'
-if len(sys.argv)==2:
- file_m=sys.argv[1]
-
-def find_cr_or_not(fs,tag):
- """find number after tag with cr or sp"""
- for line in fs:
- if tag+'\n' in line:
- #print tag+'<cr>'
- res=fs.readline()
- break
- if tag+' ' in line:
- #print 'vertices<sp>'
- res=line.split()[1]
- break
- res=res.strip("\t\n ")
- print tag+' '+res
- return res
+
+
+file_m = 'GHS3DPRL'
+if len(sys.argv) == 2:
+ file_m = sys.argv[1]
+
+
+def find_cr_or_not(fs, tag):
+ """find number after tag with cr or sp"""
+ for line in fs:
+ if tag + '\n' in line:
+ # print tag+'<cr>'
+ res = fs.readline()
+ break
+ if tag + ' ' in line:
+ # print 'vertices<sp>'
+ res = line.split()[1]
+ break
+ res = res.strip("\t\n ")
+ print(tag + ' ' + res)
+ return res
+
def m2fp(f1):
- """convert .mesh file to .points and .faces and .noboite"""
- print '\nconversion '+f1+' to .points and .faces and .noboite'
- #fs=open(f1, 'r')
- #fs=fileinput.FileInput(f1,mode='r') #mode not in v2.4.4
- fs=fileinput.FileInput(f1)
- (shortname, extension)=os.path.splitext(f1)
- f2=shortname+'.points'
- print 'creating',f2
- fp=open(f2, 'w')
- nb=find_cr_or_not(fs,'Vertices')
- np=nb #for .noboite
- fp.write(nb+'\n')
- for i in xrange(0,int(nb)):
- fp.write(fs.readline())
- fp.close()
-
- f2=shortname+'.faces'
- print 'creating',f2
- ff=open(f2, 'w')
- nb=find_cr_or_not(fs,'Triangles')
- ff.write(nb+' 0\n')
- for i in xrange(0,int(nb)):
- ff.write('3 '+fs.readline().strip('\t\n ')+' 0 0 0\n')
- ff.close()
-
- ne=find_cr_or_not(fs,'Tetrahedra')
- f3=shortname+'.noboite'
- fb=open(f3, 'w')
- npfixe="0"
- fb.write(ne+' '+np+' '+npfixe+' 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n')
- for i in xrange(0,int(ne)):
- lig=fs.readline().strip('\t\n ')
- lig=lig.split()
- fb.write(lig[0]+" "+lig[1]+" "+lig[2]+" "+lig[3]+" ")
- fb.write('\n')
- fs.close()
- fs=fileinput.FileInput(shortname+'.points')
- nb=fs.readline() #eqal to np
- for i in xrange(0,int(nb)):
- lig=fs.readline().strip('\t\n ')
- lig=lig.split()
- fb.write(lig[0]+" "+lig[1]+" "+lig[2]+" ")
- fb.write('\n0\n') #subnumber
- fs.close()
- fb.close()
-
-def rename_tepal_v1(f1,imax):
- """rename files as version v1 of tepal expect"""
- (shortname, extension)=os.path.splitext(f1)
- fs=os.path.splitext(shortname)
- i=int(fs[1].strip('.'))
- ff=fs[0]+'.'+str(imax)+'.'+string.zfill(str(i),len(str(imax)))
- #noboite en ".32.02.noboite!"
- mvcp='mv ' #ou 'cp '
- f2=shortname+'.points' ; f3=ff+os.path.splitext(f2)[1]
- print f2,'->',f3 ; os.system(mvcp+f2+' '+f3)
- f2=shortname+'.faces' ; f3=ff+os.path.splitext(f2)[1]
- print f2,'->',f3 ; os.system(mvcp+f2+' '+f3)
- f2=shortname+'.noboite' ; f3=ff+os.path.splitext(f2)[1]
- print f2,'->',f3 ; os.system(mvcp+f2+' '+f3)
- f2=shortname+'.glo' ; f3=ff+os.path.splitext(f2)[1]
- print f2,'->',f3 ; os.system(mvcp+f2+' '+f3)
- f2=shortname+'.msg' ; f3=ff+os.path.splitext(f2)[1]
- print f2,'->',f3 ; os.system(mvcp+f2+' '+f3)
-
-def my_test(a): return int(os.path.basename(a).split('.')[1])
-
-f0=file_m+'.?????.mesh'
-#print f0
-fics=glob.glob(f0)
-fics.sort(lambda a, b: cmp(my_test(b), my_test(a))) #tri ordre decroissant
-print 'conversion of files:\n',fics
-
-imax=len(fics)
+ """convert .mesh file to .points and .faces and .noboite"""
+ print('\nconversion ' + f1 + ' to .points and .faces and .noboite')
+ # fs=open(f1, 'r')
+ # fs=fileinput.FileInput(f1,mode='r') #mode not in v2.4.4
+ fs = fileinput.FileInput(f1)
+ shortname = os.path.splitext(f1)[0]
+ f2 = shortname + '.points'
+ print('creating', f2)
+ with codecs.open(f2, 'w') as fp:
+ nb = find_cr_or_not(fs, 'Vertices')
+ np = nb # for .noboite
+ fp.write(nb + '\n')
+ for _ in range(int(nb)):
+ fp.write(fs.readline())
+
+ f2 = shortname + '.faces'
+ print('creating', f2)
+ with codecs.open(f2, 'w') as ff:
+ nb = find_cr_or_not(fs, 'Triangles')
+ ff.write(nb + ' 0\n')
+ for _ in range(int(nb)):
+ ff.write('3 ' + fs.readline().strip('\t\n ') + ' 0 0 0\n')
+
+ ne = find_cr_or_not(fs, 'Tetrahedra')
+ f3 = shortname + '.noboite'
+ with codecs.open(f3, 'w') as fb:
+ npfixe = "0"
+ fb.write(ne + ' ' + np + ' ' + npfixe + ' 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n')
+ for _ in range(int(ne)):
+ lig = fs.readline().strip('\t\n ')
+ lig = lig.split()
+ fb.write(lig[0] + " " + lig[1] + " " + lig[2] + " " + lig[3] + " ")
+ fb.write('\n')
+ fs.close()
+ fs = fileinput.FileInput(shortname + '.points')
+ nb = fs.readline() # eqal to np
+ for _ in range(int(nb)):
+ lig = fs.readline().strip('\t\n ')
+ lig = lig.split()
+ fb.write(lig[0] + " " + lig[1] + " " + lig[2] + " ")
+ fb.write('\n0\n') # subnumber
+ fs.close()
+
+
+def rename_tepal_v1(f1, imax):
+ """rename files as version v1 of tepal expect"""
+ shortname = os.path.splitext(f1)[0]
+ fs = os.path.splitext(shortname)
+ i = int(fs[1].strip('.'))
+ ff = fs[0] + '.' + str(imax) + '.' + str(i).zfill(len(str(imax)))
+ # noboite en ".32.02.noboite!"
+ f2 = shortname + '.points'
+ f3 = ff + os.path.splitext(f2)[1]
+ print(f2, '->', f3)
+ shutil.move(f2, f3)
+ f2 = shortname + '.faces'
+ f3 = ff + os.path.splitext(f2)[1]
+ print(f2, '->', f3)
+ shutil.move(f2, f3)
+ f2 = shortname + '.noboite'
+ f3 = ff + os.path.splitext(f2)[1]
+ print(f2, '->', f3)
+ shutil.move(f2, f3)
+ f2 = shortname + '.glo'
+ f3 = ff + os.path.splitext(f2)[1]
+ print(f2, '->', f3)
+ shutil.move(f2, f3)
+ f2 = shortname + '.msg'
+ f3 = ff + os.path.splitext(f2)[1]
+ print(f2, '->', f3)
+ shutil.move(f2, f3)
+
+
+def my_test(a):
+ return int(os.path.basename(a).split('.')[1])
+
+
+def cmp(a, b):
+ return (a > b) - (a < b)
+
+f0 = file_m + '.?????.mesh'
+# print f0
+fics = glob.glob(f0)
+fics.sort(lambda a, b: cmp(my_test(b), my_test(a))) # tri ordre decroissant
+print('conversion of files:\n', fics)
+
+imax = len(fics)
for f in fics:
- m2fp(f)
- rename_tepal_v1(f,imax)
+ m2fp(f)
+ rename_tepal_v1(f, imax)