Exemplo n.º 1
0
        internal static DxfBlocksSection BlocksSectionFromBuffer(DxfCodePairBufferReader buffer)
        {
            var section = new DxfBlocksSection();

            section.Clear();
            while (buffer.ItemsRemain)
            {
                var pair = buffer.Peek();
                if (DxfCodePair.IsSectionStart(pair))
                {
                    // done reading blocks, onto the next section
                    break;
                }
                else if (DxfCodePair.IsSectionEnd(pair))
                {
                    // done reading blocks
                    buffer.Advance(); // swallow (0, ENDSEC)
                    break;
                }

                if (pair.Code != 0)
                {
                    throw new DxfReadException("Expected new block.", pair);
                }

                buffer.Advance(); // swallow (0, CLASS)
                var block = DxfBlock.FromBuffer(buffer);
                if (block != null)
                {
                    section.Blocks.Add(block);
                }
            }

            return(section);
        }
Exemplo n.º 2
0
        internal static DxfEntitiesSection EntitiesSectionFromBuffer(DxfCodePairBufferReader buffer)
        {
            var entities = new List <DxfEntity>();

            entities.Clear();
            while (buffer.ItemsRemain)
            {
                var pair = buffer.Peek();
                if (DxfCodePair.IsSectionEnd(pair))
                {
                    // done reading entities
                    buffer.Advance(); // swallow (0, ENDSEC)
                    break;
                }

                if (pair.Code != 0)
                {
                    throw new DxfReadException("Expected new entity.", pair);
                }

                var entity = DxfEntity.FromBuffer(buffer);
                if (entity != null)
                {
                    entities.Add(entity);
                }
            }

            var section   = new DxfEntitiesSection();
            var collected = GatherEntities(entities);

            section.Entities.AddRange(collected);
            return(section);
        }
Exemplo n.º 3
0
        internal static DxfHeaderSection HeaderSectionFromBuffer(DxfCodePairBufferReader buffer)
        {
            var                section     = new DxfHeaderSection();
            string             keyName     = null;
            Func <short, bool> shortToBool = value => value != 0;

            while (buffer.ItemsRemain)
            {
                var pair = buffer.Peek();
                buffer.Advance();
                if (DxfCodePair.IsSectionEnd(pair))
                {
                    // done reading settings
                    break;
                }

                if (pair.Code == 9)
                {
                    // what setting to get
                    keyName = pair.StringValue;
                }
                else
                {
                    section.Header.SetHeaderVariable(keyName, pair);
                    if (string.Compare(keyName, "$ACADVER", StringComparison.OrdinalIgnoreCase) == 0 && section.Header.Version >= DxfAcadVersion.R2007)
                    {
                        // R2007 and up should switch to a UTF8 reader
                        buffer.SetUtf8Reader();
                    }
                }
            }

            return(section);
        }
Exemplo n.º 4
0
        internal static DxfHeaderSection HeaderSectionFromBuffer(DxfCodePairBufferReader buffer)
        {
            var                section     = new DxfHeaderSection();
            string             keyName     = null;
            Func <short, bool> shortToBool = value => value != 0;

            while (buffer.ItemsRemain)
            {
                var pair = buffer.Peek();
                buffer.Advance();
                if (DxfCodePair.IsSectionEnd(pair))
                {
                    // done reading settings
                    break;
                }

                if (pair.Code == 9)
                {
                    // what setting to get
                    keyName = pair.StringValue;
                }
                else
                {
                    section.Header.SetHeaderVariable(keyName, pair);
                }
            }

            return(section);
        }
Exemplo n.º 5
0
        internal static DxfClassesSection ClassesSectionFromBuffer(DxfCodePairBufferReader buffer, DxfAcadVersion version)
        {
            var section = new DxfClassesSection();

            section.Clear();
            while (buffer.ItemsRemain)
            {
                var pair = buffer.Peek();
                if (DxfCodePair.IsSectionEnd(pair))
                {
                    // done reading classes
                    buffer.Advance(); // swallow (0, ENDSEC)
                    break;
                }

                if (pair.Code != 0)
                {
                    throw new DxfReadException("Expected new class.", pair);
                }

                var cls = DxfClass.FromBuffer(buffer, version);
                if (cls != null)
                {
                    section.Classes.Add(cls);
                }
            }

            return(section);
        }
Exemplo n.º 6
0
        internal static DxfObjectsSection ObjectsSectionFromBuffer(DxfCodePairBufferReader buffer)
        {
            var objects = new List <DxfObject>();

            objects.Clear();
            while (buffer.ItemsRemain)
            {
                var pair = buffer.Peek();
                if (DxfCodePair.IsSectionEnd(pair))
                {
                    // done reading objects
                    buffer.Advance(); // swallow (0, ENDSEC)
                    break;
                }

                if (pair.Code != 0)
                {
                    throw new DxfReadException("Expected new object.", pair);
                }

                var obj = DxfObject.FromBuffer(buffer);
                if (obj != null)
                {
                    objects.Add(obj);
                }
            }

            var section = new DxfObjectsSection();

            section.Objects.AddRange(objects);
            return(section);
        }
