From 5e09f0f06da3bf1ed6cdae24ec567c99f523a796 Mon Sep 17 00:00:00 2001 From: mie Date: Mon, 16 Nov 2026 10:03:15 +0300 Subject: [PATCH 1/3] feat(diagnostics): add strict mode, scanner failures and JSON diagnostics PropertyRecordScanner now records the first malformed length-prefixed string in FailureReason; NwcDocument turns it into an error-level property-scan-stopped diagnostic with stream index and offset. NwcReaderOptions.Strict throws InvalidDataException for error-level diagnostics. Document and assembly JSON export include the diagnostics array. Add strict, scanner and JSON regression tests. --- CHANGELOG.md | 1 + docs/public-api.md | 4 +- src/NwcIO/Internal/Json/JsonExporter.cs | 22 +++++++ .../Internal/Parsing/PropertyRecordScanner.cs | 2 + src/NwcIO/Model/NwcAssembly.cs | 22 +++++++ src/NwcIO/NwcDocument.cs | 20 +++++- src/NwcIO/NwcReaderOptions.cs | 6 ++ tests/NwcIO.Tests/DiagnosticsExportTests.cs | 61 +++++++++++++++++++ 8 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 tests/NwcIO.Tests/DiagnosticsExportTests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index d4bac12..b381518 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to the `NwcIO` library, the CLI and the `NwcIO.Scene` adapte ## [Unreleased] ### Added +- Reader diagnostics now cover property-scanner failures (`property-scan-stopped`, Error) and are written to document and assembly JSON. `NwcReaderOptions.Strict` throws `InvalidDataException` when an error-level diagnostic is recorded. - Reader diagnostics are printed by `nwcio --report`: skipped geometry records (`meshes-skipped`), an unrecognized container tail (`ContainerTrailingGap`), and unresolved/skipped NWF references. `NwcDocument.Diagnostics` and `NwcAssembly.Diagnostics` expose the same entries to callers. - `NwcIO.Scene` diagnostics for document content the conversion leaves out: `xrefs-skipped` (Warning, with the number of external references, for example the models of a `.nwf` file), `selection-sets-skipped`, `timeliner-skipped` and `clash-tests-skipped` (Warning, each only when the document has that content), and `properties-skipped` (Info, the number of property values left out for a reason other than an unknown unit). A document with none of this content gets none of them. diff --git a/docs/public-api.md b/docs/public-api.md index fea762e..9ee2a90 100644 --- a/docs/public-api.md +++ b/docs/public-api.md @@ -11,10 +11,10 @@ The supported surface is the `NwcIO` and `NwcIO.Model.*` namespaces listed here | Type | Description | |---|---| | `NwcDocument` | Primary document container holding the root scene graph node, meshes, materials, viewpoints, selection sets, and TimeLiner schedules. Opens `.nwc`, `.nwd`, and `.nwf` files via `NwcDocument.Open`; `.nwd` scene trees with partition nodes, and trees that place one Revit element under two parents (linked models), throw `NotSupportedException` unless `BuildSceneTree` is false. `IsRevitSource` is true when the loader plugin is exactly the Revit loader `LcNwcLoaderPlugin:lcldrevit` (Navisworks reading a `.rvt`) or the Revit NWC exporter `nwexportrevit`. `LengthUnit` and `MetresPerUnit` give the length unit of coordinates and length values: feet (0.3048 m) for those two loaders, otherwise `Unknown` and null. | -| `NwcDiagnostic` / `NwcDiagnosticSeverity` | Reader diagnostics on `NwcDocument.Diagnostics` and `NwcAssembly.Diagnostics`: skipped geometry records (`meshes-skipped`), an unrecognized container tail (`ContainerTrailingGap`), and unresolved or skipped NWF references (`NwfUnresolvedXref`, `NwfSkippedXref`). `nwcio --report` prints them. | +| `NwcDiagnostic` / `NwcDiagnosticSeverity` | Reader diagnostics on `NwcDocument.Diagnostics` and `NwcAssembly.Diagnostics`: skipped geometry records (`meshes-skipped`), a property scan that stopped on a malformed record (`property-scan-stopped`, Error), an unrecognized container tail (`ContainerTrailingGap`), and unresolved or skipped NWF references (`NwfUnresolvedXref`, `NwfSkippedXref`). They are included in JSON export and printed by `nwcio --report`. | | `NwcDocumentBuilder` | Fluent builder API for creating Navisworks documents and geometry programmatically from code. | | `NwcWriter` | Direct `#LcUStream-V1.1` binary container serializer and continuous zlib chunk writer. Throws `ArgumentException` before writing for input it cannot store faithfully: an unrecognised header, scaled transforms, line meshes, triangles other than the single strip the reader rebuilds, out-of-range vertex counts or coordinates, records or nodes without a name/class, and viewpoint names the reader would rename. Normals, materials and mesh names are not stored. | -| `NwcReaderOptions` | Per-open options: which streams to read (property records, GUIDs, transforms, meshes, viewpoints, selection sets, TimeLiner), whether to read materials (`ReadMaterials`: decoded from the file on the strict path, assigned by heuristics elsewhere), whether to build the scene tree (`BuildSceneTree`), catalog retention. | +| `NwcReaderOptions` | Per-open options: which streams to read (property records, GUIDs, transforms, meshes, viewpoints, selection sets, TimeLiner), whether to read materials (`ReadMaterials`: decoded from the file on the strict path, assigned by heuristics elsewhere), whether to build the scene tree (`BuildSceneTree`), catalog retention, and `Strict` (throw `InvalidDataException` when an error-level diagnostic is recorded). | | `NwcAssembly` | Federated multi-model assembly: `Create`, `LoadFromDirectory`, `OpenNwf` (with `UnresolvedXRefs`/`SkippedXRefs` reporting), combined meshes/materials/bounds, per-assembly exports and clash runs. | | `NwcNode` | Scene graph tree node representing layers, categories, composite elements, and geometry instances with optional transformations and properties. For Navisworks model files read through their DbCache directory (the strict path), `Meshes` lists every mesh placed on a leaf and `Mesh` the first, ids are depth-first preorder indices, `Transform` is null; `IsElement` marks Revit element nodes, `ElementGuid` is their export GUID from `LcOpGuidStore`, `IfcGlobalId` is the GlobalId the Revit IFC export gives the element: its valid `IFC_GUID` parameter, else its export GUID in the IFC base-64 form (the rule of `RvtIO.Scene`), and `Element` points every node inside an element (and the second node of a sibling pair that repeats one element) to its element node; properties include the tabs stored in `LcOaPartitionProps`; other files leave `Meshes` empty and `Mesh` null. | | `NwcPropertyRecord` | Property record (`Class`, `Name`, `Value`, `ValueType`, `Tag`, `FirstInt`). On node properties of the strict path, `Class` is the property tab name (category, for example `Element`), `Name` the property name and `FirstInt` the key object id. Records scanned from property streams (`NwcDocument.PropertyRecords`, and node properties of other files) keep their internal class names. `TabInternalName` and `InternalName` are the internal names of the tab and the key on strict-path node properties, null on scanned records. | diff --git a/src/NwcIO/Internal/Json/JsonExporter.cs b/src/NwcIO/Internal/Json/JsonExporter.cs index 46c61f6..05c5279 100644 --- a/src/NwcIO/Internal/Json/JsonExporter.cs +++ b/src/NwcIO/Internal/Json/JsonExporter.cs @@ -287,6 +287,28 @@ public static void Write(NwcDocument document, Stream output, NwcJsonExportOptio writer.WriteEndArray(); } + if (document.Diagnostics.Count > 0) + { + writer.WriteStartArray("diagnostics"); + foreach (var diagnostic in document.Diagnostics) + { + writer.WriteStartObject(); + writer.WriteString("severity", diagnostic.Severity.ToString()); + writer.WriteString("code", diagnostic.Code); + writer.WriteString("message", diagnostic.Message); + if (diagnostic.StreamIndex is int streamIndex) + { + writer.WriteNumber("streamIndex", streamIndex); + } + if (diagnostic.ByteOffset is long byteOffset) + { + writer.WriteNumber("byteOffset", byteOffset); + } + writer.WriteEndObject(); + } + writer.WriteEndArray(); + } + if (options.IncludeStreamCatalog) { WriteStreamCatalog(writer, document); diff --git a/src/NwcIO/Internal/Parsing/PropertyRecordScanner.cs b/src/NwcIO/Internal/Parsing/PropertyRecordScanner.cs index ff5296e..b70679e 100644 --- a/src/NwcIO/Internal/Parsing/PropertyRecordScanner.cs +++ b/src/NwcIO/Internal/Parsing/PropertyRecordScanner.cs @@ -182,12 +182,14 @@ private bool TryReadLengthPrefixedString(ref int pos, out string? value) value = null; if (pos + 4 > _data.Length) { + FailureReason ??= $"A length-prefixed string at byte {pos} runs past the end of the stream."; return false; } int len = ReadInt32At(pos); if (len is < 0 or > 65536 || pos + 4 + len > _data.Length) { + FailureReason ??= $"A length-prefixed string at byte {pos} has invalid length {len}."; return false; } diff --git a/src/NwcIO/Model/NwcAssembly.cs b/src/NwcIO/Model/NwcAssembly.cs index 724e69a..a491e11 100644 --- a/src/NwcIO/Model/NwcAssembly.cs +++ b/src/NwcIO/Model/NwcAssembly.cs @@ -641,6 +641,28 @@ public void SaveJson(Stream stream, NwcJsonExportOptions? options = null) writer.WriteString("size", BoundingBox.Size.ToString()); writer.WriteEndObject(); + if (Diagnostics.Count > 0) + { + writer.WriteStartArray("diagnostics"); + foreach (var diagnostic in Diagnostics) + { + writer.WriteStartObject(); + writer.WriteString("severity", diagnostic.Severity.ToString()); + writer.WriteString("code", diagnostic.Code); + writer.WriteString("message", diagnostic.Message); + if (diagnostic.StreamIndex is int streamIndex) + { + writer.WriteNumber("streamIndex", streamIndex); + } + if (diagnostic.ByteOffset is long byteOffset) + { + writer.WriteNumber("byteOffset", byteOffset); + } + writer.WriteEndObject(); + } + writer.WriteEndArray(); + } + writer.WriteStartArray("models"); foreach (var m in Models) { diff --git a/src/NwcIO/NwcDocument.cs b/src/NwcIO/NwcDocument.cs index c2efb9c..6353ea3 100644 --- a/src/NwcIO/NwcDocument.cs +++ b/src/NwcIO/NwcDocument.cs @@ -156,6 +156,7 @@ public static NwcDocument Open(Stream stream, NwcReaderOptions? options = null, string rootName = Path.GetFileNameWithoutExtension(fileInfo.SourcePath ?? fallbackName ?? "Model"); + var diagnostics = new List(); var records = new List(); if (options.ReadPropertyRecords) { @@ -163,6 +164,15 @@ public static NwcDocument Open(Stream stream, NwcReaderOptions? options = null, { var scanner = new PropertyRecordScanner(s.Data); records.AddRange(scanner.Scan()); + if (scanner.FailureReason is not null) + { + diagnostics.Add(new NwcDiagnostic( + NwcDiagnosticSeverity.Error, + "property-scan-stopped", + $"{scanner.FailureReason} Stopped at byte {scanner.StoppedAt}.", + s.Index, + scanner.StoppedAt)); + } } } @@ -269,7 +279,6 @@ public static NwcDocument Open(Stream stream, NwcReaderOptions? options = null, container = container with { Streams = [] }; } - var diagnostics = new List(); if (skippedMeshRecords > 0) { diagnostics.Add(new NwcDiagnostic( @@ -286,6 +295,15 @@ public static NwcDocument Open(Stream stream, NwcReaderOptions? options = null, $"Unrecognized {container.TrailingGap} byte tail after the last stream.")); } + if (options.Strict) + { + var errors = diagnostics.Where(d => d.Severity == NwcDiagnosticSeverity.Error).ToList(); + if (errors.Count > 0) + { + throw new InvalidDataException(string.Join(" ", errors.Select(d => d.Message))); + } + } + var doc = new NwcDocument(fileInfo, records, guids, transforms, meshes, materials, rootNode, xRefs, dbCache, savedViewpoints, selectionSets, timeLiner, container); doc.Diagnostics = diagnostics; return doc; diff --git a/src/NwcIO/NwcReaderOptions.cs b/src/NwcIO/NwcReaderOptions.cs index 602d9a5..6b7de94 100644 --- a/src/NwcIO/NwcReaderOptions.cs +++ b/src/NwcIO/NwcReaderOptions.cs @@ -39,4 +39,10 @@ public sealed class NwcReaderOptions /// Keep the stream catalog (offsets, sizes, kinds) in the document (default: true). public bool KeepStreamCatalog { get; set; } = true; + + /// + /// Throw when the reader records an error-level diagnostic + /// (default: false, diagnostics are only recorded). + /// + public bool Strict { get; set; } } diff --git a/tests/NwcIO.Tests/DiagnosticsExportTests.cs b/tests/NwcIO.Tests/DiagnosticsExportTests.cs new file mode 100644 index 0000000..a7f94d0 --- /dev/null +++ b/tests/NwcIO.Tests/DiagnosticsExportTests.cs @@ -0,0 +1,61 @@ +using System.Buffers.Binary; +using System.Text; +using NwcIO.Internal.Container; +using NwcIO.Model; +using Xunit; + +namespace NwcIO.Tests; + +public sealed class DiagnosticsExportTests +{ + [Fact] + public void Open_MalformedPropertyRecord_ReportsPropertyScanStopped() + { + using var stream = new MemoryStream(MalformedSceneTreeFile()); + + var document = NwcDocument.Open(stream); + + var diagnostic = Assert.Single(document.Diagnostics, d => d.Code == "property-scan-stopped"); + Assert.Equal(NwcDiagnosticSeverity.Error, diagnostic.Severity); + Assert.Contains("invalid length", diagnostic.Message); + } + + [Fact] + public void Open_StrictWithMalformedPropertyRecord_Throws() + { + using var stream = new MemoryStream(MalformedSceneTreeFile()); + + Assert.Throws(() => + NwcDocument.Open(stream, new NwcReaderOptions { Strict = true })); + } + + [Fact] + public void JsonExport_IncludesDocumentAndAssemblyDiagnostics() + { + using var stream = new MemoryStream(MalformedSceneTreeFile()); + var document = NwcDocument.Open(stream); + + string json = document.ToJson(); + Assert.Contains("\"diagnostics\"", json); + Assert.Contains("property-scan-stopped", json); + + var assembly = NwcAssembly.Create("Fed", document); + using var assemblyStream = new MemoryStream(); + assembly.SaveJson(assemblyStream); + string assemblyJson = Encoding.UTF8.GetString(assemblyStream.ToArray()); + Assert.Contains("\"diagnostics\"", assemblyJson); + Assert.Contains("property-scan-stopped", assemblyJson); + } + + private static byte[] MalformedSceneTreeFile() + { + // The first two ints classify the stream as SceneTree; tag 53 at byte 12 has an invalid -1 length. + var data = new byte[24]; + BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(0), 100); + BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(4), 86); + BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(8), 0); + BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(12), 53); + BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(16), -1); + return NwcContainerWriter.Write([data]); + } +} From 4e8f2b07f2e84414b8a2e74fd32af1004ccab799 Mon Sep 17 00:00:00 2001 From: sim Date: Mon, 16 Nov 2026 12:57:06 +0300 Subject: [PATCH 2/3] fix(diagnostics): stop treating lenient scanner probes as strict errors Accept -1 as a valid null string, report the first malformed probe as a Warning with its real offset, do not emit it on the strict DbCache path, and let Strict open files whose only issue is a rejected heuristic probe. --- .../Internal/Parsing/PropertyRecordScanner.cs | 16 ++++++++- src/NwcIO/NwcDocument.cs | 11 +++--- tests/NwcIO.Tests/DiagnosticsExportTests.cs | 36 ++++++++++++++----- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/src/NwcIO/Internal/Parsing/PropertyRecordScanner.cs b/src/NwcIO/Internal/Parsing/PropertyRecordScanner.cs index b70679e..e653924 100644 --- a/src/NwcIO/Internal/Parsing/PropertyRecordScanner.cs +++ b/src/NwcIO/Internal/Parsing/PropertyRecordScanner.cs @@ -36,12 +36,16 @@ public sealed class PropertyRecordScanner public string? FailureReason { get; private set; } + /// Byte offset of the first malformed probe; null when no probe failed. + public int? FailureOffset { get; private set; } + public List Scan() { var records = new List(); _pos = 0; StoppedAt = 0; FailureReason = null; + FailureOffset = null; while (_pos + 8 <= _data.Length) { @@ -183,13 +187,23 @@ private bool TryReadLengthPrefixedString(ref int pos, out string? value) if (pos + 4 > _data.Length) { FailureReason ??= $"A length-prefixed string at byte {pos} runs past the end of the stream."; + FailureOffset ??= pos; return false; } int len = ReadInt32At(pos); - if (len is < 0 or > 65536 || pos + 4 + len > _data.Length) + if (len == -1) + { + // The format uses -1 for a null string; it is valid in both strict and lenient readers. + value = null; + pos += 4; + return true; + } + + if (len < 0 || len > 65536 || pos + 4 + len > _data.Length) { FailureReason ??= $"A length-prefixed string at byte {pos} has invalid length {len}."; + FailureOffset ??= pos; return false; } diff --git a/src/NwcIO/NwcDocument.cs b/src/NwcIO/NwcDocument.cs index 6353ea3..3c14dd7 100644 --- a/src/NwcIO/NwcDocument.cs +++ b/src/NwcIO/NwcDocument.cs @@ -164,14 +164,15 @@ public static NwcDocument Open(Stream stream, NwcReaderOptions? options = null, { var scanner = new PropertyRecordScanner(s.Data); records.AddRange(scanner.Scan()); - if (scanner.FailureReason is not null) + if (segments is null && scanner.FailureReason is not null) { + // The lenient scanner searches forward; a rejected probe is not a lost record. diagnostics.Add(new NwcDiagnostic( - NwcDiagnosticSeverity.Error, - "property-scan-stopped", - $"{scanner.FailureReason} Stopped at byte {scanner.StoppedAt}.", + NwcDiagnosticSeverity.Warning, + "property-scan-failed", + scanner.FailureReason, s.Index, - scanner.StoppedAt)); + scanner.FailureOffset)); } } } diff --git a/tests/NwcIO.Tests/DiagnosticsExportTests.cs b/tests/NwcIO.Tests/DiagnosticsExportTests.cs index a7f94d0..091a1f0 100644 --- a/tests/NwcIO.Tests/DiagnosticsExportTests.cs +++ b/tests/NwcIO.Tests/DiagnosticsExportTests.cs @@ -1,4 +1,4 @@ -using System.Buffers.Binary; +using System.Buffers.Binary; using System.Text; using NwcIO.Internal.Container; using NwcIO.Model; @@ -15,18 +15,19 @@ public void Open_MalformedPropertyRecord_ReportsPropertyScanStopped() var document = NwcDocument.Open(stream); - var diagnostic = Assert.Single(document.Diagnostics, d => d.Code == "property-scan-stopped"); - Assert.Equal(NwcDiagnosticSeverity.Error, diagnostic.Severity); + var diagnostic = Assert.Single(document.Diagnostics, d => d.Code == "property-scan-failed"); + Assert.Equal(NwcDiagnosticSeverity.Warning, diagnostic.Severity); Assert.Contains("invalid length", diagnostic.Message); } [Fact] - public void Open_StrictWithMalformedPropertyRecord_Throws() + public void Open_StrictWithMalformedPropertyRecord_DoesNotThrow() { using var stream = new MemoryStream(MalformedSceneTreeFile()); - Assert.Throws(() => - NwcDocument.Open(stream, new NwcReaderOptions { Strict = true })); + var document = NwcDocument.Open(stream, new NwcReaderOptions { Strict = true }); + + Assert.DoesNotContain(document.Diagnostics, d => d.Severity == NwcDiagnosticSeverity.Error); } [Fact] @@ -37,14 +38,30 @@ public void JsonExport_IncludesDocumentAndAssemblyDiagnostics() string json = document.ToJson(); Assert.Contains("\"diagnostics\"", json); - Assert.Contains("property-scan-stopped", json); + Assert.Contains("property-scan-failed", json); var assembly = NwcAssembly.Create("Fed", document); using var assemblyStream = new MemoryStream(); assembly.SaveJson(assemblyStream); string assemblyJson = Encoding.UTF8.GetString(assemblyStream.ToArray()); Assert.Contains("\"diagnostics\"", assemblyJson); - Assert.Contains("property-scan-stopped", assemblyJson); + Assert.Contains("property-scan-failed", assemblyJson); + } + + [Fact] + public void Open_NullPropertyString_IsValidAndStrictDoesNotThrow() + { + var data = new byte[20]; + BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(0), 100); + BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(4), 86); + BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(8), 0); + BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(12), 53); + BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(16), -1); + byte[] file = NwcContainerWriter.Write([data]); + + var document = NwcDocument.Open(new MemoryStream(file), new NwcReaderOptions { Strict = true }); + + Assert.DoesNotContain(document.Diagnostics, d => d.Code == "property-scan-failed"); } private static byte[] MalformedSceneTreeFile() @@ -55,7 +72,8 @@ private static byte[] MalformedSceneTreeFile() BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(4), 86); BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(8), 0); BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(12), 53); - BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(16), -1); + BinaryPrimitives.WriteInt32LittleEndian(data.AsSpan(16), -2); return NwcContainerWriter.Write([data]); } } + From 58162c63752e130edf0849ceab78c08adc4018ee Mon Sep 17 00:00:00 2001 From: sim Date: Mon, 16 Nov 2026 23:23:36 +0300 Subject: [PATCH 3/3] fix(diagnostics): align property-scan diagnostic docs and add scanner regression tests --- CHANGELOG.md | 2 +- docs/public-api.md | 2 +- tests/NwcIO.Tests/DiagnosticsExportTests.cs | 30 +++++++++++++++++++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b381518..81d4343 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ All notable changes to the `NwcIO` library, the CLI and the `NwcIO.Scene` adapte ## [Unreleased] ### Added -- Reader diagnostics now cover property-scanner failures (`property-scan-stopped`, Error) and are written to document and assembly JSON. `NwcReaderOptions.Strict` throws `InvalidDataException` when an error-level diagnostic is recorded. +- Reader diagnostics now cover property-scanner failures (`property-scan-failed`, Warning) and are written to document and assembly JSON. `NwcReaderOptions.Strict` throws `InvalidDataException` when an error-level diagnostic is recorded. - Reader diagnostics are printed by `nwcio --report`: skipped geometry records (`meshes-skipped`), an unrecognized container tail (`ContainerTrailingGap`), and unresolved/skipped NWF references. `NwcDocument.Diagnostics` and `NwcAssembly.Diagnostics` expose the same entries to callers. - `NwcIO.Scene` diagnostics for document content the conversion leaves out: `xrefs-skipped` (Warning, with the number of external references, for example the models of a `.nwf` file), `selection-sets-skipped`, `timeliner-skipped` and `clash-tests-skipped` (Warning, each only when the document has that content), and `properties-skipped` (Info, the number of property values left out for a reason other than an unknown unit). A document with none of this content gets none of them. diff --git a/docs/public-api.md b/docs/public-api.md index 9ee2a90..41e8488 100644 --- a/docs/public-api.md +++ b/docs/public-api.md @@ -11,7 +11,7 @@ The supported surface is the `NwcIO` and `NwcIO.Model.*` namespaces listed here | Type | Description | |---|---| | `NwcDocument` | Primary document container holding the root scene graph node, meshes, materials, viewpoints, selection sets, and TimeLiner schedules. Opens `.nwc`, `.nwd`, and `.nwf` files via `NwcDocument.Open`; `.nwd` scene trees with partition nodes, and trees that place one Revit element under two parents (linked models), throw `NotSupportedException` unless `BuildSceneTree` is false. `IsRevitSource` is true when the loader plugin is exactly the Revit loader `LcNwcLoaderPlugin:lcldrevit` (Navisworks reading a `.rvt`) or the Revit NWC exporter `nwexportrevit`. `LengthUnit` and `MetresPerUnit` give the length unit of coordinates and length values: feet (0.3048 m) for those two loaders, otherwise `Unknown` and null. | -| `NwcDiagnostic` / `NwcDiagnosticSeverity` | Reader diagnostics on `NwcDocument.Diagnostics` and `NwcAssembly.Diagnostics`: skipped geometry records (`meshes-skipped`), a property scan that stopped on a malformed record (`property-scan-stopped`, Error), an unrecognized container tail (`ContainerTrailingGap`), and unresolved or skipped NWF references (`NwfUnresolvedXref`, `NwfSkippedXref`). They are included in JSON export and printed by `nwcio --report`. | +| `NwcDiagnostic` / `NwcDiagnosticSeverity` | Reader diagnostics on `NwcDocument.Diagnostics` and `NwcAssembly.Diagnostics`: skipped geometry records (`meshes-skipped`), a property scan that stopped on a malformed record (`property-scan-failed`, Warning), an unrecognized container tail (`ContainerTrailingGap`), and unresolved or skipped NWF references (`NwfUnresolvedXref`, `NwfSkippedXref`). They are included in JSON export and printed by `nwcio --report`. | | `NwcDocumentBuilder` | Fluent builder API for creating Navisworks documents and geometry programmatically from code. | | `NwcWriter` | Direct `#LcUStream-V1.1` binary container serializer and continuous zlib chunk writer. Throws `ArgumentException` before writing for input it cannot store faithfully: an unrecognised header, scaled transforms, line meshes, triangles other than the single strip the reader rebuilds, out-of-range vertex counts or coordinates, records or nodes without a name/class, and viewpoint names the reader would rename. Normals, materials and mesh names are not stored. | | `NwcReaderOptions` | Per-open options: which streams to read (property records, GUIDs, transforms, meshes, viewpoints, selection sets, TimeLiner), whether to read materials (`ReadMaterials`: decoded from the file on the strict path, assigned by heuristics elsewhere), whether to build the scene tree (`BuildSceneTree`), catalog retention, and `Strict` (throw `InvalidDataException` when an error-level diagnostic is recorded). | diff --git a/tests/NwcIO.Tests/DiagnosticsExportTests.cs b/tests/NwcIO.Tests/DiagnosticsExportTests.cs index 091a1f0..d0bc93b 100644 --- a/tests/NwcIO.Tests/DiagnosticsExportTests.cs +++ b/tests/NwcIO.Tests/DiagnosticsExportTests.cs @@ -1,4 +1,4 @@ -using System.Buffers.Binary; +using System.Buffers.Binary; using System.Text; using NwcIO.Internal.Container; using NwcIO.Model; @@ -9,7 +9,7 @@ namespace NwcIO.Tests; public sealed class DiagnosticsExportTests { [Fact] - public void Open_MalformedPropertyRecord_ReportsPropertyScanStopped() + public void Open_MalformedPropertyRecord_ReportsPropertyScanFailedWarning() { using var stream = new MemoryStream(MalformedSceneTreeFile()); @@ -18,6 +18,8 @@ public void Open_MalformedPropertyRecord_ReportsPropertyScanStopped() var diagnostic = Assert.Single(document.Diagnostics, d => d.Code == "property-scan-failed"); Assert.Equal(NwcDiagnosticSeverity.Warning, diagnostic.Severity); Assert.Contains("invalid length", diagnostic.Message); + Assert.Equal(0, diagnostic.StreamIndex); + Assert.Equal(16L, diagnostic.ByteOffset); } [Fact] @@ -64,6 +66,30 @@ public void Open_NullPropertyString_IsValidAndStrictDoesNotThrow() Assert.DoesNotContain(document.Diagnostics, d => d.Code == "property-scan-failed"); } + [Fact] + public void Scan_NullValueString_KeepsRecordAndAdvancesPastPrefix() + { + // tag 52 record: name "a", class "b", valueType 4 (string) with a null (-1) value, then tag 53 node "n". + var ms = new MemoryStream(); + using (var w = new BinaryWriter(ms, Encoding.UTF8, leaveOpen: true)) + { + w.Write(1); w.Write(52); + w.Write(1); w.Write(new byte[] { (byte)'a', 0, 0, 0 }); + w.Write(1); w.Write(new byte[] { (byte)'b', 0, 0, 0 }); + w.Write(4); w.Write(-1); + w.Write(2); w.Write(53); + w.Write(1); w.Write(new byte[] { (byte)'n', 0, 0, 0 }); + } + + var scanner = new NwcIO.Internal.Parsing.PropertyRecordScanner(ms.ToArray()); + var records = scanner.Scan(); + + Assert.Equal(2, records.Count); + Assert.Null(records[0].Value); + Assert.Equal("n", records[1].Name); + Assert.Null(scanner.FailureReason); + } + private static byte[] MalformedSceneTreeFile() { // The first two ints classify the stream as SceneTree; tag 53 at byte 12 has an invalid -1 length.