1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # =======================================
22 # Testing export/import of sample meshes from meshio repository
23 # File : SMESH_MeshioFiles.py
29 from pathlib import Path
31 from SMESH_MeshioShapes import export_mesh, import_file
34 def supported_extensions():
36 Returns a list of format extensions that salome already supports
37 for import/export operations.
50 def convert(source_file, target_file, errors):
52 Converts files with meshio shell command.
55 cmd = 'meshio convert {} {}'.format(source_file, target_file)
56 print('{} start...'.format(cmd))
58 subprocess.run(['meshio', 'convert', source_file, target_file], check=True)
63 source_ext = Path(source_file).suffix.upper()
64 target_ext = Path(target_file).suffix.upper()
66 errors.append('[{} -> {}]: {} failed!'.format(source_ext, target_ext, cmd))
71 def print_errors(errors):
73 Checks if we have got any saved error messages and print them.
80 for idx, err in enumerate(errors):
81 print('{:02}: {}'.format(idx, err))
83 print('\n===============================================')
86 def perform_ext(directory, ext, errors):
88 Exports a given mesh and imports it back for each mesh file
89 through a file with a given extension.
90 It uses shell meshio convert command for conversion.
93 for subdir, _, files in os.walk(directory):
97 if file.endswith('.md') or file.endswith('Makefile'):
100 filepath = os.path.join(subdir, file)
102 with tempfile.NamedTemporaryFile(suffix=ext) as temp_file:
103 temp_file.close() # prevents PermissionError on Windows
104 if not convert(filepath, temp_file.name, errors):
107 file_extension = Path(file).suffix
108 with tempfile.NamedTemporaryFile(suffix=file_extension) as temp_file_back:
109 temp_file.close() # prevents PermissionError on Windows
110 temp_file_back.close() # prevents PermissionError on Windows
111 convert(temp_file.name, temp_file_back.name, errors)
114 def test_shell(directory, errors):
116 Iterates salome extensions and convert meshes through
117 a corresponding format.
121 for ext in supported_extensions():
122 perform_ext(directory, ext, errors)
125 def test_salome(directory, errors):
127 Iterates over mesh files in a given directory and tries to import
128 them into salome and export back to a temp file with the same format.
131 for subdir, _, files in os.walk(directory):
135 if file.endswith('.md') or file.endswith('Makefile'):
138 # Skip files those have issues with current meshio version
139 if (file.endswith('.f3grid') or
140 file.endswith('.ugrid') or
141 file.endswith('.su2') or
142 file.endswith('element_elset.inp') or
143 file.endswith('insulated-4.1.msh') or
144 file.endswith('insulated-2.2.msh') or
145 file.endswith('simple1')):
148 filepath = os.path.join(subdir, file)
151 mesh = import_file(filepath, errors)
155 file_extension = Path(file).suffix
156 with tempfile.NamedTemporaryFile(suffix=file_extension) as temp_file:
157 temp_file.close() # prevents PermissionError on Windows
158 export_mesh(mesh, temp_file.name, errors)
163 Tests import/export of meshes from a given directory
164 and prints errors if we have any.
168 directory = 'data/meshio_meshes'
170 # Only for debug purpose.
171 # Uncomment if you need to test meshio library shell command itself.
172 # test_shell(directory, errors)
174 test_salome(directory, errors)
177 assert not len(errors)
180 if __name__ == "__main__":