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 | Copyright (C) 2011-2015 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 \*---------------------------------------------------------------------------*/
25 
26 #include "AC3DsurfaceFormatCore.H"
27 #include "clock.H"
28 #include "IFstream.H"
29 #include "IStringStream.H"
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
34 (
35  IFstream& is,
36  string& cmd,
37  string& args
38 )
39 {
40  if (is.good())
41  {
42  string line;
43  is.getLine(line);
44 
45  string::size_type space = line.find(' ');
46 
47  if (space != string::npos)
48  {
49  cmd = line.substr(0, space);
50  args = line.substr(space+1);
51 
52  return true;
53  }
54  }
55  return false;
56 }
57 
58 
59 // Read up to line starting with cmd. Sets args to rest of line.
60 // Returns true if found, false if stream is not good anymore.
62 (
63  IFstream& is,
64  const string& cmd,
65  string& args
66 )
67 {
68  while (is.good())
69  {
70  string line;
71  is.getLine(line);
72 
73  string::size_type space = line.find(' ');
74 
75  if (space != string::npos)
76  {
77  if (line.substr(0, space) == cmd)
78  {
79  args = line.substr(space+1);
80 
81  return true;
82  }
83  }
84  }
85  return false;
86 }
87 
88 
89 // Similar to cueTo(), but throws error if cmd not found
91 (
92  IFstream& is,
93  const string& cmd,
94  const string& errorMsg
95 )
96 {
97  string args;
98  if (!cueTo(is, cmd, args))
99  {
101  << "Cannot find command " << cmd
102  << " " << errorMsg
103  << exit(FatalError);
104  }
105 
106  return args;
107 }
108 
109 
111 (
112  Ostream& os,
113  const UList<surfZone>& zoneLst
114 )
115 {
116  // Write with zones as separate objects under "world" object.
117  // Header is taken over from sample file.
118  // Defines separate materials for all zones. Recycle colours.
119 
120  // Define 8 standard colours as r,g,b components
121  static scalar colourMap[] =
122  {
123  1, 1, 1,
124  1, 0, 0,
125  0, 1, 0,
126  0, 0, 1,
127  1, 1, 0,
128  0, 1, 1,
129  1, 0, 1,
130  0.5, 0.5, 1
131  };
132 
133  // Write header. Define materials.
134  os << "AC3Db" << nl;
135 
136  forAll(zoneLst, zoneI)
137  {
138  label colourI = zoneI % 8;
139  label colourCompI = 3 * colourI;
140 
141  os << "MATERIAL \"" << zoneLst[zoneI].name() << "Mat\" rgb "
142  << colourMap[colourCompI] << ' ' << colourMap[colourCompI+1]
143  << ' ' << colourMap[colourCompI+2]
144  << " amb 0.2 0.2 0.2 emis 0 0 0 spec 0.5 0.5 0.5 shi 10"
145  << " trans 0"
146  << nl;
147  }
148 
149  os << "OBJECT world" << nl
150  << "kids " << zoneLst.size() << endl;
151 }
152 
153 
154 // ************************************************************************* //
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
AC3DsurfaceFormatCore.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:74
IStringStream.H
Foam::fileFormats::AC3DsurfaceFormatCore::readCmd
static bool readCmd(IFstream &, string &cmd, string &args)
Read cmd, args from IFstream.
Definition: AC3DsurfaceFormatCore.C:34
Foam::label
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
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::ISstream::getLine
ISstream & getLine(string &)
Raw, low-level getline into a string function.
Definition: ISstreamI.H:77
clock.H
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
IFstream.H
Foam::FatalError
error FatalError
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::fileFormats::AC3DsurfaceFormatCore::writeHeader
static void writeHeader(Ostream &, const UList< surfZone > &)
Write header with materials.
Definition: AC3DsurfaceFormatCore.C:111
Foam::fileFormats::AC3DsurfaceFormatCore::cueTo
static bool cueTo(IFstream &, const string &cmd, string &args)
Cue up to cmd, reading args.
Definition: AC3DsurfaceFormatCore.C:62
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
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:60
Foam::fileFormats::AC3DsurfaceFormatCore::cueToOrDie
static string cueToOrDie(IFstream &, const string &cmd, const string &errorMsg=string::null)
Cue up to cmd, reading args or exit with a FatalError.
Definition: AC3DsurfaceFormatCore.C:91
Foam::line
A line primitive.
Definition: line.H:56
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Foam::UList::size
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
args
Foam::argList args(argc, argv)