00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #include <edgeMesh/featureEdgeMesh.H>
00055 #include <OpenFOAM/argList.H>
00056 #include <OpenFOAM/Time.H>
00057 #include <OpenFOAM/IFstream.H>
00058 #include <OpenFOAM/IStringStream.H>
00059 #include <OpenFOAM/OFstream.H>
00060 #include <OpenFOAM/Map.H>
00061
00062 using namespace Foam;
00063
00064
00065
00066 void readNASEdges
00067 (
00068 const fileName& inFileName,
00069 pointField& allPoints,
00070 edgeList& allEdges
00071 )
00072 {
00073 IFstream is(inFileName);
00074
00075 if (!is.good())
00076 {
00077 FatalErrorIn("readNASEdges")
00078 << "Cannot read file " << inFileName
00079 << exit(FatalError);
00080 }
00081
00082
00083 DynamicList<point> points;
00084
00085 DynamicList<label> pointIndices;
00086
00087
00088 DynamicList<edge> edges;
00089 DynamicList<label> edgeIndices;
00090
00091
00092 while (is.good())
00093 {
00094 string line;
00095 is.getLine(line);
00096
00097 if (line.empty() || line[0] == '$')
00098 {
00099
00100 continue;
00101 }
00102
00103
00104 if (line.size() > 72 && line[72] == '+')
00105 {
00106 line = line.substr(0, 72);
00107
00108 while (true)
00109 {
00110 string buf;
00111 is.getLine(buf);
00112
00113 if (buf.size() > 72 && buf[72] == '+')
00114 {
00115 line += buf.substr(8, 64);
00116 }
00117 else
00118 {
00119 line += buf.substr(8, buf.size()-8);
00120 break;
00121 }
00122 }
00123 }
00124
00125
00126 IStringStream lineStream(line);
00127 word cmd;
00128 lineStream >> cmd;
00129
00130 if (cmd == "GRID")
00131 {
00132 label index;
00133 lineStream >> index;
00134 pointIndices.append(index);
00135
00136 scalar x = readScalar(IStringStream(line.substr(24, 8))());
00137 scalar y = readScalar(IStringStream(line.substr(32, 8))());
00138 scalar z = readScalar(IStringStream(line.substr(40, 8))());
00139 points.append(point(x, y, z));
00140 }
00141 else if (cmd == "CBEAM")
00142 {
00143
00144 label index, group, v0, v1;
00145 lineStream >> index >> group >> v0 >> v1;
00146
00147 edgeIndices.append(index);
00148 edges.append(edge(v0, v1));
00149 }
00150 }
00151
00152 points.shrink();
00153 pointIndices.shrink();
00154 edges.shrink();
00155 edgeIndices.shrink();
00156
00157 Pout<< "Read from " << inFileName
00158 << " edges:" << edges.size() << " points:" << points.size()
00159 << endl;
00160
00161 {
00162
00163 Map<label> indexToPoint(2*pointIndices.size());
00164 forAll(pointIndices, i)
00165 {
00166 indexToPoint.insert(pointIndices[i], i);
00167 }
00168
00169
00170 forAll(edges, i)
00171 {
00172 edge& e = edges[i];
00173 e[0] = indexToPoint[e[0]];
00174 e[1] = indexToPoint[e[1]];
00175 }
00176 }
00177
00178 allPoints.transfer(points);
00179 allEdges.transfer(edges);
00180 }
00181
00182
00183
00184 void write
00185 (
00186 const Time& runTime,
00187 const fileName& inFileName,
00188 const fileName& outFileName,
00189 const edgeMesh& eMesh
00190 )
00191 {
00192 if (outFileName.ext() == "eMesh")
00193 {
00194 featureEdgeMesh fem
00195 (
00196 IOobject
00197 (
00198 outFileName,
00199 runTime.constant(),
00200 runTime,
00201 IOobject::NO_READ,
00202 IOobject::AUTO_WRITE,
00203 false
00204 ),
00205 eMesh.points(),
00206 eMesh.edges()
00207 );
00208
00209 Pout<< "Writing feature edge mesh to " << fem.objectPath()
00210 << endl;
00211
00212 fem.write();
00213 }
00214 else if (outFileName.ext() == "vtk")
00215 {
00216 OFstream str(outFileName);
00217
00218 str << "# vtk DataFile Version 2.0" << nl
00219 << "featureEdgeMesh " << inFileName << nl
00220 << "ASCII" << nl
00221 << "DATASET POLYDATA" << nl;
00222
00223 str << "POINTS " << eMesh.points().size() << " float" << nl;
00224 forAll(eMesh.points(), pointI)
00225 {
00226 const point& pt = eMesh.points()[pointI];
00227
00228 str << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
00229 }
00230
00231 str << "LINES " << eMesh.edges().size() << ' '
00232 << 3*eMesh.edges().size() << nl;
00233 forAll(eMesh.edges(), edgeI)
00234 {
00235 const edge& e = eMesh.edges()[edgeI];
00236
00237 str << "2 " << e[0] << ' ' << e[1] << nl;
00238 }
00239 }
00240 else
00241 {
00242 FatalErrorIn("write")
00243 << "Supported output formats: .eMesh, .vtk"
00244 << exit(FatalError);
00245 }
00246 }
00247
00248
00249
00250
00251 int main(int argc, char *argv[])
00252 {
00253 argList::noParallel();
00254 argList::validArgs.append("input file");
00255 argList::validArgs.append("output file");
00256 # include <OpenFOAM/setRootCase.H>
00257 # include <OpenFOAM/createTime.H>
00258
00259 const fileName inFileName(args.additionalArgs()[0]);
00260 const word outFileName(args.additionalArgs()[1]);
00261
00262 Pout<< "Input features file : " << inFileName << nl
00263 << "Output features file : " << outFileName << nl
00264 << endl;
00265
00266
00267
00268
00269
00270 if (inFileName.ext() == "nas")
00271 {
00272 pointField points;
00273 edgeList edges;
00274 readNASEdges(inFileName, points, edges);
00275
00276 edgeMesh eMesh(points, edges);
00277
00278 write(runTime, inFileName, outFileName, eMesh);
00279 }
00280 else if (inFileName.ext() == "eMesh")
00281 {
00282 featureEdgeMesh fem
00283 (
00284 IOobject
00285 (
00286 inFileName,
00287 runTime.constant(),
00288 runTime,
00289 IOobject::MUST_READ,
00290 IOobject::AUTO_WRITE,
00291 false
00292 )
00293 );
00294
00295 Pout<< "Read from " << inFileName
00296 << " edges:" << fem.edges().size()
00297 << " points:" << fem.points().size()
00298 << endl;
00299
00300 write(runTime, inFileName, outFileName, fem);
00301 }
00302 else
00303 {
00304 FatalErrorIn(args.executable())
00305 << "Can only handle NASTRAN data formats (.nas extension)."
00306 << exit(FatalError);
00307 }
00308
00309 Pout<< "End\n" << endl;
00310
00311 return 0;
00312 }
00313
00314
00315