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 -------------------------------------------------------------------------------
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  surfaceMeshExport
29 
30 Group
31  grpSurfaceUtilities
32 
33 Description
34  Export from surfMesh to various third-party surface formats with
35  optional scaling or transformations (rotate/translate) on a
36  coordinateSystem.
37 
38 Usage
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 
66 Note
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 
78 using namespace Foam;
79 
80 static 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 
94 int main(int argc, char *argv[])
95 {
97  (
98  "Export from surfMesh to various third-party surface formats"
99  );
100 
102  argList::addArgument("output", "The output surface file");
103 
105  (
106  "clean",
107  "Perform some surface checking/cleanup on the input surface"
108  );
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);
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  {
169  FatalError
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  {
184  (
185  IOobject
186  (
187  coordinateSystems::typeName,
188  runTime.constant(),
189  runTime,
192  false
193  ),
194  args.getOrDefault<fileName>("dict", "")
195  );
196 
197  if (!ioCsys.typeHeaderOk<coordinateSystems>(false))
198  {
199  FatalError
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  {
213  FatalError
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  {
230  FatalError
231  << "Cannot find -to " << csName << nl
232  << "available coordinateSystems: "
233  << flatOutput(globalCoords.names()) << nl
234  << exit(FatalError);
235  }
236 
237  toCsys = autoPtr<coordSystem::cartesian>::New(*csPtr);
238  }
239 
240  // Maybe fix this later
241  if (fromCsys && toCsys)
242  {
243  FatalError
244  << "Only allowed '-from' or '-to' option at the moment."
245  << exit(FatalError);
246  }
247  }
248 
249 
250  surfMesh smesh
251  (
252  IOobject
253  (
254  importName,
255  runTime.constant(),
256  runTime,
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 // ************************************************************************* //
Foam::word::lessExt
word lessExt() const
Definition: word.C:106
Foam::IOobject::NO_WRITE
@ NO_WRITE
Definition: IOobject.H:191
Foam::autoPtr::New
static autoPtr< T > New(Args &&... args)
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:165
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
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::tmp
A class for managing temporary objects.
Definition: PtrList.H:57
Foam::IOobject::typeHeaderOk
bool typeHeaderOk(const bool checkType=true, const bool search=true, const bool verbose=true)
Definition: IOobjectTemplates.C:32
Foam::argList::caseName
const fileName & caseName() const noexcept
Definition: argListI.H:62
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::surfMesh
A surface mesh consisting of general polygon faces that has IO capabilities and a registry for storin...
Definition: surfMesh.H:59
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
Foam::word::ext
word ext() const
Definition: word.C:119
Foam::MeshedSurface::canWriteType
static bool canWriteType(const word &fileType, bool verbose=false)
Definition: MeshedSurface.C:70
cartesianCS.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
Foam::argList::addArgument
static void addArgument(const string &argName, const string &usage="")
Definition: argList.C:294
Foam::Info
messageStream Info
argList.H
Foam::IOobject::selectIO
static IOobject selectIO(const IOobject &io, const fileName &altFile, const word &ioName="")
Definition: IOobject.C:231
Foam::FatalError
error FatalError
Foam::coordinateSystems
A centralized collection of named coordinate systems.
Definition: coordinateSystems.H:74
Foam
Definition: atmBoundaryLayer.C:26
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Definition: FlatOutput.H:217
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
Foam::argList::addBoolOption
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Definition: argList.C:317
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:49
Foam::nl
constexpr char nl
Definition: Ostream.H:424
MeshedSurfaces.H
Foam::IOobject::MUST_READ_IF_MODIFIED
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:182
Foam::name
word name(const expressions::valueTypeCode typeCode)
Definition: exprTraits.C:52
Foam::argList::noParallel
static void noParallel()
Definition: argList.C:503
Foam::IOobject::objectPath
fileName objectPath() const
Definition: IOobjectI.H:207
Foam::TimePaths::constant
const word & constant() const
Definition: TimePathsI.H:89
Foam::MeshedSurface
A surface geometry mesh with zone information, not to be confused with the similarly named surfaceMes...
Definition: triSurfaceTools.H:76
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)
coordinateSystems.H
Foam::argList::rootPath
const fileName & rootPath() const noexcept
Definition: argListI.H:56
Foam::argList::found
bool found(const word &optName) const
Definition: argListI.H:171
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:181