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 #include "triSurface.H"
00040 #include "IFstream.H"
00041 #include "IStringStream.H"
00042
00043
00044
00045 namespace Foam
00046 {
00047
00048
00049
00050
00051 static scalar parseNASCoord(const string& s)
00052 {
00053 size_t expSign = s.find_last_of("+-");
00054
00055 if (expSign != string::npos && expSign > 0 && !isspace(s[expSign-1]))
00056 {
00057 scalar mantissa = readScalar(IStringStream(s.substr(0, expSign))());
00058 scalar exponent = readScalar(IStringStream(s.substr(expSign+1))());
00059
00060 if (s[expSign] == '-')
00061 {
00062 exponent = -exponent;
00063 }
00064 return mantissa*pow(10, exponent);
00065 }
00066 else
00067 {
00068 return readScalar(IStringStream(s)());
00069 }
00070 }
00071
00072
00073 bool triSurface::readNAS(const fileName& fName)
00074 {
00075 IFstream is(fName);
00076
00077 if (!is.good())
00078 {
00079 FatalErrorIn("triSurface::readNAS(const fileName&)")
00080 << "Cannot read file " << fName
00081 << exit(FatalError);
00082 }
00083
00084
00085 DynamicList<point> points;
00086
00087 DynamicList<label> indices;
00088
00089 DynamicList<labelledTri> faces;
00090
00091 Map<label> groupToPatch;
00092 label nPatches = 0;
00093
00094 Map<word> groupToName;
00095
00096
00097
00098
00099 label ansaId = -1;
00100 word ansaType;
00101 string ansaName;
00102
00103
00104 HashSet<word> unhandledCmd;
00105
00106 while (is.good())
00107 {
00108 string line;
00109 is.getLine(line);
00110
00111
00112 if (line.substr(0, 10) == "$ANSA_NAME")
00113 {
00114 string::size_type sem0 = line.find (';', 0);
00115 string::size_type sem1 = line.find (';', sem0+1);
00116 string::size_type sem2 = line.find (';', sem1+1);
00117
00118 if
00119 (
00120 sem0 != string::npos
00121 && sem1 != string::npos
00122 && sem2 != string::npos
00123 )
00124 {
00125 ansaId = readLabel
00126 (
00127 IStringStream(line.substr(sem0+1, sem1-sem0-1))()
00128 );
00129 ansaType = line.substr(sem1+1, sem2-sem1-1);
00130
00131 string nameString;
00132 is.getLine(ansaName);
00133 if (ansaName[ansaName.size()-1] == '\r')
00134 {
00135 ansaName = ansaName.substr(1, ansaName.size()-2);
00136 }
00137 else
00138 {
00139 ansaName = ansaName.substr(1, ansaName.size()-1);
00140 }
00141
00142
00143
00144
00145 }
00146 }
00147
00148
00149
00150
00151 if
00152 (
00153 line.substr(0, 12) == "$HMNAME COMP"
00154 && line.find ('"') != string::npos
00155 )
00156 {
00157 label groupId = readLabel
00158 (
00159 IStringStream(line.substr(16, 16))()
00160 );
00161
00162 IStringStream lineStream(line.substr(32));
00163
00164 string rawName;
00165 lineStream >> rawName;
00166
00167 groupToName.insert(groupId, string::validate<word>(rawName));
00168 Info<< "group " << groupId << " => " << rawName << endl;
00169 }
00170
00171
00172 if (line.empty() || line[0] == '$')
00173 {
00174
00175 continue;
00176 }
00177
00178
00179 if (line.size() > 72 && line[72] == '+')
00180 {
00181 line = line.substr(0, 72);
00182
00183 while (true)
00184 {
00185 string buf;
00186 is.getLine(buf);
00187
00188 if (buf.size() > 72 && buf[72]=='+')
00189 {
00190 line += buf.substr(8, 64);
00191 }
00192 else
00193 {
00194 line += buf.substr(8, buf.size()-8);
00195 break;
00196 }
00197 }
00198 }
00199
00200
00201 IStringStream lineStream(line);
00202 word cmd;
00203 lineStream >> cmd;
00204
00205 if (cmd == "CTRIA3")
00206 {
00207 label groupId = readLabel(IStringStream(line.substr(16,8))());
00208 label a = readLabel(IStringStream(line.substr(24,8))());
00209 label b = readLabel(IStringStream(line.substr(32,8))());
00210 label c = readLabel(IStringStream(line.substr(40,8))());
00211
00212
00213
00214 Map<label>::const_iterator iter = groupToPatch.find(groupId);
00215
00216 label patchI;
00217 if (iter == groupToPatch.end())
00218 {
00219 patchI = nPatches++;
00220 groupToPatch.insert(groupId, patchI);
00221 Info<< "patch " << patchI << " => group " << groupId << endl;
00222 }
00223 else
00224 {
00225 patchI = iter();
00226 }
00227
00228 faces.append(labelledTri(a, b, c, patchI));
00229 }
00230 else if (cmd == "CQUAD4")
00231 {
00232 label groupId = readLabel(IStringStream(line.substr(16,8))());
00233 label a = readLabel(IStringStream(line.substr(24,8))());
00234 label b = readLabel(IStringStream(line.substr(32,8))());
00235 label c = readLabel(IStringStream(line.substr(40,8))());
00236 label d = readLabel(IStringStream(line.substr(48,8))());
00237
00238
00239 Map<label>::const_iterator iter = groupToPatch.find(groupId);
00240
00241 label patchI;
00242 if (iter == groupToPatch.end())
00243 {
00244 patchI = nPatches++;
00245 groupToPatch.insert(groupId, patchI);
00246 Info<< "patch " << patchI << " => group " << groupId << endl;
00247 }
00248 else
00249 {
00250 patchI = iter();
00251 }
00252
00253 faces.append(labelledTri(a, b, c, patchI));
00254 faces.append(labelledTri(c, d, a, patchI));
00255 }
00256 else if (cmd == "PSHELL")
00257 {
00258
00259 label groupId = readLabel(IStringStream(line.substr(8,8))());
00260 if (groupId == ansaId && ansaType == "PSHELL")
00261 {
00262 groupToName.insert(groupId, string::validate<word>(ansaName));
00263 Info<< "group " << groupId << " => " << ansaName << endl;
00264 }
00265 }
00266 else if (cmd == "GRID")
00267 {
00268 label index = readLabel(IStringStream(line.substr(8,8))());
00269 scalar x = parseNASCoord(line.substr(24, 8));
00270 scalar y = parseNASCoord(line.substr(32, 8));
00271 scalar z = parseNASCoord(line.substr(40, 8));
00272
00273 indices.append(index);
00274 points.append(point(x, y, z));
00275 }
00276 else if (cmd == "GRID*")
00277 {
00278
00279
00280
00281
00282
00283
00284 label index = readLabel(IStringStream(line.substr(8,16))());
00285 scalar x = parseNASCoord(line.substr(40, 16));
00286 scalar y = parseNASCoord(line.substr(56, 16));
00287
00288 is.getLine(line);
00289 if (line[0] != '*')
00290 {
00291 FatalErrorIn("triSurface::readNAS(const fileName&)")
00292 << "Expected continuation symbol '*' when reading GRID*"
00293 << " (double precision coordinate) output" << nl
00294 << "Read:" << line << nl
00295 << "File:" << is.name()
00296 << " line:" << is.lineNumber()
00297 << exit(FatalError);
00298 }
00299 scalar z = parseNASCoord(line.substr(8, 16));
00300
00301 indices.append(index);
00302 points.append(point(x, y, z));
00303 }
00304 else if (unhandledCmd.insert(cmd))
00305 {
00306 Info<< "Unhandled Nastran command " << line << nl
00307 << "File:" << is.name() << " line:" << is.lineNumber() << endl;
00308 }
00309 }
00310
00311 points.shrink();
00312 indices.shrink();
00313 faces.shrink();
00314
00315
00316 Info<< "Read triangles:" << faces.size() << " points:" << points.size()
00317 << endl;
00318
00319 {
00320
00321 Map<label> indexToPoint(2*indices.size());
00322 forAll(indices, i)
00323 {
00324 indexToPoint.insert(indices[i], i);
00325 }
00326
00327
00328 forAll(faces, i)
00329 {
00330 labelledTri& f = faces[i];
00331
00332 f[0] = indexToPoint[f[0]];
00333 f[1] = indexToPoint[f[1]];
00334 f[2] = indexToPoint[f[2]];
00335 }
00336 }
00337
00338
00339
00340 geometricSurfacePatchList patches(nPatches);
00341
00342 forAllConstIter(Map<word>, groupToName, iter)
00343 {
00344 label patchI = groupToPatch[iter.key()];
00345
00346 patches[patchI] = geometricSurfacePatch
00347 (
00348 "empty",
00349 iter(),
00350 patchI
00351 );
00352 }
00353
00354 Info<< "patches:" << patches << endl;
00355
00356
00357 pointField allPoints(points.xfer());
00358
00359
00360 *this = triSurface(faces, patches, allPoints, true);
00361
00362 return true;
00363 }
00364
00365
00366
00367
00368 }
00369
00370