From 379a738da4f2edf2e50274748c8de1e680a0c187 Mon Sep 17 00:00:00 2001 From: mie Date: Mon, 16 Nov 2026 10:12:15 +0300 Subject: [PATCH 1/5] feat(export): add cancellation and progress to exporters Add token/progress overloads for document and assembly exports: nwc, GLB, IFC, OBJ, JSON, BCF, viewpoints XML, clash HTML and the HTML viewer. Existing Save* signatures are unchanged. The overloads check cancellation before and after the exporter and report export-* 0/1 progress; async wrappers now pass the token into them. Add four regression tests. --- CHANGELOG.md | 1 + docs/public-api.md | 2 +- src/NwcIO/Model/NwcAssembly.cs | 99 ++++++++++++- src/NwcIO/NwcDocument.cs | 140 +++++++++++++++++-- tests/NwcIO.Tests/ExportCancellationTests.cs | 80 +++++++++++ 5 files changed, 306 insertions(+), 16 deletions(-) create mode 100644 tests/NwcIO.Tests/ExportCancellationTests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index a9d747c..60c70ca 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 +- Cancellation and progress overloads for document and assembly exports: `.nwc`, GLB, IFC, OBJ, JSON, BCF, viewpoints XML and HTML. Existing `Save*` signatures are unchanged; the new overloads report `export-*` phases and check the token before and after the exporter. `Save*Async` wrappers now pass the token into those overloads. - Cancellation and progress overloads for `NwcDocument.Open`, `NwcAssembly.LoadFromDirectory` and `NwcAssembly.OpenNwf`, plus `OpenAsync`, `LoadFromDirectoryAsync`, `OpenNwfAsync` and `Save*Async` wrappers. `NwcProgress` reports phases such as `read-container`, `read-geometry`, `open` and per-model `load-models` counts. - `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 7097536..61d20ee 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. | -| `NwcProgress` | Progress payload for the cancellation-aware overloads: a phase name, completed units, and total units (0 when unknown). | +| `NwcProgress` | Progress payload for the cancellation-aware reader and exporter overloads: a phase name (`read-container`, `read-geometry`, `open`, `load-models`, `export-glb`, `export-ifc`, `export-obj`, `export-json`, `export-bcf`, `export-html`), 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, 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. | diff --git a/src/NwcIO/Model/NwcAssembly.cs b/src/NwcIO/Model/NwcAssembly.cs index e335668..1638af4 100644 --- a/src/NwcIO/Model/NwcAssembly.cs +++ b/src/NwcIO/Model/NwcAssembly.cs @@ -501,20 +501,44 @@ public void ExportClashMatrixBcf(string path) /// Exports the entire federated assembly to a binary glTF 2.0 file (.glb). public void SaveGlb(string path, bool worldCoordinates = true) + => SaveGlb(path, worldCoordinates, null, default); + + /// Exports the assembly GLB with cancellation and progress. + public void SaveGlb(string path, bool worldCoordinates, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-glb", 0, 1)); GlbExporter.WriteFile(AllMeshes, AllMaterials, path, worldCoordinates, "NwcIO Assembly", SavedViewpoints); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-glb", 1, 1)); } /// Exports the entire federated assembly to a Wavefront OBJ file. public void SaveObj(string path, bool worldCoordinates = true) + => SaveObj(path, worldCoordinates, null, default); + + /// Exports the assembly OBJ with cancellation and progress. + public void SaveObj(string path, bool worldCoordinates, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-obj", 0, 1)); ObjExporter.WriteFile(AllMeshes, path, worldCoordinates, Name); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-obj", 1, 1)); } /// Exports the entire federated assembly with geometry, materials, and spatial hierarchy to an IFC2x3 STEP file (.ifc). public void SaveIfc(string path, NwcIfcExportOptions? options = null) + => SaveIfc(path, options, null, default); + + /// Exports the assembly IFC with cancellation and progress. + public void SaveIfc(string path, NwcIfcExportOptions? options, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-ifc", 0, 1)); IfcExporter.WriteAssembly(this, path, options); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-ifc", 1, 1)); } /// Exports the entire federated assembly with geometry, materials, and spatial hierarchy to a in IFC2x3 format. @@ -529,18 +553,34 @@ public void SaveIfc(TextWriter writer, NwcIfcExportOptions? options = null) /// Destination .bcf or .bcfzip file path. /// Optional pre-computed clash list; if null, is evaluated. public void SaveBcf(string path, IReadOnlyList? clashes = null) + => SaveBcf(path, clashes, null, default); + + /// Exports clashes to BCF with cancellation and progress. + public void SaveBcf(string path, IReadOnlyList? clashes, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-bcf", 0, 1)); clashes ??= FindClashes(); BcfExporter.WriteFile(clashes, path, Name); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-bcf", 1, 1)); } /// /// Exports detected clashes across models in the assembly to a standard buildingSMART BCF 2.1 zip stream. /// public void SaveBcf(Stream stream, IReadOnlyList? clashes = null) + => SaveBcf(stream, clashes, null, default); + + /// Exports clashes to a BCF stream with cancellation and progress. + public void SaveBcf(Stream stream, IReadOnlyList? clashes, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-bcf", 0, 1)); clashes ??= FindClashes(); BcfExporter.Write(clashes, stream, Name); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-bcf", 1, 1)); } /// @@ -549,27 +589,51 @@ public void SaveBcf(Stream stream, IReadOnlyList? clashes = null) /// Destination .bcf or .bcfzip file path. /// Optional pre-computed viewpoints list; if null, is used. public void SaveViewpointsBcf(string path, IReadOnlyList? viewpoints = null) + => SaveViewpointsBcf(path, viewpoints, null, default); + + /// Exports assembly viewpoints to BCF with cancellation and progress. + public void SaveViewpointsBcf(string path, IReadOnlyList? viewpoints, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-bcf", 0, 1)); viewpoints ??= SavedViewpoints; BcfExporter.WriteViewpointsFile(viewpoints, path, Name); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-bcf", 1, 1)); } /// /// Exports saved viewpoints of the assembly to a buildingSMART BCF 2.1 zip stream. /// public void SaveViewpointsBcf(Stream stream, IReadOnlyList? viewpoints = null) + => SaveViewpointsBcf(stream, viewpoints, null, default); + + /// Exports assembly viewpoints to a BCF stream with cancellation and progress. + public void SaveViewpointsBcf(Stream stream, IReadOnlyList? viewpoints, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-bcf", 0, 1)); viewpoints ??= SavedViewpoints; BcfExporter.WriteViewpoints(viewpoints, stream, Name); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-bcf", 1, 1)); } /// /// Exports saved viewpoints of the assembly to an Autodesk Navisworks XML viewpoints file. /// public void SaveViewpointsXml(string path, IReadOnlyList? viewpoints = null, string units = "m") + => SaveViewpointsXml(path, viewpoints, units, null, default); + + /// Exports assembly viewpoints XML with cancellation and progress. + public void SaveViewpointsXml(string path, IReadOnlyList? viewpoints, string units, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-viewpoints-xml", 0, 1)); viewpoints ??= SavedViewpoints; Sectioning.NwcXmlViewpointSerializer.Save(path, viewpoints, units); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-viewpoints-xml", 1, 1)); } /// @@ -587,9 +651,17 @@ public string ToViewpointsXml(IReadOnlyList? viewpoints = nul /// Destination .html file path. /// Optional pre-computed clash list; if null, is evaluated. public void SaveClashReportHtml(string path, IReadOnlyList? clashes = null) + => SaveClashReportHtml(path, clashes, null, default); + + /// Exports the clash report HTML with cancellation and progress. + public void SaveClashReportHtml(string path, IReadOnlyList? clashes, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-html", 0, 1)); clashes ??= FindClashes(); HtmlClashReportExporter.WriteFile(clashes, path, Name, Models.Count, AllMeshes.Count); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-html", 1, 1)); } /// @@ -605,8 +677,16 @@ public void SaveClashReportHtml(TextWriter writer, IReadOnlyList? clas /// Exports the federated assembly to a standalone interactive 3D WebGL HTML viewer (.html) with zero external dependencies. /// public void SaveHtmlViewer(string path, Viewer.NwcHtmlViewerOptions? options = null) + => SaveHtmlViewer(path, options, null, default); + + /// Exports the assembly HTML viewer with cancellation and progress. + public void SaveHtmlViewer(string path, Viewer.NwcHtmlViewerOptions? options, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-html", 0, 1)); Viewer.NwcHtmlViewerExporter.Save(path, this, options); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-html", 1, 1)); } /// @@ -650,6 +730,19 @@ public int AssignGridLocations(Grids.NwcGridSystem? gridSystem = null) /// Exports a structured summary of the federated assembly to a stream in JSON format. public void SaveJson(Stream stream, NwcJsonExportOptions? options = null) + => SaveJson(stream, options, null, default); + + /// Exports the assembly JSON to a stream with cancellation and progress. + public void SaveJson(Stream stream, NwcJsonExportOptions? options, IProgress? progress, CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-json", 0, 1)); + SaveJsonCore(stream, options); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-json", 1, 1)); + } + + private void SaveJsonCore(Stream stream, NwcJsonExportOptions? options) { ArgumentNullException.ThrowIfNull(stream); options ??= NwcJsonExportOptions.Default; @@ -706,9 +799,13 @@ public void SaveJson(Stream stream, NwcJsonExportOptions? options = null) /// Exports a structured summary of the federated assembly to a JSON file. public void SaveJson(string path, NwcJsonExportOptions? options = null) + => SaveJson(path, options, null, default); + + /// Exports the assembly JSON to a file with cancellation and progress. + public void SaveJson(string path, NwcJsonExportOptions? options, IProgress? progress, CancellationToken cancellationToken) { ArgumentException.ThrowIfNullOrEmpty(path); using var fs = File.Create(path); - SaveJson(fs, options); + SaveJson(fs, options, progress, cancellationToken); } } diff --git a/src/NwcIO/NwcDocument.cs b/src/NwcIO/NwcDocument.cs index 85782f8..895342d 100644 --- a/src/NwcIO/NwcDocument.cs +++ b/src/NwcIO/NwcDocument.cs @@ -559,14 +559,30 @@ public void ExportClashBcf(Stream stream, string? testName = null) /// Saves the document to an Autodesk Navisworks (.nwc) file. public void Save(string path, string? header = null) + => Save(path, header, null, default); + + /// Saves the document to an .nwc file with cancellation and progress. + public void Save(string path, string? header, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-nwc", 0, 1)); NwcWriter.Write(this, path, header); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-nwc", 1, 1)); } /// Saves the document to an output stream in Autodesk Navisworks (.nwc) format. public void Save(Stream stream, string? header = null) + => Save(stream, header, null, default); + + /// Saves the document to an .nwc stream with cancellation and progress. + public void Save(Stream stream, string? header, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-nwc", 0, 1)); NwcWriter.Write(this, stream, header); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-nwc", 1, 1)); } /// Serializes the document to a byte array in Autodesk Navisworks (.nwc) format. @@ -577,38 +593,86 @@ public byte[] ToByteArray(string? header = null) /// Exports 3D mesh geometry to a Wavefront OBJ file. public void SaveObj(string path, bool worldCoordinates = true) + => SaveObj(path, worldCoordinates, null, default); + + /// Exports OBJ with cancellation and progress. + public void SaveObj(string path, bool worldCoordinates, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-obj", 0, 1)); Internal.Export.ObjExporter.WriteFile(Meshes, path, worldCoordinates, FileInfo.SourcePath); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-obj", 1, 1)); } /// Exports 3D mesh geometry to a in Wavefront OBJ format. public void SaveObj(TextWriter writer, bool worldCoordinates = true) + => SaveObj(writer, worldCoordinates, null, default); + + /// Exports OBJ to a writer with cancellation and progress. + public void SaveObj(TextWriter writer, bool worldCoordinates, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-obj", 0, 1)); Internal.Export.ObjExporter.Write(Meshes, writer, worldCoordinates, FileInfo.SourcePath); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-obj", 1, 1)); } /// Exports 3D mesh geometry, materials, and scene hierarchy to a binary glTF 2.0 file (.glb). public void SaveGlb(string path, bool worldCoordinates = true) + => SaveGlb(path, worldCoordinates, null, default); + + /// Exports GLB with cancellation and progress. + public void SaveGlb(string path, bool worldCoordinates, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-glb", 0, 1)); Internal.Export.GlbExporter.WriteFile(Meshes, Materials, path, worldCoordinates, FileInfo.LoaderPlugin, SavedViewpoints); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-glb", 1, 1)); } /// Exports 3D mesh geometry, materials, and scene hierarchy to a binary glTF 2.0 stream (.glb). public void SaveGlb(Stream stream, bool worldCoordinates = true) + => SaveGlb(stream, worldCoordinates, null, default); + + /// Exports GLB to a stream with cancellation and progress. + public void SaveGlb(Stream stream, bool worldCoordinates, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-glb", 0, 1)); Internal.Export.GlbExporter.Write(Meshes, Materials, stream, worldCoordinates, FileInfo.LoaderPlugin, SavedViewpoints); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-glb", 1, 1)); } /// Exports 3D mesh geometry, materials, spatial structure, and properties to an IFC STEP physical file (.ifc). public void SaveIfc(string path, NwcIfcExportOptions? options = null) + => SaveIfc(path, options, null, default); + + /// Exports IFC with cancellation and progress. + public void SaveIfc(string path, NwcIfcExportOptions? options, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-ifc", 0, 1)); Internal.Export.IfcExporter.WriteFile(this, path, options); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-ifc", 1, 1)); } /// Exports 3D mesh geometry, materials, spatial structure, and properties to a in IFC format. public void SaveIfc(TextWriter writer, NwcIfcExportOptions? options = null) + => SaveIfc(writer, options, null, default); + + /// Exports IFC to a writer with cancellation and progress. + public void SaveIfc(TextWriter writer, NwcIfcExportOptions? options, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-ifc", 0, 1)); Internal.Export.IfcExporter.Write(this, writer, options); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-ifc", 1, 1)); } /// Serializes 3D mesh geometry, materials, spatial structure, and properties to an IFC STEP physical string. @@ -621,20 +685,44 @@ public string ToIfcString(NwcIfcExportOptions? options = null) /// Exports saved viewpoints to a buildingSMART BCF 2.1 zip archive (.bcfzip). public void SaveViewpointsBcf(string path) + => SaveViewpointsBcf(path, null, default); + + /// Exports viewpoints BCF with cancellation and progress. + public void SaveViewpointsBcf(string path, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-bcf", 0, 1)); Internal.Export.BcfExporter.WriteViewpointsFile(SavedViewpoints, path, Path.GetFileNameWithoutExtension(FileInfo.SourcePath ?? "Model")); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-bcf", 1, 1)); } /// Exports saved viewpoints to a buildingSMART BCF 2.1 zip stream. public void SaveViewpointsBcf(Stream stream) + => SaveViewpointsBcf(stream, null, default); + + /// Exports viewpoints BCF to a stream with cancellation and progress. + public void SaveViewpointsBcf(Stream stream, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-bcf", 0, 1)); Internal.Export.BcfExporter.WriteViewpoints(SavedViewpoints, stream, Path.GetFileNameWithoutExtension(FileInfo.SourcePath ?? "Model")); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-bcf", 1, 1)); } /// Exports saved viewpoints, cameras, clipping planes, and redlines to an Autodesk Navisworks XML viewpoints file. public void SaveViewpointsXml(string path, string units = "m") + => SaveViewpointsXml(path, units, null, default); + + /// Exports viewpoints XML with cancellation and progress. + public void SaveViewpointsXml(string path, string units, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-viewpoints-xml", 0, 1)); Model.Sectioning.NwcXmlViewpointSerializer.Save(path, SavedViewpoints, units); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-viewpoints-xml", 1, 1)); } /// Serializes saved viewpoints, cameras, clipping planes, and redlines to an Autodesk Navisworks XML viewpoints string. @@ -645,16 +733,32 @@ public string ToViewpointsXml(string units = "m") /// Exports the document to a JSON file. public void SaveJson(string path, NwcJsonExportOptions? options = null) + => SaveJson(path, options, null, default); + + /// Exports the document to JSON with cancellation and progress. + public void SaveJson(string path, NwcJsonExportOptions? options, IProgress? progress, CancellationToken cancellationToken) { options ??= NwcJsonExportOptions.Default; + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-json", 0, 1)); Internal.Json.JsonExporter.WriteFile(this, path, options); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-json", 1, 1)); } /// Exports the document to a JSON stream. public void SaveJson(Stream stream, NwcJsonExportOptions? options = null) + => SaveJson(stream, options, null, default); + + /// Exports the document to a JSON stream with cancellation and progress. + public void SaveJson(Stream stream, NwcJsonExportOptions? options, IProgress? progress, CancellationToken cancellationToken) { options ??= NwcJsonExportOptions.Default; + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-json", 0, 1)); Internal.Json.JsonExporter.Write(this, stream, options); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-json", 1, 1)); } /// Serializes the document to a JSON string. @@ -669,8 +773,16 @@ public string ToJson(NwcJsonExportOptions? options = null) /// Exports the document to a standalone interactive 3D WebGL HTML viewer (.html) with zero external dependencies. /// public void SaveHtmlViewer(string path, Model.Viewer.NwcHtmlViewerOptions? options = null) + => SaveHtmlViewer(path, options, null, default); + + /// Exports the HTML viewer with cancellation and progress. + public void SaveHtmlViewer(string path, Model.Viewer.NwcHtmlViewerOptions? options, IProgress? progress, CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-html", 0, 1)); Model.Viewer.NwcHtmlViewerExporter.Save(path, this, options); + cancellationToken.ThrowIfCancellationRequested(); + progress?.Report(new NwcProgress("export-html", 1, 1)); } /// @@ -698,59 +810,59 @@ public static Task OpenAsync( /// Saves the document asynchronously. The current implementation wraps the synchronous writer. public Task SaveAsync(string path, string? header = null, CancellationToken cancellationToken = default) - => Task.Run(() => Save(path, header), cancellationToken); + => Task.Run(() => Save(path, header, null, cancellationToken), cancellationToken); /// Saves the document to a stream asynchronously. public Task SaveAsync(Stream stream, string? header = null, CancellationToken cancellationToken = default) - => Task.Run(() => Save(stream, header), cancellationToken); + => Task.Run(() => Save(stream, header, null, cancellationToken), cancellationToken); /// Saves JSON asynchronously. public Task SaveJsonAsync(string path, NwcJsonExportOptions? options = null, CancellationToken cancellationToken = default) - => Task.Run(() => SaveJson(path, options), cancellationToken); + => Task.Run(() => SaveJson(path, options, null, cancellationToken), cancellationToken); /// Saves JSON to a stream asynchronously. public Task SaveJsonAsync(Stream stream, NwcJsonExportOptions? options = null, CancellationToken cancellationToken = default) - => Task.Run(() => SaveJson(stream, options), cancellationToken); + => Task.Run(() => SaveJson(stream, options, null, cancellationToken), cancellationToken); /// Saves OBJ asynchronously. public Task SaveObjAsync(string path, bool worldCoordinates = true, CancellationToken cancellationToken = default) - => Task.Run(() => SaveObj(path, worldCoordinates), cancellationToken); + => Task.Run(() => SaveObj(path, worldCoordinates, null, cancellationToken), cancellationToken); /// Saves OBJ to a text writer asynchronously. public Task SaveObjAsync(TextWriter writer, bool worldCoordinates = true, CancellationToken cancellationToken = default) - => Task.Run(() => SaveObj(writer, worldCoordinates), cancellationToken); + => Task.Run(() => SaveObj(writer, worldCoordinates, null, cancellationToken), cancellationToken); /// Saves GLB asynchronously. public Task SaveGlbAsync(string path, bool worldCoordinates = true, CancellationToken cancellationToken = default) - => Task.Run(() => SaveGlb(path, worldCoordinates), cancellationToken); + => Task.Run(() => SaveGlb(path, worldCoordinates, null, cancellationToken), cancellationToken); /// Saves GLB to a stream asynchronously. public Task SaveGlbAsync(Stream stream, bool worldCoordinates = true, CancellationToken cancellationToken = default) - => Task.Run(() => SaveGlb(stream, worldCoordinates), cancellationToken); + => Task.Run(() => SaveGlb(stream, worldCoordinates, null, cancellationToken), cancellationToken); /// Saves IFC asynchronously. public Task SaveIfcAsync(string path, NwcIfcExportOptions? options = null, CancellationToken cancellationToken = default) - => Task.Run(() => SaveIfc(path, options), cancellationToken); + => Task.Run(() => SaveIfc(path, options, null, cancellationToken), cancellationToken); /// Saves IFC to a text writer asynchronously. public Task SaveIfcAsync(TextWriter writer, NwcIfcExportOptions? options = null, CancellationToken cancellationToken = default) - => Task.Run(() => SaveIfc(writer, options), cancellationToken); + => Task.Run(() => SaveIfc(writer, options, null, cancellationToken), cancellationToken); /// Saves viewpoints as a BCF archive asynchronously. public Task SaveViewpointsBcfAsync(string path, CancellationToken cancellationToken = default) - => Task.Run(() => SaveViewpointsBcf(path), cancellationToken); + => Task.Run(() => SaveViewpointsBcf(path, null, cancellationToken), cancellationToken); /// Saves viewpoints as a BCF stream asynchronously. public Task SaveViewpointsBcfAsync(Stream stream, CancellationToken cancellationToken = default) - => Task.Run(() => SaveViewpointsBcf(stream), cancellationToken); + => Task.Run(() => SaveViewpointsBcf(stream, null, cancellationToken), cancellationToken); /// Saves viewpoints as Navisworks XML asynchronously. public Task SaveViewpointsXmlAsync(string path, string units = "m", CancellationToken cancellationToken = default) - => Task.Run(() => SaveViewpointsXml(path, units), cancellationToken); + => Task.Run(() => SaveViewpointsXml(path, units, null, cancellationToken), cancellationToken); /// Saves the HTML viewer asynchronously. public Task SaveHtmlViewerAsync(string path, Model.Viewer.NwcHtmlViewerOptions? options = null, CancellationToken cancellationToken = default) - => Task.Run(() => SaveHtmlViewer(path, options), cancellationToken); + => Task.Run(() => SaveHtmlViewer(path, options, null, cancellationToken), cancellationToken); /// /// Populates clash grid location tags across all clash detective tests in this document using the provided or attached grid system. diff --git a/tests/NwcIO.Tests/ExportCancellationTests.cs b/tests/NwcIO.Tests/ExportCancellationTests.cs new file mode 100644 index 0000000..611eabf --- /dev/null +++ b/tests/NwcIO.Tests/ExportCancellationTests.cs @@ -0,0 +1,80 @@ +using NwcIO.Model; +using Xunit; + +namespace NwcIO.Tests; + +public sealed class ExportCancellationTests +{ + [Fact] + public void DocumentExports_CanceledToken_ThrowOperationCanceled() + { + var document = Document(); + using var cancellation = new CancellationTokenSource(); + cancellation.Cancel(); + + Assert.Throws(() => + document.SaveGlb(Path.GetTempFileName(), true, null, cancellation.Token)); + Assert.Throws(() => + document.SaveJson(Path.GetTempFileName(), null, null, cancellation.Token)); + } + + [Fact] + public void DocumentExports_ReportProgress() + { + var document = Document(); + var json = new CollectingProgress(); + using var jsonStream = new MemoryStream(); + document.SaveJson(jsonStream, null, json, CancellationToken.None); + Assert.Contains(json.Reports, p => p.Phase == "export-json" && p.Completed == 1 && p.Total == 1); + + var glb = new CollectingProgress(); + using var glbStream = new MemoryStream(); + document.SaveGlb(glbStream, true, glb, CancellationToken.None); + Assert.Contains(glb.Reports, p => p.Phase == "export-glb" && p.Completed == 1 && p.Total == 1); + } + + [Fact] + public void AssemblyExports_CancelAndReportProgress() + { + var document = Document(); + var assembly = NwcAssembly.Create("Fed", document); + using var cancellation = new CancellationTokenSource(); + cancellation.Cancel(); + Assert.Throws(() => + assembly.SaveGlb(Path.GetTempFileName(), true, null, cancellation.Token)); + + var progress = new CollectingProgress(); + using var stream = new MemoryStream(); + assembly.SaveJson(stream, null, progress, CancellationToken.None); + Assert.Contains(progress.Reports, p => p.Phase == "export-json" && p.Completed == 1 && p.Total == 1); + } + + [Fact] + public async Task SaveJsonAsync_CanceledToken_IsCanceled() + { + var document = Document(); + using var cancellation = new CancellationTokenSource(); + cancellation.Cancel(); + + await Assert.ThrowsAnyAsync(() => + document.SaveJsonAsync(Path.GetTempFileName(), cancellationToken: cancellation.Token)); + } + + private static NwcDocument Document() => NwcDocument.Create(meshes: + [ + new NwcMesh + { + Id = 1, + Vertices = [new(0, 0, 0), new(1, 0, 0), new(0, 1, 0)], + Indices = [0, 1, 2], + BoundingBox = new BoundingBox3d(new(0, 0, 0), new(1, 1, 0)), + }, + ]); + + private sealed class CollectingProgress : IProgress + { + public List Reports { get; } = []; + + public void Report(NwcProgress value) => Reports.Add(value); + } +} From 48f0371e5bb199a8ada9082696ce343243280cec Mon Sep 17 00:00:00 2001 From: sim Date: Mon, 16 Nov 2026 12:38:44 +0300 Subject: [PATCH 2/5] fix(export): check cancellation before truncating or writing Assembly SaveJson checks the token before File.Create, JSON exports honour a cancel requested from the 0/1 progress report, and clash BCF/HTML exports check again after FindClashes before touching the destination. --- src/NwcIO/Model/NwcAssembly.cs | 5 +++ src/NwcIO/NwcDocument.cs | 2 + tests/NwcIO.Tests/ExportCancellationTests.cs | 45 ++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/src/NwcIO/Model/NwcAssembly.cs b/src/NwcIO/Model/NwcAssembly.cs index 1638af4..721aac5 100644 --- a/src/NwcIO/Model/NwcAssembly.cs +++ b/src/NwcIO/Model/NwcAssembly.cs @@ -561,6 +561,7 @@ public void SaveBcf(string path, IReadOnlyList? clashes, IProgress? clashes, IProgress? clashes, I cancellationToken.ThrowIfCancellationRequested(); progress?.Report(new NwcProgress("export-html", 0, 1)); clashes ??= FindClashes(); + cancellationToken.ThrowIfCancellationRequested(); HtmlClashReportExporter.WriteFile(clashes, path, Name, Models.Count, AllMeshes.Count); cancellationToken.ThrowIfCancellationRequested(); progress?.Report(new NwcProgress("export-html", 1, 1)); @@ -737,6 +740,7 @@ public void SaveJson(Stream stream, NwcJsonExportOptions? options, IProgress? progress, CancellationToken cancellationToken) { ArgumentException.ThrowIfNullOrEmpty(path); + cancellationToken.ThrowIfCancellationRequested(); using var fs = File.Create(path); SaveJson(fs, options, progress, cancellationToken); } diff --git a/src/NwcIO/NwcDocument.cs b/src/NwcIO/NwcDocument.cs index 895342d..d4a1839 100644 --- a/src/NwcIO/NwcDocument.cs +++ b/src/NwcIO/NwcDocument.cs @@ -741,6 +741,7 @@ public void SaveJson(string path, NwcJsonExportOptions? options, IProgress p.Phase == "export-json" && p.Completed == 1 && p.Total == 1); } + [Fact] + public void AssemblySaveJson_CanceledToken_DoesNotTruncateExistingFile() + { + var assembly = NwcAssembly.Create("Fed", Document()); + string path = Path.Combine(Path.GetTempPath(), $"nwcio-{Guid.NewGuid():N}.json"); + File.WriteAllText(path, "keep me"); + try + { + using var cancellation = new CancellationTokenSource(); + cancellation.Cancel(); + + Assert.Throws(() => + assembly.SaveJson(path, null, null, cancellation.Token)); + + Assert.Equal("keep me", File.ReadAllText(path)); + } + finally + { + if (File.Exists(path)) File.Delete(path); + } + } + + [Fact] + public void AssemblySaveJson_ProgressCanCancelMidExport() + { + var assembly = NwcAssembly.Create("Fed", Document()); + string path = Path.Combine(Path.GetTempPath(), $"nwcio-{Guid.NewGuid():N}.json"); + using var cancellation = new CancellationTokenSource(); + var progress = new CancellingProgress(cancellation); + try + { + Assert.Throws(() => + assembly.SaveJson(path, null, progress, cancellation.Token)); + } + finally + { + if (File.Exists(path)) File.Delete(path); + } + } + [Fact] public async Task SaveJsonAsync_CanceledToken_IsCanceled() { @@ -77,4 +117,9 @@ private sealed class CollectingProgress : IProgress public void Report(NwcProgress value) => Reports.Add(value); } + + private sealed class CancellingProgress(CancellationTokenSource cancellation) : IProgress + { + public void Report(NwcProgress value) => cancellation.Cancel(); + } } From 4ae81a6a8c1b9853885fe87b74a8e9f947cfa5f1 Mon Sep 17 00:00:00 2001 From: sim Date: Mon, 16 Nov 2026 15:40:19 +0300 Subject: [PATCH 3/5] fix(export): keep the existing JSON file until the export completes Assembly SaveJson writes to a sibling temporary file and moves it over the target only after success; mid-export cancellation no longer truncates the old file, and the progress test asserts the file content. --- src/NwcIO/Model/NwcAssembly.cs | 19 +++++++++++++++++-- tests/NwcIO.Tests/ExportCancellationTests.cs | 3 +++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/NwcIO/Model/NwcAssembly.cs b/src/NwcIO/Model/NwcAssembly.cs index 721aac5..1f0f276 100644 --- a/src/NwcIO/Model/NwcAssembly.cs +++ b/src/NwcIO/Model/NwcAssembly.cs @@ -810,7 +810,22 @@ public void SaveJson(string path, NwcJsonExportOptions? options, IProgress(() => assembly.SaveJson(path, null, progress, cancellation.Token)); + + Assert.Equal("keep me", File.ReadAllText(path)); } finally { From 6c16545544a80ecf3e821785a732f52af3687829 Mon Sep 17 00:00:00 2001 From: sim Date: Mon, 16 Nov 2026 23:54:31 +0300 Subject: [PATCH 4/5] fix(export): cover cancellation overloads with tests that fail without the token plumbing --- tests/NwcIO.Tests/ExportCancellationTests.cs | 83 +++++++++++++++++++- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/tests/NwcIO.Tests/ExportCancellationTests.cs b/tests/NwcIO.Tests/ExportCancellationTests.cs index f586365..52ae6cf 100644 --- a/tests/NwcIO.Tests/ExportCancellationTests.cs +++ b/tests/NwcIO.Tests/ExportCancellationTests.cs @@ -93,14 +93,72 @@ public void AssemblySaveJson_ProgressCanCancelMidExport() } [Fact] - public async Task SaveJsonAsync_CanceledToken_IsCanceled() + public void AssemblySaveJson_ProgressCancelsBeforeWrite_WritesNothing() + { + var assembly = NwcAssembly.Create("Fed", Document()); + using var cancellation = new CancellationTokenSource(); + using var stream = new MemoryStream(); + + Assert.Throws(() => + assembly.SaveJson(stream, null, new CancellingProgress(cancellation), cancellation.Token)); + + Assert.Equal(0, stream.Length); + } + + [Fact] + public void AssemblyBcfStream_ProgressCancelsBeforeWrite_WritesNothing() + { + var assembly = NwcAssembly.Create("Fed", Document()); + using var cancellation = new CancellationTokenSource(); + using var stream = new MemoryStream(); + + Assert.Throws(() => + assembly.SaveBcf(stream, [], new CancellingProgress(cancellation), cancellation.Token)); + + Assert.Equal(0, stream.Length); + } + + [Fact] + public void AssemblyBcfAndHtmlFiles_ProgressCancelsBeforeWrite_CreateNoFile() + { + var assembly = NwcAssembly.Create("Fed", Document()); + string bcf = Path.Combine(Path.GetTempPath(), $"nwcio-{Guid.NewGuid():N}.bcfzip"); + string html = Path.Combine(Path.GetTempPath(), $"nwcio-{Guid.NewGuid():N}.html"); + try + { + using (var cancellation = new CancellationTokenSource()) + { + Assert.Throws(() => + assembly.SaveBcf(bcf, [], new CancellingProgress(cancellation), cancellation.Token)); + } + + using (var cancellation = new CancellationTokenSource()) + { + Assert.Throws(() => + assembly.SaveClashReportHtml(html, [], new CancellingProgress(cancellation), cancellation.Token)); + } + + Assert.False(File.Exists(bcf)); + Assert.False(File.Exists(html)); + } + finally + { + if (File.Exists(bcf)) File.Delete(bcf); + if (File.Exists(html)) File.Delete(html); + } + } + + [Fact] + public async Task SaveJsonAsync_CanceledWhileWriting_IsCanceled() { var document = Document(); using var cancellation = new CancellationTokenSource(); - cancellation.Cancel(); + using var stream = new CancelOnWriteStream(cancellation); await Assert.ThrowsAnyAsync(() => - document.SaveJsonAsync(Path.GetTempFileName(), cancellationToken: cancellation.Token)); + document.SaveJsonAsync(stream, cancellationToken: cancellation.Token)); + + Assert.True(stream.Written); } private static NwcDocument Document() => NwcDocument.Create(meshes: @@ -125,4 +183,23 @@ private sealed class CancellingProgress(CancellationTokenSource cancellation) : { public void Report(NwcProgress value) => cancellation.Cancel(); } + + private sealed class CancelOnWriteStream(CancellationTokenSource cancellation) : MemoryStream + { + public bool Written { get; private set; } + + public override void Write(byte[] buffer, int offset, int count) + { + Written = true; + cancellation.Cancel(); + base.Write(buffer, offset, count); + } + + public override void Write(ReadOnlySpan buffer) + { + Written = true; + cancellation.Cancel(); + base.Write(buffer); + } + } } From 45f50abaabe6cf2d70d42357a4d598034a27bc73 Mon Sep 17 00:00:00 2001 From: sim Date: Mon, 16 Nov 2026 23:55:26 +0300 Subject: [PATCH 5/5] refactor(export): limit cancellation overloads to .nwc and JSON export --- CHANGELOG.md | 2 +- docs/public-api.md | 2 +- src/NwcIO/Model/NwcAssembly.cs | 83 --------------- src/NwcIO/NwcDocument.cs | 100 ++----------------- tests/NwcIO.Tests/ExportCancellationTests.cs | 57 ++--------- 5 files changed, 20 insertions(+), 224 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60c70ca..a608810 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 -- Cancellation and progress overloads for document and assembly exports: `.nwc`, GLB, IFC, OBJ, JSON, BCF, viewpoints XML and HTML. Existing `Save*` signatures are unchanged; the new overloads report `export-*` phases and check the token before and after the exporter. `Save*Async` wrappers now pass the token into those overloads. +- Cancellation and progress overloads for `.nwc` and JSON export: `NwcDocument.Save`, `NwcDocument.SaveJson` and `NwcAssembly.SaveJson` (path and stream). Existing signatures are unchanged; the new overloads report `export-nwc` and `export-json` phases and check the token before and after the exporter. `SaveAsync` and `SaveJsonAsync` now pass the token into them. `NwcAssembly.SaveJson(path, ...)` writes a temporary file and moves it over the target, so a cancelled export keeps an existing file. - Cancellation and progress overloads for `NwcDocument.Open`, `NwcAssembly.LoadFromDirectory` and `NwcAssembly.OpenNwf`, plus `OpenAsync`, `LoadFromDirectoryAsync`, `OpenNwfAsync` and `Save*Async` wrappers. `NwcProgress` reports phases such as `read-container`, `read-geometry`, `open` and per-model `load-models` counts. - `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 61d20ee..6d305bd 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. | -| `NwcProgress` | Progress payload for the cancellation-aware reader and exporter overloads: a phase name (`read-container`, `read-geometry`, `open`, `load-models`, `export-glb`, `export-ifc`, `export-obj`, `export-json`, `export-bcf`, `export-html`), completed units, and total units (0 when unknown). | +| `NwcProgress` | Progress payload for the cancellation-aware reader and exporter overloads: a phase name (`read-container`, `read-geometry`, `open`, `load-models`, `export-nwc`, `export-json`), 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, 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. | diff --git a/src/NwcIO/Model/NwcAssembly.cs b/src/NwcIO/Model/NwcAssembly.cs index 1f0f276..6b97cc1 100644 --- a/src/NwcIO/Model/NwcAssembly.cs +++ b/src/NwcIO/Model/NwcAssembly.cs @@ -501,44 +501,20 @@ public void ExportClashMatrixBcf(string path) /// Exports the entire federated assembly to a binary glTF 2.0 file (.glb). public void SaveGlb(string path, bool worldCoordinates = true) - => SaveGlb(path, worldCoordinates, null, default); - - /// Exports the assembly GLB with cancellation and progress. - public void SaveGlb(string path, bool worldCoordinates, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-glb", 0, 1)); GlbExporter.WriteFile(AllMeshes, AllMaterials, path, worldCoordinates, "NwcIO Assembly", SavedViewpoints); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-glb", 1, 1)); } /// Exports the entire federated assembly to a Wavefront OBJ file. public void SaveObj(string path, bool worldCoordinates = true) - => SaveObj(path, worldCoordinates, null, default); - - /// Exports the assembly OBJ with cancellation and progress. - public void SaveObj(string path, bool worldCoordinates, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-obj", 0, 1)); ObjExporter.WriteFile(AllMeshes, path, worldCoordinates, Name); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-obj", 1, 1)); } /// Exports the entire federated assembly with geometry, materials, and spatial hierarchy to an IFC2x3 STEP file (.ifc). public void SaveIfc(string path, NwcIfcExportOptions? options = null) - => SaveIfc(path, options, null, default); - - /// Exports the assembly IFC with cancellation and progress. - public void SaveIfc(string path, NwcIfcExportOptions? options, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-ifc", 0, 1)); IfcExporter.WriteAssembly(this, path, options); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-ifc", 1, 1)); } /// Exports the entire federated assembly with geometry, materials, and spatial hierarchy to a in IFC2x3 format. @@ -553,36 +529,18 @@ public void SaveIfc(TextWriter writer, NwcIfcExportOptions? options = null) /// Destination .bcf or .bcfzip file path. /// Optional pre-computed clash list; if null, is evaluated. public void SaveBcf(string path, IReadOnlyList? clashes = null) - => SaveBcf(path, clashes, null, default); - - /// Exports clashes to BCF with cancellation and progress. - public void SaveBcf(string path, IReadOnlyList? clashes, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-bcf", 0, 1)); clashes ??= FindClashes(); - cancellationToken.ThrowIfCancellationRequested(); BcfExporter.WriteFile(clashes, path, Name); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-bcf", 1, 1)); } /// /// Exports detected clashes across models in the assembly to a standard buildingSMART BCF 2.1 zip stream. /// public void SaveBcf(Stream stream, IReadOnlyList? clashes = null) - => SaveBcf(stream, clashes, null, default); - - /// Exports clashes to a BCF stream with cancellation and progress. - public void SaveBcf(Stream stream, IReadOnlyList? clashes, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-bcf", 0, 1)); clashes ??= FindClashes(); - cancellationToken.ThrowIfCancellationRequested(); BcfExporter.Write(clashes, stream, Name); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-bcf", 1, 1)); } /// @@ -591,51 +549,27 @@ public void SaveBcf(Stream stream, IReadOnlyList? clashes, IProgressDestination .bcf or .bcfzip file path. /// Optional pre-computed viewpoints list; if null, is used. public void SaveViewpointsBcf(string path, IReadOnlyList? viewpoints = null) - => SaveViewpointsBcf(path, viewpoints, null, default); - - /// Exports assembly viewpoints to BCF with cancellation and progress. - public void SaveViewpointsBcf(string path, IReadOnlyList? viewpoints, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-bcf", 0, 1)); viewpoints ??= SavedViewpoints; BcfExporter.WriteViewpointsFile(viewpoints, path, Name); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-bcf", 1, 1)); } /// /// Exports saved viewpoints of the assembly to a buildingSMART BCF 2.1 zip stream. /// public void SaveViewpointsBcf(Stream stream, IReadOnlyList? viewpoints = null) - => SaveViewpointsBcf(stream, viewpoints, null, default); - - /// Exports assembly viewpoints to a BCF stream with cancellation and progress. - public void SaveViewpointsBcf(Stream stream, IReadOnlyList? viewpoints, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-bcf", 0, 1)); viewpoints ??= SavedViewpoints; BcfExporter.WriteViewpoints(viewpoints, stream, Name); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-bcf", 1, 1)); } /// /// Exports saved viewpoints of the assembly to an Autodesk Navisworks XML viewpoints file. /// public void SaveViewpointsXml(string path, IReadOnlyList? viewpoints = null, string units = "m") - => SaveViewpointsXml(path, viewpoints, units, null, default); - - /// Exports assembly viewpoints XML with cancellation and progress. - public void SaveViewpointsXml(string path, IReadOnlyList? viewpoints, string units, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-viewpoints-xml", 0, 1)); viewpoints ??= SavedViewpoints; Sectioning.NwcXmlViewpointSerializer.Save(path, viewpoints, units); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-viewpoints-xml", 1, 1)); } /// @@ -653,18 +587,9 @@ public string ToViewpointsXml(IReadOnlyList? viewpoints = nul /// Destination .html file path. /// Optional pre-computed clash list; if null, is evaluated. public void SaveClashReportHtml(string path, IReadOnlyList? clashes = null) - => SaveClashReportHtml(path, clashes, null, default); - - /// Exports the clash report HTML with cancellation and progress. - public void SaveClashReportHtml(string path, IReadOnlyList? clashes, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-html", 0, 1)); clashes ??= FindClashes(); - cancellationToken.ThrowIfCancellationRequested(); HtmlClashReportExporter.WriteFile(clashes, path, Name, Models.Count, AllMeshes.Count); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-html", 1, 1)); } /// @@ -680,16 +605,8 @@ public void SaveClashReportHtml(TextWriter writer, IReadOnlyList? clas /// Exports the federated assembly to a standalone interactive 3D WebGL HTML viewer (.html) with zero external dependencies. /// public void SaveHtmlViewer(string path, Viewer.NwcHtmlViewerOptions? options = null) - => SaveHtmlViewer(path, options, null, default); - - /// Exports the assembly HTML viewer with cancellation and progress. - public void SaveHtmlViewer(string path, Viewer.NwcHtmlViewerOptions? options, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-html", 0, 1)); Viewer.NwcHtmlViewerExporter.Save(path, this, options); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-html", 1, 1)); } /// diff --git a/src/NwcIO/NwcDocument.cs b/src/NwcIO/NwcDocument.cs index d4a1839..cd4e630 100644 --- a/src/NwcIO/NwcDocument.cs +++ b/src/NwcIO/NwcDocument.cs @@ -593,86 +593,38 @@ public byte[] ToByteArray(string? header = null) /// Exports 3D mesh geometry to a Wavefront OBJ file. public void SaveObj(string path, bool worldCoordinates = true) - => SaveObj(path, worldCoordinates, null, default); - - /// Exports OBJ with cancellation and progress. - public void SaveObj(string path, bool worldCoordinates, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-obj", 0, 1)); Internal.Export.ObjExporter.WriteFile(Meshes, path, worldCoordinates, FileInfo.SourcePath); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-obj", 1, 1)); } /// Exports 3D mesh geometry to a in Wavefront OBJ format. public void SaveObj(TextWriter writer, bool worldCoordinates = true) - => SaveObj(writer, worldCoordinates, null, default); - - /// Exports OBJ to a writer with cancellation and progress. - public void SaveObj(TextWriter writer, bool worldCoordinates, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-obj", 0, 1)); Internal.Export.ObjExporter.Write(Meshes, writer, worldCoordinates, FileInfo.SourcePath); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-obj", 1, 1)); } /// Exports 3D mesh geometry, materials, and scene hierarchy to a binary glTF 2.0 file (.glb). public void SaveGlb(string path, bool worldCoordinates = true) - => SaveGlb(path, worldCoordinates, null, default); - - /// Exports GLB with cancellation and progress. - public void SaveGlb(string path, bool worldCoordinates, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-glb", 0, 1)); Internal.Export.GlbExporter.WriteFile(Meshes, Materials, path, worldCoordinates, FileInfo.LoaderPlugin, SavedViewpoints); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-glb", 1, 1)); } /// Exports 3D mesh geometry, materials, and scene hierarchy to a binary glTF 2.0 stream (.glb). public void SaveGlb(Stream stream, bool worldCoordinates = true) - => SaveGlb(stream, worldCoordinates, null, default); - - /// Exports GLB to a stream with cancellation and progress. - public void SaveGlb(Stream stream, bool worldCoordinates, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-glb", 0, 1)); Internal.Export.GlbExporter.Write(Meshes, Materials, stream, worldCoordinates, FileInfo.LoaderPlugin, SavedViewpoints); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-glb", 1, 1)); } /// Exports 3D mesh geometry, materials, spatial structure, and properties to an IFC STEP physical file (.ifc). public void SaveIfc(string path, NwcIfcExportOptions? options = null) - => SaveIfc(path, options, null, default); - - /// Exports IFC with cancellation and progress. - public void SaveIfc(string path, NwcIfcExportOptions? options, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-ifc", 0, 1)); Internal.Export.IfcExporter.WriteFile(this, path, options); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-ifc", 1, 1)); } /// Exports 3D mesh geometry, materials, spatial structure, and properties to a in IFC format. public void SaveIfc(TextWriter writer, NwcIfcExportOptions? options = null) - => SaveIfc(writer, options, null, default); - - /// Exports IFC to a writer with cancellation and progress. - public void SaveIfc(TextWriter writer, NwcIfcExportOptions? options, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-ifc", 0, 1)); Internal.Export.IfcExporter.Write(this, writer, options); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-ifc", 1, 1)); } /// Serializes 3D mesh geometry, materials, spatial structure, and properties to an IFC STEP physical string. @@ -685,44 +637,20 @@ public string ToIfcString(NwcIfcExportOptions? options = null) /// Exports saved viewpoints to a buildingSMART BCF 2.1 zip archive (.bcfzip). public void SaveViewpointsBcf(string path) - => SaveViewpointsBcf(path, null, default); - - /// Exports viewpoints BCF with cancellation and progress. - public void SaveViewpointsBcf(string path, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-bcf", 0, 1)); Internal.Export.BcfExporter.WriteViewpointsFile(SavedViewpoints, path, Path.GetFileNameWithoutExtension(FileInfo.SourcePath ?? "Model")); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-bcf", 1, 1)); } /// Exports saved viewpoints to a buildingSMART BCF 2.1 zip stream. public void SaveViewpointsBcf(Stream stream) - => SaveViewpointsBcf(stream, null, default); - - /// Exports viewpoints BCF to a stream with cancellation and progress. - public void SaveViewpointsBcf(Stream stream, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-bcf", 0, 1)); Internal.Export.BcfExporter.WriteViewpoints(SavedViewpoints, stream, Path.GetFileNameWithoutExtension(FileInfo.SourcePath ?? "Model")); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-bcf", 1, 1)); } /// Exports saved viewpoints, cameras, clipping planes, and redlines to an Autodesk Navisworks XML viewpoints file. public void SaveViewpointsXml(string path, string units = "m") - => SaveViewpointsXml(path, units, null, default); - - /// Exports viewpoints XML with cancellation and progress. - public void SaveViewpointsXml(string path, string units, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-viewpoints-xml", 0, 1)); Model.Sectioning.NwcXmlViewpointSerializer.Save(path, SavedViewpoints, units); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-viewpoints-xml", 1, 1)); } /// Serializes saved viewpoints, cameras, clipping planes, and redlines to an Autodesk Navisworks XML viewpoints string. @@ -775,16 +703,8 @@ public string ToJson(NwcJsonExportOptions? options = null) /// Exports the document to a standalone interactive 3D WebGL HTML viewer (.html) with zero external dependencies. /// public void SaveHtmlViewer(string path, Model.Viewer.NwcHtmlViewerOptions? options = null) - => SaveHtmlViewer(path, options, null, default); - - /// Exports the HTML viewer with cancellation and progress. - public void SaveHtmlViewer(string path, Model.Viewer.NwcHtmlViewerOptions? options, IProgress? progress, CancellationToken cancellationToken) { - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-html", 0, 1)); Model.Viewer.NwcHtmlViewerExporter.Save(path, this, options); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new NwcProgress("export-html", 1, 1)); } /// @@ -828,43 +748,43 @@ public Task SaveJsonAsync(Stream stream, NwcJsonExportOptions? options = null, C /// Saves OBJ asynchronously. public Task SaveObjAsync(string path, bool worldCoordinates = true, CancellationToken cancellationToken = default) - => Task.Run(() => SaveObj(path, worldCoordinates, null, cancellationToken), cancellationToken); + => Task.Run(() => SaveObj(path, worldCoordinates), cancellationToken); /// Saves OBJ to a text writer asynchronously. public Task SaveObjAsync(TextWriter writer, bool worldCoordinates = true, CancellationToken cancellationToken = default) - => Task.Run(() => SaveObj(writer, worldCoordinates, null, cancellationToken), cancellationToken); + => Task.Run(() => SaveObj(writer, worldCoordinates), cancellationToken); /// Saves GLB asynchronously. public Task SaveGlbAsync(string path, bool worldCoordinates = true, CancellationToken cancellationToken = default) - => Task.Run(() => SaveGlb(path, worldCoordinates, null, cancellationToken), cancellationToken); + => Task.Run(() => SaveGlb(path, worldCoordinates), cancellationToken); /// Saves GLB to a stream asynchronously. public Task SaveGlbAsync(Stream stream, bool worldCoordinates = true, CancellationToken cancellationToken = default) - => Task.Run(() => SaveGlb(stream, worldCoordinates, null, cancellationToken), cancellationToken); + => Task.Run(() => SaveGlb(stream, worldCoordinates), cancellationToken); /// Saves IFC asynchronously. public Task SaveIfcAsync(string path, NwcIfcExportOptions? options = null, CancellationToken cancellationToken = default) - => Task.Run(() => SaveIfc(path, options, null, cancellationToken), cancellationToken); + => Task.Run(() => SaveIfc(path, options), cancellationToken); /// Saves IFC to a text writer asynchronously. public Task SaveIfcAsync(TextWriter writer, NwcIfcExportOptions? options = null, CancellationToken cancellationToken = default) - => Task.Run(() => SaveIfc(writer, options, null, cancellationToken), cancellationToken); + => Task.Run(() => SaveIfc(writer, options), cancellationToken); /// Saves viewpoints as a BCF archive asynchronously. public Task SaveViewpointsBcfAsync(string path, CancellationToken cancellationToken = default) - => Task.Run(() => SaveViewpointsBcf(path, null, cancellationToken), cancellationToken); + => Task.Run(() => SaveViewpointsBcf(path), cancellationToken); /// Saves viewpoints as a BCF stream asynchronously. public Task SaveViewpointsBcfAsync(Stream stream, CancellationToken cancellationToken = default) - => Task.Run(() => SaveViewpointsBcf(stream, null, cancellationToken), cancellationToken); + => Task.Run(() => SaveViewpointsBcf(stream), cancellationToken); /// Saves viewpoints as Navisworks XML asynchronously. public Task SaveViewpointsXmlAsync(string path, string units = "m", CancellationToken cancellationToken = default) - => Task.Run(() => SaveViewpointsXml(path, units, null, cancellationToken), cancellationToken); + => Task.Run(() => SaveViewpointsXml(path, units), cancellationToken); /// Saves the HTML viewer asynchronously. public Task SaveHtmlViewerAsync(string path, Model.Viewer.NwcHtmlViewerOptions? options = null, CancellationToken cancellationToken = default) - => Task.Run(() => SaveHtmlViewer(path, options, null, cancellationToken), cancellationToken); + => Task.Run(() => SaveHtmlViewer(path, options), cancellationToken); /// /// Populates clash grid location tags across all clash detective tests in this document using the provided or attached grid system. diff --git a/tests/NwcIO.Tests/ExportCancellationTests.cs b/tests/NwcIO.Tests/ExportCancellationTests.cs index 52ae6cf..1c4f056 100644 --- a/tests/NwcIO.Tests/ExportCancellationTests.cs +++ b/tests/NwcIO.Tests/ExportCancellationTests.cs @@ -13,7 +13,7 @@ public void DocumentExports_CanceledToken_ThrowOperationCanceled() cancellation.Cancel(); Assert.Throws(() => - document.SaveGlb(Path.GetTempFileName(), true, null, cancellation.Token)); + document.Save(Path.GetTempFileName(), null, null, cancellation.Token)); Assert.Throws(() => document.SaveJson(Path.GetTempFileName(), null, null, cancellation.Token)); } @@ -27,10 +27,10 @@ public void DocumentExports_ReportProgress() document.SaveJson(jsonStream, null, json, CancellationToken.None); Assert.Contains(json.Reports, p => p.Phase == "export-json" && p.Completed == 1 && p.Total == 1); - var glb = new CollectingProgress(); - using var glbStream = new MemoryStream(); - document.SaveGlb(glbStream, true, glb, CancellationToken.None); - Assert.Contains(glb.Reports, p => p.Phase == "export-glb" && p.Completed == 1 && p.Total == 1); + var nwc = new CollectingProgress(); + using var nwcStream = new MemoryStream(); + document.Save(nwcStream, null, nwc, CancellationToken.None); + Assert.Contains(nwc.Reports, p => p.Phase == "export-nwc" && p.Completed == 1 && p.Total == 1); } [Fact] @@ -40,8 +40,10 @@ public void AssemblyExports_CancelAndReportProgress() var assembly = NwcAssembly.Create("Fed", document); using var cancellation = new CancellationTokenSource(); cancellation.Cancel(); + string path = Path.Combine(Path.GetTempPath(), $"nwcio-{Guid.NewGuid():N}.json"); Assert.Throws(() => - assembly.SaveGlb(Path.GetTempFileName(), true, null, cancellation.Token)); + assembly.SaveJson(path, null, null, cancellation.Token)); + Assert.False(File.Exists(path)); var progress = new CollectingProgress(); using var stream = new MemoryStream(); @@ -105,49 +107,6 @@ public void AssemblySaveJson_ProgressCancelsBeforeWrite_WritesNothing() Assert.Equal(0, stream.Length); } - [Fact] - public void AssemblyBcfStream_ProgressCancelsBeforeWrite_WritesNothing() - { - var assembly = NwcAssembly.Create("Fed", Document()); - using var cancellation = new CancellationTokenSource(); - using var stream = new MemoryStream(); - - Assert.Throws(() => - assembly.SaveBcf(stream, [], new CancellingProgress(cancellation), cancellation.Token)); - - Assert.Equal(0, stream.Length); - } - - [Fact] - public void AssemblyBcfAndHtmlFiles_ProgressCancelsBeforeWrite_CreateNoFile() - { - var assembly = NwcAssembly.Create("Fed", Document()); - string bcf = Path.Combine(Path.GetTempPath(), $"nwcio-{Guid.NewGuid():N}.bcfzip"); - string html = Path.Combine(Path.GetTempPath(), $"nwcio-{Guid.NewGuid():N}.html"); - try - { - using (var cancellation = new CancellationTokenSource()) - { - Assert.Throws(() => - assembly.SaveBcf(bcf, [], new CancellingProgress(cancellation), cancellation.Token)); - } - - using (var cancellation = new CancellationTokenSource()) - { - Assert.Throws(() => - assembly.SaveClashReportHtml(html, [], new CancellingProgress(cancellation), cancellation.Token)); - } - - Assert.False(File.Exists(bcf)); - Assert.False(File.Exists(html)); - } - finally - { - if (File.Exists(bcf)) File.Delete(bcf); - if (File.Exists(html)) File.Delete(html); - } - } - [Fact] public async Task SaveJsonAsync_CanceledWhileWriting_IsCanceled() {