Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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 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
81static 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
101int 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 {
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 {
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// ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition IOstream.H:437
Extract command arguments and options from the supplied argc and argv parameters.
Definition argList.H:119
static void addVerboseOption(const string &usage="", bool advanced=false)
Enable a 'verbose' bool option, with usage information.
Definition argList.C:535
static void addArgument(const string &argName, const string &usage="")
Append a (mandatory) argument to validArgs.
Definition argList.C:366
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition argList.C:389
static void noParallel()
Remove the parallel options.
Definition argList.C:599
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:400
static void addNote(const string &note)
Add extra notes for the usage information.
Definition argList.C:477
static void addOptionCompat(const word &optName, std::pair< const char *, int > compat)
Specify an alias for the option name.
Definition argList.C:433
A class for handling file names.
Definition fileName.H:75
Triangulated surface description with patch information.
Definition triSurface.H:74
static bool canWriteType(const word &fileType, bool verbose=false)
Can we write this file format?
static bool canReadType(const word &fileType, bool verbose=false)
Can we read this file format?
A class for handling words, derived from Foam::string.
Definition word.H:66
auto & name
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:519
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
Foam::argList args(argc, argv)