Skip to content

Commit 48e84bc

Browse files
feat: Add VTKHDF files management (#419)
Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent d11dd28 commit 48e84bc

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Feat: Add VTKHDF files management
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Copyright (C) 2024 - 2025 ANSYS, Inc. and/or its affiliates.
2+
# SPDX-License-Identifier: MIT
3+
#
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
"""Utils module for VTKHDF management."""
24+
from pathlib import Path
25+
from typing import Union
26+
27+
import pyvista as pv
28+
import vtk
29+
from vtkmodules.vtkIOHDF import vtkHDFWriter
30+
31+
32+
def pd_to_vtkhdf(pd: pv.PolyData, output_vtkhdf_file: Union[str, Path]) -> Path:
33+
"""Write the PyVista PolyData directly to a VTKHDF file.
34+
35+
Parameters
36+
----------
37+
pd : pv.PolyData
38+
The PyVista PolyData to be written.
39+
output_vtkhdf_file : Union[str, Path]
40+
The output VTKHDF file path.
41+
42+
Returns
43+
-------
44+
Path
45+
The path to the saved VTKHDF file.
46+
"""
47+
writer = vtkHDFWriter()
48+
writer.SetFileName(str(output_vtkhdf_file))
49+
writer.SetInputDataObject(pd)
50+
writer.Write()
51+
return Path(output_vtkhdf_file)
52+
53+
54+
def vtkhdf_to_pd(input_vtkhdf_file: Union[str, Path]) -> pv.PolyData:
55+
"""Read a VTKHDF file and convert it to PyVista PolyData.
56+
57+
Parameters
58+
----------
59+
input_vtkhdf_file : Union[str, Path]
60+
The input VTKHDF file path.
61+
62+
Returns
63+
-------
64+
pv.PolyData
65+
The converted PyVista PolyData.
66+
"""
67+
reader = vtk.vtkHDFReader()
68+
reader.SetFileName(str(input_vtkhdf_file))
69+
reader.Update()
70+
# Convert VTK object directly to PyVista PolyData
71+
pd = pv.wrap(reader.GetOutput())
72+
73+
return pd
74+
75+
76+
def ug_to_vtkhdf(ug: pv.UnstructuredGrid, output_vtkhdf_file: Union[str, Path]) -> Path:
77+
"""Write the PyVista UnstructuredGrid directly to a VTKHDF file.
78+
79+
Parameters
80+
----------
81+
ug : pv.UnstructuredGrid
82+
The PyVista UnstructuredGrid to be written.
83+
output_vtkhdf_file : Union[str, Path]
84+
The output VTKHDF file path.
85+
86+
Returns
87+
-------
88+
Path
89+
The path to the saved VTKHDF file.
90+
"""
91+
writer = vtkHDFWriter()
92+
writer.SetFileName(str(output_vtkhdf_file))
93+
writer.SetInputDataObject(ug)
94+
writer.Write()
95+
return Path(output_vtkhdf_file)
96+
97+
98+
def vtkhdf_to_ug(input_vtkhdf_file: Union[str, Path]) -> pv.UnstructuredGrid:
99+
"""Read a VTKHDF file and convert it to PyVista UnstructuredGrid.
100+
101+
Parameters
102+
----------
103+
input_vtkhdf_file : Union[str, Path]
104+
The input VTKHDF file path.
105+
106+
Returns
107+
-------
108+
pv.UnstructuredGrid
109+
The converted PyVista UnstructuredGrid.
110+
"""
111+
reader = vtk.vtkHDFReader()
112+
reader.SetFileName(str(input_vtkhdf_file))
113+
reader.Update()
114+
# Convert VTK object directly to PyVista UnstructuredGrid
115+
ug = pv.wrap(reader.GetOutput())
116+
117+
return ug

tests/test_vtkhdf_converter.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright (C) 2024 - 2025 ANSYS, Inc. and/or its affiliates.
4+
# SPDX-License-Identifier: MIT
5+
#
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
25+
"""Test script to verify the VTKHDF converter functions work correctly."""
26+
27+
import pyvista as pv
28+
from pyvista import examples
29+
30+
from ansys.tools.visualization_interface.utils.vtkhdf_converter import (
31+
pd_to_vtkhdf,
32+
ug_to_vtkhdf,
33+
vtkhdf_to_pd,
34+
vtkhdf_to_ug,
35+
)
36+
37+
38+
def test_pd_conversion(tmp_path):
39+
"""Test PolyData to VTKHDF and back conversion."""
40+
print("Testing PolyData conversion...")
41+
42+
# Create a sphere
43+
original_sphere = pv.Sphere()
44+
45+
# Create temporary file path
46+
temp_file = tmp_path / "test_pd.vtkhdf"
47+
48+
# Convert to VTKHDF using Path object
49+
saved_path = pd_to_vtkhdf(original_sphere, temp_file)
50+
51+
# Verify the returned path matches the input
52+
assert saved_path == temp_file, "Returned path should match input path"
53+
54+
# Convert back using Path object
55+
loaded_sphere = vtkhdf_to_pd(temp_file)
56+
57+
# Verify they have the same number of points and cells
58+
assert original_sphere.n_points == loaded_sphere.n_points, "Point count mismatch"
59+
assert original_sphere.n_cells == loaded_sphere.n_cells, "Cell count mismatch"
60+
61+
def test_ug_conversion(tmp_path):
62+
"""Test UnstructuredGrid to VTKHDF and back conversion."""
63+
# Create an unstructured grid
64+
original_grid = pv.UnstructuredGrid(examples.hexbeamfile)
65+
66+
# Create temporary file path
67+
temp_file = tmp_path / "test_ug.vtkhdf"
68+
69+
# Convert to VTKHDF using Path object
70+
saved_path = ug_to_vtkhdf(original_grid, temp_file)
71+
72+
# Verify the returned path matches the input
73+
assert saved_path == temp_file, "Returned path should match input path"
74+
75+
# Convert back using Path object
76+
loaded_grid = vtkhdf_to_ug(temp_file)
77+
78+
# Verify they have the same number of points and cells
79+
assert original_grid.n_points == loaded_grid.n_points, "Point count mismatch"
80+
assert original_grid.n_cells == loaded_grid.n_cells, "Cell count mismatch"

0 commit comments

Comments
 (0)