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-------------------------------------------------------------------------------
11License
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
27Application
28 surfaceConvert
29
30Group
31 grpSurfaceUtilities
32
33Description
34 Converts from one surface mesh format to another.
35
36Usage
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
55Note
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
67using namespace Foam;
68
69static 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
82static 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
102int main(int argc, char *argv[])
103{
104 argList::addNote
105 (
106 "Convert between surface formats, using triSurface library components"
107 );
108
109 argList::noParallel();
110 argList::addArgument("input", "The input surface file");
111 argList::addArgument("output", "The output surface file");
112
113 argList::addBoolOption
114 (
115 "clean",
116 "Perform some surface checking/cleanup on the input surface"
117 );
118 argList::addBoolOption
119 (
120 "group",
121 "Reorder faces into groups; one per region"
122 );
123 argList::addOption
124 (
125 "read-format",
126 "type",
127 "The input format (default: use file extension)"
128 );
129 argList::addOption
130 (
131 "write-format",
132 "type",
133 "The output format (default: use file extension)"
134 );
135 argList::addOption
136 (
137 "scale",
138 "factor",
139 "Input geometry scaling factor"
140 );
141 argList::addOption
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
157 IOstream::defaultPrecision(prec);
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 {
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 {
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// ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:326
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:124
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:278
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:178
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:323
T getOrDefault(const word &optName, const T &deflt) const
Get a value from the named option if present, or return default.
Definition: argListI.H:307
A class for handling file names.
Definition: fileName.H:76
Triangulated surface description with patch information.
Definition: triSurface.H:79
A class for handling words, derived from Foam::string.
Definition: word.H:68
word ext() const
Return file name extension (part after last .)
Definition: word.C:126
word lessExt() const
Return word without extension (part before last .)
Definition: word.C:113
Namespace for OpenFOAM.
messageStream Info
Information stream (stdout output on master, null elsewhere)
OSstream Sout
OSstream wrapped stdout (std::cout)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
Foam::argList args(argc, argv)