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-2021 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  word ext(name.ext());
72  if (ext == "gz")
73  {
74  ext = name.lessExt().ext();
75  }
76 
77  return ext;
78 }
79 
80 
81 // Non-short-circuiting check to get all warnings
82 static bool hasReadWriteTypes(const word& readType, const word& writeType)
83 {
84  volatile bool good = true;
85 
86  if (!triSurface::canReadType(readType, true))
87  {
88  good = false;
89  }
90 
91  if (!triSurface::canWriteType(writeType, true))
92  {
93  good = false;
94  }
95 
96  return good;
97 }
98 
99 
100 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
101 
102 int main(int argc, char *argv[])
103 {
105  (
106  "Convert between surface formats, using triSurface library components"
107  );
108 
110  argList::addArgument("input", "The input surface file");
111  argList::addArgument("output", "The output surface file");
112 
114  (
115  "clean",
116  "Perform some surface checking/cleanup on the input surface"
117  );
119  (
120  "group",
121  "Reorder faces into groups; one per region"
122  );
124  (
125  "read-format",
126  "type",
127  "The input format (default: use file extension)"
128  );
130  (
131  "write-format",
132  "type",
133  "The output format (default: use file extension)"
134  );
136  (
137  "scale",
138  "factor",
139  "Input geometry scaling factor"
140  );
142  (
143  "precision",
144  "int",
145  "The output precision"
146  );
147  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  if (importName == exportName)
166  {
167  FatalError
168  << "Output file would overwrite input file." << nl
169  << exit(FatalError);
170  }
171 
172  const word readFileType
173  (
174  args.getOrDefault<word>("read-format", getExtension(importName))
175  );
176 
177  const word writeFileType
178  (
179  args.getOrDefault<word>("write-format", getExtension(exportName))
180  );
181 
182 
183  // Check that reading/writing is supported
184  if (!hasReadWriteTypes(readFileType, writeFileType))
185  {
186  FatalError
187  << "Unsupported file format(s)" << nl
188  << exit(FatalError);
189  }
190 
191 
192  scalar scaleFactor(0);
193 
194  Info<< "Reading : " << importName << endl;
195  triSurface surf(importName, readFileType, scaleFactor);
196 
197  if (args.readIfPresent("scale", scaleFactor) && scaleFactor > 0)
198  {
199  Info<< "scale input " << scaleFactor << nl;
200  surf.scalePoints(scaleFactor);
201  }
202 
203 
204  Info<< "Read surface:" << endl;
205  surf.writeStats(Info);
206  Info<< endl;
207 
208  if (args.found("clean"))
209  {
210  Info<< "Cleaning up surface" << endl;
211  surf.cleanup(true);
212 
213  Info<< "After cleaning up surface:" << endl;
214  surf.writeStats(Info);
215  Info<< endl;
216  }
217 
218  const bool sortByRegion = args.found("group");
219  if (sortByRegion)
220  {
221  Info<< "Reordering faces into groups; one per region." << endl;
222  }
223  else
224  {
225  Info<< "Maintaining face ordering" << endl;
226  }
227 
228  Info<< "writing " << exportName << endl;
229 
230  surf.write(exportName, writeFileType, sortByRegion);
231 
232  Info<< "\nEnd\n" << endl;
233 
234  return 0;
235 }
236 
237 // ************************************************************************* //
Foam::word::lessExt
word lessExt() const
Definition: word.C:106
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
Foam::fileName
A class for handling file names.
Definition: fileName.H:71
Foam::argList::getOrDefault
T getOrDefault(const word &optName, const T &deflt) const
Definition: argListI.H:300
Foam::argList::addNote
static void addNote(const string &note)
Definition: argList.C:405
Foam::argList::addOptionCompat
static void addOptionCompat(const word &optName, std::pair< const char *, int > compat)
Definition: argList.C:361
Foam::argList
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:119
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
Foam::triSurface::canReadType
static bool canReadType(const word &fileType, bool verbose=false)
Definition: triSurfaceIO.C:68
Foam::word::ext
word ext() const
Definition: word.C:119
triSurface.H
Foam::argList::get
T get(const label index) const
Definition: argListI.H:271
Foam::argList::readIfPresent
bool readIfPresent(const word &optName, T &val) const
Definition: argListI.H:316
OFstream.H
Foam::argList::addArgument
static void addArgument(const string &argName, const string &usage="")
Definition: argList.C:294
Foam::triSurface
Triangulated surface description with patch information.
Definition: triSurface.H:72
Foam::Info
messageStream Info
argList.H
fileName.H
Foam::FatalError
error FatalError
Foam::Ostream::write
virtual bool write(const token &tok)=0
Foam
Definition: atmBoundaryLayer.C:26
Foam::triSurface::canWriteType
static bool canWriteType(const word &fileType, bool verbose=false)
Definition: triSurfaceIO.C:80
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
Foam::OSstream::precision
virtual int precision() const
Definition: OSstream.C:319
Foam::argList::addBoolOption
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Definition: argList.C:317
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision() noexcept
Definition: IOstream.H:338
Time.H
Foam::nl
constexpr char nl
Definition: Ostream.H:424
Foam::name
word name(const expressions::valueTypeCode typeCode)
Definition: exprTraits.C:52
Foam::argList::noParallel
static void noParallel()
Definition: argList.C:503
Foam::Sout
OSstream Sout
Foam::argList::addOption
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Definition: argList.C:328
args
Foam::argList args(argc, argv)
Foam::argList::found
bool found(const word &optName) const
Definition: argListI.H:171