Exemplo n.º 7
0
 internal static void SwallowSection(DxfCodePairBufferReader buffer)
 {
     while (buffer.ItemsRemain)
     {
         var pair = buffer.Peek();
         buffer.Advance();
         if (DxfCodePair.IsSectionEnd(pair))
         {
             break;
         }
     }
 }
Exemplo n.º 8
0
        internal static DxfHeaderSection HeaderSectionFromBuffer(DxfCodePairBufferReader buffer)
        {
            var                section     = new DxfHeaderSection();
            string             keyName     = null;
            Func <short, bool> shortToBool = value => value != 0;

            while (buffer.ItemsRemain)
            {
                var pair = buffer.Peek();
                buffer.Advance();
                if (DxfCodePair.IsSectionEnd(pair))
                {
                    // done reading settings
                    break;
                }

                if (pair.Code == 9)
                {
                    // what setting to get
                    keyName = pair.StringValue;
                }
                else
                {
                    section.Header.SetHeaderVariable(keyName, pair);
                    if (string.Compare(keyName, "$DWGCODEPAGE", StringComparison.OrdinalIgnoreCase) == 0 && section.Header.Version <= DxfAcadVersion.R2004)
                    {
                        // R2004 and before should honor the specified code page
                        if (DxfEncodingHelper.TryParseEncoding(pair.StringValue, out var codePage))
                        {
                            var encoding = Encoding.GetEncoding(codePage);
                            buffer.SetReaderEncoding(encoding);
                        }
                    }
                    else if (string.Compare(keyName, "$ACADVER", StringComparison.OrdinalIgnoreCase) == 0 && section.Header.Version >= DxfAcadVersion.R2007)
                    {
                        // R2007 and up should switch to a UTF8 reader
                        buffer.SetReaderEncoding(Encoding.UTF8);
                    }
                }
            }

            return(section);
        }
        internal static DxfThumbnailImageSection ThumbnailImageSectionFromBuffer(DxfCodePairBufferReader buffer)
        {
            if (buffer.ItemsRemain)
            {
                var lengthPair = buffer.Peek();
                buffer.Advance();

                if (lengthPair.Code != 90)
                {
                    return(null);
                }

                var length = lengthPair.IntegerValue;
                var lines  = new List <string>();
                while (buffer.ItemsRemain)
                {
                    var pair = buffer.Peek();
                    buffer.Advance();

                    if (DxfCodePair.IsSectionEnd(pair))
                    {
                        break;
                    }

                    Debug.Assert(pair.Code == 310);
                    lines.Add(pair.StringValue);
                }

                var section = new DxfThumbnailImageSection();
                section.Clear();
                section.RawData = DxfCommonConverters.HexBytes(string.Join(string.Empty, lines.ToArray()));
                return(section);
            }

            return(null);
        }
Exemplo n.º 10
0
        internal static DxfThumbnailImageSection ThumbnailImageSectionFromBuffer(DxfCodePairBufferReader buffer)
        {
            if (buffer.ItemsRemain)
            {
                var lengthPair = buffer.Peek();
                buffer.Advance();

                if (lengthPair.Code != 90)
                {
                    return(null);
                }

                var length  = lengthPair.IntegerValue;
                var rawData = new List <byte>();
                while (buffer.ItemsRemain)
                {
                    var pair = buffer.Peek();
                    buffer.Advance();

                    if (DxfCodePair.IsSectionEnd(pair))
                    {
                        break;
                    }

                    Debug.Assert(pair.Code == 310);
                    rawData.AddRange(pair.BinaryValue);
                }

                var section = new DxfThumbnailImageSection();
                section.Clear();
                section.RawData = rawData.ToArray();
                return(section);
            }

            return(null);
        }
