|
| 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 |
0 commit comments