From 9b319dab61f72edd33bdb94b75b781822c791496 Mon Sep 17 00:00:00 2001
From: Henry Weller <http://cfd.direct>
Date: Tue, 24 Jan 2017 16:18:20 +0000
Subject: [PATCH] blockMesh: Delete the polyMesh directory before meshing

unless the blockMeshDict is in the polyMesh directory or the "-noClean" option
is specified.

This avoids problems running snappyHexMesh without first clearing files from
polyMesh which interfere with the operation of snappyHexMesh.
---
 .../mesh/generation/blockMesh/blockMesh.C     | 31 ++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/applications/utilities/mesh/generation/blockMesh/blockMesh.C b/applications/utilities/mesh/generation/blockMesh/blockMesh.C
index a7860c8f531..4b3b7266087 100644
--- a/applications/utilities/mesh/generation/blockMesh/blockMesh.C
+++ b/applications/utilities/mesh/generation/blockMesh/blockMesh.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2017 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -76,6 +76,11 @@ int main(int argc, char *argv[])
         "blockTopology",
         "write block edges and centres as .obj files"
     );
+    argList::addBoolOption
+    (
+        "noClean",
+        "keep the existing files in the polyMesh"
+    );
     argList::addOption
     (
         "dict",
@@ -156,6 +161,30 @@ int main(int argc, char *argv[])
         dictPath = runTime.system()/regionPath/dictName;
     }
 
+    if (!args.optionFound("noClean"))
+    {
+        fileName polyMeshPath
+        (
+            runTime.path()/runTime.constant()/regionPath/polyMesh::meshSubDir
+        );
+
+        if (exists(polyMeshPath))
+        {
+            if (exists(polyMeshPath/dictName))
+            {
+                Info<< "Not deleting polyMesh directory " << nl
+                    << "    " << polyMeshPath << nl
+                    << "    because it contains " << dictName << endl;
+            }
+            else
+            {
+                Info<< "Deleting polyMesh directory" << nl
+                    << "    " << polyMeshPath << endl;
+                rmDir(polyMeshPath);
+            }
+        }
+    }
+
     IOobject meshDictIO
     (
         dictPath,
-- 
GitLab