AC3DsurfaceFormatCore.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) 2020 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 \*---------------------------------------------------------------------------*/
28 
29 #include "AC3DsurfaceFormatCore.H"
30 #include "clock.H"
31 #include "IFstream.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 // Define 8 standard colours as r,g,b components
36 static float colourMap[] =
37 {
38  1, 1, 1,
39  1, 0, 0,
40  0, 1, 0,
41  0, 0, 1,
42  1, 1, 0,
43  0, 1, 1,
44  1, 0, 1,
45  0.5, 0.5, 1
46 };
47 
48 
49 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
50 
52 (
53  IFstream& is,
54  string& cmd,
55  string& args
56 )
57 {
58  if (is.good())
59  {
60  string line;
61  is.getLine(line);
62 
63  const auto space = line.find(' ');
64 
65  if (space && space != string::npos)
66  {
67  cmd = line.substr(0, space);
68  args = line.substr(space+1);
69 
70  return true;
71  }
72  }
73 
74  return false;
75 }
76 
77 
79 (
80  IFstream& is,
81  const string& cmd,
82  string& args
83 )
84 {
85  string line;
86  while (is.good())
87  {
88  is.getLine(line);
89 
90  const auto space = line.find(' ');
91 
92  if (space && space != string::npos && cmd == line.substr(0, space))
93  {
94  args = line.substr(space+1);
95 
96  return true;
97  }
98  }
99 
100  return false;
101 }
102 
103 
105 (
106  IFstream& is,
107  const string& cmd,
108  const string& errorMsg
109 )
110 {
111  string args;
112  if (!cueTo(is, cmd, args))
113  {
115  << "Cannot find command " << cmd
116  << " " << errorMsg
117  << exit(FatalError);
118  }
119 
120  return args;
121 }
122 
123 
125 (
126  Ostream& os,
127  const UList<surfZone>& zones
128 )
129 {
130  // Write with zones as separate objects under "world" object.
131  // Header is taken over from sample file.
132  // Defines separate materials for all zones. Recycle colours.
133 
134  // Write header. Define materials.
135  os << "AC3Db" << nl;
136 
137  forAll(zones, zonei)
138  {
139  const label colourI = zonei % 8;
140  const label colourCompI = 3 * colourI;
141 
142  os << "MATERIAL \"" << zones[zonei].name() << "Mat\" rgb "
143  << colourMap[colourCompI] << ' ' << colourMap[colourCompI+1]
144  << ' ' << colourMap[colourCompI+2]
145  << " amb 0.2 0.2 0.2 emis 0 0 0 spec 0.5 0.5 0.5 shi 10"
146  << " trans 0"
147  << nl;
148  }
149 
150  os << "OBJECT world" << nl
151  << "kids " << zones.size() << endl;
152 }
153 
154 
155 // ************************************************************************* //
Foam::ISstream::getLine
ISstream & getLine(std::string &str, char delim='\n')
Raw, low-level getline (until delimiter) into a string.
Definition: ISstreamI.H:76
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:53
AC3DsurfaceFormatCore.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Foam::fileFormats::AC3DsurfaceFormatCore::readCmd
static bool readCmd(IFstream &is, string &cmd, string &args)
Read cmd, args from IFstream.
Definition: AC3DsurfaceFormatCore.C:52
clock.H
colourMap
static float colourMap[]
Definition: AC3DsurfaceFormatCore.C:36
IFstream.H
Foam::FatalError
error FatalError
os
OBJstream os(runTime.globalPath()/outputName)
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::fileFormats::AC3DsurfaceFormatCore::cueTo
static bool cueTo(IFstream &, const string &cmd, string &args)
Read up to a line starting with cmd. Sets args to rest of line.
Definition: AC3DsurfaceFormatCore.C:79
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::fileFormats::AC3DsurfaceFormatCore::writeHeader
static void writeHeader(Ostream &os, const UList< surfZone > &zones)
Write header with materials for each zone.
Definition: AC3DsurfaceFormatCore.C:125
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::fileFormats::AC3DsurfaceFormatCore::cueToOrDie
static string cueToOrDie(IFstream &, const string &cmd, const string &errorMsg=string::null)
Like cueTo(), but FatalError if not found.
Definition: AC3DsurfaceFormatCore.C:105
Foam::line
A line primitive.
Definition: line.H:53
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
args
Foam::argList args(argc, argv)