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