Example #1
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);
        }
Example #2
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;
        }