/// <summary> /// Read a single DXF record from an ASCII DXF file consisting of the record type and value /// </summary> /// <param name="rec"></param> /// <param name="lineNumber"></param> /// <param name="readingTextEntity"></param> /// <returns></returns> public bool ReadASCIIDXFRecord(out DXFRecord rec, ref int lineNumber, bool readingTextEntity = false) { rec = new DXFRecord(); var recTypeString = _aSCIIdxfFile.ReadLine(); rec.recType = Convert.ToUInt16(recTypeString); if (_aSCIIdxfFile.EndOfStream) { return(false); // may be ASCII but not DXF format } lineNumber++; if (rec.recType <= ASCIIDXFRecordsMax) { var line = _aSCIIdxfFile.ReadLine(); switch (ASCIIDXFRecordTypeLookUp[rec.recType]) { case ASCIIDXFRecordType_Integer: rec.i = Convert.ToInt32(line); break; case ASCIIDXFRecordType_Real: rec.r = Convert.ToDouble(line); break; case ASCIIDXFRecordType_String: { rec.s = line; // We do not want to remove leading or trailing spaces from // the text in DXF text entities (TEXT and M TEXT). These // group codes 1 (for TEXT) and 1&3 (for M TEXT). If the caller // has advised it is reading a DXF text entity we no do not // strip the spaces from the string value. if (readingTextEntity) { if (rec.recType != 1 && rec.recType != 3) { rec.s = rec.s.Trim(); } } else { rec.s = rec.s.Trim(); } break; } } lineNumber++; } return(true); }
private bool GetStartOfNextEntity(out DXFRecord rec) { do { if (!ReadDXFRecord(out rec)) { return(false); } } while (rec.recType != 0); // ReSharper disable once StringLiteralTypo return(rec.s == "ENDSEC"); }
/// <summary> /// Reads the next record from the DXF file /// </summary> /// <param name="rec"></param> /// <returns></returns> public bool ReadDXFRecord(out DXFRecord rec) { if (_reuseRecord) { rec = _lastRecord; _reuseRecord = false; return(true); } else { var result = DXFFileIsBinary ? ReadBinaryDXFRecord(out rec, ref _dxfLine) : ReadASCIIDXFRecord(out rec, ref _dxfLine); _lastRecord = rec; return(result); } }
/// <summary> /// Locates a named section in a DXF file to begin reading records from /// </summary> /// <param name="name0"></param> /// <param name="name2"></param> /// <returns></returns> public bool FindSection(string name0, string name2) { var TestString = ""; var rec = new DXFRecord(); var _haveFoundSection = false; while (!_haveFoundSection) { // Scan for a section start var foundSectionStart = false; while (!foundSectionStart) { if (!ReadDXFRecord(out rec)) { return(false); } if (rec.recType != 0) { continue; } TestString = rec.s.ToUpper(CultureInfo.InvariantCulture); // ReSharper disable once StringLiteralTypo foundSectionStart = string.Compare(TestString, name0, StringComparison.InvariantCulture) == 0 || TestString == "ENDSEC" || TestString == "SECTION"; } // ReSharper disable once StringLiteralTypo if (TestString != "ENDSEC") // empty section { if (!ReadDXFRecord(out rec)) { return(false); } TestString = rec.s.ToUpper(CultureInfo.InvariantCulture); } _haveFoundSection = rec.recType == 2 && string.Compare(TestString, name2, StringComparison.InvariantCulture) == 0; } return(true); }
/// <summary> /// Read a DXF record from a binary DXF file /// </summary> /// <param name="rec"></param> /// <param name="lineNumber"></param> /// <param name="readingTextEntity"></param> /// <returns></returns> public bool ReadBinaryDXFRecord(out DXFRecord rec, ref int lineNumber, bool readingTextEntity = false) { rec = new DXFRecord(); var buffer = new byte[100]; // Read the record number if (_dXFFileIsPostR13c3) { _binarydxfFile.Read(buffer, 0, 2); rec.recType = BitConverter.ToUInt16(buffer); } else { var binaryRecType1 = _binarydxfFile.ReadByte(); if (binaryRecType1 != 255) { rec.recType = (ushort)binaryRecType1; } else // escaped record number { _binarydxfFile.Read(buffer, 0, 2); rec.recType = BitConverter.ToUInt16(buffer); } } if (rec.recType >= BinaryDXFRecordTypeLookUp.Length) { return(false); } switch (BinaryDXFRecordTypeLookUp[rec.recType]) { case BinaryDXFRecordType_String: var builder = new StringBuilder(); // read chars until we find a #0 byte theByte; do { theByte = (byte)_binarydxfFile.ReadByte(); if (theByte != 0) { builder.Append((char)theByte); } } while (theByte != 0); rec.s = builder.ToString(); // We do not want to remove leading or trailing spaces from // the text in DXF text entities (TEXT and MTEXT). These // group codes 1 (for TEXT) and 1&3 (for MTEXT). If the caller // has advised it is reading a DXF text entity we no do not // strip the spaces from the string value. if (readingTextEntity) { if (!(rec.recType == 1 || rec.recType == 3)) { rec.s = rec.s.Trim(); } } else { rec.s = rec.s.Trim(); } break; case BinaryDXFRecordType_Real: _binarydxfFile.Read(buffer, 0, 8); rec.r = BitConverter.ToDouble(buffer); break; case BinaryDXFRecordType_Integer1: _binarydxfFile.Read(buffer, 0, 1); rec.i = buffer[0]; break; case BinaryDXFRecordType_Integer2: _binarydxfFile.Read(buffer, 0, 2); rec.i = BitConverter.ToInt16(buffer); break; case BinaryDXFRecordType_Integer4: _binarydxfFile.Read(buffer, 0, 4); rec.i = BitConverter.ToInt32(buffer); break; case BinaryDXFRecordType_Integer8: _binarydxfFile.Read(buffer, 0, 4); rec.i = BitConverter.ToUInt32(buffer); break; case BinaryDXFRecordType_ExtendedData: rec.s = ""; var recLength = _binarydxfFile.ReadByte(); _binarydxfFile.Position += recLength; break; default: return(false); } lineNumber++; return(true); }