surfaceMeshConvert.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  surfaceMeshConvert
26 
27 Description
28  Converts between surface formats with optional scaling or
29  transformations (rotate/translate) on a coordinateSystem.
30 
31 Usage
32  \b surfaceMeshConvert inputFile outputFile [OPTION]
33 
34  Options:
35  - \par -clean
36  Perform some surface checking/cleanup on the input surface.
37 
38  - \par -scaleIn <scale>
39  Specify a scaling factor when reading files.
40 
41  - \par -scaleOut <scale>
42  Specify a scaling factor when writing files.
43 
44  - \par -from <coordinateSystem>
45  Specify a coordinate System when reading files.
46 
47  - \par -to <coordinateSystem>
48  Specify a coordinate System when writing files.
49 
50  - \par -tri
51  Triangulate surface.
52 
53  Note:
54  The filename extensions are used to determine the file format type.
55 
56 \*---------------------------------------------------------------------------*/
57 
58 #include "argList.H"
59 #include "Time.H"
60 
61 #include "MeshedSurfaces.H"
62 #include "coordinateSystems.H"
63 
64 using namespace Foam;
65 
66 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
67 
68 int main(int argc, char *argv[])
69 {
71  (
72  "convert between surface formats"
73  );
74 
76  argList::validArgs.append("surface file");
77  argList::validArgs.append("output surface file");
78 
80  (
81  "clean",
82  "perform some surface checking/cleanup on the input surface"
83  );
85  (
86  "scaleIn",
87  "factor",
88  "geometry scaling factor on input"
89  );
91  (
92  "scaleOut",
93  "factor",
94  "geometry scaling factor on output"
95  );
97  (
98  "from",
99  "system",
100  "specify the source coordinate system, applied after '-scaleIn'"
101  );
103  (
104  "to",
105  "system",
106  "specify the target coordinate system, applied before '-scaleOut'"
107  );
109  (
110  "tri",
111  "triangulate surface"
112  );
113 
114 
115  argList args(argc, argv);
116  Time runTime(args.rootPath(), args.caseName());
117 
118  const fileName importName = args[1];
119  const fileName exportName = args[2];
120 
121  // disable inplace editing
122  if (importName == exportName)
123  {
125  << "Output file " << exportName << " would overwrite input file."
126  << exit(FatalError);
127  }
128 
129  // check that reading/writing is supported
130  if
131  (
132  !MeshedSurface<face>::canRead(importName, true)
133  || !MeshedSurface<face>::canWriteType(exportName.ext(), true)
134  )
135  {
136  return 1;
137  }
138 
139 
140  // Get the coordinate transformations
141  autoPtr<coordinateSystem> fromCsys;
143 
144  if (args.optionFound("from") || args.optionFound("to"))
145  {
147 
148  if (args.optionFound("from"))
149  {
150  const word csName = args["from"];
151  fromCsys = csLst[csName].clone();
152  }
153 
154  if (args.optionFound("to"))
155  {
156  const word csName = args["to"];
157  toCsys = csLst[csName].clone();
158  }
159  }
160 
161 
162  {
163  MeshedSurface<face> surf(importName);
164 
165  if (args.optionFound("clean"))
166  {
167  surf.cleanup(true);
168  }
169 
170  scalar scaleIn = 0;
171  if (args.optionReadIfPresent("scaleIn", scaleIn) && scaleIn > 0)
172  {
173  Info<< " -scaleIn " << scaleIn << endl;
174  surf.scalePoints(scaleIn);
175  }
176 
177 
178  if (fromCsys.valid())
179  {
180  Info<< " -from " << fromCsys().name() << endl;
181  tmp<pointField> tpf = fromCsys().localPosition(surf.points());
182  surf.movePoints(tpf());
183  }
184 
185  if (toCsys.valid())
186  {
187  Info<< " -to " << toCsys().name() << endl;
188  tmp<pointField> tpf = toCsys().globalPosition(surf.points());
189  surf.movePoints(tpf());
190  }
191 
192  scalar scaleOut = 0;
193  if (args.optionReadIfPresent("scaleOut", scaleOut) && scaleOut > 0)
194  {
195  Info<< " -scaleOut " << scaleOut << endl;
196  surf.scalePoints(scaleOut);
197  }
198 
199  if (args.optionFound("tri"))
200  {
201  Info<< "triangulate" << endl;
202  surf.triangulate();
203  }
204 
205  Info<< "writing " << exportName;
206  surf.write(exportName);
207  }
208 
209  Info<< "\nEnd\n" << endl;
210 
211  return 0;
212 }
213 
214 // ************************************************************************* //
Provides a centralised coordinateSystem collection.
A class for handling file names.
Definition: fileName.H:79
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
const fileName & rootPath() const
Return root path.
Definition: argListI.H:42
A surface geometry mesh with zone information, not to be confused with the similarly named surfaceMes...
Definition: MeshedSurface.H:72
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
static void noParallel()
Remove the parallel options.
Definition: argList.C:175
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:153
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:204
word ext() const
Return file name extension (part after last .)
Definition: fileName.C:299
A class for handling words, derived from string.
Definition: word.H:59
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:102
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:128
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set)
Definition: autoPtrI.H:83
const fileName & caseName() const
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:48
messageStream Info
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:118
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:159
A class for managing temporary objects.
Definition: PtrList.H:53
Foam::argList args(argc, argv)
Namespace for OpenFOAM.