From 4ade0a320a5b1bc2b2c557bcb015a8d06e0c0ab8 Mon Sep 17 00:00:00 2001 From: sim <79989386+mahach666@users.noreply.github.com> Date: Fri, 4 Dec 2026 12:15:52 +0300 Subject: [PATCH] Read light objects in Navisworks partition trailers --- CHANGELOG.md | 1 + docs/format-notes.md | 3 +- src/NwcIO/Internal/Parsing/HierarchyParser.cs | 34 +++++++++++- src/NwcIO/Internal/Parsing/LcObjectReader.cs | 3 + tests/NwcIO.Tests/HierarchyParserTests.cs | 55 +++++++++++++++++-- 5 files changed, 88 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a67f7a6..178f765 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +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. ## [0.18.0] - 2026-11-23 diff --git a/docs/format-notes.md b/docs/format-notes.md index 72fe3b5..6954344 100644 --- a/docs/format-notes.md +++ b/docs/format-notes.md @@ -63,7 +63,8 @@ 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. The trailer holds no objects. Its grammar is `[i32 2][i32] 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]`. The last four doubles describe a plan box. + - `27` partition light: `[i32 1][i32 selector (0 or 1) or new ptr185][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,5,6,7,10,11` `f64`, `2,3` `i32`, `4` and `9` string (type 9 was never seen; it is read as a string), `8` class-52 pointer; `31` material; `40` translation; `41` translation and quaternion (`x -> R(q)x + t`); `39` 3x3 column-major matrix and translation; `79` hyperlinks; `200` and `185` material JSON. - **Shared subtrees**: a child pointer may back-reference an earlier node, so the hierarchy is a directed acyclic graph. A node reached through several parents appears once per path when the tree is expanded. diff --git a/src/NwcIO/Internal/Parsing/HierarchyParser.cs b/src/NwcIO/Internal/Parsing/HierarchyParser.cs index 894a12e..9f586c2 100644 --- a/src/NwcIO/Internal/Parsing/HierarchyParser.cs +++ b/src/NwcIO/Internal/Parsing/HierarchyParser.cs @@ -32,6 +32,7 @@ internal static class HierarchyParser private static ReadOnlySpan AttributeClasses => [86, 31, 40, 41, 39, 79, 200]; private static ReadOnlySpan ExtraClasses => [185]; + private static ReadOnlySpan PartitionLightClasses => [27]; private static ReadOnlySpan PartitionRootClasses => [86, 79, 66, 70]; @@ -300,6 +301,7 @@ public Parser(LcObjectReader reader, int guidCount, Func?> 52 => ReadUsernameBody(), 86 => ReadPropertyTab(), 31 => ReadMaterial(), + 27 => ReadPartitionLight(), 40 => ReadTransform(doubles: 3), 41 => ReadTransform(doubles: 7), 39 => ReadMatrix(), @@ -384,8 +386,7 @@ private Node ReadNode(int classId) return new Node(classId, name ?? username.Display ?? "", username.Internal ?? "", x, hidden, materialName, transform, tabs, hyperlinks, ownProperties, children, size, propertyReferences, height); } - // The NODE32 trailer (docs/format-notes.md §2.2), read and dropped: [i32 2][i32] 3 x f64 [i32][str loader plugin][i32 format version] - // [i32][i32] 6 x f64 [i32][str][i32][str source path] 3 x f64, then 4 x f64 (the source's plan box), [i32][i32]. It holds no objects. + // The NODE32 trailer starts with its light objects, then holds the source file metadata (docs/format-notes.md §2.2). private void ReadPartitionTrailer() { if (_reader.ReadInt32() != 2) @@ -393,7 +394,13 @@ private void ReadPartitionTrailer() throw _reader.Fail("partition trailer does not start with 2"); } - _reader.ReadInt32(); + // A new light takes at least 120 bytes: its pointer, marker, selector and thirteen doubles. + int lightCount = _reader.ReadCount(120); + for (int i = 0; i < lightCount; i++) + { + _reader.ReadPointer(PartitionLightClasses, _readBody, allowNull: false, allowBackReference: false); + } + SkipDoubles(3); _reader.ReadInt32(); _reader.ReadString(); @@ -410,6 +417,27 @@ private void ReadPartitionTrailer() _reader.ReadInt32(); } + private object ReadPartitionLight() + { + if (_reader.ReadInt32() != 1) + { + throw _reader.Fail("class-27 light marker is not 1"); + } + + // A built-in light has a selector. A light with an asset has a class-185 object in its place. + if (_reader.PeekInt32() is 0 or 1) + { + _reader.ReadInt32(); + } + else + { + _reader.ReadPointer(ExtraClasses, _readBody, allowNull: false, allowBackReference: false); + } + + SkipDoubles(13); + return Dropped; + } + private void SkipDoubles(int count) { for (int i = 0; i < count; i++) diff --git a/src/NwcIO/Internal/Parsing/LcObjectReader.cs b/src/NwcIO/Internal/Parsing/LcObjectReader.cs index c08605b..aa3bac5 100644 --- a/src/NwcIO/Internal/Parsing/LcObjectReader.cs +++ b/src/NwcIO/Internal/Parsing/LcObjectReader.cs @@ -33,6 +33,9 @@ public LcObjectReader(byte[] data, string segment) /// The id the next new object takes; inside a body, the body's own id is Next - 1 until it reads a pointer. public int Next => FirstId + _objects.Count; + public int PeekInt32() => BinaryPrimitives.TryReadInt32LittleEndian(_data.AsSpan(Position), out int value) + ? value : throw Fail("i32 passes the end of the data"); + public int ReadInt32() => BinaryPrimitives.ReadInt32LittleEndian(Take(4, "i32")); public bool TryReadSharedIndex(out int index) diff --git a/tests/NwcIO.Tests/HierarchyParserTests.cs b/tests/NwcIO.Tests/HierarchyParserTests.cs index a71ed71..63c0656 100644 --- a/tests/NwcIO.Tests/HierarchyParserTests.cs +++ b/tests/NwcIO.Tests/HierarchyParserTests.cs @@ -346,6 +346,31 @@ public void Read_TwoPartitionsWithLeaves_ReadsBothSubtrees() Assert.Same(nodes[3], nodes[4].Parent); } + [Fact] + public void Read_PartitionLights_PreserveTheFollowingPartition() + { + var w = Begin(n: 2); + Partition(w, "A", 1, p => Leaf(p, 0, _ => { }), lights: + [ + p => PartitionLight(p, selector: 0), + p => PartitionLight(p, selector: 1), + p => PartitionLight(p, assetClass: 185), + ]); + Partition(w, "B", 0, _ => { }); + + var nodes = HierarchyParser.Read(End(w), "Model"); + Assert.Equal(["Model", "A", "L", "B"], nodes.Select(node => node.Name)); + } + + [Theory] + [InlineData(0, null, "class-27 light marker is not 1")] + [InlineData(1, 86, "class 86, which this slot does not allow")] + public void Read_InvalidPartitionLight_Throws(int marker, int? assetClass, string rule) + { + var w = Partition(Begin(), "P", 0, _ => { }, lights: [p => PartitionLight(p, marker: marker, assetClass: assetClass)]); + AssertRule(rule, End(w)); + } + [Fact] public void Read_PartitionTrailerTagNot2_Throws() => AssertRule("partition trailer does not start with 2", End(Partition(Begin(), "P", 0, _ => { }, trailerTag: 3))); @@ -508,7 +533,8 @@ internal static byte[] PartitionHierarchyForF1() => End(Partition(Begin(), "P", // A class-32 node named after its source file: an empty string, a class-53 body without attributes whose childCount children // the callback writes, then the trailer. - internal static LcGraphWriter Partition(LcGraphWriter w, string name, int childCount, Action children, int trailerTag = 2) + internal static LcGraphWriter Partition(LcGraphWriter w, string name, int childCount, Action children, + int trailerTag = 2, Action[]? lights = null) { w.New(32); w.Str(""); @@ -517,9 +543,13 @@ internal static LcGraphWriter Partition(LcGraphWriter w, string name, int childC w.Int(0).Int(0).Int(0).Int(childCount); children(w); - // [i32 2][i32] 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 (the source's plan box), [i32][i32] - w.Int(trailerTag).Int(0).Double(0).Double(0).Double(0).Int(0); + w.Int(trailerTag).Int(lights?.Length ?? 0); + foreach (var light in lights ?? []) + { + light(w); + } + + w.Double(0).Double(0).Double(0).Int(0); w.Str("Loader").Int(448).Int(0).Int(1); w.Double(0).Double(0).Double(1).Double(0).Double(1).Double(0); w.Int(0).Str(null).Int(1).Str("dir/" + name); @@ -528,6 +558,23 @@ internal static LcGraphWriter Partition(LcGraphWriter w, string name, int childC return w.Int(1).Int(0); } + private static void PartitionLight(LcGraphWriter w, int marker = 1, int selector = 0, int? assetClass = null) + { + w.New(27); + w.Int(marker); + if (assetClass is int classId) + { + w.New(classId); + w.Str("{}").Str(null).Int(0); + } + else + { + w.Int(selector); + } + + Doubles(w, 13); + } + // A group whose one leaf holds a three-item class-86 tab. internal static byte[] PropertyLeafHierarchy() => GroupWithLeaf(1, w => PropertyTab(w, 3));