triSurfaceNew.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) 2020-2022 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "triSurface.H"
29 #include "Fstream.H"
30 #include "MeshedSurface.H"
31 #include "UnsortedMeshedSurface.H"
32 
33 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
34 
37 (
38  const fileName& name,
39  const word& fileType
40 )
41 {
42  const word ext(name.ext());
43 
44  if (fileType.empty())
45  {
46  // Handle empty/missing type
47 
48  if (ext.empty())
49  {
51  << "Cannot determine format from filename" << nl
52  << " " << name << nl
53  << exit(FatalError);
54  }
55 
56  return New(name, ext);
57  }
58  else if (fileType == "gz")
59  {
60  // Degenerate call
61  return New(name.lessExt(), name.stem().ext());
62  }
63  else if (ext == "gz")
64  {
65  // Handle trailing "gz" on file name
66  return New(name.lessExt(), fileType);
67  }
68 
69  // if (check && !exists(name))
70  // {
71  // FatalErrorInFunction
72  // << "No such file " << name << nl
73  // << exit(FatalError);
74  // }
75 
76 
77  // Hard-coded readers
78  if (fileType == "ftr")
79  {
80  auto surf = autoPtr<triSurface>::New();
81 
82  IFstream is(name);
83  surf->readNative(is);
84  return surf;
85  }
86  else if (fileType == "stl")
87  {
88  auto surf = autoPtr<triSurface>::New();
89 
90  surf->readSTL(name); // ASCII
91  return surf;
92  }
93  else if (fileType == "stlb")
94  {
95  auto surf = autoPtr<triSurface>::New();
96 
97  surf->readSTL(name, true); // Force BINARY
98  return surf;
99  }
100 
101  {
102  // UnsortedMeshedSurface
103  using proxyType = UnsortedMeshedSurface<labelledTri>;
104  if (proxyType::readTypes().found(fileType))
105  {
106  auto surf = autoPtr<triSurface>::New();
107 
108  surf->transfer(*proxyType::New(name, fileType));
109  return surf;
110  }
111  }
112 
113  // MeshedSurface
114  {
115  using proxyType = MeshedSurface<labelledTri>;
116  if (proxyType::readTypes().found(fileType))
117  {
118  auto surf = autoPtr<triSurface>::New();
119 
120  surf->transfer(*proxyType::New(name, fileType));
121  return surf;
122  }
123  }
124 
125  {
127  << "Unknown surface format " << fileType
128  << " for reading file " << name << nl
129  << "Valid types:" << nl
130  << " " << flatOutput(readTypes().sortedToc()) << nl
131  << exit(FatalError);
132  }
134  // Failed
135  return nullptr;
136 }
137 
138 
141 {
142  if (name.has_ext("gz"))
143  {
144  // Handle trailing "gz" on file name
145  return New(name.lessExt(), name.stem().ext());
146  }
147 
148  return New(name, name.ext());
149 }
150 
151 
152 // ************************************************************************* //
A surface geometry mesh, in which the surface zone information is conveyed by the &#39;zoneId&#39; associated...
Definition: MeshedSurface.H:76
A class for handling file names.
Definition: fileName.H:72
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...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
A surface geometry mesh with zone information, not to be confused with the similarly named surfaceMes...
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
bool found(const T &val, label pos=0) const
Same as contains()
Definition: UList.H:879
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
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 expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
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
word lessExt() const
Return word without extension (part before last .)
Definition: wordI.H:192
static autoPtr< triSurface > New(const fileName &name, const word &fileType)
Read construct from filename with given file type.
Definition: triSurfaceNew.C:30
Input from file stream, using an ISstream.
Definition: IFstream.H:49
List< label > sortedToc(const UList< bool > &bools)
Return the (sorted) values corresponding to &#39;true&#39; entries.
Definition: BitOps.C:195
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition: autoPtr.H:178
static wordHashSet readTypes()
Known readable file-types, including via friends or proxies.
Definition: triSurfaceIO.C:33
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225