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-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  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  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  "Import from various third-party surface formats into surfMesh"
99  );
100 
102  argList::addArgument("surface", "The input surface file");
103 
105  (
106  "clean",
107  "Perform some surface checking/cleanup on the input surface"
108  );
110  (
111  "name",
112  "name",
113  "The surface name when writing (default is 'default')"
114  );
116  (
117  "read-format",
118  "type",
119  "Input format (default: use file extension)"
120  );
122  (
123  "read-scale",
124  "factor",
125  "Input geometry scaling factor"
126  );
128  (
129  "write-scale",
130  "factor",
131  "Output geometry scaling factor"
132  );
133 
134  argList::addOptionCompat("read-scale", {"scaleIn", 1912});
135  argList::addOptionCompat("write-scale", {"scaleOut", 1912});
136 
137  argList::addOption("dict", "file", "Alternative coordinateSystems");
138 
140  (
141  "from",
142  "system",
143  "The source coordinate system, applied after '-read-scale'",
144  true // advanced
145  );
147  (
148  "to",
149  "system",
150  "The target coordinate system, applied before '-write-scale'",
151  true // advanced
152  );
153 
154  #include "setRootCase.H"
155  #include "createTime.H"
156 
157  // try for the latestTime, but create "constant" as needed
158  instantList Times = runTime.times();
159  if (Times.size())
160  {
161  label startTime = Times.size()-1;
163  }
164  else
165  {
167  }
168 
169 
170  const auto importName = args.get<fileName>(1);
171  const auto exportName = args.getOrDefault<word>("name", "default");
172 
173  const word readFileType
174  (
175  args.getOrDefault<word>("read-format", getExtension(importName))
176  );
177 
178  // Check that reading is supported
179  if (!meshedSurface::canRead(readFileType, true))
180  {
181  FatalError
182  << "Unsupported file format(s)" << nl
183  << exit(FatalError);
184  }
185 
186 
187  scalar scaleFactor(0);
188 
189  // The coordinate transformations (must be cartesian)
192 
193  if (args.found("from") || args.found("to"))
194  {
196  (
197  IOobject
198  (
199  coordinateSystems::typeName,
200  runTime.constant(),
201  runTime,
204  false
205  ),
206  args.getOrDefault<fileName>("dict", "")
207  );
208 
209  if (!ioCsys.typeHeaderOk<coordinateSystems>(false))
210  {
211  FatalError
212  << ioCsys.objectPath() << nl
213  << exit(FatalError);
214  }
215 
216  coordinateSystems globalCoords(ioCsys);
217 
218  if (args.found("from"))
219  {
220  const word csName(args["from"]);
221  const auto* csPtr = globalCoords.cfind(csName);
222 
223  if (!csPtr)
224  {
225  FatalError
226  << "Cannot find -from " << csName << nl
227  << "available coordinateSystems: "
228  << flatOutput(globalCoords.names()) << nl
229  << exit(FatalError);
230  }
231 
232  fromCsys = autoPtr<coordSystem::cartesian>::New(*csPtr);
233  }
234 
235  if (args.found("to"))
236  {
237  const word csName(args["to"]);
238  const auto* csPtr = globalCoords.cfind(csName);
239 
240  if (!csPtr)
241  {
242  FatalError
243  << "Cannot find -to " << csName << nl
244  << "available coordinateSystems: "
245  << flatOutput(globalCoords.names()) << nl
246  << exit(FatalError);
247  }
248 
249  toCsys = autoPtr<coordSystem::cartesian>::New(*csPtr);
250  }
251 
252  // Maybe fix this later
253  if (fromCsys && toCsys)
254  {
255  FatalError
256  << "Only allowed '-from' or '-to' option at the moment."
257  << exit(FatalError);
258  }
259  }
260 
261 
262  meshedSurface surf(importName, readFileType);
263 
264  if (args.readIfPresent("read-scale", scaleFactor) && scaleFactor > 0)
265  {
266  Info<< "scale input " << scaleFactor << nl;
267  surf.scalePoints(scaleFactor);
268  }
269 
270  if (args.found("clean"))
271  {
272  surf.cleanup(true);
273  }
274 
275  if (fromCsys)
276  {
277  Info<< "move points from coordinate system: "
278  << fromCsys->name() << endl;
279  tmp<pointField> tpf = fromCsys->localPosition(surf.points());
280  surf.movePoints(tpf());
281  }
282 
283  if (toCsys)
284  {
285  Info<< "move points to coordinate system: "
286  << toCsys->name() << endl;
287  tmp<pointField> tpf = toCsys->globalPosition(surf.points());
288  surf.movePoints(tpf());
289  }
290 
291  if (args.readIfPresent("write-scale", scaleFactor) && scaleFactor > 0)
292  {
293  Info<< "scale output " << scaleFactor << nl;
294  surf.scalePoints(scaleFactor);
295  }
296 
297  surfMesh smesh
298  (
299  IOobject
300  (
301  exportName,
302  runTime.constant(),
303  runTime
304  ),
305  std::move(surf)
306  );
307 
308 
309  Info<< "writing surfMesh:\n " << smesh.objectPath() << endl;
310  smesh.write();
311 
312  Info<< "\nEnd\n" << endl;
313 
314  return 0;
315 }
316 
317 // ************************************************************************* //
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::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::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::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
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::MeshedSurface::canRead
static bool canRead(const fileName &name, bool verbose=false)
Definition: MeshedSurface.C:87
Foam::argList::addArgument
static void addArgument(const string &argName, const string &usage="")
Definition: argList.C:294
Foam::Info
messageStream Info
argList.H
Foam::TimePaths::times
instantList times() const
Definition: TimePaths.C:142
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::Ostream::write
virtual bool write(const token &tok)=0
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
setRootCase.H
Foam::nl
constexpr char nl
Definition: Ostream.H:424
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:58
Foam::Time::setTime
virtual void setTime(const Time &t)
Definition: Time.C:996
MeshedSurfaces.H
createTime.H
Foam::name
word name(const expressions::valueTypeCode typeCode)
Definition: exprTraits.C:52
startTime
Foam::label startTime
Definition: checkTimeOptions.H:1
Foam::instant
An instant of time. Contains the time value and name.
Definition: instant.H:48
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::found
bool found(const word &optName) const
Definition: argListI.H:171
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:181