surfaceMeshImport.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-2022 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  surfaceMeshImport
29 
30 Group
31  grpSurfaceUtilities
32 
33 Description
34  Import from various third-party surface formats into surfMesh
35  with optional scaling or transformations (rotate/translate)
36  on a coordinateSystem.
37 
38 Usage
39  \b surfaceMeshImport inputFile [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 -read-format <type>
49  Specify input 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  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  return
83  (
84  name.has_ext("gz")
85  ? name.stem().ext()
86  : name.ext()
87  );
88 }
89 
90 
91 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
92 
93 int main(int argc, char *argv[])
94 {
96  (
97  "Import from various third-party surface formats into surfMesh"
98  );
99 
101  argList::addArgument("surface", "The input surface file");
102 
104  (
105  "clean",
106  "Perform some surface checking/cleanup on the input surface"
107  );
109  (
110  "name",
111  "name",
112  "The surface name when writing (default is 'default')"
113  );
115  (
116  "read-format",
117  "type",
118  "Input format (default: use file extension)"
119  );
121  (
122  "read-scale",
123  "factor",
124  "Input geometry scaling factor"
125  );
127  (
128  "write-scale",
129  "factor",
130  "Output geometry scaling factor"
131  );
132 
133  argList::addOptionCompat("read-scale", {"scaleIn", 1912});
134  argList::addOptionCompat("write-scale", {"scaleOut", 1912});
135 
136  argList::addOption("dict", "file", "Alternative coordinateSystems");
137 
139  (
140  "from",
141  "system",
142  "The source coordinate system, applied after '-read-scale'",
143  true // advanced
144  );
146  (
147  "to",
148  "system",
149  "The target coordinate system, applied before '-write-scale'",
150  true // advanced
151  );
152 
153  #include "setRootCase.H"
154  #include "createTime.H"
155 
156  // try for the latestTime, but create "constant" as needed
157  instantList Times = runTime.times();
158  if (Times.size())
159  {
160  label startTime = Times.size()-1;
162  }
163  else
164  {
166  }
167 
168 
169  const auto importName = args.get<fileName>(1);
170  const auto exportName = args.getOrDefault<word>("name", "default");
171 
172  const word readFileType
173  (
174  args.getOrDefault<word>("read-format", getExtension(importName))
175  );
176 
177  // Check that reading is supported
178  if (!meshedSurface::canRead(readFileType, true))
179  {
180  FatalError
181  << "Unsupported file format(s)" << nl
182  << exit(FatalError);
183  }
184 
185 
186  scalar scaleFactor(0);
187 
188  // The coordinate transformations (must be cartesian)
191 
192  if (args.found("from") || args.found("to"))
193  {
195  (
196  IOobject
197  (
198  coordinateSystems::typeName,
199  runTime.constant(),
200  runTime,
203  false
204  ),
205  args.getOrDefault<fileName>("dict", "")
206  );
207 
208  if (!ioCsys.typeHeaderOk<coordinateSystems>(false))
209  {
210  FatalError
211  << ioCsys.objectPath() << nl
212  << exit(FatalError);
213  }
214 
215  coordinateSystems globalCoords(ioCsys);
216 
217  if (args.found("from"))
218  {
219  const word csName(args["from"]);
220  const auto* csPtr = globalCoords.cfind(csName);
221 
222  if (!csPtr)
223  {
224  FatalError
225  << "Cannot find -from " << csName << nl
226  << "available coordinateSystems: "
227  << flatOutput(globalCoords.names()) << nl
228  << exit(FatalError);
229  }
230 
231  fromCsys = autoPtr<coordSystem::cartesian>::New(*csPtr);
232  }
233 
234  if (args.found("to"))
235  {
236  const word csName(args["to"]);
237  const auto* csPtr = globalCoords.cfind(csName);
238 
239  if (!csPtr)
240  {
241  FatalError
242  << "Cannot find -to " << csName << nl
243  << "available coordinateSystems: "
244  << flatOutput(globalCoords.names()) << nl
245  << exit(FatalError);
246  }
247 
248  toCsys = autoPtr<coordSystem::cartesian>::New(*csPtr);
249  }
250 
251  // Maybe fix this later
252  if (fromCsys && toCsys)
253  {
254  FatalError
255  << "Only allowed '-from' or '-to' option at the moment."
256  << exit(FatalError);
257  }
258  }
259 
260 
261  meshedSurface surf(importName, readFileType);
262 
263  if (args.readIfPresent("read-scale", scaleFactor) && scaleFactor > 0)
264  {
265  Info<< "scale input " << scaleFactor << nl;
266  surf.scalePoints(scaleFactor);
267  }
268 
269  if (args.found("clean"))
270  {
271  surf.cleanup(true);
272  }
273 
274  if (fromCsys)
275  {
276  Info<< "move points from coordinate system: "
277  << fromCsys->name() << endl;
278  tmp<pointField> tpf = fromCsys->localPosition(surf.points());
279  surf.movePoints(tpf());
280  }
281 
282  if (toCsys)
283  {
284  Info<< "move points to coordinate system: "
285  << toCsys->name() << endl;
286  tmp<pointField> tpf = toCsys->globalPosition(surf.points());
287  surf.movePoints(tpf());
288  }
289 
290  if (args.readIfPresent("write-scale", scaleFactor) && scaleFactor > 0)
291  {
292  Info<< "scale output " << scaleFactor << nl;
293  surf.scalePoints(scaleFactor);
294  }
295 
296  surfMesh smesh
297  (
298  IOobject
299  (
300  exportName,
301  runTime.constant(),
302  runTime
303  ),
304  std::move(surf)
305  );
306 
307 
308  Info<< "writing surfMesh:\n " << smesh.objectPath() << endl;
309  smesh.write();
310 
311  Info<< "\nEnd\n" << endl;
312 
313  return 0;
314 }
315 
316 // ************************************************************************* //
point globalPosition(const point &local) const
From local coordinate position to global (cartesian) position.
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:453
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:118
A class for handling file names.
Definition: fileName.H:71
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
static bool canRead(const fileName &name, bool verbose=false)
Can we read this file format?
Definition: MeshedSurface.C:87
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
A centralized collection of named coordinate systems.
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:365
static void noParallel()
Remove the parallel options.
Definition: argList.C:551
static void addOptionCompat(const word &optName, std::pair< const char *, int > compat)
Specify an alias for the option name.
Definition: argList.C:409
Ignore writing from objectRegistry::writeObject()
T getOrDefault(const word &optName, const T &deflt) const
Get a value from the named option if present, or return default.
Definition: argListI.H:300
fileName objectPath() const
The complete path + object name.
Definition: IOobjectI.H:239
point localPosition(const point &global) const
From global (cartesian) position to local coordinate position.
word ext() const
Return file name extension (part after last .)
Definition: wordI.H:171
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
bool has_ext() const
Various checks for extensions.
Definition: stringI.H:43
A class for handling words, derived from Foam::string.
Definition: word.H:63
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:376
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
virtual void setTime(const Time &t)
Reset the time and time-index to those of the given time.
Definition: Time.C:977
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.
A surface mesh consisting of general polygon faces that has IO capabilities and a registry for storin...
Definition: surfMesh.H:59
const word & constant() const noexcept
Return constant name.
Definition: TimePathsI.H:89
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name...
Definition: instant.H:53
instantList times() const
Search the case for valid time directories.
Definition: TimePaths.C:142
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:271
virtual const word & name() const
Return the name.
static void addArgument(const string &argName, const string &usage="")
Append a (mandatory) argument to validArgs.
Definition: argList.C:342
messageStream Info
Information stream (stdout output on master, null elsewhere)
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:316
A class for managing temporary objects.
Definition: HashPtrTable.H:50
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition: autoPtr.H:178
Foam::argList args(argc, argv)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:166
Foam::label startTime
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:171
Namespace for OpenFOAM.
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:231
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225