Exemplo n.º 11
0
        internal static DxfBlock FromBuffer(DxfCodePairBufferReader buffer)
        {
            if (!buffer.ItemsRemain)
            {
                return null;
            }

            var block = new DxfBlock();
            var readingBlockStart = true;
            var readingBlockEnd = false;
            var entities = new List<DxfEntity>();
            while (buffer.ItemsRemain)
            {
                var pair = buffer.Peek();
                if (DxfCodePair.IsSectionEnd(pair))
                {
                    // done reading blocks
                    buffer.Advance(); // swallow (0, ENDSEC)
                    break;
                }
                else if (IsBlockStart(pair))
                {
                    if (readingBlockStart)
                    {
                        // if another block is found, stop reading this one because some blocks don't specify (0, ENDBLK)
                        break;
                    }

                    break;
                }
                else if (IsBlockEnd(pair))
                {
                    if (!readingBlockStart) throw new DxfReadException("Unexpected block end", pair);
                    readingBlockStart = false;
                    readingBlockEnd = true;
                    buffer.Advance(); // swallow (0, ENDBLK)
                }
                else if (pair.Code == 0)
                {
                    // should be an entity
                    var entity = DxfEntity.FromBuffer(buffer);
                    if (entity != null)
                    {
                        // entity could be null if it's unsupported
                        entities.Add(entity);
                    }
                }
                else
                {
                    // read value pair
                    if (readingBlockStart)
                    {
                        buffer.Advance();
                        switch (pair.Code)
                        {
                            case 1:
                                block.XrefName = pair.StringValue;
                                break;
                            case 2:
                                block.Name = pair.StringValue;
                                break;
                            case 3:
                                break;
                            case 4:
                                block.Description = pair.StringValue;
                                break;
                            case 5:
                                ((IDxfItemInternal)block).Handle = DxfCommonConverters.UIntHandle(pair.StringValue);
                                break;
                            case 8:
                                block.Layer = pair.StringValue;
                                break;
                            case 10:
                                block.BasePoint = block.BasePoint.WithUpdatedX(pair.DoubleValue);
                                break;
                            case 20:
                                block.BasePoint = block.BasePoint.WithUpdatedY(pair.DoubleValue);
                                break;
                            case 30:
                                block.BasePoint = block.BasePoint.WithUpdatedZ(pair.DoubleValue);
                                break;
                            case 67:
                                block.IsInPaperSpace = DxfCommonConverters.BoolShort(pair.ShortValue);
                                break;
                            case 70:
                                block.Flags = pair.ShortValue;
                                break;
                            case 330:
                                ((IDxfItemInternal)block).OwnerHandle = DxfCommonConverters.UIntHandle(pair.StringValue);
                                break;
                            case DxfCodePairGroup.GroupCodeNumber:
                                var groupName = DxfCodePairGroup.GetGroupName(pair.StringValue);
                                block.ExtensionDataGroups.Add(DxfCodePairGroup.FromBuffer(buffer, groupName));
                                break;
                            case (int)DxfXDataType.ApplicationName:
                                block.XData = DxfXData.FromBuffer(buffer, pair.StringValue);
                                break;
                        }
                    }
                    else if (readingBlockEnd)
                    {
                        block.EndBlock.ApplyCodePairs(buffer);
                    }
                    else
                    {
                        throw new DxfReadException("Unexpected pair in block", pair);
                    }
                }
            }

            var collected = DxfEntitiesSection.GatherEntities(entities);
            foreach (var entity in collected)
            {
                block.Entities.Add(entity);
            }

            return block;
        }
Exemplo n.º 12
0
        internal static DxfTablesSection TablesSectionFromBuffer(DxfCodePairBufferReader buffer)
        {
            var section = new DxfTablesSection();

            section.Clear();
            while (buffer.ItemsRemain)
            {
                var pair = buffer.Peek();
                buffer.Advance();
                if (DxfCodePair.IsSectionEnd(pair))
                {
                    break;
                }

                if (!IsTableStart(pair))
                {
                    throw new DxfReadException("Expected start of table.", pair);
                }

                var table = DxfTable.FromBuffer(buffer);
                if (table != null)
                {
                    switch (table.TableType)
                    {
                    case DxfTableType.AppId:
                        section.AppIdTable = (DxfAppIdTable)table;
                        break;

                    case DxfTableType.BlockRecord:
                        section.BlockRecordTable = (DxfBlockRecordTable)table;
                        break;

                    case DxfTableType.DimStyle:
                        section.DimStyleTable = (DxfDimStyleTable)table;
                        break;

                    case DxfTableType.Layer:
                        section.LayerTable = (DxfLayerTable)table;
                        break;

                    case DxfTableType.LType:
                        section.LTypeTable = (DxfLTypeTable)table;
                        break;

                    case DxfTableType.Style:
                        section.StyleTable = (DxfStyleTable)table;
                        break;

                    case DxfTableType.Ucs:
                        section.UcsTable = (DxfUcsTable)table;
                        break;

                    case DxfTableType.View:
                        section.ViewTable = (DxfViewTable)table;
                        break;

                    case DxfTableType.ViewPort:
                        section.ViewPortTable = (DxfViewPortTable)table;
                        break;

                    default:
                        throw new DxfReadException($"Unexpected table type {table.TableType}", pair);
                    }
                }
            }

            return(section);
        }