Loading...
Searching...
No Matches
surfaceMeshExport.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) 2018-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 surfaceMeshExport
29
30Group
31 grpSurfaceUtilities
32
33Description
34 Export from surfMesh to various third-party surface formats with
35 optional scaling or transformations (rotate/translate) on a
36 coordinateSystem.
37
38Usage
39 \b surfaceMeshExport outputFile [OPTION]
40
41 Options:
42 - \par -clean
43 Perform some surface checking/cleanup on the input surface.
44
45 - \par -name <name>
46 Specify an alternative surface name when writing.
47
48 - \par -write-format <type>
49 Specify output file format
50
51 - \par -read-scale <scale>
52 Scale factor when reading files.
53
54 - \par -write-scale <scale>
55 Scale factor when writing files.
56
57 - \par -dict <dictionary>
58 Specify an alternative dictionary for constant/coordinateSystems.
59
60 - \par -from <coordinateSystem>
61 Specify a coordinate system when reading files.
62
63 - \par -to <coordinateSystem>
64 Specify a coordinate system when writing files.
65
66Note
67 The filename extensions are used to determine the file format type.
68
69\*---------------------------------------------------------------------------*/
70
71#include "argList.H"
72#include "Time.H"
73
74#include "MeshedSurfaces.H"
75#include "coordinateSystems.H"
76#include "cartesianCS.H"
77
78using namespace Foam;
79
80static word getExtension(const fileName& name)
81{
82 return
83 (
84 name.has_ext("gz")
85 ? name.stem().ext()
86 : name.ext()
87 );
88}
89
90
91// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
92
93int main(int argc, char *argv[])
94{
96 (
97 "Export from surfMesh to various third-party surface formats"
98 );
99
101 argList::addArgument("output", "The output surface file");
102
104 (
105 "clean",
106 "Perform some surface checking/cleanup on the input surface"
107 );
110 (
111 "name",
112 "name",
113 "Specify an alternative surface name when reading - "
114 "default is 'default'"
115 );
117 (
118 "write-format",
119 "type",
120 "Output format (default: use file extension)"
121 );
123 (
124 "read-scale",
125 "factor",
126 "Input geometry scaling factor"
127 );
129 (
130 "write-scale",
131 "factor",
132 "Output geometry scaling factor"
133 );
134
135 argList::addOptionCompat("read-scale", {"scaleIn", 1912});
136 argList::addOptionCompat("write-scale", {"scaleOut", 1912});
137
138 argList::addOption("dict", "file", "Alternative coordinateSystems");
139
141 (
142 "from",
143 "system",
144 "The source coordinate system, applied after '-read-scale'",
145 true // advanced
146 );
148 (
149 "to",
150 "system",
151 "The target coordinate system, applied before '-write-scale'",
152 true // advanced
153 );
154
155 argList args(argc, argv);
156 Time runTime(args.rootPath(), args.caseName());
157
158 const auto exportName = args.get<fileName>(1);
159 const auto importName = args.getOrDefault<word>("name", "default");
160
161 const int optVerbose = args.verbose();
162
163 const word writeFileType
164 (
165 args.getOrDefault<word>("write-format", getExtension(exportName))
166 );
167
168 // Check read/write support for formats
169 if (!meshedSurface::canWriteType(writeFileType, true))
170 {
172 << "Unsupported file format(s)" << nl
173 << exit(FatalError);
174 }
175
176
177 scalar scaleFactor(0);
178
179 // The coordinate transformations (must be cartesian)
182
183 if (args.found("from") || args.found("to"))
184 {
186 (
188 (
189 coordinateSystems::typeName,
190 runTime.constant(),
191 runTime,
195 ),
196 args.getOrDefault<fileName>("dict", "")
197 );
198
199 if (!ioCsys.typeHeaderOk<coordinateSystems>(false))
200 {
202 << ioCsys.objectPath() << nl
203 << exit(FatalError);
204 }
205
206 coordinateSystems globalCoords(ioCsys);
207
208 if (args.found("from"))
209 {
210 const word csName(args["from"]);
211 const auto* csPtr = globalCoords.cfind(csName);
212
213 if (!csPtr)
214 {
216 << "Cannot find -from " << csName << nl
217 << "available coordinateSystems: "
218 << flatOutput(globalCoords.names()) << nl
219 << exit(FatalError);
220 }
221
222 fromCsys = autoPtr<coordSystem::cartesian>::New(*csPtr);
223 }
224
225 if (args.found("to"))
226 {
227 const word csName(args["to"]);
228 const auto* csPtr = globalCoords.cfind(csName);
229
230 if (!csPtr)
231 {
233 << "Cannot find -to " << csName << nl
234 << "available coordinateSystems: "
235 << flatOutput(globalCoords.names()) << nl
236 << exit(FatalError);
237 }
238
240 }
241
242 // Maybe fix this later
243 if (fromCsys && toCsys)
244 {
246 << "Only allowed '-from' or '-to' option at the moment."
247 << exit(FatalError);
248 }
249 }
250
251
252 surfMesh smesh
253 (
255 (
256 importName,
257 runTime.constant(),
258 runTime,
261 )
262 );
263
264 Info<< "read surfMesh:\n " << smesh.objectPath() << endl;
265
266
267 // Simply copy for now, but really could have a separate write method
268
269 meshedSurface surf(smesh);
270
271 if (args.readIfPresent("read-scale", scaleFactor) && scaleFactor > 0)
272 {
273 Info<< "scale input " << scaleFactor << nl;
274 surf.scalePoints(scaleFactor);
275 }
276
277 if (args.found("clean"))
278 {
279 surf.cleanup(optVerbose);
280 }
281
282 if (fromCsys)
283 {
284 Info<< "move points from coordinate system: "
285 << fromCsys->name() << endl;
286 tmp<pointField> tpf = fromCsys->localPosition(surf.points());
287 surf.movePoints(tpf());
288 }
289
290 if (toCsys)
291 {
292 Info<< "move points to coordinate system: "
293 << toCsys->name() << endl;
294 tmp<pointField> tpf = toCsys->globalPosition(surf.points());
295 surf.movePoints(tpf());
296 }
297
298 if (args.readIfPresent("write-scale", scaleFactor) && scaleFactor > 0)
299 {
300 Info<< "scale output " << scaleFactor << endl;
301 surf.scalePoints(scaleFactor);
302 }
303
304 surf.writeStats(Info);
305 Info<< endl;
306
307 Info<< "writing " << exportName << nl;
308 surf.write(exportName, writeFileType);
309
310 Info<< "\nEnd\n" << endl;
311
312 return 0;
313}
314
315// ************************************************************************* //
@ NO_REGISTER
Do not request registration (bool: false).
@ MUST_READ
Reading required.
@ NO_WRITE
Ignore writing from objectRegistry::writeObject().
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
bool typeHeaderOk(const bool checkType=true, const bool search=true, const bool verbose=true)
Read header (respects is_globalIOobject trait) and check its info. A void type suppresses trait and t...
static IOobject selectIO(const IOobject &io, const fileName &altFile, const word &ioName="")
Return the IOobject, but also consider an alternative file name.
Definition IOobject.C:256
fileName objectPath() const
The complete path + object name.
Definition IOobjectI.H:313
static bool canWriteType(const word &fileType, bool verbose=false)
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
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
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition autoPtr.H:178
A centralized collection of named coordinate systems.
A class for handling file names.
Definition fileName.H:75
A surface mesh consisting of general polygon faces that has IO capabilities and a registry for storin...
Definition surfMesh.H:67
A class for managing temporary objects.
Definition tmp.H:75
A class for handling words, derived from Foam::string.
Definition word.H:66
engineTime & runTime
auto & name
Namespace for OpenFOAM.
messageStream Info
Information stream (stdout output on master, null elsewhere).
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
MeshedSurface< face > meshedSurface
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition FlatOutput.H:217
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)