From 6ef1ebc5eb15fd7a5dc8977ea0f84a02e9b0913a Mon Sep 17 00:00:00 2001
From: Andrew Maclean <andrew.amaclean@gmail.com>
Date: Mon, 17 Jun 2024 15:09:21 +1000
Subject: [PATCH] Adding WriteLegacyLinearCells and WriteXMLLinearCells

---
 src/PythonicAPI.md                           |   2 +
 src/PythonicAPI/IO/WriteLegacyLinearCells.md |   9 +
 src/PythonicAPI/IO/WriteLegacyLinearCells.py | 179 ++++++++++++++++++
 src/PythonicAPI/IO/WriteXMLLinearCells.md    |   9 +
 src/PythonicAPI/IO/WriteXMLLinearCells.py    | 187 +++++++++++++++++++
 5 files changed, 386 insertions(+)
 create mode 100644 src/PythonicAPI/IO/WriteLegacyLinearCells.md
 create mode 100755 src/PythonicAPI/IO/WriteLegacyLinearCells.py
 create mode 100644 src/PythonicAPI/IO/WriteXMLLinearCells.md
 create mode 100755 src/PythonicAPI/IO/WriteXMLLinearCells.py

diff --git a/src/PythonicAPI.md b/src/PythonicAPI.md
index 970a05b3fe6..395110e23ab 100644
--- a/src/PythonicAPI.md
+++ b/src/PythonicAPI.md
@@ -85,6 +85,8 @@ This Python script, [SelectExamples](../PythonicAPI/Utilities/SelectExamples), w
 | Example Name | Description | Image |
 | -------------- | ------------- | ------- |
 [ReadLegacyUnstructuredGrid](/PythonicAPI/IO/ReadLegacyUnstructuredGrid) | Read an unstructured grid that contains 11 linear cells.
+[WriteLegacyLinearCells](/PythonicAPI/IO/WriteLegacyLinearCells) | Write each linear cell into a legacy UnstructuredGrid file (.vtk).
+[WriteXMLLinearCells](/PythonicAPI/IO/WriteXMLLinearCells) | Write each linear cell into an XML UnstructuredGrid file (.vtu).
 
 ### Image Format
 
