surfaceConvert.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-2016 OpenFOAM Foundation
9  Copyright (C) 2020-2023 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  surfaceConvert
29 
30 Group
31  grpSurfaceUtilities
32 
33 Description
34  Converts from one surface mesh format to another.
35 
36 Usage
37  \b surfaceConvert inputFile outputFile [OPTION]
38 
39  Options:
40  - \par -clean
41  Perform some surface checking/cleanup on the input surface
42 
43  - \par -read-format <type>
44  Specify input file format
45 
46  - \par -write-format <type>
47  Specify output file format
48 
49  - \par -scale <scale>
50  Specify a scaling factor for writing the files
51 
52  - \par -group
53  Orders faces by region
54 
55 Note
56  The filename extensions are used to determine the file format type.
57 
58 \*---------------------------------------------------------------------------*/
59 
60 #include "argList.H"
61 #include "fileName.H"
62 #include "triSurface.H"
63 #include "OFstream.H"
64 #include "OSspecific.H"
65 #include "Time.H"
66 
67 using namespace Foam;
68 
69 static word getExtension(const fileName& name)
70 {
71  return
72  (
73  name.has_ext("gz")
74  ? name.stem().ext()
75  : name.ext()
76  );
77 }
78 
79 
80 // Non-short-circuiting check to get all warnings
81 static bool hasReadWriteTypes(const word& readType, const word& writeType)
82 {
83  volatile bool good = true;
84 
85  if (!triSurface::canReadType(readType, true))
86  {
87  good = false;
88  }
89 
90  if (!triSurface::canWriteType(writeType, true))
91  {
92  good = false;
93  }
94 
95  return good;
96 }
97 
98 
99 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
100 
101 int main(int argc, char *argv[])
102 {
104  (
105  "Convert between surface formats, using triSurface library components"
106  );
107 
109  argList::addArgument("input", "The input surface file");
110  argList::addArgument("output", "The output surface file");
111 
113  (
114  "clean",
115  "Perform some surface checking/cleanup on the input surface"
116  );
118  (
119  "group",
120  "Reorder faces into groups; one per region"
121  );
123  (
124  "read-format",
125  "type",
126  "The input format (default: use file extension)"
127  );
129  (
130  "write-format",
131  "type",
132  "The output format (default: use file extension)"
133  );
135  (
136  "scale",
137  "factor",
138  "Input geometry scaling factor"
139  );
141  (
142  "precision",
143  "int",
144  "The output precision"
145  );
146  argList::addOptionCompat("precision", {"writePrecision", 1812});
148 
149  argList args(argc, argv);
150 
151  {
152  const unsigned prec = args.getOrDefault<unsigned>("precision", 0u);
153  if (prec)
154  {
155  Info<< "Output write precision set to " << prec << endl;
156 
158  Sout.precision(prec);
159  }
160  }
161 
162  const auto importName = args.get<fileName>(1);
163  const auto exportName = args.get<fileName>(2);
164 
165  const int optVerbose = args.verbose();
166 
167  if (importName == exportName)
168  {
169  FatalError
170  << "Output file would overwrite input file." << nl
171  << exit(FatalError);
172  }
173 
174  const word readFileType
175  (
176  args.getOrDefault<word>("read-format", getExtension(importName))
177  );
178 
179  const word writeFileType
180  (
181  args.getOrDefault<word>("write-format", getExtension(exportName))
182  );
183 
184 
185  // Check that reading/writing is supported
186  if (!hasReadWriteTypes(readFileType, writeFileType))
187  {
188  FatalError
189  << "Unsupported file format(s)" << nl
190  << exit(FatalError);
191  }
192 
193 
194  scalar scaleFactor(0);
195 
196  Info<< "Reading : " << importName << endl;
197  triSurface surf(importName, readFileType, scaleFactor);
198 
199  if (args.readIfPresent("scale", scaleFactor) && scaleFactor > 0)
200  {
201  Info<< "scale input " << scaleFactor << nl;
202  surf.scalePoints(scaleFactor);
203  }
204 
205 
206  Info<< "Read surface:" << endl;
207  surf.writeStats(Info);
208  Info<< endl;
209 
210  if (args.found("clean"))
211  {
212  Info<< "Cleaning up surface" << endl;
213  surf.cleanup(optVerbose);
214 
215  Info<< "After cleaning up surface:" << endl;
216  surf.writeStats(Info);
217  Info<< endl;
218  }
219 
220  const bool sortByRegion = args.found("group");
221  if (sortByRegion)
222  {
223  Info<< "Reordering faces into groups; one per region." << endl;
224  }
225  else
226  {
227  Info<< "Maintaining face ordering" << endl;
228  }
229 
230  Info<< "writing " << exportName << endl;
231 
232  surf.write(exportName, writeFileType, sortByRegion);
233 
234  Info<< "\nEnd\n" << endl;
235 
236  return 0;
237 }
238 
239 // ************************************************************************* //
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...
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:423
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:374
static void noParallel()
Remove the parallel options.
Definition: argList.C:584
static void addOptionCompat(const word &optName, std::pair< const char *, int > compat)
Specify an alias for the option name.
Definition: argList.C:418
static bool canWriteType(const word &fileType, bool verbose=false)
Can we write this file format?
Definition: triSurfaceIO.C:80
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
word ext() const
Return file name extension (part after last .)
Definition: wordI.H:171
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
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
virtual int precision() const override
Get precision of output field.
Definition: OSstream.C:334
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
OSstream Sout
OSstream wrapped stdout (std::cout)
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
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
static void addVerboseOption(const string &usage="", bool advanced=false)
Enable a &#39;verbose&#39; bool option, with usage information.
Definition: argList.C:520
static bool canReadType(const word &fileType, bool verbose=false)
Can we read this file format?
Definition: triSurfaceIO.C:68
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
Triangulated surface description with patch information.
Definition: triSurface.H:71
Foam::argList args(argc, argv)
int verbose() const noexcept
Return the verbose flag.
Definition: argListI.H:121
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:171
Namespace for OpenFOAM.