FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/clock.H>
28 #include <OpenFOAM/IFstream.H>
29 #include <OpenFOAM/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  (
102  "fileFormats::AC3DsurfaceFormat::read(const fileName&)"
103  )
104  << "Cannot find command " << cmd
105  << " " << errorMsg
106  << exit(FatalError);
107  }
108 
109  return args;
110 }
111 
112 
114 (
115  Ostream& os,
116  const UList<surfZone>& zoneLst
117 )
118 {
119  // Write with zones as separate objects under "world" object.
120  // Header is taken over from sample file.
121  // Defines separate materials for all zones. Recycle colours.
122 
123  // Define 8 standard colours as r,g,b components
124  static scalar colourMap[] =
125  {
126  1, 1, 1,
127  1, 0, 0,
128  0, 1, 0,
129  0, 0, 1,
130  1, 1, 0,
131  0, 1, 1,
132  1, 0, 1,
133  0.5, 0.5, 1
134  };
135 
136  // Write header. Define materials.
137  os << "AC3Db" << nl;
138 
139  forAll(zoneLst, zoneI)
140  {
141  label colourI = zoneI % 8;
142  label colourCompI = 3 * colourI;
143 
144  os << "MATERIAL \"" << zoneLst[zoneI].name() << "Mat\" rgb "
145  << colourMap[colourCompI] << ' ' << colourMap[colourCompI+1]
146  << ' ' << colourMap[colourCompI+2]
147  << " amb 0.2 0.2 0.2 emis 0 0 0 spec 0.5 0.5 0.5 shi 10"
148  << " trans 0"
149  << nl;
150  }
151 
152  os << "OBJECT world" << nl
153  << "kids " << zoneLst.size() << endl;
154 }
155 
156 
157 // ************************ vim: set sw=4 sts=4 et: ************************ //