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
4 changes: 2 additions & 2 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ matrix.PopulateStandardMatrix(defaultTolerance: 0.002);

// Configure tolerance per cell in document units; PopulateStandardMatrix fills cells in
// discipline order: [0]=Architecture-Structure, [1]=Architecture-MEP, [2]=Structure-MEP
matrix.Cells.Single(c => c.DisciplineA == "Architecture" && c.DisciplineB == "Structure").Tolerance = 0.005; // 5 document units
matrix.Cells.Single(c => c.DisciplineA == "Structure" && c.DisciplineB == "MEP").Tolerance = 0.010; // 10 document units
matrix.Cells.Single(c => c.DisciplineA == "Architecture" && c.DisciplineB == "Structure").Tolerance = 0.005;
matrix.Cells.Single(c => c.DisciplineA == "Structure" && c.DisciplineB == "MEP").Tolerance = 0.010;

// Execute the entire coordination matrix
matrix.Execute(assembly);
Expand Down
11 changes: 11 additions & 0 deletions docs/public-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ synchronous counterparts. Directory discovery and reference resolution use synch
The `load-models` phase reports each folder model, or the primary NWF and each of its reference entries,
including missing, duplicate and unsupported references.

`NwcDocument.LengthUnit` and `MetresPerUnit` describe the original coordinates. Set
`NwcReaderOptions.LengthUnit` to override inference. Clash detection, proximity grouping,
clash matrix cells and revision diff all interpret tolerances in the input coordinate units.
Grid locations and length measurements keep these values and display the corresponding unit
symbol. The measurement overloads accept a unit when their input is a standalone point or mesh.
IFC converts known coordinates to its selected output unit. BCF camera positions and orthogonal
view scales are exported in metres, using the original coordinates when the unit is unknown.
Viewpoint XML labels default to the document's unit when known and accept an explicit label.
`NwcAssembly.HasMixedLengthUnits` and the `MixedLengthUnits` diagnostic report mixed units.
Assembly geometry retains its original coordinates.

## Coordination & Clash Detection (`NwcIO.Model.Clash`)

