From 17eabf69e8ef0b48aa6560122a901faba89b4557 Mon Sep 17 00:00:00 2001 From: mahach666 Date: Tue, 1 Dec 2026 17:12:56 +0300 Subject: [PATCH] feat: decode LAYER table and resolve BYLAYER entity colors --- CHANGELOG.md | 1 + docs/public-api.md | 7 +- src/DwgIO.Cli/Export/DwgCadHtmlViewer.cs | 121 ++++++- src/DwgIO.Cli/Export/DwgSvgExporter.cs | 16 +- src/DwgIO.Cli/Program.cs | 11 + src/DwgIO.Scene/DwgSceneReader.cs | 10 +- src/DwgIO/DwgDocument.cs | 64 +++- src/DwgIO/DwgReaderOptions.cs | 4 + src/DwgIO/Internal/Parsing/EntityDecoder.cs | 114 ++++++- src/DwgIO/Internal/Parsing/LayerParser.cs | 156 +++++++++ src/DwgIO/Json/DwgJsonExporter.cs | 40 +++ src/DwgIO/Model/DwgEntity.cs | 18 ++ src/DwgIO/Model/DwgLayerInfo.cs | 43 +++ src/DwgIO/Model/DwgObjectInfo.cs | 15 + tests/DwgIO.Tests/LayersTests.cs | 331 ++++++++++++++++++++ 15 files changed, 914 insertions(+), 37 deletions(-) create mode 100644 src/DwgIO/Internal/Parsing/LayerParser.cs create mode 100644 src/DwgIO/Model/DwgLayerInfo.cs create mode 100644 tests/DwgIO.Tests/LayersTests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index e31742a..65bc6f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ API between minor releases. ## [Unreleased] ### Added +- `LAYER` (`0x33`) table decoding (`DwgLayerInfo`, `DwgDocument.Layers`, `DwgReaderOptions.ReadLayers`) and entity `LayerHandle` / `LayerName` / `EffectiveColorIndex` / `EffectiveTrueColorRgb` `BYLAYER` colour resolution across `DwgIO`, `DwgIO.Scene`, JSON export, SVG, `dwgio inspect` and the 2D HTML viewer (with interactive layer visibility chips). - `AcDb:Header` parsing (`DwgHeaderVariables`, `DwgInsUnits`, `DwgPoint2D`, `DwgPoint3D`, `DwgDocument.HeaderVariables`, `DwgReaderOptions.ReadHeader`) exposing `$INSUNITS`, `$MEASUREMENT`, extents/limits, units formatting, point style (`$PDMODE`/`$PDSIZE`), linetype/text/dim scales, timestamps, `$HANDSEED`, and GUIDs in JSON export, `dwgio inspect`, `DwgSceneOptions.UseDrawingUnits` and the 2D HTML viewer. - `AcDb:Classes` parsing (`DwgClassInfo`, `DwgDocument.Classes`, `DwgReaderOptions.ReadClasses`) resolving class names (codes 500+) and `IsEntity` flags in `DwgObjectInfo`, JSON export and `dwgio inspect`. - Entity geometry decoding for `LWPOLYLINE` (including bulges and elevation), `ELLIPSE`, `SOLID`/`TRACE`, `3DFACE`, `RAY`, `XLINE` and 24-bit `TrueColorRgb` across `DwgIO`, `DwgIO.Scene`, SVG and the 2D AutoCAD HTML viewer. diff --git a/docs/public-api.md b/docs/public-api.md index 66c224f..115a53c 100644 --- a/docs/public-api.md +++ b/docs/public-api.md @@ -12,8 +12,8 @@ change in any release and is visible only to the test project through `Internals | Type | Use | |---|---| -| `DwgDocument` | Opens a drawing (`Open` / `OpenAsync` from path or stream) and gives `SourcePath` (null for streams), `Path`, `Version`, file and container headers, header variables (`HeaderVariables`), section catalog, metadata, handle map, object inventory, entities and `Diagnostics`. `ReadSection(name)` returns decompressed section bytes; `ReleaseSectionBuffers()` drops retained raw buffers; `SaveJson` / `ToJson` write JSON. | -| `DwgReaderOptions` | What `Open` reads and enforces: `ReadSectionMap`, `ReadSummaryInfo`, `ReadMetadata`, `ReadHeader`, `ReadClasses`, `ReadObjects`, `ReadEntities`, `VerifyChecksums`, `Strict` (throws on diagnostics), `ReleaseSectionBuffers`, `MaxFileBytes`, `MaxSectionBytes`, `MaxDecompressedBytes`, `MaxObjectCount`, `Progress` and `DiagnosticOutput`. | +| `DwgDocument` | Opens a drawing (`Open` / `OpenAsync` from path or stream) and gives `SourcePath` (null for streams), `Path`, `Version`, file and container headers, header variables (`HeaderVariables`), `Layers`, section catalog, metadata, handle map, object inventory, entities and `Diagnostics`. `ReadSection(name)` returns decompressed section bytes; `ReleaseSectionBuffers()` drops retained raw buffers; `SaveJson` / `ToJson` write JSON. | +| `DwgReaderOptions` | What `Open` reads and enforces: `ReadSectionMap`, `ReadSummaryInfo`, `ReadMetadata`, `ReadHeader`, `ReadClasses`, `ReadObjects`, `ReadLayers`, `ReadEntities`, `VerifyChecksums`, `Strict` (throws on diagnostics), `ReleaseSectionBuffers`, `MaxFileBytes`, `MaxSectionBytes`, `MaxDecompressedBytes`, `MaxObjectCount`, `Progress` and `DiagnosticOutput`. | | `DwgReadProgress` | Synchronous phase updates (`Phase`, `Completed`, `Total`) delivered via `DwgReaderOptions.Progress`. | | `DwgExitCodes` | Standard CLI exit codes (`Ok = 0`, `Usage = 1`, `FileNotFound = 2`, `UnsupportedVersion = 3`, `ParseError = 4`, `NoPreview = 5`). | | `DwgException` | Thrown for a damaged or truncated file, exceeded reader limits, `Strict` diagnostic failures, and by `ReadSection` when buffers were released or a section is missing. | @@ -37,7 +37,8 @@ change in any release and is visible only to the test project through `Internals | `DwgHandleMap`, `DwgHandleMapEntry` | The handle to offset index of the object stream; `Find(handle)` looks up one object. | | `DwgObjectInfo`, `DwgHandle` | One record of the object stream: offset, size, type code (`TypeName` resolves fixed names and `AcDb:Classes` names) and handle. | | `DwgClassInfo` | One class definition from `AcDb:Classes` (codes 500+): `ClassNumber`, `AppName`, `CppClassName`, `DxfName`, `ItemClassId`, `IsEntity`, `InstanceCount`. | -| `DwgEntity`, `DwgEntityKind` | A decoded entity with its handle, type, colour (`ColorIndex` and optional `TrueColorRgb`), linetype scale, `IsModelSpace` flag and decoded `Geometry` for LINE, CIRCLE, ARC, POINT, LWPOLYLINE, ELLIPSE, SOLID/TRACE, 3DFACE, RAY and XLINE. | +| `DwgLayerInfo` | One layer record (`0x33`) from `AcDb:AcDbObjects`: `Handle`, `Name`, `ColorIndex`, `TrueColorRgb`, `IsFrozen`, `IsOff`/`IsOn`, `IsLocked`, `IsPlottable`, `LineWeight` and `IsVisible`. | +| `DwgEntity`, `DwgEntityKind` | A decoded entity with its handle, type, colour (`ColorIndex`, `TrueColorRgb`, and BYLAYER-resolved `EffectiveColorIndex`/`EffectiveTrueColorRgb`), layer (`LayerHandle`, `LayerName`), linetype scale, `IsModelSpace` flag and decoded `Geometry` for LINE, CIRCLE, ARC, POINT, LWPOLYLINE, ELLIPSE, SOLID/TRACE, 3DFACE, RAY and XLINE. | | `DwgDocument.AppInfoHistory` | Decompressed bytes of `AcDb:AppInfoHistory` when available. The record layout is unknown. | | `DwgRevHistory` | The save references of `AcDb:RevHistory`: layout version pair and one value per recorded save. | | `DwgAuxHeader` | The auxiliary header of `AcDb:AuxHeader`: version pair, save counter, creation and last-update times, next handle. | diff --git a/src/DwgIO.Cli/Export/DwgCadHtmlViewer.cs b/src/DwgIO.Cli/Export/DwgCadHtmlViewer.cs index bb28b6c..8e51606 100644 --- a/src/DwgIO.Cli/Export/DwgCadHtmlViewer.cs +++ b/src/DwgIO.Cli/Export/DwgCadHtmlViewer.cs @@ -75,6 +75,25 @@ private static string BuildPayloadJson(DwgDocument document, DwgViewerOptions op }); w.WriteNumber("totalEntities", document.Entities.Count); + w.WriteStartArray("layers"); + foreach (var layer in document.Layers) + { + w.WriteStartObject(); + w.WriteString("handle", layer.Handle.ToString("X", CultureInfo.InvariantCulture)); + w.WriteString("name", layer.Name); + w.WriteNumber("colorIndex", layer.ColorIndex); + bool layerAdaptive = layer.TrueColorRgb is null && IsAdaptiveColor(layer.ColorIndex); + w.WriteBoolean("isAdaptiveColor", layerAdaptive); + w.WriteString("rgb", FormatFixedRgb(layer.ColorIndex, layer.TrueColorRgb)); + w.WriteBoolean("isOn", layer.IsOn); + w.WriteBoolean("isFrozen", layer.IsFrozen); + w.WriteBoolean("isLocked", layer.IsLocked); + w.WriteBoolean("visible", layer.IsVisible); + w.WriteEndObject(); + } + + w.WriteEndArray(); + w.WriteStartArray("entities"); int drawnCount = 0; int skippedNonFinite = 0; @@ -102,15 +121,28 @@ private static string BuildPayloadJson(DwgDocument document, DwgViewerOptions op } drawnCount++; + int effColorIndex = entity.EffectiveColorIndex; + uint? effTrueColor = entity.EffectiveTrueColorRgb; w.WriteStartObject(); w.WriteString("handle", entity.Handle.ToString("X", CultureInfo.InvariantCulture)); w.WriteString("type", entity.TypeName); w.WriteString("kind", entity.Kind.ToString().ToLowerInvariant()); - w.WriteNumber("colorIndex", entity.ColorIndex); - bool adaptive = entity.TrueColorRgb is null && IsAdaptiveColor(entity.ColorIndex); + w.WriteString("layer", entity.LayerName ?? "0"); + if (entity.LayerHandle is { } layerHandle) + { + w.WriteString("layerHandle", layerHandle.ToString("X", CultureInfo.InvariantCulture)); + } + + w.WriteNumber("colorIndex", effColorIndex); + if (effColorIndex != entity.ColorIndex) + { + w.WriteNumber("rawColorIndex", entity.ColorIndex); + } + + bool adaptive = effTrueColor is null && IsAdaptiveColor(effColorIndex); w.WriteBoolean("isAdaptiveColor", adaptive); - w.WriteString("rgb", FormatFixedRgb(entity.ColorIndex, entity.TrueColorRgb)); - if (entity.TrueColorRgb is { } trueColor) + w.WriteString("rgb", FormatFixedRgb(effColorIndex, effTrueColor)); + if (effTrueColor is { } trueColor) { w.WriteString("trueColorRgb", string.Create(CultureInfo.InvariantCulture, $"#{trueColor:X6}")); } @@ -419,6 +451,29 @@ private static void Expand(double x, double y, ref double minX, ref double minY, color: var(--text); border-color: var(--accent); } + .layer-list { + display: flex; + flex-wrap: wrap; + gap: 4px; + max-height: 76px; + overflow-y: auto; + } + .layer-chip { + display: inline-flex; + align-items: center; + gap: 4px; + background: var(--bg-item); + color: var(--text); + border: 1px solid var(--border); + border-radius: 3px; + padding: 2px 6px; + font-size: 10px; + cursor: pointer; + } + .layer-chip.hidden-layer { + opacity: 0.45; + text-decoration: line-through; + } .entity-list { flex: 1; overflow-y: auto; @@ -551,6 +606,7 @@ private static void Expand(double x, double y, ref double minX, ref double minY,