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-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 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 word ext(name.ext());
83 if (ext == "gz")
84 {
85 ext = name.lessExt().ext();
86 }
87
88 return ext;
89}
90
91
92// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
93
94int main(int argc, char *argv[])
95{
96 argList::addNote
97 (
98 "Export from surfMesh to various third-party surface formats"
99 );
100
101 argList::noParallel();
102 argList::addArgument("output", "The output surface file");
103
104 argList::addBoolOption
105 (
106 "clean",
107 "Perform some surface checking/cleanup on the input surface"
108 );
109 argList::addOption
110 (
111 "name",
112 "name",
113 "Specify an alternative surface name when reading - "
114 "default is 'default'"
115 );
116 argList::addOption
117 (
118 "write-format",
119 "type",
120 "Output format (default: use file extension)"
121 );
122 argList::addOption
123 (
124 "read-scale",
125 "factor",
126 "Input geometry scaling factor"
127 );
128 argList::addOption
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
140 argList::addOption
141 (
142 "from",
143 "system",
144 "The source coordinate system, applied after '-read-scale'",
145 true // advanced
146 );
147 argList::addOption
148 (
149 "to",
150 "system",
151 "The target coordinate system, applied before '-write-scale'",
152 true // advanced
153 );
154
155 argList args(argc, argv);
157
158 const auto exportName = args.get<fileName>(1);
159 const auto importName = args.getOrDefault<word>("name", "default");
160
161 const word writeFileType
162 (
163 args.getOrDefault<word>("write-format", getExtension(exportName))
164 );
165
166 // Check read/write support for formats
167 if (!meshedSurface::canWriteType(writeFileType, true))
168 {
170 << "Unsupported file format(s)" << nl
171 << exit(FatalError);
172 }
173
174
175 scalar scaleFactor(0);
176
177 // The coordinate transformations (must be cartesian)
180
181 if (args.found("from") || args.found("to"))
182 {
183 IOobject ioCsys = IOobject::selectIO
184 (
186 (
187 coordinateSystems::typeName,
188 runTime.constant(),
189 runTime,
190 IOobject::MUST_READ,
191 IOobject::NO_WRITE,
192 false
193 ),
194 args.getOrDefault<fileName>("dict", "")
195 );
196
197 if (!ioCsys.typeHeaderOk<coordinateSystems>(false))
198 {
200 << ioCsys.objectPath() << nl
201 << exit(FatalError);
202 }
203
204 coordinateSystems globalCoords(ioCsys);
205
206 if (args.found("from"))
207 {
208 const word csName(args["from"]);
209 const auto* csPtr = globalCoords.cfind(csName);
210
211 if (!csPtr)
212 {
214 << "Cannot find -from " << csName << nl
215 << "available coordinateSystems: "
216 << flatOutput(globalCoords.names()) << nl
217 << exit(FatalError);
218 }
219
220 fromCsys = autoPtr<coordSystem::cartesian>::New(*csPtr);
221 }
222
223 if (args.found("to"))
224 {
225 const word csName(args["to"]);
226 const auto* csPtr = globalCoords.cfind(csName);
227
228 if (!csPtr)
229 {
231 << "Cannot find -to " << csName << nl
232 << "available coordinateSystems: "
233 << flatOutput(globalCoords.names()) << nl
234 << exit(FatalError);
235 }
236
238 }
239
240 // Maybe fix this later
241 if (fromCsys && toCsys)
242 {
244 << "Only allowed '-from' or '-to' option at the moment."
245 << exit(FatalError);
246 }
247 }
248
249
250 surfMesh smesh
251 (
253 (
254 importName,
255 runTime.constant(),
256 runTime,
257 IOobject::MUST_READ_IF_MODIFIED,
258 IOobject::NO_WRITE
259 )
260 );
261
262 Info<< "read surfMesh:\n " << smesh.objectPath() << endl;
263
264
265 // Simply copy for now, but really could have a separate write method
266
267 meshedSurface surf(smesh);
268
269 if (args.readIfPresent("read-scale", scaleFactor) && scaleFactor > 0)
270 {
271 Info<< "scale input " << scaleFactor << nl;
272 surf.scalePoints(scaleFactor);
273 }
274
275 if (args.found("clean"))
276 {
277 surf.cleanup(true);
278 }
279
280 if (fromCsys)
281 {
282 Info<< "move points from coordinate system: "
283 << fromCsys->name() << endl;
284 tmp<pointField> tpf = fromCsys->localPosition(surf.points());
285 surf.movePoints(tpf());
286 }
287
288 if (toCsys)
289 {
290 Info<< "move points to coordinate system: "
291 << toCsys->name() << endl;
292 tmp<pointField> tpf = toCsys->globalPosition(surf.points());
293 surf.movePoints(tpf());
294 }
295
296 if (args.readIfPresent("write-scale", scaleFactor) && scaleFactor > 0)
297 {
298 Info<< "scale output " << scaleFactor << endl;
299 surf.scalePoints(scaleFactor);
300 }
301
302 surf.writeStats(Info);
303 Info<< endl;
304
305 Info<< "writing " << exportName << nl;
306 surf.write(exportName, writeFileType);
307
308 Info<< "\nEnd\n" << endl;
309
310 return 0;
311}
312
313// ************************************************************************* //
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
bool typeHeaderOk(const bool checkType=true, const bool search=true, const bool verbose=true)
Read header (uses typeFilePath to find file) and check its info.
fileName objectPath() const
The complete path + object name.
Definition: IOobjectI.H:214
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
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
const fileName & rootPath() const noexcept
Return root path.
Definition: argListI.H:63
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
const fileName & caseName() const noexcept
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:69
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A centralized collection of named coordinate systems.
A class for handling file names.
Definition: fileName.H:76
A surface mesh consisting of general polygon faces that has IO capabilities and a registry for storin...
Definition: surfMesh.H:68
A class for managing temporary objects.
Definition: tmp.H:65
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
engineTime & runTime
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:372
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:215
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)