FreeFOAM The Cross-Platform CFD Toolkit
surfaceFeatureConvert.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 Application
25  surfaceFeatureConvert
26 
27 Description
28  Extracts and writes surface features to file
29 
30 Usage
31 
32  - surfaceFeatureConvert [OPTIONS] <input file> <output file>
33 
34  @param <input file> \n
35  @todo Detailed description of argument.
36 
37  @param <output file> \n
38  @todo Detailed description of argument.
39 
40  @param -case <dir>\n
41  Case directory.
42 
43  @param -help \n
44  Display help message.
45 
46  @param -doc \n
47  Display Doxygen API documentation page for this application.
48 
49  @param -srcDoc \n
50  Display Doxygen source documentation page for this application.
51 
52 \*---------------------------------------------------------------------------*/
53 
55 #include <OpenFOAM/argList.H>
56 #include <OpenFOAM/Time.H>
57 #include <OpenFOAM/IFstream.H>
58 #include <OpenFOAM/IStringStream.H>
59 #include <OpenFOAM/OFstream.H>
60 #include <OpenFOAM/Map.H>
61 
62 using namespace Foam;
63 
64 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65 
66 void readNASEdges
67 (
68  const fileName& inFileName,
69  pointField& allPoints,
70  edgeList& allEdges
71 )
72 {
73  IFstream is(inFileName);
74 
75  if (!is.good())
76  {
77  FatalErrorIn("readNASEdges")
78  << "Cannot read file " << inFileName
79  << exit(FatalError);
80  }
81 
82  // coordinates of point
84  // Nastran index of point
85  DynamicList<label> pointIndices;
86 
87  // beams
88  DynamicList<edge> edges;
89  DynamicList<label> edgeIndices;
90 
91 
92  while (is.good())
93  {
94  string line;
95  is.getLine(line);
96 
97  if (line.empty() || line[0] == '$')
98  {
99  // Skip empty and comment
100  continue;
101  }
102 
103  // Check if character 72 is continuation
104  if (line.size() > 72 && line[72] == '+')
105  {
106  line = line.substr(0, 72);
107 
108  while (true)
109  {
110  string buf;
111  is.getLine(buf);
112 
113  if (buf.size() > 72 && buf[72] == '+')
114  {
115  line += buf.substr(8, 64);
116  }
117  else
118  {
119  line += buf.substr(8, buf.size()-8);
120  break;
121  }
122  }
123  }
124 
125  // Read first word
126  IStringStream lineStream(line);
127  word cmd;
128  lineStream >> cmd;
129 
130  if (cmd == "GRID")
131  {
132  label index;
133  lineStream >> index;
134  pointIndices.append(index);
135 
136  scalar x = readScalar(IStringStream(line.substr(24, 8))());
137  scalar y = readScalar(IStringStream(line.substr(32, 8))());
138  scalar z = readScalar(IStringStream(line.substr(40, 8))());
139  points.append(point(x, y, z));
140  }
141  else if (cmd == "CBEAM")
142  {
143  // Read shell type since gives patchnames.
144  label index, group, v0, v1;
145  lineStream >> index >> group >> v0 >> v1;
146 
147  edgeIndices.append(index);
148  edges.append(edge(v0, v1));
149  }
150  }
151 
152  points.shrink();
153  pointIndices.shrink();
154  edges.shrink();
155  edgeIndices.shrink();
156 
157  Pout<< "Read from " << inFileName
158  << " edges:" << edges.size() << " points:" << points.size()
159  << endl;
160 
161  {
162  // Build inverse mapping (index to point)
163  Map<label> indexToPoint(2*pointIndices.size());
164  forAll(pointIndices, i)
165  {
166  indexToPoint.insert(pointIndices[i], i);
167  }
168 
169  // Relabel edges
170  forAll(edges, i)
171  {
172  edge& e = edges[i];
173  e[0] = indexToPoint[e[0]];
174  e[1] = indexToPoint[e[1]];
175  }
176  }
177 
178  allPoints.transfer(points);
179  allEdges.transfer(edges);
180 }
181 
182 
183 
184 void write
185 (
186  const Time& runTime,
187  const fileName& inFileName,
188  const fileName& outFileName,
189  const edgeMesh& eMesh
190 )
191 {
192  if (outFileName.ext() == "eMesh")
193  {
194  featureEdgeMesh fem
195  (
196  IOobject
197  (
198  outFileName, // name
199  runTime.constant(), // instance
200  runTime, // registry
203  false
204  ),
205  eMesh.points(),
206  eMesh.edges()
207  );
208 
209  Pout<< "Writing feature edge mesh to " << fem.objectPath()
210  << endl;
211 
212  fem.write();
213  }
214  else if (outFileName.ext() == "vtk")
215  {
216  OFstream str(outFileName);
217 
218  str << "# vtk DataFile Version 2.0" << nl
219  << "featureEdgeMesh " << inFileName << nl
220  << "ASCII" << nl
221  << "DATASET POLYDATA" << nl;
222 
223  str << "POINTS " << eMesh.points().size() << " float" << nl;
224  forAll(eMesh.points(), pointI)
225  {
226  const point& pt = eMesh.points()[pointI];
227 
228  str << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
229  }
230 
231  str << "LINES " << eMesh.edges().size() << ' '
232  << 3*eMesh.edges().size() << nl;
233  forAll(eMesh.edges(), edgeI)
234  {
235  const edge& e = eMesh.edges()[edgeI];
236 
237  str << "2 " << e[0] << ' ' << e[1] << nl;
238  }
239  }
240  else
241  {
242  FatalErrorIn("write")
243  << "Supported output formats: .eMesh, .vtk"
244  << exit(FatalError);
245  }
246 }
247 
248 
249 // Main program:
250 
251 int main(int argc, char *argv[])
252 {
254  argList::validArgs.append("input file");
255  argList::validArgs.append("output file");
256 # include <OpenFOAM/setRootCase.H>
257 # include <OpenFOAM/createTime.H>
258 
259  const fileName inFileName(args.additionalArgs()[0]);
260  const word outFileName(args.additionalArgs()[1]);
261 
262  Pout<< "Input features file : " << inFileName << nl
263  << "Output features file : " << outFileName << nl
264  << endl;
265 
266 
267  // Read
268  // ~~~~
269 
270  if (inFileName.ext() == "nas")
271  {
273  edgeList edges;
274  readNASEdges(inFileName, points, edges);
275 
276  edgeMesh eMesh(points, edges);
277 
278  write(runTime, inFileName, outFileName, eMesh);
279  }
280  else if (inFileName.ext() == "eMesh")
281  {
282  featureEdgeMesh fem
283  (
284  IOobject
285  (
286  inFileName, // name
287  runTime.constant(), // instance
288  runTime, // registry
291  false
292  )
293  );
294 
295  Pout<< "Read from " << inFileName
296  << " edges:" << fem.edges().size()
297  << " points:" << fem.points().size()
298  << endl;
299 
300  write(runTime, inFileName, outFileName, fem);
301  }
302  else
303  {
305  << "Can only handle NASTRAN data formats (.nas extension)."
306  << exit(FatalError);
307  }
308 
309  Pout<< "End\n" << endl;
310 
311  return 0;
312 }
313 
314 
315 // ************************ vim: set sw=4 sts=4 et: ************************ //