public void ApplyCodePairs(DxfCodePairBufferReader buffer) { var pair = buffer.Peek(); buffer.Advance(); switch (pair.Code) { case 5: ((IDxfItemInternal)this).Handle = DxfCommonConverters.UIntHandle(pair.StringValue); break; case 8: // just a re-iteration of the layer break; case 67: // just a re-iteration of the paper space setting break; case 100: Debug.Assert(pair.StringValue == AcDbEntityText || pair.StringValue == AcDbBlockEndText); break; case DxfCodePairGroup.GroupCodeNumber: var groupName = DxfCodePairGroup.GetGroupName(pair.StringValue); ExtensionDataGroups.Add(DxfCodePairGroup.FromBuffer(buffer, groupName)); break; } }
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; }
internal static DxfTable FromBuffer(DxfCodePairBufferReader buffer) { var pair = buffer.Peek(); buffer.Advance(); if (pair.Code != 2) { throw new DxfReadException("Expected table type.", pair); } var tableType = pair.StringValue; // read common table values var commonValues = new List <DxfCodePair>(); var groups = new List <DxfCodePairGroup>(); pair = buffer.Peek(); while (pair != null && pair.Code != 0) { buffer.Advance(); if (pair.Code == DxfCodePairGroup.GroupCodeNumber) { var groupName = DxfCodePairGroup.GetGroupName(pair.StringValue); groups.Add(DxfCodePairGroup.FromBuffer(buffer, groupName)); } else { commonValues.Add(pair); } pair = buffer.Peek(); } DxfTable result; switch (tableType) { case DxfTable.AppIdText: result = DxfAppIdTable.ReadFromBuffer(buffer); break; case DxfTable.BlockRecordText: result = DxfBlockRecordTable.ReadFromBuffer(buffer); break; case DxfTable.DimStyleText: result = DxfDimStyleTable.ReadFromBuffer(buffer); break; case DxfTable.LayerText: result = DxfLayerTable.ReadFromBuffer(buffer); break; case DxfTable.LTypeText: result = DxfLTypeTable.ReadFromBuffer(buffer); break; case DxfTable.StyleText: result = DxfStyleTable.ReadFromBuffer(buffer); break; case DxfTable.UcsText: result = DxfUcsTable.ReadFromBuffer(buffer); break; case DxfTable.ViewText: result = DxfViewTable.ReadFromBuffer(buffer); break; case DxfTable.ViewPortText: result = DxfViewPortTable.ReadFromBuffer(buffer); break; default: SwallowTable(buffer); result = null; break; } if (result != null) { // set common values foreach (var common in commonValues) { switch (common.Code) { case 5: result.Handle = DxfCommonConverters.UIntHandle(common.StringValue); break; case 70: // entry count, read dynamically break; case 330: result.OwnerHandle = DxfCommonConverters.UIntHandle(common.StringValue); break; } } foreach (var group in groups) { result.ExtensionDataGroups.Add(group); } result.AfterRead(); } return(result); }