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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ Report security vulnerabilities privately through the **Security** tab.
- Warnings are treated as errors (`<TreatWarningsAsErrors>true</TreatWarningsAsErrors>`).
- 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.
Expand Down
344 changes: 344 additions & 0 deletions src/NwcIO/Compatibility/NwcAssembly.Compatibility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,344 @@
using System.Text;
using NwcIO.Internal.Export;
using NwcIO.Model.Spatial;

namespace NwcIO.Model;

public sealed partial class NwcAssembly
{
/// <summary>
/// Applies an appearance profiler across all contributing models in the federated assembly.
/// </summary>
/// <returns>The total number of scene nodes and meshes whose appearance was overridden.</returns>
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;
}

/// <summary>
/// Detects spatial clashes between meshes belonging to different models. Meshes whose world bounding boxes
/// come within <paramref name="tolerance"/> of each other are reported; the overlap box is empty when they
/// only come close.
/// </summary>
public IReadOnlyList<NwcClash> FindClashes(double tolerance = 0.0)
{
if (Models.Count < 2)
{
return Array.Empty<NwcClash>();
}

var clashes = new List<NwcClash>();
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;
}

/// <summary>Configured Clash Detective tests and results for this federated assembly.</summary>
public List<Clash.NwcClashTest> ClashTests { get; } = [];

/// <summary>Spatial grid system (axes and levels) associated with the federated assembly.</summary>
public Grids.NwcGridSystem? GridSystem { get; set; }

/// <summary>
/// Executes full mesh-level triangle collision detection across all contributing models of the assembly.
/// </summary>
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;
}

/// <summary>Exports detected clash results to Autodesk Navisworks Clash Report XML format.</summary>
public void ExportClashReportXml(string path)
{
Clash.NwcXmlClashSerializer.Save(path, ClashTests, Name);
}

/// <summary>Exports detected clash results to a buildingSMART BCF 2.1 zip archive (.bcfzip).</summary>
public void ExportClashBcf(string path)
{
Clash.NwcClashBcfExporter.SaveTests(path, ClashTests, Name);
}

/// <summary>Exports the 4D TimeLiner schedule to Navisworks XML exchange format (.xml).</summary>
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);
}

/// <summary>Loads or imports a 4D TimeLiner schedule from Navisworks XML exchange format (.xml).</summary>
public void ImportTimeLinerXml(string path)
{
TimeLiner = Model.TimeLiner.NwcTimeLinerXmlSerializer.Open(path);
}

/// <summary>Exports the 4D TimeLiner schedule to Microsoft Project XML format (.xml).</summary>
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);
}

/// <summary>Loads or imports a 4D TimeLiner schedule from Microsoft Project XML format (.xml).</summary>
public void ImportTimeLinerMsProject(string path)
{
TimeLiner = Model.TimeLiner.NwcTimeLinerMsProjectXmlSerializer.Open(path);
}

/// <summary>Exports the 4D TimeLiner schedule to Primavera P6 XER format (.xer).</summary>
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);
}

/// <summary>Loads or imports a 4D TimeLiner schedule from Primavera P6 XER format (.xer).</summary>
public void ImportTimeLinerPrimavera(string path)
{
TimeLiner = Model.TimeLiner.NwcTimeLinerPrimaveraXerSerializer.Open(path);
}

/// <summary>Exports the coordination clash matrix to JSON format.</summary>
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);
}

/// <summary>Exports the coordination clash matrix summary to a Markdown table.</summary>
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);
}

/// <summary>Exports the coordination clash matrix conflicts to a buildingSMART BCF 2.1 zip archive.</summary>
public void ExportClashMatrixBcf(string path)
{
if (ClashMatrix is null) throw new InvalidOperationException("Assembly does not have a configured Clash Matrix.");
ClashMatrix.SaveBcf(path);
}

/// <summary>Exports the entire federated assembly to a binary glTF 2.0 file (.glb).</summary>
public void SaveGlb(string path, bool worldCoordinates = true)
{
GlbExporter.WriteFile(AllMeshes, AllMaterials, path, worldCoordinates, "NwcIO Assembly", SavedViewpoints);
}

/// <summary>Exports the entire federated assembly to a Wavefront OBJ file.</summary>
public void SaveObj(string path, bool worldCoordinates = true)
{
ObjExporter.WriteFile(AllMeshes, path, worldCoordinates, Name, GetModelLabel);
}

/// <summary>Exports the entire federated assembly with geometry, materials, and spatial hierarchy to an IFC2x3 STEP file (.ifc).</summary>
public void SaveIfc(string path, NwcIfcExportOptions? options = null)
{
IfcExporter.WriteAssembly(this, path, options);
}

