From af17a351ac96322816d2d5c5a7de718b2457107d Mon Sep 17 00:00:00 2001 From: sim <79989386+mahach666@users.noreply.github.com> Date: Fri, 4 Dec 2026 16:01:41 +0300 Subject: [PATCH] Read back-references to partition lights --- CHANGELOG.md | 2 +- docs/format-notes.md | 2 +- src/NwcIO/Internal/Parsing/HierarchyParser.cs | 6 ++-- tests/NwcIO.Tests/HierarchyParserTests.cs | 34 +++++++++++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3a1358..13116ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ All notable changes to the `NwcIO` library, the CLI and the `NwcIO.Scene` adapte ### Fixed - Read IFC string properties and class-96 point geometry in Navisworks model files. The scene adapter preserves point geometry (#291). -- Read the scene tree of NWD files that store lights in the partition trailer. +- Read the scene tree of NWD files that store lights or references to earlier lights in the partition trailer. - Open mixed IFC/DWG NWD files with named materials, shared assets, 64-bit properties and CAD text geometry. Preserve CAD annotation text, bounds and placement in the read model. ## [0.18.0] - 2026-11-23 diff --git a/docs/format-notes.md b/docs/format-notes.md index b877457..d35830a 100644 --- a/docs/format-notes.md +++ b/docs/format-notes.md @@ -63,7 +63,7 @@ Once inflated, streams serve dedicated roles identified by their signature prefi - `53` group: `[str name][ptr 52 username][i32 flags][i32 X][i32 Y][Y x attribute ptr][i32 c][c x child ptr]`. Children are class 22 or 53 and are never null. - `22` leaf: the same layout without `c` and children. - `X` (element index): 0 on every node that is not a Revit element; on an element node, `X - 1` indexes the GUID table (§2.3). The exporter writes some elements (for example grids) as two sibling nodes with the same `X`, not always adjacent; NwcIO makes the first the element and points the second to it. Such a pair takes two equal GUID table entries (`X - 1` and `X`), so the table size is the number of stored nodes with `X > 0` and the next element's index is `X + 2`. The same `X` under different parents, including a node and its descendant, is a linked placement: NwcIO keeps it with `NwcNode.IsLinkPlacement`, points `Element` at the first placement, and `NwcIO.Scene` writes it as its own `SceneModel`. - - `32` (`LcOaPartition`): `[str]` (empty in every file seen), a class-53 body whose name is the partition's source file name, then a trailer. Class 32 nodes are the top-level children of a `.nwd`; class 53 nodes are the top-level children of a `.nwc`. The cache's separate `LcOpNwdPartition` stream starts directly with a class-32 pointer and omits the children's count and slots. Its trailer starts with the light count and light objects, followed by source metadata. Its grammar is `[i32 2][i32 n] n x ptr27 3 x f64 [i32][str loader plugin][i32 format version][i32][i32] 6 x f64 [i32][str][i32][str source path] 3 x f64 4 x f64 [i32][i32]`. The last four doubles describe a plan box. + - `32` (`LcOaPartition`): `[str]` (empty in every file seen), a class-53 body whose name is the partition's source file name, then a trailer. Class 32 nodes are the top-level children of a `.nwd`; class 53 nodes are the top-level children of a `.nwc`. The cache's separate `LcOpNwdPartition` stream starts directly with a class-32 pointer and omits the children's count and slots. Its trailer starts with the light count and light objects, followed by source metadata. Its grammar is `[i32 2][i32 n] n x ptr27 3 x f64 [i32][str loader plugin][i32 format version][i32][i32] 6 x f64 [i32][str][i32][str source path] 3 x f64 4 x f64 [i32][i32]`. A light pointer can define a new class-27 object or refer to a previously read class-27 object. The last four doubles describe a plan box. - `27` partition light: `[i32 1][i32 selector (0 or 1), new ptr185 or shared asset index][13 x f64]`. The class-185 asset uses the same layout as material JSON. Lights are read and skipped. - `52` display/internal name pair: `[str name][str internal]`. It is the node username, the property tab username and every property key. - **Attributes** (slots may be null): `86` property tab `[str name][ptr 52][i32 flags][i32 n] n x ([ptr 52 key][i32 type][value])` with value by type `0` none, `1,6,7,10,11` `f64`, `5,15` `i64` (Unix seconds and signed integer, respectively), `2,3` `i32`, `4` and `9` string (type 9 was never seen; it is read as a string), `8` class-52 pointer, `12,13` three or two `f64` coordinates; `31` material; `40` translation; `41` translation and quaternion (`x -> R(q)x + t`); `39` 3x3 column-major matrix and translation; `79` hyperlinks; `200` wraps an inline or shared class-185 material asset (§2.4). diff --git a/src/NwcIO/Internal/Parsing/HierarchyParser.cs b/src/NwcIO/Internal/Parsing/HierarchyParser.cs index 2719d0d..94f178a 100644 --- a/src/NwcIO/Internal/Parsing/HierarchyParser.cs +++ b/src/NwcIO/Internal/Parsing/HierarchyParser.cs @@ -397,11 +397,11 @@ private void ReadPartitionTrailer() throw _reader.Fail("partition trailer does not start with 2"); } - // A new light takes at least 120 bytes: its pointer, marker, selector and thirteen doubles. - int lightCount = _reader.ReadCount(120); + // A shared light takes only the two words of its back-reference. + int lightCount = _reader.ReadCount(8); for (int i = 0; i < lightCount; i++) { - _reader.ReadPointer(PartitionLightClasses, _readBody, allowNull: false, allowBackReference: false); + _reader.ReadPointer(PartitionLightClasses, _readBody, allowNull: false); } SkipDoubles(3); diff --git a/tests/NwcIO.Tests/HierarchyParserTests.cs b/tests/NwcIO.Tests/HierarchyParserTests.cs index 30709e6..a4f9ddf 100644 --- a/tests/NwcIO.Tests/HierarchyParserTests.cs +++ b/tests/NwcIO.Tests/HierarchyParserTests.cs @@ -445,6 +445,40 @@ public void Read_PartitionLights_PreserveTheFollowingPartition() Assert.Equal(["Model", "A", "L", "B"], nodes.Select(node => node.Name)); } + [Theory] + [InlineData(1)] + [InlineData(8)] + public void Read_SharedPartitionLights_ResolvesEarlierLightObjects(int count) + { + var w = Begin(n: 2); + int lightId = 0; + Partition(w, "A", 0, _ => { }, lights: + [ + p => + { + lightId = p.Next; + PartitionLight(p); + }, + ]); + Action[] references = Enumerable.Repeat>(p => p.Ref(lightId), count).ToArray(); + Partition(w, "B", 1, p => Leaf(p, 0, _ => { }), lights: references); + + var nodes = HierarchyParser.Read(End(w), "Model"); + Assert.Equal(["Model", "A", "B", "L"], nodes.Select(node => node.Name)); + Assert.Same(nodes[2], nodes[3].Parent); + } + + [Fact] + public void Read_PartitionLightReferenceToOtherClass_Throws() + { + var w = Begin(n: 2); + int partitionId = w.Next; + Partition(w, "A", 0, _ => { }); + Partition(w, "B", 0, _ => { }, lights: [p => p.Ref(partitionId)]); + + AssertRule("back-reference to class 32, which this slot does not allow", End(w)); + } + [Theory] [InlineData(0, null, "class-27 light marker is not 1")] [InlineData(1, 86, "class 86, which this slot does not allow")]