Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to the `NwcIO` library, the CLI and the `NwcIO.Scene` adapte
## [Unreleased]

### Added
- Cancellation and progress overloads for `NwcDocument.Open`, `NwcAssembly.LoadFromDirectory` (also with several patterns and recursion) and `NwcAssembly.OpenNwf` and `NwcAssembly.OpenManifest`, plus `OpenAsync`, `LoadFromDirectoryAsync`, `OpenNwfAsync` and `SaveAsync` (`.nwc`) wrappers on `Task.Run`. `NwcProgress` reports `read-container`, `read-geometry`, `open` and per-model `load-models` counts (`OpenNwf` counts every reference, so `Completed` reaches `Total`). The token is checked between phases and models; a running container read is not interrupted, and `SaveAsync` checks it only before it starts.
- `meshes-skipped` reader diagnostic: `NwcDocument.Diagnostics` reports geometry records that the lenient reader path skipped (vertex count out of range, truncated or malformed record). `nwcio --report` prints `NwcDocument.Diagnostics` and `NwcAssembly.Diagnostics` for a single file, a folder and an `.nwf`.
- `HierarchyParser` keeps linked Revit placements instead of throwing: a repeated element under another parent is marked `NwcNode.IsLinkPlacement` and points at the first placement. `NwcSceneReader` writes each placement as its own `SceneModel`, so its geometry and `SourceId` are preserved without duplicate ids inside one model.
- `NwcIO.Scene` converts an `NwcAssembly` into one scene with one `SceneModel` per component document, unique keys from file names, and scene-wide element, geometry and material ids. The `.nwf` document itself is skipped when components exist.
Expand Down
1 change: 1 addition & 0 deletions docs/public-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +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 throw `NotSupportedException` unless `BuildSceneTree` is false. Trees that place one Revit element under two parents (linked models) are decoded: the repeated node is marked `IsLinkPlacement`. `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. |
| `NwcProgress` | Progress payload for the cancellation-aware overloads: a phase name, completed units, and total units (0 when unknown). |
| `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, triangle indices outside the vertex buffer, out-of-range vertex counts or coordinates, records or nodes without a name/class, and viewpoint names the reader would rename. Mesh ids, names and triangle indices are stored; normals and materials are not. `Materials`, `XRefs`, `DbCache`, `AppearanceProfiler`, `ClashTests` and `GridSystem` are not written; the output is read back by NwcIO but has not been verified in Autodesk Navisworks. |
| `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, named in property records elsewhere, with a neutral inferred colour unless a `revit_Color` record gives one), whether to build the scene tree (`BuildSceneTree`), catalog retention. |
Expand Down
88 changes: 83 additions & 5 deletions src/NwcIO/Model/NwcAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,29 @@ public static NwcAssembly LoadFromDirectory(
IReadOnlyList<string> searchPatterns,
bool recursive = false,
NwcReaderOptions? options = null)
=> LoadFromDirectory(directoryPath, searchPatterns, recursive, options, null, default);

/// <summary>
/// Loads a folder of models with cancellation and per-model progress.
/// </summary>
public static NwcAssembly LoadFromDirectory(
string directoryPath,
string searchPattern,
NwcReaderOptions? options,
IProgress<NwcProgress>? progress,
CancellationToken cancellationToken)
=> LoadFromDirectory(directoryPath, [searchPattern], recursive: false, options, progress, cancellationToken);

/// <summary>
/// Loads files matching any of <paramref name="searchPatterns"/> with cancellation and per-model progress.
/// </summary>
public static NwcAssembly LoadFromDirectory(
string directoryPath,
IReadOnlyList<string> searchPatterns,
bool recursive,
NwcReaderOptions? options,
IProgress<NwcProgress>? progress,
CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(searchPatterns);
if (!Directory.Exists(directoryPath))
Expand All @@ -244,10 +267,13 @@ public static NwcAssembly LoadFromDirectory(
$"No files matching '{string.Join(";", searchPatterns)}' found in {directoryPath}");
}

cancellationToken.ThrowIfCancellationRequested();
var docs = new List<NwcDocument>(files.Length);
foreach (var file in files)
for (int i = 0; i < files.Length; i++)
{
docs.Add(NwcDocument.Open(file, options));
cancellationToken.ThrowIfCancellationRequested();
docs.Add(NwcDocument.Open(files[i], options, null, cancellationToken));
progress?.Report(new NwcProgress("load-models", i + 1, files.Length));
}

string assemblyName = Path.GetFileName(directoryPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar));
Expand All @@ -265,6 +291,16 @@ public static NwcAssembly LoadFromDirectory(
/// </summary>
/// <exception cref="FileNotFoundException">The manifest is missing, or none of its models could be resolved.</exception>
public static NwcAssembly OpenManifest(string manifestPath, NwcReaderOptions? options = null)
=> OpenManifest(manifestPath, options, null, default);

/// <summary>
/// Opens a federation manifest with cancellation and progress for each listed model.
/// </summary>
public static NwcAssembly OpenManifest(
string manifestPath,
NwcReaderOptions? options,
IProgress<NwcProgress>? progress,
CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(manifestPath);
if (!File.Exists(manifestPath))
Expand All @@ -281,8 +317,11 @@ public static NwcAssembly OpenManifest(string manifestPath, NwcReaderOptions? op
var unresolved = new List<NwcXRef>();
var skipped = new List<NwcXRef>();

int processed = 0;
foreach (var modelPath in manifest.ModelPaths)
{
cancellationToken.ThrowIfCancellationRequested();
progress?.Report(new NwcProgress("load-models", processed++, manifest.ModelPaths.Count));
var xref = new NwcXRef(modelPath.Replace('\\', Path.DirectorySeparatorChar), "");
if (!xref.TryResolve(baseDir, out string resolved))
{
Expand All @@ -299,7 +338,7 @@ public static NwcAssembly OpenManifest(string manifestPath, NwcReaderOptions? op

if (loadedPaths.Add(Path.GetFullPath(resolved)))
{
loadedDocs.Add(NwcDocument.Open(resolved, options));
loadedDocs.Add(NwcDocument.Open(resolved, options, null, cancellationToken));
}
}

Expand All @@ -309,6 +348,7 @@ public static NwcAssembly OpenManifest(string manifestPath, NwcReaderOptions? op
$"No .nwc or .nwd models listed in manifest '{manifestPath}' could be resolved.");
}

progress?.Report(new NwcProgress("load-models", manifest.ModelPaths.Count, manifest.ModelPaths.Count));
string fileName = Path.GetFileNameWithoutExtension(manifestPath);
string assemblyName = fileName.EndsWith(".nwf", StringComparison.OrdinalIgnoreCase)
? Path.GetFileNameWithoutExtension(fileName)
Expand All @@ -325,13 +365,24 @@ public static NwcAssembly OpenManifest(string manifestPath, NwcReaderOptions? op
/// <exception cref="InvalidDataException">A file is not an LcUStream container, or its DbCache directory names model segments that break the format.</exception>
/// <exception cref="NotSupportedException">A referenced .nwd has a scene tree with partition (class-32) nodes; open with <see cref="NwcReaderOptions.BuildSceneTree"/> off.</exception>
public static NwcAssembly OpenNwf(string nwfPath, NwcReaderOptions? options = null)
=> OpenNwf(nwfPath, options, null, default);

/// <summary>
/// Opens a .nwf assembly with cancellation and progress for each loaded model.
/// </summary>
public static NwcAssembly OpenNwf(
string nwfPath,
NwcReaderOptions? options,
IProgress<NwcProgress>? progress,
CancellationToken cancellationToken)
{
if (!File.Exists(nwfPath))
{
throw new FileNotFoundException($"NWF file not found: {nwfPath}", nwfPath);
}

var primaryDoc = NwcDocument.Open(nwfPath, options);
cancellationToken.ThrowIfCancellationRequested();
var primaryDoc = NwcDocument.Open(nwfPath, options, null, cancellationToken);
var baseDir = Path.GetDirectoryName(Path.GetFullPath(nwfPath)) ?? ".";

var loadedDocs = new List<NwcDocument> { primaryDoc };
Expand All @@ -340,31 +391,41 @@ public static NwcAssembly OpenNwf(string nwfPath, NwcReaderOptions? options = nu
: StringComparer.Ordinal);
var unresolved = new List<NwcXRef>();
var skipped = new List<NwcXRef>();
int total = 1 + (primaryDoc.XRefs?.References.Count ?? 0);
progress?.Report(new NwcProgress("load-models", 1, total));

if (primaryDoc.XRefs is not null && primaryDoc.XRefs.References.Count > 0)
{
int processed = 1;
foreach (var xref in primaryDoc.XRefs.References)
{
cancellationToken.ThrowIfCancellationRequested();
// Every reference counts, loaded or not, so Completed reaches Total.
processed++;
if (!xref.TryResolve(baseDir, out string resolved))
{
unresolved.Add(xref);
progress?.Report(new NwcProgress("load-models", processed, total));
continue;
}

// Load every resolved file once, whatever the stored path spelled like.
if (!loadedPaths.Add(Path.GetFullPath(resolved)))
{
progress?.Report(new NwcProgress("load-models", processed, total));
continue;
}

if (!resolved.EndsWith(".nwc", StringComparison.OrdinalIgnoreCase) &&
!resolved.EndsWith(".nwd", StringComparison.OrdinalIgnoreCase))
{
skipped.Add(xref);
progress?.Report(new NwcProgress("load-models", processed, total));
continue;
}

loadedDocs.Add(NwcDocument.Open(resolved, options));
loadedDocs.Add(NwcDocument.Open(resolved, options, null, cancellationToken));
progress?.Report(new NwcProgress("load-models", processed, total));
}
}

Expand All @@ -388,6 +449,23 @@ public static NwcAssembly OpenNwf(string nwfPath, NwcReaderOptions? options = nu
return assembly;
}

/// <summary>Loads a folder asynchronously. The current implementation wraps the synchronous loader.</summary>
public static Task<NwcAssembly> LoadFromDirectoryAsync(
string directoryPath,
string searchPattern = "*.nwc",
NwcReaderOptions? options = null,
IProgress<NwcProgress>? progress = null,
CancellationToken cancellationToken = default)
=> Task.Run(() => LoadFromDirectory(directoryPath, searchPattern, options, progress, cancellationToken), cancellationToken);

/// <summary>Opens a .nwf asynchronously. The current implementation wraps the synchronous loader.</summary>
public static Task<NwcAssembly> OpenNwfAsync(
string nwfPath,
NwcReaderOptions? options = null,
IProgress<NwcProgress>? progress = null,
CancellationToken cancellationToken = default)
=> Task.Run(() => OpenNwf(nwfPath, options, progress, cancellationToken), cancellationToken);

/// <summary>
/// Applies an appearance profiler across all contributing models in the federated assembly.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/NwcIO/Model/NwcProgress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NwcIO.Model;

/// <summary>Progress reported by long-running reader operations.</summary>
/// <param name="Phase">Operation phase, for example "load-models".</param>
/// <param name="Completed">Completed units, such as mesh pairs or model files.</param>
/// <param name="Total">Total units for the phase, or 0 when unknown.</param>
public sealed record NwcProgress(string Phase, int Completed, int Total);
Loading
Loading