/// <summary>Exports the entire federated assembly with geometry, materials, and spatial hierarchy to a <see cref="TextWriter"/> in IFC2x3 format.</summary>
public void SaveIfc(TextWriter writer, NwcIfcExportOptions? options = null)
{
IfcExporter.WriteAssembly(this, writer, options);
}

/// <summary>
/// Exports detected clashes across models in the assembly to a standard buildingSMART BCF 2.1 zip archive (.bcfzip).
/// </summary>
/// <param name="path">Destination .bcf or .bcfzip file path.</param>
/// <param name="clashes">Optional pre-computed clash list; if null, <see cref="FindClashes"/> is evaluated.</param>
public void SaveBcf(string path, IReadOnlyList<NwcClash>? clashes = null)
{
clashes ??= FindClashes();
BcfExporter.WriteFile(clashes, path, Name);
}

/// <summary>
/// Exports detected clashes across models in the assembly to a standard buildingSMART BCF 2.1 zip stream.
/// </summary>
public void SaveBcf(Stream stream, IReadOnlyList<NwcClash>? clashes = null)
{
clashes ??= FindClashes();
BcfExporter.Write(clashes, stream, Name);
}

/// <summary>
/// Exports saved viewpoints of the assembly to a buildingSMART BCF 2.1 zip archive (.bcfzip).
/// </summary>
/// <param name="path">Destination .bcf or .bcfzip file path.</param>
/// <param name="viewpoints">Optional pre-computed viewpoints list; if null, <see cref="SavedViewpoints"/> is used.</param>
public void SaveViewpointsBcf(string path, IReadOnlyList<NwcSavedViewpoint>? viewpoints = null)
{
viewpoints ??= SavedViewpoints;
BcfExporter.WriteViewpointsFile(viewpoints, path, Name);
}

/// <summary>
/// Exports saved viewpoints of the assembly to a buildingSMART BCF 2.1 zip stream.
/// </summary>
public void SaveViewpointsBcf(Stream stream, IReadOnlyList<NwcSavedViewpoint>? viewpoints = null)
{
viewpoints ??= SavedViewpoints;
BcfExporter.WriteViewpoints(viewpoints, stream, Name);
}

/// <summary>
/// Exports saved viewpoints of the assembly to an Autodesk Navisworks XML viewpoints file.
/// </summary>
public void SaveViewpointsXml(string path, IReadOnlyList<NwcSavedViewpoint>? viewpoints = null, string units = "m")
{
viewpoints ??= SavedViewpoints;
Sectioning.NwcXmlViewpointSerializer.Save(path, viewpoints, units);
}

/// <summary>
/// Serializes saved viewpoints of the assembly to an Autodesk Navisworks XML viewpoints string.
/// </summary>
public string ToViewpointsXml(IReadOnlyList<NwcSavedViewpoint>? viewpoints = null, string units = "m")
{
viewpoints ??= SavedViewpoints;
return Sectioning.NwcXmlViewpointSerializer.Serialize(viewpoints, units);
}

/// <summary>
/// Exports an interactive, self-contained HTML clash detection report with a discipline clash matrix and filterable clash table.
/// </summary>
/// <param name="path">Destination .html file path.</param>
/// <param name="clashes">Optional pre-computed clash list; if null, <see cref="FindClashes"/> is evaluated.</param>
public void SaveClashReportHtml(string path, IReadOnlyList<NwcClash>? clashes = null)
{
clashes ??= FindClashes();
HtmlClashReportExporter.WriteFile(clashes, path, Name, Models.Count, AllMeshes.Count);
}

/// <summary>
/// Exports an interactive, self-contained HTML clash detection report to a <see cref="TextWriter"/>.
/// </summary>
public void SaveClashReportHtml(TextWriter writer, IReadOnlyList<NwcClash>? clashes = null)
{
clashes ??= FindClashes();
HtmlClashReportExporter.Write(clashes, writer, Name, Models.Count, AllMeshes.Count);
}

/// <summary>
/// Exports the federated assembly to a standalone interactive 3D WebGL HTML viewer (.html) with zero external dependencies.
/// </summary>
public void SaveHtmlViewer(string path, Viewer.NwcHtmlViewerOptions? options = null)
{
Viewer.NwcHtmlViewerExporter.Save(path, this, options);
}

/// <summary>
/// Compares this federated assembly (as baseline) against a revision assembly, detecting additions, deletions, coordinate shifts, and property changes.
/// </summary>
public Diff.NwcDiffReport Compare(NwcAssembly revision, Diff.NwcDiffOptions? options = null) =>
Diff.NwcDiffEngine.Compare(this, revision, options);

/// <summary>
/// Populates clash grid location tags across all clash tests in this assembly and its contributing models using the provided or attached grid system.
/// </summary>
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;
}
}
Loading
Loading