surfaceMeshImport.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  surfaceMeshImport
26 
27 Description
28  Import from various third-party surface formats into surfMesh
29  with optional scaling or transformations (rotate/translate)
30  on a coordinateSystem.
31 
32 Usage
33  \b surfaceMeshImport inputFile [OPTION]
34 
35  Options:
36  - \par -clean \n
37  Perform some surface checking/cleanup on the input surface.
38 
39  - \par -name <name> \n
40  Specify an alternative surface name when writing.
41 
42  - \par -scaleIn <scale> \n
43  Specify a scaling factor when reading files.
44 
45  - \par -scaleOut <scale> \n
46  Specify a scaling factor when writing files.
47 
48  - \par -from <coordinateSystem> \n
49  Specify a coordinate system when reading files.
50 
51  - \par -to <coordinateSystem> \n
52  Specify a coordinate system when writing files.
53 
54  Note:
55  The filename extensions are used to determine the file format type.
56 
57 \*---------------------------------------------------------------------------*/
58 
59 #include "argList.H"
60 #include "Time.H"
61 
62 #include "MeshedSurfaces.H"
63 #include "coordinateSystems.H"
64 
65 using namespace Foam;
66 
67 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
68 
69 int main(int argc, char *argv[])
70 {
72  (
73  "import from various third-party surface formats into surfMesh"
74  );
75 
77  argList::validArgs.append("surface file");
78 
80  (
81  "clean",
82  "perform some surface checking/cleanup on the input surface"
83  );
85  (
86  "name",
87  "name",
88  "specify an alternative surface name when writing - "
89  "default is 'default'"
90  );
92  (
93  "scaleIn",
94  "factor",
95  "geometry scaling factor on input - default is 1"
96  );
98  (
99  "scaleOut",
100  "factor",
101  "geometry scaling factor on output - default is 1"
102  );
104  (
105  "from",
106  "coordinateSystem",
107  "specify a local coordinate system when reading files."
108  );
110  (
111  "to",
112  "coordinateSystem",
113  "specify a local coordinate system when writing files."
114  );
115 
116  #include "setRootCase.H"
117  #include "createTime.H"
118 
119  // try for the latestTime, but create "constant" as needed
120  instantList Times = runTime.times();
121  if (Times.size())
122  {
123  label startTime = Times.size()-1;
124  runTime.setTime(Times[startTime], startTime);
125  }
126  else
127  {
128  runTime.setTime(instant(0, runTime.constant()), 0);
129  }
130 
131 
132  const fileName importName = args[1];
133  const word exportName = args.optionLookupOrDefault<word>("name", "default");
134 
135  // check that reading is supported
136  if (!MeshedSurface<face>::canRead(importName, true))
137  {
138  return 1;
139  }
140 
141 
142  // Get the coordinate transformations
143  autoPtr<coordinateSystem> fromCsys;
145 
146  if (args.optionFound("from") || args.optionFound("to"))
147  {
149  (
151  );
152 
153  if (args.optionFound("from"))
154  {
155  const word csName = args["from"];
156  fromCsys = csLst[csName].clone();
157  }
158 
159  if (args.optionFound("to"))
160  {
161  const word csName = args["to"];
162  toCsys = csLst[csName].clone();
163  }
164  }
165 
166 
167  MeshedSurface<face> surf(importName);
168 
169  if (args.optionFound("clean"))
170  {
171  surf.cleanup(true);
172  }
173 
174 
175  scalar scaleIn = 0;
176  if (args.optionReadIfPresent("scaleIn", scaleIn) && scaleIn > 0)
177  {
178  Info<< " -scaleIn " << scaleIn << endl;
179  surf.scalePoints(scaleIn);
180  }
181 
182  if (fromCsys.valid())
183  {
184  Info<< " -from " << fromCsys().name() << endl;
185  tmp<pointField> tpf = fromCsys().localPosition(surf.points());
186  surf.movePoints(tpf());
187  }
188 
189  if (toCsys.valid())
190  {
191  Info<< " -to " << toCsys().name() << endl;
192  tmp<pointField> tpf = toCsys().globalPosition(surf.points());
193  surf.movePoints(tpf());
194  }
195 
196  scalar scaleOut = 0;
197  if (args.optionReadIfPresent("scaleOut", scaleOut) && scaleOut > 0)
198  {
199  Info<< " -scaleOut " << scaleOut << endl;
200  surf.scalePoints(scaleOut);
201  }
202 
203  surfMesh smesh
204  (
205  IOobject
206  (
207  exportName,
208  runTime.constant(),
209  runTime
210  ),
211  move(surf)
212  );
213 
214 
215  Info<< "writing surfMesh:\n " << smesh.relativeObjectPath() << endl;
216  smesh.write();
217 
218  Info<< "\nEnd\n" << endl;
219 
220  return 0;
221 }
222 
223 // ************************************************************************* //
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
A surface geometry mesh with zone information, not to be confused with the similarly named surfaceMes...
Definition: MeshedSurface.H:85
virtual Ostream & write(const char)=0
Write character.
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
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:159
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:118
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:204
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
T optionLookupOrDefault(const word &opt, const T &deflt) const
Read a value from the named option if present.
Definition: argListI.H:243
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set)
Definition: autoPtrI.H:83
Provides a centralised coordinateSystem collection.
A class for handling file names.
Definition: fileName.H:82
An instant of time. Contains the time value and name.
Definition: instant.H:67
A surface mesh consisting of general polygon faces.
Definition: surfMesh.H:60
A class for managing temporary objects.
Definition: tmp.H:55
A class for handling words, derived from string.
Definition: word.H:62
int main(int argc, char *argv[])
Definition: financialFoam.C:44
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
messageStream Info
scalar startTime(conversionProperties.lookup< scalar >("startTime"))
Foam::argList args(argc, argv)