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-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  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});
147 
148  argList args(argc, argv);
149 
150  {
151  const unsigned prec = args.getOrDefault<unsigned>("precision", 0u);
152  if (prec)
153  {
154  Info<< "Output write precision set to " << prec << endl;
155 
157  Sout.precision(prec);
158  }
159  }
160 
161  const auto importName = args.get<fileName>(1);
162  const auto exportName = args.get<fileName>(2);
163 
164  if (importName == exportName)
165  {
166  FatalError
167  << "Output file would overwrite input file." << nl
168  << exit(FatalError);
169  }
170 
171  const word readFileType
172  (
173  args.getOrDefault<word>("read-format", getExtension(importName))
174  );
175 
176  const word writeFileType
177  (
178  args.getOrDefault<word>("write-format", getExtension(exportName))
179  );
180 
181 
182  // Check that reading/writing is supported
183  if (!hasReadWriteTypes(readFileType, writeFileType))
184  {
185  FatalError
186  << "Unsupported file format(s)" << nl
187  << exit(FatalError);
188  }
189 
190 
191  scalar scaleFactor(0);
192 
193  Info<< "Reading : " << importName << endl;
194  triSurface surf(importName, readFileType, scaleFactor);
195 
196  if (args.readIfPresent("scale", scaleFactor) && scaleFactor > 0)
197  {
198  Info<< "scale input " << scaleFactor << nl;
199  surf.scalePoints(scaleFactor);
200  }
201 
202 
203  Info<< "Read surface:" << endl;
204  surf.writeStats(Info);
205  Info<< endl;
206 
207  if (args.found("clean"))
208  {
209  Info<< "Cleaning up surface" << endl;
210  surf.cleanup(true);
211 
212  Info<< "After cleaning up surface:" << endl;
213  surf.writeStats(Info);
214  Info<< endl;
215  }
216 
217  const bool sortByRegion = args.found("group");
218  if (sortByRegion)
219  {
220  Info<< "Reordering faces into groups; one per region." << endl;
221  }
222  else
223  {
224  Info<< "Maintaining face ordering" << endl;
225  }
226 
227  Info<< "writing " << exportName << endl;
228 
229  surf.write(exportName, writeFileType, sortByRegion);
230 
231  Info<< "\nEnd\n" << endl;
232 
233  return 0;
234 }
235 
236 // ************************************************************************* //
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:453
A class for handling file names.
Definition: fileName.H:71
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:305
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:49
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:416
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:365
static void noParallel()
Remove the parallel options.
Definition: argList.C:551
static void addOptionCompat(const word &optName, std::pair< const char *, int > compat)
Specify an alias for the option name.
Definition: argList.C:409
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 INVALID.
Definition: exprTraits.C:52
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
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:376
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
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:342
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:72
Foam::argList args(argc, argv)
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:171
Namespace for OpenFOAM.