示例#1
0
        //--------------------------------------------------------------------------------------------------

        public string ReadString()
        {
            Debug.Assert(DxfUtils.GetTypeForGroupCode(_GroupCode) == DxfUtils.Types.String);

            string value = null;

            if (_IsBinary)
            {
                var list     = new List <byte>();
                int readByte = _Stream.ReadByte();
                while (readByte > 0)
                {
                    list.Add((byte)readByte);
                    readByte = _Stream.ReadByte();
                }

                value = _Encoding.GetString(list.ToArray());
            }
            else
            {
                if (!_Reader.EndOfStream)
                {
                    value = _Reader.ReadLine()?.Trim();
                }
            }

            _ReadGroupCode();
            return(value);
        }
示例#2
0
        //--------------------------------------------------------------------------------------------------

        public double ReadDouble()
        {
            Debug.Assert(DxfUtils.GetTypeForGroupCode(_GroupCode) == DxfUtils.Types.Double);

            double value = 0.0;

            if (_IsBinary)
            {
                var buffer = new byte[DxfUtils.GetBinarySize(DxfUtils.Types.Double)];
                if (_Stream.Read(buffer, 0, buffer.Length) != buffer.Length)
                {
                    Messages.Error($"Binary DXF is invalid/not complete.");
                    _GroupCode = -1;
                }
                else
                {
                    value = BitConverter.ToDouble(buffer, 0);
                }
            }
            else
            {
                if (!_Reader.EndOfStream)
                {
                    var s = _Reader.ReadLine()?.Trim();
                    if (!s.IsNullOrEmpty())
                    {
                        double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out value);
                    }
                }
            }

            _ReadGroupCode();
            return(value);
        }
示例#3
0
        //--------------------------------------------------------------------------------------------------

        public void Skip()
        {
            if (_IsBinary)
            {
                var type = DxfUtils.GetTypeForGroupCode(_GroupCode);
                switch (type)
                {
                case DxfUtils.Types.String:
                {
                    int readByte;
                    do
                    {
                        readByte = _Stream.ReadByte();
                    } while (readByte > 0);

                    break;
                }

                case DxfUtils.Types.ByteChunk:
                {
                    int readByte = _Stream.ReadByte();
                    if (readByte == -1)
                    {
                        Messages.Error($"Binary DXF is invalid/not complete.");
                        _GroupCode = -1;
                        return;
                    }

                    _Stream.Seek(readByte, SeekOrigin.Current);
                    break;
                }

                default:
                {
                    int typeSize = DxfUtils.GetBinarySize(type);
                    if (typeSize == 0)
                    {
                        Messages.Error($"Binary DXF has group code {_GroupCode} of unknown type, the file cannot correctly parsed, in line {_Line}");
                        _GroupCode = -1;
                        return;
                    }

                    _Stream.Seek(typeSize, SeekOrigin.Current);
                    break;
                }
                }
            }
            else
            {
                if (_Reader.EndOfStream)
                {
                    return;
                }

                _Reader.ReadLine();
            }
            _ReadGroupCode();
        }
示例#4
0
        //--------------------------------------------------------------------------------------------------

        public void _WriteBinary(int groupcode, object value)
        {
            if (groupcode <= 255)
            {
                _Stream.WriteByte((byte)groupcode);
            }
            else
            {
                _Stream.WriteByte(255);
                _Stream.WriteByte((byte)(groupcode & 0xff));
                _Stream.WriteByte((byte)((groupcode >> 8) & 0xff));
            }

            var type = DxfUtils.GetTypeForGroupCode(groupcode);

            Debug.Assert(type != DxfUtils.Types.Unknown);

            switch (value)
            {
            case string stringValue:
                Debug.Assert(type == DxfUtils.Types.String);
                var stringBytes = _Encoding.GetBytes(stringValue);
                _Stream.Write(stringBytes, 0, stringBytes.Length);
                _Stream.WriteByte(0);
                break;

            case int intValue:
                Debug.Assert(type == DxfUtils.Types.Int16 || type == DxfUtils.Types.Int32);
                _Stream.WriteByte((byte)(intValue & 0xff));
                _Stream.WriteByte((byte)((intValue >> 8) & 0xff));
                if (type == DxfUtils.Types.Int32)
                {
                    _Stream.WriteByte((byte)((intValue >> 16) & 0xff));
                    _Stream.WriteByte((byte)((intValue >> 24) & 0xff));
                }
                break;

            case double doubleValue:
                Debug.Assert(type == DxfUtils.Types.Double);
                var doubleBytes = BitConverter.GetBytes(doubleValue);
                _Stream.Write(doubleBytes, 0, doubleBytes.Length);
                break;

            default:
                Messages.Error($"DxfWriter: Unknown value type {value.GetType().Name} for code {groupcode}.");
                break;
            }
        }