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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ API between minor releases.
- Select multiple elements in the model tree and open the viewer context menu from its rows.
- Filter the model tree by source model, level and category, and optionally isolate the matches in 3D.
- Export explicitly placed source models together in one standalone HTML viewer with independent visibility.
- Import BCF 2.1 topics and open their cameras, clipping and matched components in the offline viewer.

### Fixed

Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ pair to add more inputs. The viewer lists all models in the Linked files panel w
visibility controls. The first input supplies any startup clipping. Saved views from added sources
are placed with their models. A saved section box requires a yaw that is a multiple of 90 degrees.

Pass `--bcf` to include topics from a local [BCF 2.1 archive](https://github.com/buildingSMART/BCF-XML/tree/v2.1/Documentation).

```sh
ioviewer architecture.ifc --bcf review.bcfzip -o review.html
```

The archive uses the same metre, Z-up coordinates as the output scene. For combined models those are
the first input's coordinates. In **BCF topics**, open a topic to see its status, description and
available viewpoints. A viewpoint restores its camera, up to six clipping planes, and matched
selection and visibility exceptions. The viewer interprets the BCF 2.1 camera scale as vertical.
It matches IFC GlobalIds exactly. Without a GlobalId, it supports Revit UniqueId for component
references whose originating system is `Autodesk Revit` or `Revit`. Missing or ambiguous references
are reported without preventing the rest of the viewpoint from opening.

The archive limit is 64 MiB and 4,096 entries, with 2 MiB per XML file and 32 MiB of imported XML.
The importer reads topic and viewpoint XML. Snapshots, attachments, markup lines and component
colours are outside this import.

Export structured JSON snapshots:

```sh
Expand Down
5 changes: 4 additions & 1 deletion docs/public-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ The model data is the IOScene scene model, referenced by the `IOViewer` package.

- `ViewerDocument` holds the scene, startup clipping, and the model keys hidden at start in `HiddenModelKeys`.
`ShowHostInModelList` adds the first model to the Linked files controls so it can be hidden independently.
`ViewpointStates` adds optional state to saved cameras. `Title` reads and writes `Scene.Title`.
`ViewpointStates` adds optional state to saved cameras. `BcfTopics` holds imported summaries and viewpoints
with cameras already in scene coordinates. `Title` reads and writes `Scene.Title`.
- `ViewerSectionPlane`: Cutting plane defined by axis or custom normal. `Offset` is the signed distance in metres
from the scene origin along its unit normal. By default the plane cuts the side the normal points to. `Inverted`
cuts the opposite side.
Expand All @@ -40,6 +41,8 @@ The model data is the IOScene scene model, referenced by the `IOViewer` package.

Use a unique `SceneViewpoint.Id` and the same key in `ViewerDocument.ViewpointStates`. A `ViewerViewpointState` can
restore clipping, element visibility and linked-file checkboxes. The view list and the tour apply the same state.
`SelectedElementIds` replaces the selection when supplied. `VisibleElementIds` isolates a set of elements
and is exclusive with `HiddenElementIds`. All three lists use scene element IDs.

```csharp
doc.Scene.Viewpoints.Add(new SceneViewpoint
Expand Down
256 changes: 256 additions & 0 deletions src/IOViewer.Cli/BcfReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
using System.Globalization;
using System.IO.Compression;
using System.Xml;
using System.Xml.Linq;
using IOScene;
using IOViewer.Model;

namespace IOViewer.Cli;

internal static class BcfReader
{
private const int MaxArchiveBytes = 64 * 1024 * 1024;
private const int MaxEntries = 4096;
private const int MaxXmlBytes = 2 * 1024 * 1024;
private const int MaxTotalXmlBytes = 32 * 1024 * 1024;
private const int MaxPlanes = 6;

internal static List<ViewerBcfTopic> Read(string path, SceneDocument scene, Action<string> warn)
{
using var file = File.OpenRead(path);
if (file.Length > MaxArchiveBytes) throw new InvalidDataException("The BCF archive exceeds 64 MiB.");
using var zip = new ZipArchive(file, ZipArchiveMode.Read);
if (zip.Entries.Count > MaxEntries) throw new InvalidDataException("The BCF archive exceeds 4096 entries.");
var entries = new Dictionary<string, ZipArchiveEntry>(StringComparer.OrdinalIgnoreCase);
foreach (var entry in zip.Entries)
{
string name = entry.FullName;
if (!ValidPath(name.TrimEnd('/'))) throw new InvalidDataException("The BCF archive contains an invalid entry path.");
if (!entries.TryAdd(name, entry)) throw new InvalidDataException("The BCF archive contains duplicate entry paths.");
}

int remaining = MaxTotalXmlBytes;
if (!entries.TryGetValue("bcf.version", out var version)
|| ReadXml(version, ref remaining) is not { } root || root.Name != "Version"
|| (string?)root.Attribute("VersionId") != "2.1")
throw new InvalidDataException("Expected a bcf.version file declaring BCF 2.1.");

var bounds = scene.ComputeBounds();
var index = new Components(scene);
var topics = new List<ViewerBcfTopic>();
foreach (var entry in entries.Values.Where(e => e.Name.Equals("markup.bcf", StringComparison.OrdinalIgnoreCase))
.OrderBy(e => e.FullName, StringComparer.Ordinal))
{
string[] segments = entry.FullName.Split('/');
if (segments.Length != 2 || !Guid.TryParseExact(segments[0], "D", out var folder)) continue;
var markup = ReadTopicXml(entry, ref remaining, warn);
if (markup is null || markup.Name != "Markup" || markup.Element("Topic") is not { } topic
|| !Guid.TryParseExact((string?)topic.Attribute("Guid"), "D", out var id) || id != folder
|| topic.Element("Title")?.Value.Trim() is not { Length: > 0 } title)
{
warn($"Skipped invalid topic {folder}.");
continue;
}
var result = new ViewerBcfTopic
{
Id = id.ToString("D"), Title = title,
Status = (string?)topic.Attribute("TopicStatus"), Description = (string?)topic.Element("Description"),
};
var visited = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var reference in markup.Elements("Viewpoints").OrderBy(ViewIndex))
{
string? name = reference.Element("Viewpoint")?.Value.Trim();
if (string.IsNullOrEmpty(name)) continue; // A snapshot-only viewpoint is allowed.
if (!ValidPath(name) || name.Contains('/') || !name.EndsWith(".bcfv", StringComparison.OrdinalIgnoreCase)
|| !entries.TryGetValue($"{segments[0]}/{name}", out var viewFile))
{
warn($"Topic '{title}' has a missing or invalid viewpoint reference.");
continue;
}
if (!visited.Add(name)) continue;
var view = ReadTopicXml(viewFile, ref remaining, warn);
if (view is null || view.Name != "VisualizationInfo" || !TryCamera(view, bounds, out var camera, out double? height))
{
warn($"Topic '{title}' contains a viewpoint without a supported camera.");
continue;
}
var saved = new ViewerBcfViewpoint
{
Camera = camera, OrthographicHeight = height, State = new()
{
SectionPlanes = ReadPlanes(view, title, warn),
},
};
index.Apply(view.Element("Components"), saved, title, warn);
result.Viewpoints.Add(saved);
}
topics.Add(result);
}
if (topics.Count == 0) throw new InvalidDataException("The BCF archive has no readable topics.");
return topics;
}

private static int ViewIndex(XElement view) => int.TryParse(view.Element("Index")?.Value,
NumberStyles.Integer, CultureInfo.InvariantCulture, out int index) ? index : int.MaxValue;

private static bool ValidPath(string name) =>
!name.Contains('\\') && !name.Contains(':') && !name.Any(char.IsControl)
&& name.Split('/').All(part => part.Length > 0 && part is not "." and not "..");

private static XElement? ReadTopicXml(ZipArchiveEntry entry, ref int remaining, Action<string> warn)
{
try { return ReadXml(entry, ref remaining); }
catch (XmlException) { warn($"Malformed XML in '{entry.FullName}'."); return null; }
}

private static XElement? ReadXml(ZipArchiveEntry entry, ref int remaining)
{
if (entry.Length > MaxXmlBytes || entry.Length > remaining)
throw new InvalidDataException("BCF XML exceeds the 2 MiB per file or 32 MiB total limit.");
int length = (int)entry.Length;
remaining -= length;
using var data = entry.Open();
byte[] buffer = new byte[length];
data.ReadExactly(buffer);
if (data.ReadByte() != -1) throw new InvalidDataException("BCF entry exceeds its declared size.");
using var xml = XmlReader.Create(new MemoryStream(buffer), new XmlReaderSettings
{
DtdProcessing = DtdProcessing.Prohibit, XmlResolver = null, MaxCharactersInDocument = MaxXmlBytes,
});
return XDocument.Load(xml).Root;
}

private static bool TryCamera(XElement view, BoundingBox3d bounds, out SceneCamera camera, out double? height)
{
camera = new SceneCamera();
height = null;
var ortho = view.Element("OrthogonalCamera");
var perspective = view.Element("PerspectiveCamera");
var source = ortho ?? perspective;
if (source is null || (ortho is not null && perspective is not null)
|| !TryVector(source.Element("CameraViewPoint"), out var position)
|| !TryVector(source.Element("CameraDirection"), out var direction) || !Unit(direction, out direction)
|| !TryVector(source.Element("CameraUpVector"), out var up) || !Unit(up, out up)
|| !Unit(Vector3d.Cross(direction, up), out _)) return false;

if (ortho is not null)
{
if (!Number(ortho.Element("ViewToWorldScale"), out double scale) || scale <= 0) return false;
height = scale;
camera.Projection = SceneProjection.Orthographic;
}
else
{
if (!Number(source.Element("FieldOfView"), out double fov) || fov is < 45 or > 60) return false;
camera.FieldOfView = fov * Math.PI / 180;
}
double depth = bounds.IsEmpty ? 0 : Vector3d.Dot(bounds.Center - position, direction);
camera.Position = position;
camera.Target = position + direction * (double.IsFinite(depth) && depth > 0 ? depth : 1);
camera.Up = up;
return camera.Target != position && Finite(camera.Target);
}

private static List<ViewerSectionPlane> ReadPlanes(XElement view, string title, Action<string> warn)
{
var planes = new List<ViewerSectionPlane>();
foreach (var plane in view.Element("ClippingPlanes")?.Elements("ClippingPlane") ?? [])
{
if (planes.Count == MaxPlanes)
{
warn($"Topic '{title}' exceeds the six clipping planes supported by the viewer.");
break;
}
if (TryVector(plane.Element("Location"), out var location)
&& TryVector(plane.Element("Direction"), out var direction) && Unit(direction, out var normal)
&& double.IsFinite(Vector3d.Dot(normal, location)))
planes.Add(new() { Axis = ViewerSectionAxis.Custom, Normal = normal, Offset = Vector3d.Dot(normal, location) });
else warn($"Topic '{title}' contains an invalid clipping plane.");
}
return planes;
}

private static bool TryVector(XElement? element, out Vector3d vector)
{
vector = default;
if (!Number(element?.Element("X"), out double x) || !Number(element?.Element("Y"), out double y)
|| !Number(element?.Element("Z"), out double z)) return false;
vector = new(x, y, z);
return true;
}

private static bool Number(XElement? element, out double value) =>
double.TryParse(element?.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out value) && double.IsFinite(value);

private static bool Finite(Vector3d v) => double.IsFinite(v.X) && double.IsFinite(v.Y) && double.IsFinite(v.Z);

private static bool Unit(Vector3d vector, out Vector3d normal)
{
normal = default;
double scale = Math.Max(Math.Abs(vector.X), Math.Max(Math.Abs(vector.Y), Math.Abs(vector.Z)));
if (scale == 0 || !double.IsFinite(scale)) return false;
normal = new Vector3d(vector.X / scale, vector.Y / scale, vector.Z / scale).Normalized();
return true;
}

private sealed class Components
{
private readonly Dictionary<string, long?> _ifc = new(StringComparer.Ordinal);
private readonly Dictionary<string, long?> _revit = new(StringComparer.OrdinalIgnoreCase);

internal Components(SceneDocument scene)
{
var formats = scene.Models.ToDictionary(model => model.Id, model => model.SourceFormat);
foreach (var element in scene.Elements)
{
string? format = formats.GetValueOrDefault(element.ModelId);
Add(_ifc, element.IfcGuid ?? (format == "ifc" ? element.SourceId : null), element.Id);
if (format == "rvt") Add(_revit, element.SourceId, element.Id);
}
}

private static void Add(Dictionary<string, long?> index, string? identity, long id)
{
if (!string.IsNullOrEmpty(identity) && !index.TryAdd(identity, id)) index[identity] = null;
}

internal void Apply(XElement? components, ViewerBcfViewpoint view, string title, Action<string> warn)
{
view.State.SelectedElementIds = Match(components?.Element("Selection")?.Elements("Component") ?? [], view);
if (components?.Element("Visibility") is { } visibility)
{
var refs = visibility.Element("Exceptions")?.Elements("Component").ToArray() ?? [];
var exceptions = Match(refs, view);
string? defaultVisibility = (string?)visibility.Attribute("DefaultVisibility");
if (defaultVisibility is null or "true" or "1") view.State.HiddenElementIds = exceptions;
else if (defaultVisibility is "false" or "0")
{
if (refs.Length == 0 || exceptions.Count > 0) view.State.VisibleElementIds = exceptions;
}
else warn($"Topic '{title}' contains an invalid visibility default.");
}
if (view.MissingComponents > 0 || view.AmbiguousComponents > 0)
warn($"Topic '{title}' has {view.MissingComponents} missing and {view.AmbiguousComponents} ambiguous component references.");
}

private List<long> Match(IEnumerable<XElement> components, ViewerBcfViewpoint view)
{
var found = new HashSet<long>();
foreach (var component in components)
{
string? ifcGuid = (string?)component.Attribute("IfcGuid");
string? system = component.Element("OriginatingSystem")?.Value.Trim();
string? key = !string.IsNullOrEmpty(ifcGuid) ? ifcGuid
: string.Equals(system, "Autodesk Revit", StringComparison.OrdinalIgnoreCase)
|| string.Equals(system, "Revit", StringComparison.OrdinalIgnoreCase)
? component.Element("AuthoringToolId")?.Value.Trim()
: null;
var index = !string.IsNullOrEmpty(ifcGuid) ? _ifc : _revit;
if (key is null || !index.TryGetValue(key, out long? id)) view.MissingComponents++;
else if (id is null) view.AmbiguousComponents++;
else found.Add(id.Value);
}
return [.. found];
}
}
}
21 changes: 21 additions & 0 deletions src/IOViewer.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ with NwcIO. Conversion diagnostics are reported as warnings or errors.
Add another model. Translation is in metres and yaw in degrees about +Z.
Repeat the pair for each model. Use 0,0,0,0 for an already aligned source.
--title <title> Page title shown in the browser tab.
--bcf <file> Import topics from a local BCF 2.1 archive in the scene's coordinates.
--wireframe Enable wireframe display mode by default.
--white-mode Enable white/clay architectural rendering mode by default.
--no-properties Exclude element properties to reduce the file size.
Expand Down Expand Up @@ -79,6 +80,7 @@ public static int Main(string[] args)
string? outputPath = null;
string? customTitle = null;
string? explicitJsonPath = null;
string? bcfPath = null;
var additional = new List<(string Path, Transform3d Placement)>();
bool wireframe = false;
bool whiteMode = false;
Expand Down Expand Up @@ -110,6 +112,14 @@ public static int Main(string[] args)
}
customTitle = args[++i];
break;
case "--bcf":
if (bcfPath is not null || i + 1 >= args.Length || args[i + 1].StartsWith('-'))
{
Console.Error.WriteLine("Error: Specify one local archive with --bcf <file>.");
return 1;
}
bcfPath = args[++i];
break;
case "--add":
if (i + 3 >= args.Length || args[i + 1].StartsWith('-') || args[i + 2] != "--placement"
|| !SceneFederation.TryParsePlacement(args[i + 3], out var placement))
Expand Down Expand Up @@ -185,6 +195,11 @@ public static int Main(string[] args)
Console.Error.WriteLine($"Error: File not found: '{Printable(source.Path)}'.");
return 2;
}
if (bcfPath is not null && !File.Exists(bcfPath))
{
Console.Error.WriteLine($"Error: File not found: '{Printable(bcfPath)}'.");
return 2;
}

outputPath ??= Path.ChangeExtension(inputPath, ".html");

Expand All @@ -207,6 +222,12 @@ public static int Main(string[] args)
adapterCode = Math.Max(adapterCode, ExitCode(diagnostics));
}
}
if (bcfPath is not null)
{
sourcePath = bcfPath;
doc.BcfTopics.AddRange(BcfReader.Read(bcfPath, doc.Scene,
warning => Console.Error.WriteLine($"Warning: BCF: {Printable(warning)}")));
}
}
catch (Exception ex)
{
Expand Down
Loading
Loading