| Type | Description |
Expand Down
1 change: 1 addition & 0 deletions src/NwcIO.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Reads Autodesk Navisworks files (.nwc, .nwd, .nwf) or assemblies of models
--clash-xml <file.xml> Run clash detection and export Navisworks Clash Report XML (.xml)
--clash-bcf <file.bcfzip> Run clash detection and export buildingSMART BCF 2.1 archive (.bcfzip)
--clash-matrix <file.json> Execute multi-discipline coordination matrix and export JSON report
Matrix tolerances are in document units
--clash-matrix-md <file.md> Export clash matrix coordination report as Markdown table
--clash-matrix-bcf <file.bcfzip> Export clash matrix conflicts to buildingSMART BCF 2.1 zip
--clash-group <mode> Group clashes: proximity, element, grid, layer
Expand Down
31 changes: 20 additions & 11 deletions src/NwcIO/Compatibility/Clash/NwcClashBcfExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ public static void Write(Stream stream, NwcClashTest test, string projectName =
/// <summary>
/// Saves clash tests, including both standalone results and grouped clashes, to one BCF 2.1 archive file.
/// </summary>
internal static void SaveTests(string path, IEnumerable<NwcClashTest> tests, string projectName = "Navisworks Clash Coordination")
internal static void SaveTests(
string path, IEnumerable<NwcClashTest> tests, string projectName = "Navisworks Clash Coordination",
NwcLengthUnit lengthUnit = NwcLengthUnit.Unknown)
{
ArgumentNullException.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(tests);
Expand All @@ -79,14 +81,16 @@ internal static void SaveTests(string path, IEnumerable<NwcClashTest> tests, str
}

using var fs = File.Create(path);
WriteTests(fs, tests, projectName);
WriteTests(fs, tests, projectName, lengthUnit);
}

/// <summary>
/// Writes clash tests, including both standalone results and grouped clashes, to one BCF 2.1 zip stream:
/// a topic per clash group and a topic per clash that is in no group.
/// </summary>
internal static void WriteTests(Stream stream, IEnumerable<NwcClashTest> tests, string projectName = "Navisworks Clash Coordination")
internal static void WriteTests(
Stream stream, IEnumerable<NwcClashTest> tests, string projectName = "Navisworks Clash Coordination",
NwcLengthUnit lengthUnit = NwcLengthUnit.Unknown)
{
ArgumentNullException.ThrowIfNull(stream);
ArgumentNullException.ThrowIfNull(tests);
Expand All @@ -98,11 +102,11 @@ internal static void WriteTests(Stream stream, IEnumerable<NwcClashTest> tests,

foreach (var test in tests)
{
WriteTest(archive, test, projectName);
WriteTest(archive, test, projectName, lengthUnit);
}
}

private static void WriteTest(ZipArchive archive, NwcClashTest test, string projectName)
private static void WriteTest(ZipArchive archive, NwcClashTest test, string projectName, NwcLengthUnit lengthUnit)
{
// Export Clash Groups as unified topics
foreach (var group in test.Groups)
Expand All @@ -120,7 +124,7 @@ private static void WriteTest(ZipArchive archive, NwcClashTest test, string proj
using (var s = vpEntry.Open())
using (var xml = XmlWriter.Create(s, Settings))
{
WriteGroupViewpointXml(xml, group);
WriteGroupViewpointXml(xml, group, lengthUnit);
}
}

Expand All @@ -140,7 +144,7 @@ private static void WriteTest(ZipArchive archive, NwcClashTest test, string proj
using (var s = vpEntry.Open())
using (var xml = XmlWriter.Create(s, Settings))
{
WriteViewpointXml(xml, clash);
WriteViewpointXml(xml, clash, lengthUnit);
}
}
}
Expand Down Expand Up @@ -259,7 +263,7 @@ private static void WriteMarkupXml(XmlWriter xml, NwcClashResult clash, string p
xml.WriteEndDocument();
}

private static void WriteViewpointXml(XmlWriter xml, NwcClashResult clash)
private static void WriteViewpointXml(XmlWriter xml, NwcClashResult clash, NwcLengthUnit lengthUnit = NwcLengthUnit.Unknown)
{
xml.WriteStartDocument();
xml.WriteStartElement("VisualizationInfo");
Expand Down Expand Up @@ -290,7 +294,8 @@ private static void WriteViewpointXml(XmlWriter xml, NwcClashResult clash)
// Camera positioning
xml.WriteStartElement("PerspectiveCamera");

var target = clash.ClashPoint;
double scale = clash.LengthUnit.GetMetresPerUnit() ?? lengthUnit.GetMetresPerUnit() ?? 1.0;
var target = clash.ClashPoint * scale;
var offset = new Vector3d(5.0, -5.0, 3.5);
var eye = target + offset;
var dir = (target - eye).Normalize();
Expand Down Expand Up @@ -386,7 +391,7 @@ private static void WriteGroupMarkupXml(XmlWriter xml, NwcClashGroup group, stri
xml.WriteEndDocument();
}

private static void WriteGroupViewpointXml(XmlWriter xml, NwcClashGroup group)
private static void WriteGroupViewpointXml(XmlWriter xml, NwcClashGroup group, NwcLengthUnit lengthUnit)
{
xml.WriteStartDocument();
xml.WriteStartElement("VisualizationInfo");
Expand Down Expand Up @@ -420,7 +425,11 @@ private static void WriteGroupViewpointXml(XmlWriter xml, NwcClashGroup group)
// Camera positioning
xml.WriteStartElement("PerspectiveCamera");

var target = group.ClashPoint;
var units = group.Clashes.Select(clash => clash.LengthUnit).Distinct().Take(2).ToArray();
double scale = units is [var unit]
? unit.GetMetresPerUnit() ?? lengthUnit.GetMetresPerUnit() ?? 1.0
: lengthUnit.GetMetresPerUnit() ?? 1.0;
var target = group.ClashPoint * scale;
var offset = new Vector3d(8.0, -8.0, 5.0);
var eye = target + offset;
var dir = (target - eye).Normalize();
Expand Down
5 changes: 4 additions & 1 deletion src/NwcIO/Compatibility/Clash/NwcClashDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static NwcClashTest Run(
}

RunWithCancellation(test, itemsA.Count, progress,
() => RunCoreMultiModel(itemsA, itemsB, test, progress, cancellationToken), cancellationToken);
() => RunCoreMultiModel(itemsA, itemsB, test, assembly.CommonLengthUnit, progress, cancellationToken), cancellationToken);
return test;
}

Expand Down Expand Up @@ -196,6 +196,7 @@ private static void RunCore(

var result = new NwcClashResult(clashName, obj1, obj2, clashPoint, distance, NwcClashStatus.New)
{
LengthUnit = document.LengthUnit,
Description = test.TestType switch
{
NwcClashTestType.Clearance => $"Clearance between {item1Name} and {item2Name}",
Expand All @@ -221,6 +222,7 @@ private static void RunCoreMultiModel(
List<(NwcMesh Mesh, string ModelName, string? Layer, string ModelKey)> itemsA,
List<(NwcMesh Mesh, string ModelName, string? Layer, string ModelKey)> itemsB,
NwcClashTest test,
NwcLengthUnit lengthUnit,
IProgress<NwcProgress>? progress,
CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -298,6 +300,7 @@ private static void RunCoreMultiModel(

var result = new NwcClashResult(clashName, obj1, obj2, clashPoint, distance, NwcClashStatus.New)
{
LengthUnit = lengthUnit,
Description = test.TestType switch
{
NwcClashTestType.Clearance => $"Clearance between {itemA.ModelName} ({name1}) and {itemB.ModelName} ({name2})",
Expand Down
6 changes: 5 additions & 1 deletion src/NwcIO/Compatibility/Clash/NwcClashMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public void Run(NwcAssembly assembly)
}

var modelKeys = NwcElementGuid.ModelKeys(assembly.Models);
var lengthUnit = assembly.CommonLengthUnit;
var keyByDocument = new Dictionary<NwcDocument, string>(ReferenceEqualityComparer.Instance);
for (int i = 0; i < assembly.Models.Count; i++)
{
Expand Down Expand Up @@ -318,7 +319,10 @@ public void Run(NwcAssembly assembly)
IfcGuid = NwcElementGuid.ForMesh(mB, keyByDocument[docB]),
};

var clash = new NwcClashResult($"Clash_{clashId++}", c1, c2, clashPoint, -penetration);
var clash = new NwcClashResult($"Clash_{clashId++}", c1, c2, clashPoint, -penetration)
{
LengthUnit = lengthUnit,
};

if (!test.Rules.Any(rule => rule.ShouldIgnore(clash)))
{
Expand Down
2 changes: 2 additions & 0 deletions src/NwcIO/Compatibility/Clash/NwcClashTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ public sealed class NwcClashResult
public NwcClashObject Item1 { get; set; } = new();
public NwcClashObject Item2 { get; set; } = new();
public Vector3d ClashPoint { get; set; }
internal NwcLengthUnit LengthUnit { get; init; }

public double Distance { get; set; } // Negative for hard clash penetration, positive for clearance
public string? GridLocation { get; set; }
public DateTimeOffset CreatedDate { get; set; } = DateTimeOffset.UtcNow;
Expand Down
6 changes: 6 additions & 0 deletions src/NwcIO/Compatibility/Diff/NwcDiffEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ private sealed class DiffItem
public Guid? ElementGuid { get; init; }
public NwcNode? Node { get; init; }
public NwcMesh? Mesh { get; init; }
public NwcLengthUnit LengthUnit { get; init; }
public NwcTransform? Transform { get; init; }
public IReadOnlyList<NwcPropertyRecord> Properties { get; init; } = [];
public BoundingBox3d WorldBounds { get; set; } = BoundingBox3d.Empty;
Expand Down Expand Up @@ -119,6 +120,7 @@ private static void PerformDiff(
ChangeKind = NwcChangeKind.Added,
RevisionNode = rev.Node,
RevisionMesh = rev.Mesh,
LengthUnit = rev.LengthUnit,
});
}
else
Expand Down Expand Up @@ -181,6 +183,7 @@ private static void PerformDiff(
RevisionNode = rev.Node,
BaselineMesh = baseItem.Mesh,
RevisionMesh = rev.Mesh,
LengthUnit = rev.Mesh is not null || baseItem.Mesh is null ? rev.LengthUnit : baseItem.LengthUnit,
};
diff.PropertyChanges.AddRange(propChanges);

Expand Down Expand Up @@ -211,6 +214,7 @@ private static void PerformDiff(
ChangeKind = NwcChangeKind.Deleted,
BaselineNode = b.Node,
BaselineMesh = b.Mesh,
LengthUnit = b.LengthUnit,
});
}
}
Expand Down Expand Up @@ -367,6 +371,7 @@ node.Mesh is not null ||
ElementGuid = node.ElementGuid,
Node = node,
Mesh = node.Mesh,
LengthUnit = doc.LengthUnit,
Transform = node.Transform,
Properties = node.Properties,
WorldBounds = bounds,
Expand Down Expand Up @@ -401,6 +406,7 @@ node.Mesh is not null ||
Name = name,
ClassName = "MeshGeometry",
Mesh = m,
LengthUnit = doc.LengthUnit,
Transform = m.Transform,
WorldBounds = m.GetWorldBoundingBox(),
MatchKey = matchKey,
Expand Down
4 changes: 3 additions & 1 deletion src/NwcIO/Compatibility/Diff/NwcDiffTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public sealed class NwcElementDiff

public NwcMesh? RevisionMesh { get; init; }

internal NwcLengthUnit LengthUnit { get; init; }

/// <summary>
/// Gets the best available 3D centroid for this element (from mesh or transform).
/// </summary>
Expand Down Expand Up @@ -358,7 +360,7 @@ void WriteTopic(NwcElementDiff item, string changePrefix, string priority)
xml.WriteStartElement("VisualizationInfo");
xml.WriteAttributeString("Guid", vpGuid);

var center = item.Centroid;
var center = item.Centroid * (item.LengthUnit.GetMetresPerUnit() ?? 1.0);
var eye = center + new Vector3d(5.0, -5.0, 4.0);
var dir = (center - eye).Normalize();

Expand Down
32 changes: 17 additions & 15 deletions src/NwcIO/Compatibility/Export/BcfExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public static void WriteFile(IReadOnlyList<NwcClash> clashes, string path, strin
/// <summary>
/// Writes the saved viewpoints list as a BCF 2.1 (.bcfzip) file to disk.
/// </summary>
public static void WriteViewpointsFile(IReadOnlyList<NwcSavedViewpoint> viewpoints, string path, string? assemblyName = null)
public static void WriteViewpointsFile(
IReadOnlyList<NwcSavedViewpoint> viewpoints, string path, string? assemblyName = null, double metresPerUnit = 1.0)
{
ArgumentNullException.ThrowIfNull(viewpoints);
ArgumentNullException.ThrowIfNull(path);
Expand All @@ -52,15 +53,15 @@ public static void WriteViewpointsFile(IReadOnlyList<NwcSavedViewpoint> viewpoin
}

using var fs = File.Create(path);
WriteViewpoints(viewpoints, fs, assemblyName);
WriteViewpoints(viewpoints, fs, assemblyName, metresPerUnit);
}

/// <summary>
/// Writes the saved viewpoints list as a BCF 2.1 (.bcfzip) archive into the provided stream.
/// </summary>
public static void WriteViewpoints(
IReadOnlyList<NwcSavedViewpoint> viewpoints, Stream stream, string? assemblyName = null,
CancellationToken cancellationToken = default)
double metresPerUnit = 1.0, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(viewpoints);
ArgumentNullException.ThrowIfNull(stream);
Expand Down Expand Up @@ -97,7 +98,7 @@ public static void WriteViewpoints(
using (var s = viewpointEntry.Open())
using (var xml = XmlWriter.Create(s, XmlSettings))
{
WriteCameraViewpointXml(xml, vp.Camera, viewpointGuid);
WriteCameraViewpointXml(xml, vp.Camera, viewpointGuid, metresPerUnit);
}
}
}
Expand Down Expand Up @@ -184,7 +185,7 @@ private static void WriteMarkupXml(
xml.WriteElementString("CreationAuthor", "NwcIO");

string desc = FormattableString.Invariant(
$"Geometric clash detected between {clash.ModelAName} (Mesh #{clash.MeshA.Id}, {clash.IfcGuidA}) and {clash.ModelBName} (Mesh #{clash.MeshB.Id}, {clash.IfcGuidB}). Overlap volume: {clash.Volume:F4} m³ at center ({clash.Center.X:F3}, {clash.Center.Y:F3}, {clash.Center.Z:F3}).");
$"Geometric clash detected between {clash.ModelAName} (Mesh #{clash.MeshA.Id}, {clash.IfcGuidA}) and {clash.ModelBName} (Mesh #{clash.MeshB.Id}, {clash.IfcGuidB}). Overlap volume: {clash.Volume:F4} cubic document units at center ({clash.Center.X:F3}, {clash.Center.Y:F3}, {clash.Center.Z:F3}).");
xml.WriteElementString("Description", desc);
xml.WriteEndElement(); // Topic

Expand Down Expand Up @@ -244,8 +245,9 @@ private static void WriteViewpointXml(XmlWriter xml, NwcClash clash, string view
xml.WriteEndElement(); // Components

// Camera Calculation
var center = clash.Center;
var size = clash.IntersectionBox.Size;
double scale = clash.ModelA.LengthUnit == clash.ModelB.LengthUnit ? clash.ModelA.MetresPerUnit ?? 1.0 : 1.0;
var center = clash.Center * scale;
var size = clash.IntersectionBox.Size * scale;
double maxDim = Math.Max(size.X, Math.Max(size.Y, size.Z));
double viewDistance = maxDim > 0.05 ? Math.Max(3.0, maxDim * 4.0) : 5.0;

Expand Down Expand Up @@ -331,7 +333,7 @@ private static void WriteViewpointMarkupXml(
xml.WriteEndDocument();
}

private static void WriteCameraViewpointXml(XmlWriter xml, NwcCamera cam, string viewpointGuid)
private static void WriteCameraViewpointXml(XmlWriter xml, NwcCamera cam, string viewpointGuid, double metresPerUnit)
{
xml.WriteStartDocument();
xml.WriteStartElement("VisualizationInfo");
Expand All @@ -343,9 +345,9 @@ private static void WriteCameraViewpointXml(XmlWriter xml, NwcCamera cam, string
{
xml.WriteStartElement("OrthogonalCamera");
xml.WriteStartElement("CameraViewPoint");
xml.WriteElementString("X", cam.Position.X.ToString("F4", CultureInfo.InvariantCulture));
xml.WriteElementString("Y", cam.Position.Y.ToString("F4", CultureInfo.InvariantCulture));
xml.WriteElementString("Z", cam.Position.Z.ToString("F4", CultureInfo.InvariantCulture));
xml.WriteElementString("X", (cam.Position.X * metresPerUnit).ToString("F4", CultureInfo.InvariantCulture));
xml.WriteElementString("Y", (cam.Position.Y * metresPerUnit).ToString("F4", CultureInfo.InvariantCulture));
xml.WriteElementString("Z", (cam.Position.Z * metresPerUnit).ToString("F4", CultureInfo.InvariantCulture));
xml.WriteEndElement();

xml.WriteStartElement("CameraDirection");
Expand All @@ -360,16 +362,16 @@ private static void WriteCameraViewpointXml(XmlWriter xml, NwcCamera cam, string
xml.WriteElementString("Z", cam.Up.Z.ToString("F4", CultureInfo.InvariantCulture));
xml.WriteEndElement();

xml.WriteElementString("ViewToWorldScale", cam.FocalDistance.ToString("F4", CultureInfo.InvariantCulture));
xml.WriteElementString("ViewToWorldScale", (cam.FocalDistance * metresPerUnit).ToString("F4", CultureInfo.InvariantCulture));
xml.WriteEndElement();
}
else
{
xml.WriteStartElement("PerspectiveCamera");
xml.WriteStartElement("CameraViewPoint");
xml.WriteElementString("X", cam.Position.X.ToString("F4", CultureInfo.InvariantCulture));
xml.WriteElementString("Y", cam.Position.Y.ToString("F4", CultureInfo.InvariantCulture));
xml.WriteElementString("Z", cam.Position.Z.ToString("F4", CultureInfo.InvariantCulture));
xml.WriteElementString("X", (cam.Position.X * metresPerUnit).ToString("F4", CultureInfo.InvariantCulture));
xml.WriteElementString("Y", (cam.Position.Y * metresPerUnit).ToString("F4", CultureInfo.InvariantCulture));
xml.WriteElementString("Z", (cam.Position.Z * metresPerUnit).ToString("F4", CultureInfo.InvariantCulture));
xml.WriteEndElement();

xml.WriteStartElement("CameraDirection");
Expand Down
Loading
Loading