OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

surfaceFeatureConvert.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Application
00026     surfaceFeatureConvert
00027 
00028 Description
00029     Extracts and writes surface features to file
00030 
00031 \*---------------------------------------------------------------------------*/
00032 
00033 #include "featureEdgeMesh.H"
00034 #include "argList.H"
00035 #include "Time.H"
00036 #include "IFstream.H"
00037 #include "IStringStream.H"
00038 #include "OFstream.H"
00039 #include "Map.H"
00040 
00041 using namespace Foam;
00042 
00043 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00044 
00045 void readNASEdges
00046 (
00047     const fileName& inFileName,
00048     pointField& allPoints,
00049     edgeList& allEdges
00050 )
00051 {
00052     IFstream is(inFileName);
00053 
00054     if (!is.good())
00055     {
00056         FatalErrorIn("readNASEdges")
00057             << "Cannot read file " << inFileName
00058             << exit(FatalError);
00059     }
00060 
00061     // coordinates of point
00062     DynamicList<point> points;
00063     // Nastran index of point
00064     DynamicList<label> pointIndices;
00065 
00066     // beams
00067     DynamicList<edge> edges;
00068     DynamicList<label> edgeIndices;
00069 
00070 
00071     while (is.good())
00072     {
00073         string line;
00074         is.getLine(line);
00075 
00076         if (line.empty() || line[0] == '$')
00077         {
00078             // Skip empty and comment
00079             continue;
00080         }
00081 
00082         // Check if character 72 is continuation
00083         if (line.size() > 72 && line[72] == '+')
00084         {
00085             line = line.substr(0, 72);
00086 
00087             while (true)
00088             {
00089                 string buf;
00090                 is.getLine(buf);
00091 
00092                 if (buf.size() > 72 && buf[72] == '+')
00093                 {
00094                     line += buf.substr(8, 64);
00095                 }
00096                 else
00097                 {
00098                     line += buf.substr(8, buf.size()-8);
00099                     break;
00100                 }
00101             }
00102         }
00103 
00104         // Read first word
00105         IStringStream lineStream(line);
00106         word cmd;
00107         lineStream >> cmd;
00108 
00109         if (cmd == "GRID")
00110         {
00111             label index;
00112             lineStream >> index;
00113             pointIndices.append(index);
00114 
00115             scalar x = readScalar(IStringStream(line.substr(24, 8))());
00116             scalar y = readScalar(IStringStream(line.substr(32, 8))());
00117             scalar z = readScalar(IStringStream(line.substr(40, 8))());
00118             points.append(point(x, y, z));
00119         }
00120         else if (cmd == "CBEAM")
00121         {
00122             // Read shell type since gives patchnames.
00123             label index, group, v0, v1;
00124             lineStream >> index >> group >> v0 >> v1;
00125 
00126             edgeIndices.append(index);
00127             edges.append(edge(v0, v1));
00128         }
00129     }
00130 
00131     points.shrink();
00132     pointIndices.shrink();
00133     edges.shrink();
00134     edgeIndices.shrink();
00135 
00136     Pout<< "Read from " << inFileName
00137         << " edges:" << edges.size() << " points:" << points.size()
00138         << endl;
00139 
00140     {
00141         // Build inverse mapping (index to point)
00142         Map<label> indexToPoint(2*pointIndices.size());
00143         forAll(pointIndices, i)
00144         {
00145             indexToPoint.insert(pointIndices[i], i);
00146         }
00147 
00148         // Relabel edges
00149         forAll(edges, i)
00150         {
00151             edge& e = edges[i];
00152             e[0] = indexToPoint[e[0]];
00153             e[1] = indexToPoint[e[1]];
00154         }
00155     }
00156 
00157     allPoints.transfer(points);
00158     allEdges.transfer(edges);
00159 }
00160 
00161 
00162 
00163 void write
00164 (
00165     const Time& runTime,
00166     const fileName& inFileName,
00167     const fileName& outFileName,
00168     const edgeMesh& eMesh
00169 )
00170 {
00171     if (outFileName.ext() == "eMesh")
00172     {
00173         featureEdgeMesh fem
00174         (
00175             IOobject
00176             (
00177                 outFileName,        // name
00178                 runTime.constant(), // instance
00179                 runTime,            // registry
00180                 IOobject::NO_READ,
00181                 IOobject::AUTO_WRITE,
00182                 false
00183             ),
00184             eMesh.points(),
00185             eMesh.edges()
00186         );
00187 
00188         Pout<< "Writing feature edge mesh to " << fem.objectPath()
00189             << endl;
00190 
00191         fem.write();
00192     }
00193     else if (outFileName.ext() == "vtk")
00194     {
00195         OFstream str(outFileName);
00196 
00197         str << "# vtk DataFile Version 2.0" << nl
00198             << "featureEdgeMesh " << inFileName << nl
00199             << "ASCII" << nl
00200             << "DATASET POLYDATA" << nl;
00201 
00202         str << "POINTS " << eMesh.points().size() << " float" << nl;
00203         forAll(eMesh.points(), pointI)
00204         {
00205             const point& pt = eMesh.points()[pointI];
00206 
00207             str << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
00208         }
00209 
00210         str << "LINES " << eMesh.edges().size() << ' '
00211             << 3*eMesh.edges().size() << nl;
00212         forAll(eMesh.edges(), edgeI)
00213         {
00214             const edge& e = eMesh.edges()[edgeI];
00215 
00216             str << "2 " << e[0] << ' ' << e[1] << nl;
00217         }
00218     }
00219     else
00220     {
00221         FatalErrorIn("write")
00222             << "Supported output formats: .eMesh, .vtk"
00223             << exit(FatalError);
00224     }
00225 }
00226 
00227 
00228 // Main program:
00229 
00230 int main(int argc, char *argv[])
00231 {
00232     argList::noParallel();
00233     argList::validArgs.append("input file");
00234     argList::validArgs.append("output file");
00235 #   include "setRootCase.H"
00236 #   include "createTime.H"
00237 
00238     const fileName inFileName(args.additionalArgs()[0]);
00239     const word outFileName(args.additionalArgs()[1]);
00240 
00241     Pout<< "Input features file  : " << inFileName << nl
00242         << "Output features file : " << outFileName << nl
00243         << endl;
00244 
00245 
00246     // Read
00247     // ~~~~
00248 
00249     if (inFileName.ext() == "nas")
00250     {
00251         pointField points;
00252         edgeList edges;
00253         readNASEdges(inFileName, points, edges);
00254 
00255         edgeMesh eMesh(points, edges);
00256 
00257         write(runTime, inFileName, outFileName, eMesh);
00258     }
00259     else if (inFileName.ext() == "eMesh")
00260     {
00261         featureEdgeMesh fem
00262         (
00263             IOobject
00264             (
00265                 inFileName,         // name
00266                 runTime.constant(), // instance
00267                 runTime,            // registry
00268                 IOobject::MUST_READ,
00269                 IOobject::AUTO_WRITE,
00270                 false
00271             )
00272         );
00273 
00274         Pout<< "Read from " << inFileName
00275             << " edges:" << fem.edges().size()
00276             << " points:" << fem.points().size()
00277             << endl;
00278 
00279         write(runTime, inFileName, outFileName, fem);
00280     }
00281     else
00282     {
00283         FatalErrorIn(args.executable())
00284             << "Can only handle NASTRAN data formats (.nas extension)."
00285             << exit(FatalError);
00286     }
00287 
00288     Pout<< "End\n" << endl;
00289 
00290     return 0;
00291 }
00292 
00293 
00294 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd