From fec5093c041be4f792b05a6c3757dd8043f03b21 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Thu, 4 Jun 2020 22:02:57 +0200
Subject: [PATCH] ENH: add blockMesh -write-vtk option

- generates a vtk file in an ASCII, XML format (blockTopology.vtu).

  More information than the equivalent obj file since it includes
  connectivity, cell shapes and cell IDs (equivalent to the block
  numbers).
---
 .../mesh/generation/blockMesh/blockMesh.C     | 43 ++++++++++++++++++-
 1 file changed, 41 insertions(+), 2 deletions(-)

diff --git a/applications/utilities/mesh/generation/blockMesh/blockMesh.C b/applications/utilities/mesh/generation/blockMesh/blockMesh.C
index 4486ea2295e..0eec9f01b8e 100644
--- a/applications/utilities/mesh/generation/blockMesh/blockMesh.C
+++ b/applications/utilities/mesh/generation/blockMesh/blockMesh.C
@@ -46,6 +46,9 @@ Usage
       - \par -write-obj
         Write topology as a set of edges in OBJ format and exit.
 
+      - \par -write-vtk
+        Write topology as VTK file (xml, ascii) and exit.
+
       - \par -merge-points
         Merge points instead of default topological merge
 
@@ -71,6 +74,7 @@ Usage
 #include "IOPtrList.H"
 
 #include "blockMesh.H"
+#include "foamVtkInternalMeshWriter.H"
 #include "attachPolyTopoChanger.H"
 #include "polyTopoChange.H"
 #include "emptyPolyPatch.H"
@@ -122,6 +126,12 @@ int main(int argc, char *argv[])
     );
     argList::addOptionCompat("write-obj", {"blockTopology", 1912});
 
+    argList::addBoolOption
+    (
+        "write-vtk",
+        "Write topology as VTK file and exit"
+    );
+
     argList::addBoolOption
     (
         "merge-points",
@@ -226,7 +236,7 @@ int main(int argc, char *argv[])
         {
             OFstream os(runTime.path()/"blockTopology.obj");
 
-            Info<< "Writing block structure as obj format: "
+            Info<< "Writing block structure in obj format: "
                 << os.name().name() << endl;
 
             blocks.writeTopology(os);
@@ -236,7 +246,7 @@ int main(int argc, char *argv[])
         {
             OFstream os(runTime.path()/"blockCentres.obj");
 
-            Info<< "Writing block centres as obj format: "
+            Info<< "Writing block centres in obj format: "
                 << os.name().name() << endl;
 
             for (const point& cc : blocks.topology().cellCentres())
@@ -246,6 +256,35 @@ int main(int argc, char *argv[])
         }
     }
 
+    if (args.found("write-vtk"))
+    {
+        quickExit = true;
+
+        // non-legacy and ASCII (mesh is small, want readable output)
+        const vtk::outputOptions writeOpts = vtk::formatType::INLINE_ASCII;
+
+        Info<< nl;
+
+        const polyMesh& topoMesh = blocks.topology();
+        const vtk::vtuCells topoCells(topoMesh, writeOpts);
+
+        vtk::internalMeshWriter writer
+        (
+            topoMesh,
+            topoCells,
+            writeOpts,
+            runTime.path()/"blockTopology"
+        );
+
+        Info<< "Writing block topology in vtk format: "
+            << args.relativePath(writer.output()).c_str() << endl;
+
+        writer.writeGeometry();
+        writer.beginCellData();
+        writer.writeCellIDs();
+    }
+
+
     if (quickExit)
     {
         Info<< "\nEnd\n" << endl;
-- 
GitLab