diff --git a/src/PythonicAPI/IO/WriteLegacyLinearCells.md b/src/PythonicAPI/IO/WriteLegacyLinearCells.md
new file mode 100644
index 00000000000..95000b36303
--- /dev/null
+++ b/src/PythonicAPI/IO/WriteLegacyLinearCells.md
@@ -0,0 +1,9 @@
+### Description
+
+This example uses vtkUnstructuredGridWriter to write each linear cell into a *.vtk* file. The files are written into the current directory.
+
+!!! seealso
+    [WriteXMLLinearCells](../WriteXMLLinearCells) writes the same files using the VTK XML format.
+
+!!! cite
+    The [VTK File Formats](https://docs.vtk.org/en/latest/design_documents/VTKFileFormats.html) document discusses the XML file format.
diff --git a/src/PythonicAPI/IO/WriteLegacyLinearCells.py b/src/PythonicAPI/IO/WriteLegacyLinearCells.py
new file mode 100755
index 00000000000..564ee49d381
--- /dev/null
+++ b/src/PythonicAPI/IO/WriteLegacyLinearCells.py
@@ -0,0 +1,179 @@
+#!/usr/bin/env python3
+
+# noinspection PyUnresolvedReferences
+import vtkmodules.vtkInteractionStyle
+# noinspection PyUnresolvedReferences
+import vtkmodules.vtkRenderingOpenGL2
+from vtkmodules.vtkCommonCore import vtkPoints
+from vtkmodules.vtkCommonDataModel import (
+    vtkHexagonalPrism,
+    vtkHexahedron,
+    vtkLine,
+    vtkPentagonalPrism,
+    vtkPixel,
+    vtkPolyLine,
+    vtkPolyVertex,
+    vtkPolygon,
+    vtkPyramid,
+    vtkQuad,
+    vtkTetra,
+    vtkTriangle,
+    vtkTriangleStrip,
+    vtkUnstructuredGrid,
+    vtkVertex,
+    vtkVoxel,
+    vtkWedge
+)
+from vtkmodules.vtkIOLegacy import vtkUnstructuredGridWriter
+
+
+def main():
+    linear_cells = make_linear_cells()
+
+    # Write each linear cell into  a file
+    for k, v in linear_cells.items():
+        file_name = k + '.vtk'
+        print(f'Writing: {file_name}')
+        writer = vtkUnstructuredGridWriter(file_name=file_name)
+        v >> writer
+        writer.Write()
+
+
+def make_linear_cells():
+    linear_cells = dict()
+
+    linear_cells['Vertex'] = make_unstructured_grid(vtkVertex())
+    linear_cells['PolyVertex'] = make_poly_vertex()
+    linear_cells['Line'] = make_unstructured_grid(vtkLine())
+    linear_cells['PolyLine'] = make_poly_line()
+    linear_cells['Triangle'] = make_unstructured_grid(vtkTriangle())
+    linear_cells['TriangleStrip'] = make_triangle_strip()
+    linear_cells['Polygon'] = make_polygon()
+    linear_cells['Pixel'] = make_unstructured_grid(vtkPixel())
+    linear_cells['Quad'] = make_unstructured_grid(vtkQuad())
+    linear_cells['Tetra'] = make_unstructured_grid(vtkTetra())
+    linear_cells['Voxel'] = make_unstructured_grid(vtkVoxel())
+    linear_cells['Hexahedron'] = make_unstructured_grid(vtkHexahedron())
+    linear_cells['Wedge'] = make_unstructured_grid(vtkWedge())
+    linear_cells['Pyramid'] = make_unstructured_grid(vtkPyramid())
+    linear_cells['PentagonalPrism'] = make_unstructured_grid(vtkPentagonalPrism())
+    linear_cells['HexagonalPrism'] = make_unstructured_grid(vtkHexagonalPrism())
+
+    return linear_cells
+
+
+def make_unstructured_grid(a_cell):
+    pcoords = a_cell.parametric_coords
+    for i in range(0, a_cell.number_of_points):
+        a_cell.point_ids.SetId(i, i)
+        a_cell.points.SetPoint(i, (pcoords[3 * i]), (pcoords[3 * i + 1]), (pcoords[3 * i + 2]))
+
+    ug = vtkUnstructuredGrid()
+    ug.points = a_cell.points
+    ug.InsertNextCell(a_cell.cell_type, a_cell.point_ids)
+    return ug
+
+
+def make_poly_vertex():
+    # A polyvertex is a cell representing a set of 3D vertices.
+    number_of_vertices = 6
+
+    points = vtkPoints()
+    points.InsertNextPoint(0, 0, 0)
+    points.InsertNextPoint(1, 0, 0)
+    points.InsertNextPoint(0, 1, 0)
+    points.InsertNextPoint(0, 0, 1)
+    points.InsertNextPoint(1, 0, .4)
+    points.InsertNextPoint(0, 1, .6)
+
+    poly_vertex = vtkPolyVertex()
+    poly_vertex.point_ids.SetNumberOfIds(number_of_vertices)
+
+    for i in range(0, number_of_vertices):
+        poly_vertex.point_ids.SetId(i, i)
+
+    ug = vtkUnstructuredGrid()
+    ug.SetPoints(points)
+    ug.InsertNextCell(poly_vertex.cell_type, poly_vertex.point_ids)
+
+    return ug
+
+
+def make_poly_line():
+    # A polyline is a cell that represents a set of 1D lines
+    number_of_vertices = 5
+
+    points = vtkPoints()
+    points.InsertNextPoint(0, .5, 0)
+    points.InsertNextPoint(.5, 0, 0)
+    points.InsertNextPoint(1, .3, 0)
+    points.InsertNextPoint(1.5, .4, 0)
+    points.InsertNextPoint(2.0, .4, 0)
+
+    poly_line = vtkPolyLine()
+    poly_line.point_ids.SetNumberOfIds(number_of_vertices)
+
+    for i in range(0, number_of_vertices):
+        poly_line.point_ids.SetId(i, i)
+
+    ug = vtkUnstructuredGrid()
+    ug.SetPoints(points)
+    ug.InsertNextCell(poly_line.cell_type, poly_line.point_ids)
+
+    return ug
+
+
+def make_triangle_strip():
+    # A triangle is a cell that represents a triangle strip
+    number_of_vertices = 10
+
+    points = vtkPoints()
+    points.InsertNextPoint(0, 0, 0)
+    points.InsertNextPoint(.5, 1, 0)
+    points.InsertNextPoint(1, -.1, 0)
+    points.InsertNextPoint(1.5, .8, 0)
+    points.InsertNextPoint(2.0, -.1, 0)
+    points.InsertNextPoint(2.5, .9, 0)
+    points.InsertNextPoint(3.0, 0, 0)
+    points.InsertNextPoint(3.5, .8, 0)
+    points.InsertNextPoint(4.0, -.2, 0)
+    points.InsertNextPoint(4.5, 1.1, 0)
+
+    triangle_strip = vtkTriangleStrip()
+    triangle_strip.point_ids.SetNumberOfIds(number_of_vertices)
+    for i in range(0, number_of_vertices):
+        triangle_strip.point_ids.SetId(i, i)
+
+    ug = vtkUnstructuredGrid()
+    ug.SetPoints(points)
+    ug.InsertNextCell(triangle_strip.cell_type, triangle_strip.point_ids)
+
+    return ug
+
+
+def make_polygon():
+    # A polygon is a cell that represents a polygon
+    number_of_vertices = 6
+
+    points = vtkPoints()
+    points.InsertNextPoint(0, 0, 0)
+    points.InsertNextPoint(1, -.1, 0)
+    points.InsertNextPoint(.8, .5, 0)
+    points.InsertNextPoint(1, 1, 0)
+    points.InsertNextPoint(.6, 1.2, 0)
+    points.InsertNextPoint(0, .8, 0)
+
+    polygon = vtkPolygon()
+    polygon.point_ids.SetNumberOfIds(number_of_vertices)
+    for i in range(0, number_of_vertices):
+        polygon.point_ids.SetId(i, i)
+
+    ug = vtkUnstructuredGrid()
+    ug.SetPoints(points)
+    ug.InsertNextCell(polygon.cell_type, polygon.point_ids)
+
+    return ug
+
+
+if __name__ == '__main__':
+    main()
diff --git a/src/PythonicAPI/IO/WriteXMLLinearCells.md b/src/PythonicAPI/IO/WriteXMLLinearCells.md
new file mode 100644
index 00000000000..3269aaabfe7
--- /dev/null
+++ b/src/PythonicAPI/IO/WriteXMLLinearCells.md
@@ -0,0 +1,9 @@
+### Description
+
+This example uses vtkXMLDataSetWriter to write each linear cell into a *.vtu* file. The files are written into the current directory.
+
+!!! seealso
+    [WriteLegacyLinearCells](../WriteLegacyLinearCells) writes the same files using the VTK legacy format.
+
+!!! cite
+    The [VTK File Formats](https://docs.vtk.org/en/latest/design_documents/VTKFileFormats.html) document discusses the XML file format.
diff --git a/src/PythonicAPI/IO/WriteXMLLinearCells.py b/src/PythonicAPI/IO/WriteXMLLinearCells.py
new file mode 100755
index 00000000000..f3f31fe4622
--- /dev/null
+++ b/src/PythonicAPI/IO/WriteXMLLinearCells.py
@@ -0,0 +1,187 @@
+#!/usr/bin/env python3
+
+# noinspection PyUnresolvedReferences
+import vtkmodules.vtkInteractionStyle
+# noinspection PyUnresolvedReferences
+import vtkmodules.vtkRenderingOpenGL2
+from vtkmodules.vtkCommonCore import vtkPoints
+from vtkmodules.vtkCommonDataModel import (
+    vtkHexagonalPrism,
+    vtkHexahedron,
+    vtkLine,
+    vtkPentagonalPrism,
+    vtkPixel,
+    vtkPolyLine,
+    vtkPolyVertex,
+    vtkPolygon,
+    vtkPyramid,
+    vtkQuad,
+    vtkTetra,
+    vtkTriangle,
+    vtkTriangleStrip,
+    vtkUnstructuredGrid,
+    vtkVertex,
+    vtkVoxel,
+    vtkWedge
+)
+from vtkmodules.vtkIOXML import vtkXMLDataSetWriter
+
+
+def main():
+    linear_cells = make_linear_cells()
+
+    # Write each linear cell into  a file
+    for k, v in linear_cells.items():
+        file_name = k + '.vtu'
+        print(f'Writing: {file_name}')
+        writer = vtkXMLDataSetWriter(file_name=file_name)
+        v >> writer
+        writer.Write()
+
+    # # Write each grid into  a file
+    # for i in range(0, len(uGrids)):
+    #     print('Writing: ', filenames[i])
+    #     writer = vtkXMLDataSetWriter()
+    #     writer.SetFileName(filenames[i])
+    #     writer.SetInputData(uGrids[i])
+    #     writer.Write()
+
+
+def make_linear_cells():
+    linear_cells = dict()
+
+    linear_cells['Vertex'] = make_unstructured_grid(vtkVertex())
+    linear_cells['PolyVertex'] = make_poly_vertex()
+    linear_cells['Line'] = make_unstructured_grid(vtkLine())
+    linear_cells['PolyLine'] = make_poly_line()
+    linear_cells['Triangle'] = make_unstructured_grid(vtkTriangle())
+    linear_cells['TriangleStrip'] = make_triangle_strip()
+    linear_cells['Polygon'] = make_polygon()
+    linear_cells['Pixel'] = make_unstructured_grid(vtkPixel())
+    linear_cells['Quad'] = make_unstructured_grid(vtkQuad())
+    linear_cells['Tetra'] = make_unstructured_grid(vtkTetra())
+    linear_cells['Voxel'] = make_unstructured_grid(vtkVoxel())
+    linear_cells['Hexahedron'] = make_unstructured_grid(vtkHexahedron())
+    linear_cells['Wedge'] = make_unstructured_grid(vtkWedge())
+    linear_cells['Pyramid'] = make_unstructured_grid(vtkPyramid())
+    linear_cells['PentagonalPrism'] = make_unstructured_grid(vtkPentagonalPrism())
+    linear_cells['HexagonalPrism'] = make_unstructured_grid(vtkHexagonalPrism())
+
+    return linear_cells
+
+
+def make_unstructured_grid(a_cell):
+    pcoords = a_cell.parametric_coords
+    for i in range(0, a_cell.number_of_points):
+        a_cell.point_ids.SetId(i, i)
+        a_cell.points.SetPoint(i, (pcoords[3 * i]), (pcoords[3 * i + 1]), (pcoords[3 * i + 2]))
+
+    ug = vtkUnstructuredGrid()
+    ug.points = a_cell.points
+    ug.InsertNextCell(a_cell.cell_type, a_cell.point_ids)
+    return ug
+
+
+def make_poly_vertex():
+    # A polyvertex is a cell representing a set of 3D vertices.
+    number_of_vertices = 6
+
+    points = vtkPoints()
+    points.InsertNextPoint(0, 0, 0)
+    points.InsertNextPoint(1, 0, 0)
+    points.InsertNextPoint(0, 1, 0)
+    points.InsertNextPoint(0, 0, 1)
+    points.InsertNextPoint(1, 0, .4)
+    points.InsertNextPoint(0, 1, .6)
+
+    poly_vertex = vtkPolyVertex()
+    poly_vertex.point_ids.SetNumberOfIds(number_of_vertices)
+
+    for i in range(0, number_of_vertices):
+        poly_vertex.point_ids.SetId(i, i)
+
+    ug = vtkUnstructuredGrid()
+    ug.SetPoints(points)
+    ug.InsertNextCell(poly_vertex.cell_type, poly_vertex.point_ids)
+
+    return ug
+
+
+def make_poly_line():
+    # A polyline is a cell that represents a set of 1D lines
+    number_of_vertices = 5
+
+    points = vtkPoints()
+    points.InsertNextPoint(0, .5, 0)
+    points.InsertNextPoint(.5, 0, 0)
+    points.InsertNextPoint(1, .3, 0)
+    points.InsertNextPoint(1.5, .4, 0)
+    points.InsertNextPoint(2.0, .4, 0)
+
+    poly_line = vtkPolyLine()
+    poly_line.point_ids.SetNumberOfIds(number_of_vertices)
+
+    for i in range(0, number_of_vertices):
+        poly_line.point_ids.SetId(i, i)
+
+    ug = vtkUnstructuredGrid()
+    ug.SetPoints(points)
+    ug.InsertNextCell(poly_line.cell_type, poly_line.point_ids)
+
+    return ug
+
+
+def make_triangle_strip():
+    # A triangle is a cell that represents a triangle strip
+    number_of_vertices = 10
+
+    points = vtkPoints()
+    points.InsertNextPoint(0, 0, 0)
+    points.InsertNextPoint(.5, 1, 0)
+    points.InsertNextPoint(1, -.1, 0)
+    points.InsertNextPoint(1.5, .8, 0)
+    points.InsertNextPoint(2.0, -.1, 0)
+    points.InsertNextPoint(2.5, .9, 0)
+    points.InsertNextPoint(3.0, 0, 0)
+    points.InsertNextPoint(3.5, .8, 0)
+    points.InsertNextPoint(4.0, -.2, 0)
+    points.InsertNextPoint(4.5, 1.1, 0)
+
+    triangle_strip = vtkTriangleStrip()
+    triangle_strip.point_ids.SetNumberOfIds(number_of_vertices)
+    for i in range(0, number_of_vertices):
+        triangle_strip.point_ids.SetId(i, i)
+
+    ug = vtkUnstructuredGrid()
+    ug.SetPoints(points)
+    ug.InsertNextCell(triangle_strip.cell_type, triangle_strip.point_ids)
+
+    return ug
+
+
+def make_polygon():
+    # A polygon is a cell that represents a polygon
+    number_of_vertices = 6
+
+    points = vtkPoints()
+    points.InsertNextPoint(0, 0, 0)
+    points.InsertNextPoint(1, -.1, 0)
+    points.InsertNextPoint(.8, .5, 0)
+    points.InsertNextPoint(1, 1, 0)
+    points.InsertNextPoint(.6, 1.2, 0)
+    points.InsertNextPoint(0, .8, 0)
+
+    polygon = vtkPolygon()
+    polygon.point_ids.SetNumberOfIds(number_of_vertices)
+    for i in range(0, number_of_vertices):
+        polygon.point_ids.SetId(i, i)
+
+    ug = vtkUnstructuredGrid()
+    ug.SetPoints(points)
+    ug.InsertNextCell(polygon.cell_type, polygon.point_ids)
+
+    return ug
+
+
+if __name__ == '__main__':
+    main()
-- 
GitLab