surfaceFeatureConvert.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2015 OpenFOAM Foundation
9  Copyright (C) 2015-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Application
28  surfaceFeatureConvert
29 
30 Group
31  grpSurfaceUtilities
32 
33 Description
34  Convert between edgeMesh formats.
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #include "argList.H"
39 #include "Time.H"
40 
41 #include "edgeMesh.H"
42 
43 using namespace Foam;
44 
45 static word getExtension(const fileName& name)
46 {
47  return
48  (
49  name.has_ext("gz")
50  ? name.stem().ext()
51  : name.ext()
52  );
53 }
54 
55 
56 // Non-short-circuiting check to get all warnings
57 static bool hasReadWriteTypes(const word& readType, const word& writeType)
58 {
59  volatile bool good = true;
60 
61  if (!edgeMesh::canReadType(readType, true))
62  {
63  good = false;
64  }
65 
66  if (!edgeMesh::canWriteType(writeType, true))
67  {
68  good = false;
69  }
70 
71  return good;
72 }
73 
74 
75 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
76 
77 int main(int argc, char *argv[])
78 {
80  (
81  "Convert between edgeMesh formats"
82  );
84  argList::addArgument("input", "The input edge file");
85  argList::addArgument("output", "The output edge file");
87  (
88  "read-format",
89  "type",
90  "The input format (default: use file extension)"
91  );
93  (
94  "write-format",
95  "type",
96  "The output format (default: use file extension)"
97  );
99  (
100  "scale",
101  "factor",
102  "Input geometry scaling factor"
103  );
104 
105  argList args(argc, argv);
107 
108  const auto importName = args.get<fileName>(1);
109  const auto exportName = args.get<fileName>(2);
110 
111  // Disable inplace editing
112  if (importName == exportName)
113  {
114  FatalError
115  << "Output file would overwrite input file."
116  << exit(FatalError);
117  }
118 
119  const word readFileType
120  (
121  args.getOrDefault<word>("read-format", getExtension(importName))
122  );
123 
124  const word writeFileType
125  (
126  args.getOrDefault<word>("write-format", getExtension(exportName))
127  );
128 
129  // Check that reading/writing is supported
130  if (!hasReadWriteTypes(readFileType, writeFileType))
131  {
132  FatalError
133  << "Unsupported file format(s)" << nl
134  << exit(FatalError);
135  }
136 
137  edgeMesh mesh(importName, readFileType);
138 
139  Info<< "\nRead edgeMesh " << importName << nl;
140  mesh.writeStats(Info);
141  Info<< nl
142  << "\nwriting " << exportName;
143 
144  scalar scaleFactor(0);
145  if (args.readIfPresent("scale", scaleFactor) && scaleFactor > 0)
146  {
147  Info<< " with scaling " << scaleFactor << endl;
148  mesh.scalePoints(scaleFactor);
149  }
150  else
151  {
152  Info<< " without scaling" << endl;
153  }
154 
155  mesh.write(exportName, writeFileType);
156  mesh.writeStats(Info);
157 
158  Info<< "\nEnd\n" << endl;
159 
160  return 0;
161 }
162 
163 
164 // ************************************************************************* //
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:462
A class for handling file names.
Definition: fileName.H:72
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
static bool canReadType(const word &fileType, bool verbose=false)
Can we read this file format?
Definition: edgeMesh.C:54
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static void noParallel()
Remove the parallel options.
Definition: argList.C:584
T getOrDefault(const word &optName, const T &deflt) const
Get a value from the named option if present, or return default.
Definition: argListI.H:300
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
word ext() const
Return file name extension (part after last .)
Definition: wordI.H:171
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
bool has_ext() const
Various checks for extensions.
Definition: stringI.H:43
A class for handling words, derived from Foam::string.
Definition: word.H:63
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:118
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Add an option to validOptions with usage information.
Definition: argList.C:385
const fileName & caseName() const noexcept
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:62
virtual bool write(const bool writeOnProc=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1102
static bool canWriteType(const word &fileType, bool verbose=false)
Can we write this file format type?
Definition: edgeMesh.C:66
const fileName & rootPath() const noexcept
Return root path.
Definition: argListI.H:56
Mesh data needed to do the Finite Area discretisation.
Definition: edgeFaMesh.H:47
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:271
static void addArgument(const string &argName, const string &usage="")
Append a (mandatory) argument to validArgs.
Definition: argList.C:351
messageStream Info
Information stream (stdout output on master, null elsewhere)
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:316
Foam::argList args(argc, argv)
Namespace for OpenFOAM.