From fbf426a495493c6c5e3d57a68370ccd4d73ac68f Mon Sep 17 00:00:00 2001 From: mahach666 <79989386+mahach666@users.noreply.github.com> Date: Wed, 18 Nov 2026 03:03:13 +0300 Subject: [PATCH] Separate frozen convenience APIs from native file operations --- CONTRIBUTING.md | 8 + .../NwcAssembly.Compatibility.cs | 344 ++++++++++++++++++ .../NwcDocument.Compatibility.cs | 305 ++++++++++++++++ src/NwcIO/Internal/{Export => }/IfcGuid.cs | 2 +- src/NwcIO/Internal/Parsing/HierarchyParser.cs | 2 +- src/NwcIO/Model/Clash/NwcClashTypes.cs | 4 +- src/NwcIO/Model/Diff/NwcDiffTypes.cs | 2 +- src/NwcIO/Model/NwcAssembly.cs | 340 +---------------- src/NwcIO/NwcDocument.cs | 301 +-------------- tests/NwcIO.Tests/IfcExportTests.cs | 4 +- tests/NwcIO.Tests/NwcElementGuidTests.cs | 1 + 11 files changed, 667 insertions(+), 646 deletions(-) create mode 100644 src/NwcIO/Compatibility/NwcAssembly.Compatibility.cs create mode 100644 src/NwcIO/Compatibility/NwcDocument.Compatibility.cs rename src/NwcIO/Internal/{Export => }/IfcGuid.cs (99%) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index bac9936..b10e890 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -23,6 +23,14 @@ Report security vulnerabilities privately through the **Security** tab. - Warnings are treated as errors (`true`). - Zero native or third-party library dependencies for core reading and exporting functionality. +## Library boundaries + +The native reader, writer and JSON output follow the Navisworks file model. +The frozen convenience methods on `NwcDocument` and `NwcAssembly` are in +`src/NwcIO/Compatibility`. They keep their existing signatures and assembly +so applications can continue using them while replacements are developed. +Keep new native-format operations with the reader or writer. + ## Test Fixtures Tests requiring real Navisworks files are skipped automatically unless the environment variable `NWCIO_TEST_FILES` is set to a directory containing private test models. The repository ships no proprietary Navisworks models. diff --git a/src/NwcIO/Compatibility/NwcAssembly.Compatibility.cs b/src/NwcIO/Compatibility/NwcAssembly.Compatibility.cs new file mode 100644 index 0000000..b32d3c5 --- /dev/null +++ b/src/NwcIO/Compatibility/NwcAssembly.Compatibility.cs @@ -0,0 +1,344 @@ +using System.Text; +using NwcIO.Internal.Export; +using NwcIO.Model.Spatial; + +namespace NwcIO.Model; + +public sealed partial class NwcAssembly +{ + /// + /// Applies an appearance profiler across all contributing models in the federated assembly. + /// + /// The total number of scene nodes and meshes whose appearance was overridden. + public int ApplyAppearanceProfiler(Appearance.NwcAppearanceProfiler profiler) + { + ArgumentNullException.ThrowIfNull(profiler); + int total = 0; + for (int i = 0; i < Models.Count; i++) + { + total += Models[i].ApplyAppearanceProfiler(profiler); + } + return total; + } + + /// + /// Detects spatial clashes between meshes belonging to different models. Meshes whose world bounding boxes + /// come within of each other are reported; the overlap box is empty when they + /// only come close. + /// + public IReadOnlyList FindClashes(double tolerance = 0.0) + { + if (Models.Count < 2) + { + return Array.Empty(); + } + + var clashes = new List(); + var modelKeys = NwcElementGuid.ModelKeys(Models); + + // Build per-model spatial indexes + var modelIndexes = new (NwcDocument Doc, NwcSpatialIndex Index)[Models.Count]; + for (int i = 0; i < Models.Count; i++) + { + modelIndexes[i] = (Models[i], Models[i].SpatialIndex); + } + + // Cross-compare each pair of models (i < j) + for (int i = 0; i < Models.Count; i++) + { + var (docA, _) = modelIndexes[i]; + + for (int j = i + 1; j < Models.Count; j++) + { + var (docB, indexB) = modelIndexes[j]; + + for (int m = 0; m < docA.Meshes.Count; m++) + { + var meshA = docA.Meshes[m]; + var boxA = meshA.GetWorldBoundingBox(); + if (boxA.IsEmpty) continue; + + var expanded = tolerance > 0 + ? new BoundingBox3d( + boxA.Min - new Vector3d(tolerance, tolerance, tolerance), + boxA.Max + new Vector3d(tolerance, tolerance, tolerance)) + : boxA; + + var candidatesB = indexB.QueryBox(expanded); + for (int n = 0; n < candidatesB.Count; n++) + { + var meshB = candidatesB[n]; + var boxB = meshB.GetWorldBoundingBox(); + if (expanded.Intersects(boxB)) + { + var overlap = new BoundingBox3d( + new Vector3d( + Math.Max(boxA.Min.X, boxB.Min.X), + Math.Max(boxA.Min.Y, boxB.Min.Y), + Math.Max(boxA.Min.Z, boxB.Min.Z)), + new Vector3d( + Math.Min(boxA.Max.X, boxB.Max.X), + Math.Min(boxA.Max.Y, boxB.Max.Y), + Math.Min(boxA.Max.Z, boxB.Max.Z))); + if (overlap.IsEmpty) + { + // Near miss: point at the middle of the gap (or of the overlap on axes that overlap). + var point = new Vector3d( + (Math.Max(boxA.Min.X, boxB.Min.X) + Math.Min(boxA.Max.X, boxB.Max.X)) * 0.5, + (Math.Max(boxA.Min.Y, boxB.Min.Y) + Math.Min(boxA.Max.Y, boxB.Max.Y)) * 0.5, + (Math.Max(boxA.Min.Z, boxB.Min.Z) + Math.Min(boxA.Max.Z, boxB.Max.Z)) * 0.5); + overlap = new BoundingBox3d(point, point); + } + + clashes.Add(new NwcClash(docA, meshA, docB, meshB, overlap) + { + ModelKeyA = modelKeys[i], + ModelKeyB = modelKeys[j], + }); + } + } + } + } + } + + return clashes; + } + + /// Configured Clash Detective tests and results for this federated assembly. + public List ClashTests { get; } = []; + + /// Spatial grid system (axes and levels) associated with the federated assembly. + public Grids.NwcGridSystem? GridSystem { get; set; } + + /// + /// Executes full mesh-level triangle collision detection across all contributing models of the assembly. + /// + public Clash.NwcClashTest RunClashDetection(string testName = "Federated Clash Test", double tolerance = 0.001) + { + var test = new Clash.NwcClashTest(testName, Clash.NwcClashTestType.Hard, tolerance); + Clash.NwcClashDetector.Run(this, test); + ClashTests.Add(test); + return test; + } + + /// Exports detected clash results to Autodesk Navisworks Clash Report XML format. + public void ExportClashReportXml(string path) + { + Clash.NwcXmlClashSerializer.Save(path, ClashTests, Name); + } + + /// Exports detected clash results to a buildingSMART BCF 2.1 zip archive (.bcfzip). + public void ExportClashBcf(string path) + { + Clash.NwcClashBcfExporter.SaveTests(path, ClashTests, Name); + } + + /// Exports the 4D TimeLiner schedule to Navisworks XML exchange format (.xml). + public void ExportTimeLinerXml(string path) + { + if (TimeLiner is null) throw new InvalidOperationException("Assembly does not contain a TimeLiner schedule."); + Model.TimeLiner.NwcTimeLinerXmlSerializer.Save(TimeLiner, path); + } + + /// Loads or imports a 4D TimeLiner schedule from Navisworks XML exchange format (.xml). + public void ImportTimeLinerXml(string path) + { + TimeLiner = Model.TimeLiner.NwcTimeLinerXmlSerializer.Open(path); + } + + /// Exports the 4D TimeLiner schedule to Microsoft Project XML format (.xml). + public void ExportTimeLinerMsProject(string path) + { + if (TimeLiner is null) throw new InvalidOperationException("Assembly does not contain a TimeLiner schedule."); + Model.TimeLiner.NwcTimeLinerMsProjectXmlSerializer.Save(TimeLiner, path); + } + + /// Loads or imports a 4D TimeLiner schedule from Microsoft Project XML format (.xml). + public void ImportTimeLinerMsProject(string path) + { + TimeLiner = Model.TimeLiner.NwcTimeLinerMsProjectXmlSerializer.Open(path); + } + + /// Exports the 4D TimeLiner schedule to Primavera P6 XER format (.xer). + public void ExportTimeLinerPrimavera(string path) + { + if (TimeLiner is null) throw new InvalidOperationException("Assembly does not contain a TimeLiner schedule."); + Model.TimeLiner.NwcTimeLinerPrimaveraXerSerializer.Save(TimeLiner, path); + } + + /// Loads or imports a 4D TimeLiner schedule from Primavera P6 XER format (.xer). + public void ImportTimeLinerPrimavera(string path) + { + TimeLiner = Model.TimeLiner.NwcTimeLinerPrimaveraXerSerializer.Open(path); + } + + /// Exports the coordination clash matrix to JSON format. + public void ExportClashMatrixJson(string path, bool indent = true) + { + if (ClashMatrix is null) throw new InvalidOperationException("Assembly does not have a configured Clash Matrix."); + ClashMatrix.SaveJson(path, indent); + } + + /// Exports the coordination clash matrix summary to a Markdown table. + public void ExportClashMatrixMarkdown(string path) + { + if (ClashMatrix is null) throw new InvalidOperationException("Assembly does not have a configured Clash Matrix."); + File.WriteAllText(path, ClashMatrix.ToMarkdownTable(), Encoding.UTF8); + } + + /// Exports the coordination clash matrix conflicts to a buildingSMART BCF 2.1 zip archive. + public void ExportClashMatrixBcf(string path) + { + if (ClashMatrix is null) throw new InvalidOperationException("Assembly does not have a configured Clash Matrix."); + ClashMatrix.SaveBcf(path); + } + + /// Exports the entire federated assembly to a binary glTF 2.0 file (.glb). + public void SaveGlb(string path, bool worldCoordinates = true) + { + GlbExporter.WriteFile(AllMeshes, AllMaterials, path, worldCoordinates, "NwcIO Assembly", SavedViewpoints); + } + + /// Exports the entire federated assembly to a Wavefront OBJ file. + public void SaveObj(string path, bool worldCoordinates = true) + { + ObjExporter.WriteFile(AllMeshes, path, worldCoordinates, Name, GetModelLabel); + } + + /// 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) + { + IfcExporter.WriteAssembly(this, path, options); + } + + /// Exports the entire federated assembly with geometry, materials, and spatial hierarchy to a in IFC2x3 format. + public void SaveIfc(TextWriter writer, NwcIfcExportOptions? options = null) + { + IfcExporter.WriteAssembly(this, writer, options); + } + + /// + /// Exports detected clashes across models in the assembly to a standard buildingSMART BCF 2.1 zip archive (.bcfzip). + /// + /// Destination .bcf or .bcfzip file path. + /// Optional pre-computed clash list; if null, is evaluated. + public void SaveBcf(string path, IReadOnlyList? clashes = null) + { + clashes ??= FindClashes(); + BcfExporter.WriteFile(clashes, path, Name); + } + + /// + /// 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) + { + clashes ??= FindClashes(); + BcfExporter.Write(clashes, stream, Name); + } + + /// + /// Exports saved viewpoints of the assembly to a buildingSMART BCF 2.1 zip archive (.bcfzip). + /// + /// Destination .bcf or .bcfzip file path. + /// Optional pre-computed viewpoints list; if null, is used. + public void SaveViewpointsBcf(string path, IReadOnlyList? viewpoints = null) + { + viewpoints ??= SavedViewpoints; + BcfExporter.WriteViewpointsFile(viewpoints, path, Name); + } + + /// + /// Exports saved viewpoints of the assembly to a buildingSMART BCF 2.1 zip stream. + /// + public void SaveViewpointsBcf(Stream stream, IReadOnlyList? viewpoints = null) + { + viewpoints ??= SavedViewpoints; + BcfExporter.WriteViewpoints(viewpoints, stream, Name); + } + + /// + /// Exports saved viewpoints of the assembly to an Autodesk Navisworks XML viewpoints file. + /// + public void SaveViewpointsXml(string path, IReadOnlyList? viewpoints = null, string units = "m") + { + viewpoints ??= SavedViewpoints; + Sectioning.NwcXmlViewpointSerializer.Save(path, viewpoints, units); + } + + /// + /// Serializes saved viewpoints of the assembly to an Autodesk Navisworks XML viewpoints string. + /// + public string ToViewpointsXml(IReadOnlyList? viewpoints = null, string units = "m") + { + viewpoints ??= SavedViewpoints; + return Sectioning.NwcXmlViewpointSerializer.Serialize(viewpoints, units); + } + + /// + /// Exports an interactive, self-contained HTML clash detection report with a discipline clash matrix and filterable clash table. + /// + /// Destination .html file path. + /// Optional pre-computed clash list; if null, is evaluated. + public void SaveClashReportHtml(string path, IReadOnlyList? clashes = null) + { + clashes ??= FindClashes(); + HtmlClashReportExporter.WriteFile(clashes, path, Name, Models.Count, AllMeshes.Count); + } + + /// + /// Exports an interactive, self-contained HTML clash detection report to a . + /// + public void SaveClashReportHtml(TextWriter writer, IReadOnlyList? clashes = null) + { + clashes ??= FindClashes(); + HtmlClashReportExporter.Write(clashes, writer, Name, Models.Count, AllMeshes.Count); + } + + /// + /// 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) + { + Viewer.NwcHtmlViewerExporter.Save(path, this, options); + } + + /// + /// Compares this federated assembly (as baseline) against a revision assembly, detecting additions, deletions, coordinate shifts, and property changes. + /// + public Diff.NwcDiffReport Compare(NwcAssembly revision, Diff.NwcDiffOptions? options = null) => + Diff.NwcDiffEngine.Compare(this, revision, options); + + /// + /// Populates clash grid location tags across all clash tests in this assembly and its contributing models using the provided or attached grid system. + /// + public int AssignGridLocations(Grids.NwcGridSystem? gridSystem = null) + { + var grids = gridSystem ?? GridSystem; + if (grids is null) + { + foreach (var m in Models) + { + var extracted = m.ExtractGridSystem(); + if (extracted.Lines.Count > 0) + { + grids = extracted; + break; + } + } + } + grids ??= new Grids.NwcGridSystem("Assembly Grids"); + GridSystem = grids; + + int count = 0; + foreach (var test in ClashTests) + { + count += grids.AssignGridLocations(test); + } + foreach (var m in Models) + { + count += grids.AssignGridLocations(m); + } + return count; + } +} diff --git a/src/NwcIO/Compatibility/NwcDocument.Compatibility.cs b/src/NwcIO/Compatibility/NwcDocument.Compatibility.cs new file mode 100644 index 0000000..91726e0 --- /dev/null +++ b/src/NwcIO/Compatibility/NwcDocument.Compatibility.cs @@ -0,0 +1,305 @@ +using NwcIO.Model; + +namespace NwcIO; + +public sealed partial class NwcDocument +{ + /// Applies an appearance profiler to colorize the document based on property rules. + /// The total number of scene nodes and meshes whose appearance was overridden. + public int ApplyAppearanceProfiler(Model.Appearance.NwcAppearanceProfiler profiler) + { + ArgumentNullException.ThrowIfNull(profiler); + AppearanceProfiler = profiler; + return profiler.Apply(this); + } + + /// Exports the document appearance profiler rules to a Navisworks XML format (.xml). + public void ExportAppearanceProfilerXml(string path) + { + if (AppearanceProfiler is null) + throw new InvalidOperationException("Document does not contain an Appearance Profiler."); + Model.Appearance.NwcXmlAppearanceProfilerSerializer.Save(AppearanceProfiler, path); + } + + /// Exports the document appearance profiler rules to an XML stream. + public void ExportAppearanceProfilerXml(Stream stream) + { + if (AppearanceProfiler is null) + throw new InvalidOperationException("Document does not contain an Appearance Profiler."); + Model.Appearance.NwcXmlAppearanceProfilerSerializer.Write(AppearanceProfiler, stream); + } + + /// Exports the document selection sets and search sets to Navisworks XML format. + public void ExportSelectionSetsXml(string path) => Model.Sets.NwcXmlSetSerializer.Save(SelectionSets, path); + + /// Exports the document selection sets and search sets to an XML stream. + public void ExportSelectionSetsXml(Stream stream) => Model.Sets.NwcXmlSetSerializer.Write(SelectionSets, stream); + + /// Finds a selection set or search set by name in the document sets hierarchy. + public Model.Sets.NwcSetItem? FindSet(string name) + { + ArgumentNullException.ThrowIfNull(name); + return FindSetRecursive(SelectionSets, name); + } + + private static Model.Sets.NwcSetItem? FindSetRecursive(IEnumerable items, string name) + { + foreach (var item in items) + { + if (string.Equals(item.Name, name, StringComparison.OrdinalIgnoreCase)) + { + return item; + } + if (item is Model.Sets.NwcSelectionSetFolder folder) + { + var found = FindSetRecursive(folder.Children, name); + if (found is not null) return found; + } + } + return null; + } + + /// Finds all scene graph nodes matching a search condition. + public List FindNodes(Model.Sets.NwcSearchCondition condition) + { + ArgumentNullException.ThrowIfNull(condition); + var result = new List(); + if (RootNode is null) return result; + + void Walk(NwcNode node) + { + if (condition.Evaluate(node)) result.Add(node); + for (int i = 0; i < node.Children.Count; i++) Walk(node.Children[i]); + } + + Walk(RootNode); + return result; + } + + /// Exports the 4D TimeLiner schedule to CSV format. + public void ExportTimeLinerCsv(string path) + { + if (TimeLiner is null) throw new InvalidOperationException("Document does not contain a TimeLiner schedule."); + Model.TimeLiner.NwcTimeLinerCsvSerializer.Save(TimeLiner, path); + } + + /// Exports the 4D TimeLiner schedule to a CSV stream. + public void ExportTimeLinerCsv(Stream stream) + { + if (TimeLiner is null) throw new InvalidOperationException("Document does not contain a TimeLiner schedule."); + Model.TimeLiner.NwcTimeLinerCsvSerializer.Write(TimeLiner, stream); + } + + /// Exports the 4D TimeLiner schedule to Navisworks XML exchange format (.xml). + public void ExportTimeLinerXml(string path) + { + if (TimeLiner is null) throw new InvalidOperationException("Document does not contain a TimeLiner schedule."); + Model.TimeLiner.NwcTimeLinerXmlSerializer.Save(TimeLiner, path); + } + + /// Exports the 4D TimeLiner schedule to a Navisworks XML exchange stream. + public void ExportTimeLinerXml(Stream stream) + { + if (TimeLiner is null) throw new InvalidOperationException("Document does not contain a TimeLiner schedule."); + Model.TimeLiner.NwcTimeLinerXmlSerializer.Write(TimeLiner, stream); + } + + /// Loads or imports a 4D TimeLiner schedule from Navisworks XML exchange format (.xml). + public void ImportTimeLinerXml(string path) + { + TimeLiner = Model.TimeLiner.NwcTimeLinerXmlSerializer.Open(path); + } + + /// Exports the 4D TimeLiner schedule to Microsoft Project XML format (.xml). + public void ExportTimeLinerMsProject(string path) + { + if (TimeLiner is null) throw new InvalidOperationException("Document does not contain a TimeLiner schedule."); + Model.TimeLiner.NwcTimeLinerMsProjectXmlSerializer.Save(TimeLiner, path); + } + + /// Exports the 4D TimeLiner schedule to a Microsoft Project XML stream. + public void ExportTimeLinerMsProject(Stream stream) + { + if (TimeLiner is null) throw new InvalidOperationException("Document does not contain a TimeLiner schedule."); + Model.TimeLiner.NwcTimeLinerMsProjectXmlSerializer.Write(TimeLiner, stream); + } + + /// Loads or imports a 4D TimeLiner schedule from Microsoft Project XML format (.xml). + public void ImportTimeLinerMsProject(string path) + { + TimeLiner = Model.TimeLiner.NwcTimeLinerMsProjectXmlSerializer.Open(path); + } + + /// Exports the 4D TimeLiner schedule to Primavera P6 XER format (.xer). + public void ExportTimeLinerPrimavera(string path) + { + if (TimeLiner is null) throw new InvalidOperationException("Document does not contain a TimeLiner schedule."); + Model.TimeLiner.NwcTimeLinerPrimaveraXerSerializer.Save(TimeLiner, path); + } + + /// Exports the 4D TimeLiner schedule to a Primavera P6 XER stream. + public void ExportTimeLinerPrimavera(Stream stream) + { + if (TimeLiner is null) throw new InvalidOperationException("Document does not contain a TimeLiner schedule."); + Model.TimeLiner.NwcTimeLinerPrimaveraXerSerializer.Write(TimeLiner, stream); + } + + /// Loads or imports a 4D TimeLiner schedule from Primavera P6 XER format (.xer). + public void ImportTimeLinerPrimavera(string path) + { + TimeLiner = Model.TimeLiner.NwcTimeLinerPrimaveraXerSerializer.Open(path); + } + + /// + /// Executes clash detection across the document using the specified or default test configuration. + /// + public Model.Clash.NwcClashTest RunClashDetection(Model.Clash.NwcClashTest? test = null) + { + test ??= new Model.Clash.NwcClashTest("Clash Test"); + Model.Clash.NwcClashDetector.Run(this, test); + if (!ClashTests.Contains(test)) ClashTests.Add(test); + return test; + } + + /// Exports detected clash results to Autodesk Navisworks Clash Report XML format. + public void ExportClashReportXml(string path, string? testName = null) + { + var tests = string.IsNullOrEmpty(testName) + ? ClashTests + : ClashTests.Where(t => string.Equals(t.Name, testName, StringComparison.OrdinalIgnoreCase)).ToList(); + Model.Clash.NwcXmlClashSerializer.Save(path, tests); + } + + /// Exports detected clash results to an XML stream. + public void ExportClashReportXml(Stream stream, string? testName = null) + { + var tests = string.IsNullOrEmpty(testName) + ? ClashTests + : ClashTests.Where(t => string.Equals(t.Name, testName, StringComparison.OrdinalIgnoreCase)).ToList(); + Model.Clash.NwcXmlClashSerializer.Write(stream, tests); + } + + /// Exports detected clash results to a buildingSMART BCF 2.1 zip archive (.bcfzip). + public void ExportClashBcf(string path, string? testName = null) + { + var tests = string.IsNullOrEmpty(testName) + ? ClashTests + : ClashTests.Where(t => string.Equals(t.Name, testName, StringComparison.OrdinalIgnoreCase)); + Model.Clash.NwcClashBcfExporter.SaveTests(path, tests, Path.GetFileNameWithoutExtension(FileInfo.SourcePath ?? "Model")); + } + + /// Exports detected clash results to a BCF 2.1 stream. + public void ExportClashBcf(Stream stream, string? testName = null) + { + var tests = string.IsNullOrEmpty(testName) + ? ClashTests + : ClashTests.Where(t => string.Equals(t.Name, testName, StringComparison.OrdinalIgnoreCase)); + Model.Clash.NwcClashBcfExporter.WriteTests(stream, tests, Path.GetFileNameWithoutExtension(FileInfo.SourcePath ?? "Model")); + } + + /// Loads clash detective tests and results from a Navisworks Clash Report XML file. + public List LoadClashReportXml(string path) + { + var tests = Model.Clash.NwcXmlClashSerializer.Open(path); + ClashTests.AddRange(tests); + return tests; + } + + /// Exports 3D mesh geometry to a Wavefront OBJ file. + public void SaveObj(string path, bool worldCoordinates = true) + { + Internal.Export.ObjExporter.WriteFile(Meshes, path, worldCoordinates, FileInfo.SourcePath); + } + + /// Exports 3D mesh geometry to a in Wavefront OBJ format. + public void SaveObj(TextWriter writer, bool worldCoordinates = true) + { + Internal.Export.ObjExporter.Write(Meshes, writer, worldCoordinates, FileInfo.SourcePath); + } + + /// Exports 3D mesh geometry, materials, and scene hierarchy to a binary glTF 2.0 file (.glb). + public void SaveGlb(string path, bool worldCoordinates = true) + { + Internal.Export.GlbExporter.WriteFile(Meshes, Materials, path, worldCoordinates, FileInfo.LoaderPlugin, SavedViewpoints); + } + + /// Exports 3D mesh geometry, materials, and scene hierarchy to a binary glTF 2.0 stream (.glb). + public void SaveGlb(Stream stream, bool worldCoordinates = true) + { + Internal.Export.GlbExporter.Write(Meshes, Materials, stream, worldCoordinates, FileInfo.LoaderPlugin, SavedViewpoints); + } + + /// Exports 3D mesh geometry, materials, spatial structure, and properties to an IFC STEP physical file (.ifc). + public void SaveIfc(string path, NwcIfcExportOptions? options = null) + { + Internal.Export.IfcExporter.WriteFile(this, path, options); + } + + /// Exports 3D mesh geometry, materials, spatial structure, and properties to a in IFC format. + public void SaveIfc(TextWriter writer, NwcIfcExportOptions? options = null) + { + Internal.Export.IfcExporter.Write(this, writer, options); + } + + /// Serializes 3D mesh geometry, materials, spatial structure, and properties to an IFC STEP physical string. + public string ToIfcString(NwcIfcExportOptions? options = null) + { + using var sw = new StringWriter(); + SaveIfc(sw, options); + return sw.ToString(); + } + + /// Exports saved viewpoints to a buildingSMART BCF 2.1 zip archive (.bcfzip). + public void SaveViewpointsBcf(string path) + { + Internal.Export.BcfExporter.WriteViewpointsFile(SavedViewpoints, path, Path.GetFileNameWithoutExtension(FileInfo.SourcePath ?? "Model")); + } + + /// Exports saved viewpoints to a buildingSMART BCF 2.1 zip stream. + public void SaveViewpointsBcf(Stream stream) + { + Internal.Export.BcfExporter.WriteViewpoints(SavedViewpoints, stream, Path.GetFileNameWithoutExtension(FileInfo.SourcePath ?? "Model")); + } + + /// Exports saved viewpoints, cameras, clipping planes, and redlines to an Autodesk Navisworks XML viewpoints file. + public void SaveViewpointsXml(string path, string units = "m") + { + Model.Sectioning.NwcXmlViewpointSerializer.Save(path, SavedViewpoints, units); + } + + /// Serializes saved viewpoints, cameras, clipping planes, and redlines to an Autodesk Navisworks XML viewpoints string. + public string ToViewpointsXml(string units = "m") + { + return Model.Sectioning.NwcXmlViewpointSerializer.Serialize(SavedViewpoints, units); + } + + /// + /// 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) + { + Model.Viewer.NwcHtmlViewerExporter.Save(path, this, options); + } + + /// + /// Compares this document (as baseline) against a revision document, detecting additions, deletions, coordinate shifts, and property changes. + /// + public Model.Diff.NwcDiffReport Compare(NwcDocument revision, Model.Diff.NwcDiffOptions? options = null) => + Model.Diff.NwcDiffEngine.Compare(this, revision, options); + + /// + /// Populates clash grid location tags across all clash detective tests in this document using the provided or attached grid system. + /// + public int AssignGridLocations(Model.Grids.NwcGridSystem? gridSystem = null) + { + var grids = gridSystem ?? GridSystem ?? ExtractGridSystem(); + GridSystem = grids; + return grids.AssignGridLocations(this); + } + + /// + /// Automatically extracts grid lines from scene hierarchy nodes and metadata. + /// + public Model.Grids.NwcGridSystem ExtractGridSystem() => + Model.Grids.NwcGridSystem.ExtractFromDocument(this); +} diff --git a/src/NwcIO/Internal/Export/IfcGuid.cs b/src/NwcIO/Internal/IfcGuid.cs similarity index 99% rename from src/NwcIO/Internal/Export/IfcGuid.cs rename to src/NwcIO/Internal/IfcGuid.cs index 92ae4f0..8e8cf25 100644 --- a/src/NwcIO/Internal/Export/IfcGuid.cs +++ b/src/NwcIO/Internal/IfcGuid.cs @@ -1,7 +1,7 @@ using System.Security.Cryptography; using System.Text; -namespace NwcIO.Internal.Export; +namespace NwcIO.Internal; /// /// IFC GlobalId encoding: 128-bit GUID to 22-character base64-like string. diff --git a/src/NwcIO/Internal/Parsing/HierarchyParser.cs b/src/NwcIO/Internal/Parsing/HierarchyParser.cs index 5ee477a..263b786 100644 --- a/src/NwcIO/Internal/Parsing/HierarchyParser.cs +++ b/src/NwcIO/Internal/Parsing/HierarchyParser.cs @@ -1,5 +1,5 @@ using System.Globalization; -using NwcIO.Internal.Export; +using NwcIO.Internal; using NwcIO.Model; namespace NwcIO.Internal.Parsing; diff --git a/src/NwcIO/Model/Clash/NwcClashTypes.cs b/src/NwcIO/Model/Clash/NwcClashTypes.cs index bdc9d22..8d7162c 100644 --- a/src/NwcIO/Model/Clash/NwcClashTypes.cs +++ b/src/NwcIO/Model/Clash/NwcClashTypes.cs @@ -1,5 +1,5 @@ using System.Globalization; -using NwcIO.Internal.Export; +using NwcIO.Internal; using NwcIO.Model.Sets; namespace NwcIO.Model.Clash; @@ -205,7 +205,7 @@ public NwcClashObject(string name, int? elementId = null, string? path = null) Path = path ?? name; if (elementId.HasValue) { - IfcGuid = Internal.Export.IfcGuid.FromElementId(elementId.Value, "NwcClash"); + IfcGuid = Internal.IfcGuid.FromElementId(elementId.Value, "NwcClash"); } } } diff --git a/src/NwcIO/Model/Diff/NwcDiffTypes.cs b/src/NwcIO/Model/Diff/NwcDiffTypes.cs index 0d7e277..8892eae 100644 --- a/src/NwcIO/Model/Diff/NwcDiffTypes.cs +++ b/src/NwcIO/Model/Diff/NwcDiffTypes.cs @@ -3,7 +3,7 @@ using System.Text; using System.Text.Json; using System.Xml; -using NwcIO.Internal.Export; +using NwcIO.Internal; namespace NwcIO.Model.Diff; diff --git a/src/NwcIO/Model/NwcAssembly.cs b/src/NwcIO/Model/NwcAssembly.cs index 6bd85b9..308feaf 100644 --- a/src/NwcIO/Model/NwcAssembly.cs +++ b/src/NwcIO/Model/NwcAssembly.cs @@ -1,5 +1,3 @@ -using System.Text; -using NwcIO.Internal.Export; using NwcIO.Internal.Json; using NwcIO.Model.Spatial; @@ -9,7 +7,7 @@ namespace NwcIO.Model; /// A federated multi-model BIM assembly combining multiple Navisworks documents /// (.nwc, .nwd, or .nwf external references) in a unified coordinate system. /// -public sealed class NwcAssembly +public sealed partial class NwcAssembly { private NwcSpatialIndex? _spatialIndex; private readonly Dictionary _meshModelIndexes; @@ -464,133 +462,6 @@ public static Task OpenNwfAsync( CancellationToken cancellationToken = default) => Task.Run(() => OpenNwf(nwfPath, options, progress, cancellationToken), cancellationToken); - /// - /// Applies an appearance profiler across all contributing models in the federated assembly. - /// - /// The total number of scene nodes and meshes whose appearance was overridden. - public int ApplyAppearanceProfiler(Appearance.NwcAppearanceProfiler profiler) - { - ArgumentNullException.ThrowIfNull(profiler); - int total = 0; - for (int i = 0; i < Models.Count; i++) - { - total += Models[i].ApplyAppearanceProfiler(profiler); - } - return total; - } - - /// - /// Detects spatial clashes between meshes belonging to different models. Meshes whose world bounding boxes - /// come within of each other are reported; the overlap box is empty when they - /// only come close. - /// - public IReadOnlyList FindClashes(double tolerance = 0.0) - { - if (Models.Count < 2) - { - return Array.Empty(); - } - - var clashes = new List(); - var modelKeys = NwcElementGuid.ModelKeys(Models); - - // Build per-model spatial indexes - var modelIndexes = new (NwcDocument Doc, NwcSpatialIndex Index)[Models.Count]; - for (int i = 0; i < Models.Count; i++) - { - modelIndexes[i] = (Models[i], Models[i].SpatialIndex); - } - - // Cross-compare each pair of models (i < j) - for (int i = 0; i < Models.Count; i++) - { - var (docA, _) = modelIndexes[i]; - - for (int j = i + 1; j < Models.Count; j++) - { - var (docB, indexB) = modelIndexes[j]; - - for (int m = 0; m < docA.Meshes.Count; m++) - { - var meshA = docA.Meshes[m]; - var boxA = meshA.GetWorldBoundingBox(); - if (boxA.IsEmpty) continue; - - var expanded = tolerance > 0 - ? new BoundingBox3d( - boxA.Min - new Vector3d(tolerance, tolerance, tolerance), - boxA.Max + new Vector3d(tolerance, tolerance, tolerance)) - : boxA; - - var candidatesB = indexB.QueryBox(expanded); - for (int n = 0; n < candidatesB.Count; n++) - { - var meshB = candidatesB[n]; - var boxB = meshB.GetWorldBoundingBox(); - if (expanded.Intersects(boxB)) - { - var overlap = new BoundingBox3d( - new Vector3d( - Math.Max(boxA.Min.X, boxB.Min.X), - Math.Max(boxA.Min.Y, boxB.Min.Y), - Math.Max(boxA.Min.Z, boxB.Min.Z)), - new Vector3d( - Math.Min(boxA.Max.X, boxB.Max.X), - Math.Min(boxA.Max.Y, boxB.Max.Y), - Math.Min(boxA.Max.Z, boxB.Max.Z))); - if (overlap.IsEmpty) - { - // Near miss: point at the middle of the gap (or of the overlap on axes that overlap). - var point = new Vector3d( - (Math.Max(boxA.Min.X, boxB.Min.X) + Math.Min(boxA.Max.X, boxB.Max.X)) * 0.5, - (Math.Max(boxA.Min.Y, boxB.Min.Y) + Math.Min(boxA.Max.Y, boxB.Max.Y)) * 0.5, - (Math.Max(boxA.Min.Z, boxB.Min.Z) + Math.Min(boxA.Max.Z, boxB.Max.Z)) * 0.5); - overlap = new BoundingBox3d(point, point); - } - - clashes.Add(new NwcClash(docA, meshA, docB, meshB, overlap) - { - ModelKeyA = modelKeys[i], - ModelKeyB = modelKeys[j], - }); - } - } - } - } - } - - return clashes; - } - - /// Configured Clash Detective tests and results for this federated assembly. - public List ClashTests { get; } = []; - - /// Spatial grid system (axes and levels) associated with the federated assembly. - public Grids.NwcGridSystem? GridSystem { get; set; } - - /// - /// Executes full mesh-level triangle collision detection across all contributing models of the assembly. - /// - public Clash.NwcClashTest RunClashDetection(string testName = "Federated Clash Test", double tolerance = 0.001) - { - var test = new Clash.NwcClashTest(testName, Clash.NwcClashTestType.Hard, tolerance); - Clash.NwcClashDetector.Run(this, test); - ClashTests.Add(test); - return test; - } - - /// Exports detected clash results to Autodesk Navisworks Clash Report XML format. - public void ExportClashReportXml(string path) - { - Clash.NwcXmlClashSerializer.Save(path, ClashTests, Name); - } - - /// Exports detected clash results to a buildingSMART BCF 2.1 zip archive (.bcfzip). - public void ExportClashBcf(string path) - { - Clash.NwcClashBcfExporter.SaveTests(path, ClashTests, Name); - } - /// Creates a Navisworks Federated Project Manifest (NWF) representation of this assembly. public Clash.NwcNwfManifest ToNwfManifest() { @@ -632,78 +503,6 @@ public void SaveNwf(string path) manifest.Save(path); } - /// Exports the 4D TimeLiner schedule to Navisworks XML exchange format (.xml). - public void ExportTimeLinerXml(string path) - { - if (TimeLiner is null) throw new InvalidOperationException("Assembly does not contain a TimeLiner schedule."); - Model.TimeLiner.NwcTimeLinerXmlSerializer.Save(TimeLiner, path); - } - - /// Loads or imports a 4D TimeLiner schedule from Navisworks XML exchange format (.xml). - public void ImportTimeLinerXml(string path) - { - TimeLiner = Model.TimeLiner.NwcTimeLinerXmlSerializer.Open(path); - } - - /// Exports the 4D TimeLiner schedule to Microsoft Project XML format (.xml). - public void ExportTimeLinerMsProject(string path) - { - if (TimeLiner is null) throw new InvalidOperationException("Assembly does not contain a TimeLiner schedule."); - Model.TimeLiner.NwcTimeLinerMsProjectXmlSerializer.Save(TimeLiner, path); - } - - /// Loads or imports a 4D TimeLiner schedule from Microsoft Project XML format (.xml). - public void ImportTimeLinerMsProject(string path) - { - TimeLiner = Model.TimeLiner.NwcTimeLinerMsProjectXmlSerializer.Open(path); - } - - /// Exports the 4D TimeLiner schedule to Primavera P6 XER format (.xer). - public void ExportTimeLinerPrimavera(string path) - { - if (TimeLiner is null) throw new InvalidOperationException("Assembly does not contain a TimeLiner schedule."); - Model.TimeLiner.NwcTimeLinerPrimaveraXerSerializer.Save(TimeLiner, path); - } - - /// Loads or imports a 4D TimeLiner schedule from Primavera P6 XER format (.xer). - public void ImportTimeLinerPrimavera(string path) - { - TimeLiner = Model.TimeLiner.NwcTimeLinerPrimaveraXerSerializer.Open(path); - } - - /// Exports the coordination clash matrix to JSON format. - public void ExportClashMatrixJson(string path, bool indent = true) - { - if (ClashMatrix is null) throw new InvalidOperationException("Assembly does not have a configured Clash Matrix."); - ClashMatrix.SaveJson(path, indent); - } - - /// Exports the coordination clash matrix summary to a Markdown table. - public void ExportClashMatrixMarkdown(string path) - { - if (ClashMatrix is null) throw new InvalidOperationException("Assembly does not have a configured Clash Matrix."); - File.WriteAllText(path, ClashMatrix.ToMarkdownTable(), Encoding.UTF8); - } - - /// Exports the coordination clash matrix conflicts to a buildingSMART BCF 2.1 zip archive. - public void ExportClashMatrixBcf(string path) - { - if (ClashMatrix is null) throw new InvalidOperationException("Assembly does not have a configured Clash Matrix."); - ClashMatrix.SaveBcf(path); - } - - /// Exports the entire federated assembly to a binary glTF 2.0 file (.glb). - public void SaveGlb(string path, bool worldCoordinates = true) - { - GlbExporter.WriteFile(AllMeshes, AllMaterials, path, worldCoordinates, "NwcIO Assembly", SavedViewpoints); - } - - /// Exports the entire federated assembly to a Wavefront OBJ file. - public void SaveObj(string path, bool worldCoordinates = true) - { - ObjExporter.WriteFile(AllMeshes, path, worldCoordinates, Name, GetModelLabel); - } - internal string? GetModelLabel(NwcMesh mesh) { int modelIndex = GetModelIndex(mesh); @@ -769,143 +568,6 @@ public void SaveNwc(string path) ToDocument().Save(path); } - /// 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) - { - IfcExporter.WriteAssembly(this, path, options); - } - - /// Exports the entire federated assembly with geometry, materials, and spatial hierarchy to a in IFC2x3 format. - public void SaveIfc(TextWriter writer, NwcIfcExportOptions? options = null) - { - IfcExporter.WriteAssembly(this, writer, options); - } - - /// - /// Exports detected clashes across models in the assembly to a standard buildingSMART BCF 2.1 zip archive (.bcfzip). - /// - /// Destination .bcf or .bcfzip file path. - /// Optional pre-computed clash list; if null, is evaluated. - public void SaveBcf(string path, IReadOnlyList? clashes = null) - { - clashes ??= FindClashes(); - BcfExporter.WriteFile(clashes, path, Name); - } - - /// - /// 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) - { - clashes ??= FindClashes(); - BcfExporter.Write(clashes, stream, Name); - } - - /// - /// Exports saved viewpoints of the assembly to a buildingSMART BCF 2.1 zip archive (.bcfzip). - /// - /// Destination .bcf or .bcfzip file path. - /// Optional pre-computed viewpoints list; if null, is used. - public void SaveViewpointsBcf(string path, IReadOnlyList? viewpoints = null) - { - viewpoints ??= SavedViewpoints; - BcfExporter.WriteViewpointsFile(viewpoints, path, Name); - } - - /// - /// Exports saved viewpoints of the assembly to a buildingSMART BCF 2.1 zip stream. - /// - public void SaveViewpointsBcf(Stream stream, IReadOnlyList? viewpoints = null) - { - viewpoints ??= SavedViewpoints; - BcfExporter.WriteViewpoints(viewpoints, stream, Name); - } - - /// - /// Exports saved viewpoints of the assembly to an Autodesk Navisworks XML viewpoints file. - /// - public void SaveViewpointsXml(string path, IReadOnlyList? viewpoints = null, string units = "m") - { - viewpoints ??= SavedViewpoints; - Sectioning.NwcXmlViewpointSerializer.Save(path, viewpoints, units); - } - - /// - /// Serializes saved viewpoints of the assembly to an Autodesk Navisworks XML viewpoints string. - /// - public string ToViewpointsXml(IReadOnlyList? viewpoints = null, string units = "m") - { - viewpoints ??= SavedViewpoints; - return Sectioning.NwcXmlViewpointSerializer.Serialize(viewpoints, units); - } - - /// - /// Exports an interactive, self-contained HTML clash detection report with a discipline clash matrix and filterable clash table. - /// - /// Destination .html file path. - /// Optional pre-computed clash list; if null, is evaluated. - public void SaveClashReportHtml(string path, IReadOnlyList? clashes = null) - { - clashes ??= FindClashes(); - HtmlClashReportExporter.WriteFile(clashes, path, Name, Models.Count, AllMeshes.Count); - } - - /// - /// Exports an interactive, self-contained HTML clash detection report to a . - /// - public void SaveClashReportHtml(TextWriter writer, IReadOnlyList? clashes = null) - { - clashes ??= FindClashes(); - HtmlClashReportExporter.Write(clashes, writer, Name, Models.Count, AllMeshes.Count); - } - - /// - /// 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) - { - Viewer.NwcHtmlViewerExporter.Save(path, this, options); - } - - /// - /// Compares this federated assembly (as baseline) against a revision assembly, detecting additions, deletions, coordinate shifts, and property changes. - /// - public Diff.NwcDiffReport Compare(NwcAssembly revision, Diff.NwcDiffOptions? options = null) => - Diff.NwcDiffEngine.Compare(this, revision, options); - - /// - /// Populates clash grid location tags across all clash tests in this assembly and its contributing models using the provided or attached grid system. - /// - public int AssignGridLocations(Grids.NwcGridSystem? gridSystem = null) - { - var grids = gridSystem ?? GridSystem; - if (grids is null) - { - foreach (var m in Models) - { - var extracted = m.ExtractGridSystem(); - if (extracted.Lines.Count > 0) - { - grids = extracted; - break; - } - } - } - grids ??= new Grids.NwcGridSystem("Assembly Grids"); - GridSystem = grids; - - int count = 0; - foreach (var test in ClashTests) - { - count += grids.AssignGridLocations(test); - } - foreach (var m in Models) - { - count += grids.AssignGridLocations(m); - } - return count; - } - /// 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); diff --git a/src/NwcIO/NwcDocument.cs b/src/NwcIO/NwcDocument.cs index 9e9f6df..bf6ee4d 100644 --- a/src/NwcIO/NwcDocument.cs +++ b/src/NwcIO/NwcDocument.cs @@ -7,7 +7,7 @@ namespace NwcIO; /// /// An opened Navisworks document (.nwc cache, .nwd document) parsed without Navisworks installed. /// -public sealed class NwcDocument +public sealed partial class NwcDocument { private NwcDocument( NwcFileInfo fileInfo, @@ -401,207 +401,6 @@ public static NwcDocument Create( return doc; } - /// Applies an appearance profiler to colorize the document based on property rules. - /// The total number of scene nodes and meshes whose appearance was overridden. - public int ApplyAppearanceProfiler(Model.Appearance.NwcAppearanceProfiler profiler) - { - ArgumentNullException.ThrowIfNull(profiler); - AppearanceProfiler = profiler; - return profiler.Apply(this); - } - - /// Exports the document appearance profiler rules to a Navisworks XML format (.xml). - public void ExportAppearanceProfilerXml(string path) - { - if (AppearanceProfiler is null) - throw new InvalidOperationException("Document does not contain an Appearance Profiler."); - Model.Appearance.NwcXmlAppearanceProfilerSerializer.Save(AppearanceProfiler, path); - } - - /// Exports the document appearance profiler rules to an XML stream. - public void ExportAppearanceProfilerXml(Stream stream) - { - if (AppearanceProfiler is null) - throw new InvalidOperationException("Document does not contain an Appearance Profiler."); - Model.Appearance.NwcXmlAppearanceProfilerSerializer.Write(AppearanceProfiler, stream); - } - - /// Exports the document selection sets and search sets to Navisworks XML format. - public void ExportSelectionSetsXml(string path) => Model.Sets.NwcXmlSetSerializer.Save(SelectionSets, path); - - /// Exports the document selection sets and search sets to an XML stream. - public void ExportSelectionSetsXml(Stream stream) => Model.Sets.NwcXmlSetSerializer.Write(SelectionSets, stream); - - /// Finds a selection set or search set by name in the document sets hierarchy. - public Model.Sets.NwcSetItem? FindSet(string name) - { - ArgumentNullException.ThrowIfNull(name); - return FindSetRecursive(SelectionSets, name); - } - - private static Model.Sets.NwcSetItem? FindSetRecursive(IEnumerable items, string name) - { - foreach (var item in items) - { - if (string.Equals(item.Name, name, StringComparison.OrdinalIgnoreCase)) - { - return item; - } - if (item is Model.Sets.NwcSelectionSetFolder folder) - { - var found = FindSetRecursive(folder.Children, name); - if (found is not null) return found; - } - } - return null; - } - - /// Finds all scene graph nodes matching a search condition. - public List FindNodes(Model.Sets.NwcSearchCondition condition) - { - ArgumentNullException.ThrowIfNull(condition); - var result = new List(); - if (RootNode is null) return result; - - void Walk(NwcNode node) - { - if (condition.Evaluate(node)) result.Add(node); - for (int i = 0; i < node.Children.Count; i++) Walk(node.Children[i]); - } - - Walk(RootNode); - return result; - } - - /// Exports the 4D TimeLiner schedule to CSV format. - public void ExportTimeLinerCsv(string path) - { - if (TimeLiner is null) throw new InvalidOperationException("Document does not contain a TimeLiner schedule."); - Model.TimeLiner.NwcTimeLinerCsvSerializer.Save(TimeLiner, path); - } - - /// Exports the 4D TimeLiner schedule to a CSV stream. - public void ExportTimeLinerCsv(Stream stream) - { - if (TimeLiner is null) throw new InvalidOperationException("Document does not contain a TimeLiner schedule."); - Model.TimeLiner.NwcTimeLinerCsvSerializer.Write(TimeLiner, stream); - } - - /// Exports the 4D TimeLiner schedule to Navisworks XML exchange format (.xml). - public void ExportTimeLinerXml(string path) - { - if (TimeLiner is null) throw new InvalidOperationException("Document does not contain a TimeLiner schedule."); - Model.TimeLiner.NwcTimeLinerXmlSerializer.Save(TimeLiner, path); - } - - /// Exports the 4D TimeLiner schedule to a Navisworks XML exchange stream. - public void ExportTimeLinerXml(Stream stream) - { - if (TimeLiner is null) throw new InvalidOperationException("Document does not contain a TimeLiner schedule."); - Model.TimeLiner.NwcTimeLinerXmlSerializer.Write(TimeLiner, stream); - } - - /// Loads or imports a 4D TimeLiner schedule from Navisworks XML exchange format (.xml). - public void ImportTimeLinerXml(string path) - { - TimeLiner = Model.TimeLiner.NwcTimeLinerXmlSerializer.Open(path); - } - - /// Exports the 4D TimeLiner schedule to Microsoft Project XML format (.xml). - public void ExportTimeLinerMsProject(string path) - { - if (TimeLiner is null) throw new InvalidOperationException("Document does not contain a TimeLiner schedule."); - Model.TimeLiner.NwcTimeLinerMsProjectXmlSerializer.Save(TimeLiner, path); - } - - /// Exports the 4D TimeLiner schedule to a Microsoft Project XML stream. - public void ExportTimeLinerMsProject(Stream stream) - { - if (TimeLiner is null) throw new InvalidOperationException("Document does not contain a TimeLiner schedule."); - Model.TimeLiner.NwcTimeLinerMsProjectXmlSerializer.Write(TimeLiner, stream); - } - - /// Loads or imports a 4D TimeLiner schedule from Microsoft Project XML format (.xml). - public void ImportTimeLinerMsProject(string path) - { - TimeLiner = Model.TimeLiner.NwcTimeLinerMsProjectXmlSerializer.Open(path); - } - - /// Exports the 4D TimeLiner schedule to Primavera P6 XER format (.xer). - public void ExportTimeLinerPrimavera(string path) - { - if (TimeLiner is null) throw new InvalidOperationException("Document does not contain a TimeLiner schedule."); - Model.TimeLiner.NwcTimeLinerPrimaveraXerSerializer.Save(TimeLiner, path); - } - - /// Exports the 4D TimeLiner schedule to a Primavera P6 XER stream. - public void ExportTimeLinerPrimavera(Stream stream) - { - if (TimeLiner is null) throw new InvalidOperationException("Document does not contain a TimeLiner schedule."); - Model.TimeLiner.NwcTimeLinerPrimaveraXerSerializer.Write(TimeLiner, stream); - } - - /// Loads or imports a 4D TimeLiner schedule from Primavera P6 XER format (.xer). - public void ImportTimeLinerPrimavera(string path) - { - TimeLiner = Model.TimeLiner.NwcTimeLinerPrimaveraXerSerializer.Open(path); - } - - /// - /// Executes clash detection across the document using the specified or default test configuration. - /// - public Model.Clash.NwcClashTest RunClashDetection(Model.Clash.NwcClashTest? test = null) - { - test ??= new Model.Clash.NwcClashTest("Clash Test"); - Model.Clash.NwcClashDetector.Run(this, test); - if (!ClashTests.Contains(test)) ClashTests.Add(test); - return test; - } - - /// Exports detected clash results to Autodesk Navisworks Clash Report XML format. - public void ExportClashReportXml(string path, string? testName = null) - { - var tests = string.IsNullOrEmpty(testName) - ? ClashTests - : ClashTests.Where(t => string.Equals(t.Name, testName, StringComparison.OrdinalIgnoreCase)).ToList(); - Model.Clash.NwcXmlClashSerializer.Save(path, tests); - } - - /// Exports detected clash results to an XML stream. - public void ExportClashReportXml(Stream stream, string? testName = null) - { - var tests = string.IsNullOrEmpty(testName) - ? ClashTests - : ClashTests.Where(t => string.Equals(t.Name, testName, StringComparison.OrdinalIgnoreCase)).ToList(); - Model.Clash.NwcXmlClashSerializer.Write(stream, tests); - } - - /// Exports detected clash results to a buildingSMART BCF 2.1 zip archive (.bcfzip). - public void ExportClashBcf(string path, string? testName = null) - { - var tests = string.IsNullOrEmpty(testName) - ? ClashTests - : ClashTests.Where(t => string.Equals(t.Name, testName, StringComparison.OrdinalIgnoreCase)); - Model.Clash.NwcClashBcfExporter.SaveTests(path, tests, Path.GetFileNameWithoutExtension(FileInfo.SourcePath ?? "Model")); - } - - /// Exports detected clash results to a BCF 2.1 stream. - public void ExportClashBcf(Stream stream, string? testName = null) - { - var tests = string.IsNullOrEmpty(testName) - ? ClashTests - : ClashTests.Where(t => string.Equals(t.Name, testName, StringComparison.OrdinalIgnoreCase)); - Model.Clash.NwcClashBcfExporter.WriteTests(stream, tests, Path.GetFileNameWithoutExtension(FileInfo.SourcePath ?? "Model")); - } - - /// Loads clash detective tests and results from a Navisworks Clash Report XML file. - public List LoadClashReportXml(string path) - { - var tests = Model.Clash.NwcXmlClashSerializer.Open(path); - ClashTests.AddRange(tests); - return tests; - } - /// /// Saves the document to a .nwc file in NwcIO's own layout, not verified to open in Autodesk Navisworks. When is non-empty it is the /// source of truth for the scene tree; is serialized only when no records exist, so edits to the @@ -643,74 +442,6 @@ public byte[] ToByteArray(string? header = null) return NwcWriter.WriteToBytes(this, header); } - /// Exports 3D mesh geometry to a Wavefront OBJ file. - public void SaveObj(string path, bool worldCoordinates = true) - { - Internal.Export.ObjExporter.WriteFile(Meshes, path, worldCoordinates, FileInfo.SourcePath); - } - - /// Exports 3D mesh geometry to a in Wavefront OBJ format. - public void SaveObj(TextWriter writer, bool worldCoordinates = true) - { - Internal.Export.ObjExporter.Write(Meshes, writer, worldCoordinates, FileInfo.SourcePath); - } - - /// Exports 3D mesh geometry, materials, and scene hierarchy to a binary glTF 2.0 file (.glb). - public void SaveGlb(string path, bool worldCoordinates = true) - { - Internal.Export.GlbExporter.WriteFile(Meshes, Materials, path, worldCoordinates, FileInfo.LoaderPlugin, SavedViewpoints); - } - - /// Exports 3D mesh geometry, materials, and scene hierarchy to a binary glTF 2.0 stream (.glb). - public void SaveGlb(Stream stream, bool worldCoordinates = true) - { - Internal.Export.GlbExporter.Write(Meshes, Materials, stream, worldCoordinates, FileInfo.LoaderPlugin, SavedViewpoints); - } - - /// Exports 3D mesh geometry, materials, spatial structure, and properties to an IFC STEP physical file (.ifc). - public void SaveIfc(string path, NwcIfcExportOptions? options = null) - { - Internal.Export.IfcExporter.WriteFile(this, path, options); - } - - /// Exports 3D mesh geometry, materials, spatial structure, and properties to a in IFC format. - public void SaveIfc(TextWriter writer, NwcIfcExportOptions? options = null) - { - Internal.Export.IfcExporter.Write(this, writer, options); - } - - /// Serializes 3D mesh geometry, materials, spatial structure, and properties to an IFC STEP physical string. - public string ToIfcString(NwcIfcExportOptions? options = null) - { - using var sw = new StringWriter(); - SaveIfc(sw, options); - return sw.ToString(); - } - - /// Exports saved viewpoints to a buildingSMART BCF 2.1 zip archive (.bcfzip). - public void SaveViewpointsBcf(string path) - { - Internal.Export.BcfExporter.WriteViewpointsFile(SavedViewpoints, path, Path.GetFileNameWithoutExtension(FileInfo.SourcePath ?? "Model")); - } - - /// Exports saved viewpoints to a buildingSMART BCF 2.1 zip stream. - public void SaveViewpointsBcf(Stream stream) - { - Internal.Export.BcfExporter.WriteViewpoints(SavedViewpoints, stream, Path.GetFileNameWithoutExtension(FileInfo.SourcePath ?? "Model")); - } - - /// Exports saved viewpoints, cameras, clipping planes, and redlines to an Autodesk Navisworks XML viewpoints file. - public void SaveViewpointsXml(string path, string units = "m") - { - Model.Sectioning.NwcXmlViewpointSerializer.Save(path, SavedViewpoints, units); - } - - /// Serializes saved viewpoints, cameras, clipping planes, and redlines to an Autodesk Navisworks XML viewpoints string. - public string ToViewpointsXml(string units = "m") - { - return Model.Sectioning.NwcXmlViewpointSerializer.Serialize(SavedViewpoints, units); - } - /// Exports the document to a JSON file. public void SaveJson(string path, NwcJsonExportOptions? options = null) => SaveJson(path, options, null, default); @@ -751,20 +482,6 @@ public string ToJson(NwcJsonExportOptions? options = null) return System.Text.Encoding.UTF8.GetString(ms.ToArray()); } - /// - /// 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) - { - Model.Viewer.NwcHtmlViewerExporter.Save(path, this, options); - } - - /// - /// Compares this document (as baseline) against a revision document, detecting additions, deletions, coordinate shifts, and property changes. - /// - public Model.Diff.NwcDiffReport Compare(NwcDocument revision, Model.Diff.NwcDiffOptions? options = null) => - Model.Diff.NwcDiffEngine.Compare(this, revision, options); - /// Opens a file asynchronously. The current implementation wraps the synchronous reader. public static Task OpenAsync( string path, @@ -790,22 +507,6 @@ public Task SaveAsync(string path, string? header = null, CancellationToken canc public Task SaveAsync(Stream stream, string? header = null, CancellationToken cancellationToken = default) => Task.Run(() => Save(stream, header, null, cancellationToken), cancellationToken); - /// - /// Populates clash grid location tags across all clash detective tests in this document using the provided or attached grid system. - /// - public int AssignGridLocations(Model.Grids.NwcGridSystem? gridSystem = null) - { - var grids = gridSystem ?? GridSystem ?? ExtractGridSystem(); - GridSystem = grids; - return grids.AssignGridLocations(this); - } - - /// - /// Automatically extracts grid lines from scene hierarchy nodes and metadata. - /// - public Model.Grids.NwcGridSystem ExtractGridSystem() => - Model.Grids.NwcGridSystem.ExtractFromDocument(this); - private static NwcLengthUnit InferLengthUnit(string? loaderPlugin) => loaderPlugin is "LcNwcLoaderPlugin:lcldrevit" or "nwexportrevit" ? NwcLengthUnit.Feet diff --git a/tests/NwcIO.Tests/IfcExportTests.cs b/tests/NwcIO.Tests/IfcExportTests.cs index aa744c4..6b164e3 100644 --- a/tests/NwcIO.Tests/IfcExportTests.cs +++ b/tests/NwcIO.Tests/IfcExportTests.cs @@ -1488,7 +1488,7 @@ private static NwcDocument CreateFullClassificationDocument() public void SaveIfc_LinkedNode_SuppliesElementNameAndGuid(bool assembly) { var guid = Guid.Parse("00112233-4455-6677-8899-aabbccddeeff"); - string globalId = NwcIO.Internal.Export.IfcGuid.ToIfcGuid(guid); + string globalId = NwcIO.Internal.IfcGuid.ToIfcGuid(guid); var node = new NwcNode { Id = 7, @@ -1521,7 +1521,7 @@ public void SaveIfc_LinkedNode_SuppliesElementNameAndGuid(bool assembly) } string ifc = sw.ToString(); - string expectedGuid = NwcIO.Internal.Export.IfcGuid.FromKey($"{NwcIO.Internal.Export.NwcElementGuid.ModelKey(doc)}:element:{globalId}:mesh:42"); + string expectedGuid = NwcIO.Internal.IfcGuid.FromKey($"{NwcIO.Internal.Export.NwcElementGuid.ModelKey(doc)}:element:{globalId}:mesh:42"); Assert.Contains($"IFCWALL('{expectedGuid}',", ifc); Assert.Contains("'Concrete Wall 200'", ifc); Assert.DoesNotContain("Mesh_42", ifc); diff --git a/tests/NwcIO.Tests/NwcElementGuidTests.cs b/tests/NwcIO.Tests/NwcElementGuidTests.cs index 376ee80..ed6393b 100644 --- a/tests/NwcIO.Tests/NwcElementGuidTests.cs +++ b/tests/NwcIO.Tests/NwcElementGuidTests.cs @@ -1,3 +1,4 @@ +using NwcIO.Internal; using NwcIO.Internal.Export; using NwcIO.Model; using Xunit;