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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ Reads `.rvt` projects and `.rfa` families. Testing covers sample projects saved

The tested projects include basic and advanced architecture, structure and MEP samples, plus several larger multi-discipline models.

The stored class schema determines the decoding layout, including element row and ID widths. The saving year in `BasicFileInfo` is a fallback when the schema release cannot be recognized. Diagnostics report both years when they differ; an older saving year does not override a recognized newer schema.

## Limitations

- Version 0.4.0 is a preview. The API can change before 1.0.
Expand Down
8 changes: 4 additions & 4 deletions src/RvtIO/Parsing/ElemTableLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ internal static class ElemTableLayout
internal static int? IdWidth(FormatsSchema schema)
=> schema.Get("Identifier") is { Fields: [{ } field] } && LeafWidth(field.Type) is int width && Agrees(SchemaRelease.IdWidth(schema.Release), width) ? width : null;

/// <summary>True when the schema declares an element id width that the release in force does not have (a file saved by an older
/// release than its schema): the layout of that release is not the layout of the bytes.</summary>
/// <summary>True when the declared element id width contradicts the recognized release. Such a schema must not fall back
/// to a layout with a different width.</summary>
internal static bool DisagreesWithRelease(FormatsSchema schema)
=> schema.Get("Identifier") is { Fields: [{ } field] } && LeafWidth(field.Type) is int width && !Agrees(SchemaRelease.IdWidth(schema.Release), width);

/// <summary>The widths the schema declares must be those of the release in force (the older of the saved and the schema release);
/// a file whose two releases disagree on the layout is not decoded by a layout it does not have.</summary>
/// <summary>Declared widths must agree with the recognized schema release, or with the saving release when the schema release
/// is unknown. Contradictory evidence is not decoded with a layout it does not have.</summary>
private static bool Agrees(int? releaseWidth, int declared) => releaseWidth is not int expected || expected == declared;

/// <summary>The rows of a decoded Global/ElemTable stream, or null unless the stream is this schema's ElemTable: the class
Expand Down
2 changes: 1 addition & 1 deletion src/RvtIO/Parsing/ElemTableReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static Dictionary<uint, uint> Read(RootStorage root,
{
RecordStream(diagnostics, "Global/ElemTable", data.Length, 1, 1, 0, 0, 0, exactTable.Length);
// Every version but R22: the layout comes from the file's own schema, not from a constant. The shipped R22 layout
// is used only for a file whose release in force (the older of the saved and the schema release) is 2022 or unknown.
// is used only for a file whose schema release (falling back to the saving release) is 2022 or unknown.
var formats = FormatsSchema.TryRead(root);
bool shipped = exactTable.Length >= 2 && BitConverter.ToUInt16(exactTable, 0) == ElementExportIds.ElemTableClassId
&& SchemaRelease.AllowsShipped(formats?.Release ?? SchemaRelease.Of(root, null), 2022);
Expand Down
12 changes: 6 additions & 6 deletions src/RvtIO/Parsing/SchemaRelease.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ internal static class SchemaRelease
internal static int? Saved(string? revitVersion)
=> int.TryParse(revitVersion, out int year) && year is >= 2000 and <= 2100 ? year : null;

/// <summary>The older of the two releases, which decides every layout and row-size choice; the one that is known when only one
/// is, null when neither is.</summary>
/// <summary>The stored schema decides layouts and row sizes. The saving release is only a fallback when the schema release
/// is unknown; an older or misread BasicFileInfo year must not override the layout of the bytes.</summary>
internal static int? Effective(int? saved, int? schema)
=> saved is int s && schema is int c ? Math.Min(s, c) : saved ?? schema;
=> schema ?? saved;

/// <summary>The release that decides the layouts of an open file: the older of the saved release and the schema's release
/// (<paramref name="schemaRelease"/>), null when neither is known. Every layout and row-size choice reads this one value.</summary>
/// <summary>The release that decides the layouts of an open file, preferring <paramref name="schemaRelease"/> over the saving
/// release. Every layout and row-size choice reads this one value.</summary>
internal static int? Of(OpenMcdf.RootStorage root, int? schemaRelease)
=> Effective(Saved(BasicFileInfoReader.Read(root).GetValueOrDefault("RevitVersion")), schemaRelease);

Expand All @@ -64,7 +64,7 @@ internal static class SchemaRelease
internal static string Describe(int? saved, int? schema)
{
string Show(int? year) => year?.ToString() ?? "unknown";
string note = saved is int s && schema is int c && s != c ? $", the layout of release {Math.Min(s, c)} is used" : "";
string note = saved is int s && schema is int c && s != c ? $", the layout of release {Effective(s, c)} is used" : "";
return $"Release: saved by {Show(saved)}, schema of {Show(schema)}{note}";
}
}
29 changes: 29 additions & 0 deletions tests/RvtIO.Tests/Fixtures/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,32 @@ solid and the host frame, not element names or a type-name size.
This covers unfilled extruded openings in straight walls. Curved hosts, curtain
hosts and the separately cached quantities of certified cut-pair bodies are not
covered by this fixture.

# Schema release selection regression (#31)

`SchemaReleaseTests.SyntheticFilesKeepTheDeclaredTableAndRoutingWidths` builds
small compound files with paged schema and element-table streams. It covers
both directions of a saving/schema year mismatch across the 32/64-bit ID
boundary, matching years, an unknown schema release and a missing saving year.
It requires no downloaded files.

`SchemaReleaseExportTests` additionally reads public Autodesk samples and copies
whose labelled `BasicFileInfo` `Format` year is changed. Only that saving-year
field is normalized when comparing the full model, IFC and compact JSON with
properties; all remaining content must match. The 2024 sample is tested both
with the native wide-schema reader and with `R22Layout` geometry decoding.
The tests are skipped unless the following variables name downloaded files:

| Variable | Public sample | SHA-256 |
| --- | --- | --- |
| `RVTIO_SCHEMA_SAMPLE_2022` | [Revit 2022 structural basic sample](https://revit.downloads.autodesk.com/download/2022RVT_RTM/Docs/InProd/rstbasicsampleproject.rvt) | `054a6e5803098dece0f7aa8d5248a74915696daed1a56492b95ce54d0f1de1de` |
| `RVTIO_SCHEMA_SAMPLE_2024` | [Snowdon Towers Sample Site](https://revit.downloads.autodesk.com/download/2024RVT_RTM/Docs/InProd/Samples/Snowdon%20Towers%20Sample%20Site.rvt) | `6bf4e490959c687e9ee851662c2f0f202ef486b00bf95d61c05f3fee34a2ebe5` |

The repository contains no Revit files. To run these checks in PowerShell after
downloading the samples outside the checkout:

```powershell
$env:RVTIO_SCHEMA_SAMPLE_2022 = 'C:\Samples\rstbasicsampleproject.rvt'
$env:RVTIO_SCHEMA_SAMPLE_2024 = 'C:\Samples\Snowdon Towers Sample Site.rvt'
dotnet test tests/RvtIO.Tests/RvtIO.Tests.csproj --filter 'FullyQualifiedName~SchemaRelease'
```
16 changes: 11 additions & 5 deletions tests/RvtIO.Tests/FormatsSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public SchemaBuilder Class(string name, int baseId, uint version, params (string

/// <summary>The classes the ElemTable layout needs, in the order and shape of Revit 2022 (28-byte rows, 4-byte ids) or
/// Revit 2024 and later (40-byte rows, 8-byte ids, m_history declared first). ElemTable is id 19.</summary>
private static byte[] ElemTableSchema(bool wideIds)
internal static byte[] ElemTableSchema(bool wideIds, int? markerId = null)
{
byte idType = wideIds ? (byte)0x0B : (byte)0x04;
string idName = wideIds ? "m_id64" : "m_id";
Expand All @@ -52,11 +52,17 @@ private static byte[] ElemTableSchema(bool wideIds)
.Class("ElementHistory", 0, 8, ("m_originalElementId", 0x0E, 0, 14), ("m_creationDate", 0x0E, 0, 15),
("m_lastModificationDate", 0x0E, 0, 15), ("m_lastUserModificationDate", 0x0E, 0, 15)) // 16
.Class("PartitionId", 0, 1, ("m_id", 0x04, 0, 0)); // 17
return (wideIds
(wideIds
? b.Class("ElemRec", 0, 10, ("m_history", 0x0E, 0, 16), ("m_id", 0x0E, 0, 13), ("m_OwningElementId", 0x0E, 0, 13), ("m_partitionId", 0x0E, 0, 17))
: b.Class("ElemRec", 0, 10, ("m_id", 0x0E, 0, 13), ("m_history", 0x0E, 0, 16), ("m_partitionId", 0x0E, 0, 17), ("m_OwningElementId", 0x0E, 0, 13))) // 18
.Class("ElemTable", 0, 9, ("m_elemArr", 0x0E, 0x50, 18), ("m_bExpandAllOnLoad", 0x01, 0, 0)) // 19
.Build();
.Class("ElemTable", 0, 9, ("m_elemArr", 0x0E, 0x50, 18), ("m_bExpandAllOnLoad", 0x01, 0, 0)); // 19
if (markerId is int marker)
{
for (int id = 20; id < marker; id++) b.Class("Filler" + id, 0, 1);
foreach (string name in new[] { "ContentMarker", "ContentKey", "SegmentMarker", "SegmentCheckback", "SignatureMarker" })
b.Class(name, 0, 1);
}
return b.Build();
}

[Theory]
Expand Down Expand Up @@ -140,7 +146,7 @@ public void InMemoryPointerMembersOfElemRecTakeNoBytes()
Assert.Equal(40, ElemTableLayout.RowLength(schema));
}

private static byte[] Rows(bool wideIds, params (long Id, long Original, int Created, int Partition, long Owner)[] rows)
internal static byte[] Rows(bool wideIds, params (long Id, long Original, int Created, int Partition, long Owner)[] rows)
{
var s = new MemoryStream(); var w = new BinaryWriter(s);
w.Write((ushort)19); w.Write((uint)rows.Length);
Expand Down
77 changes: 77 additions & 0 deletions tests/RvtIO.Tests/SchemaReleaseExportTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.Security.Cryptography;
using System.Text.Json;
using System.Text.Json.Serialization;
using RvtIO.Compact;
using RvtIO.Ifc;
using Xunit;

namespace RvtIO.Tests;

/// <summary>Full reads and exports of public Autodesk samples with only the BasicFileInfo Format year changed.
/// Download URLs and hashes are recorded in Fixtures/README.md; the repository contains no Revit files.</summary>
public class SchemaReleaseExportTests
{
[SkippableTheory]
[InlineData("RVTIO_SCHEMA_SAMPLE_2022", 2022, 2025, false)]
[InlineData("RVTIO_SCHEMA_SAMPLE_2024", 2024, 2022, false)]
[InlineData("RVTIO_SCHEMA_SAMPLE_2024", 2024, 2022, true)]
public void ChangingTheSavingYearPreservesTheFullReadAndExports(string variable, int schemaYear, int savingYear, bool r22Layout)
{
string original = TestFiles.FileFromEnvironment(variable);
string copy = SchemaReleaseTests.CopyWithSavedRelease(original, savingYear);
try
{
var options = new RvtReaderOptions { R22Layout = r22Layout, SystemFamilyNameLocale = "ENU" };
var originalReader = new RvtFileReader(options);
var expected = originalReader.Read(original);
var copyReader = new RvtFileReader(options);
var actual = copyReader.Read(copy);
Assert.Equal(schemaYear.ToString(), expected.Metadata.RevitVersion);
Assert.Equal(savingYear.ToString(), actual.Metadata.RevitVersion);
Assert.True(expected.ElemTableIsExact);
Assert.True(actual.ElemTableIsExact);
Assert.NotEmpty(expected.Elements);
Assert.Equal(expected.Elements.Select(e => e.Id), actual.Elements.Select(e => e.Id));
Assert.Equal(expected.GeometryUndecoded, actual.GeometryUndecoded);
Assert.Equal(expected.WideLayoutNotRewritten, actual.WideLayoutNotRewritten);

// The changed saving year is observable metadata (also used in the IFC application header).
// Normalize that one field; all other metadata, element data and export content must match.
actual.Metadata.RevitVersion = expected.Metadata.RevitVersion;
var json = new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals };
Assert.Equal(Hash(s => JsonSerializer.Serialize(s, expected, json)), Hash(s => JsonSerializer.Serialize(s, actual, json)));

var timestamp = DateTimeOffset.FromUnixTimeSeconds(0);
var originalIfc = new IfcExporter { Timestamp = timestamp };
var copyIfc = new IfcExporter { Timestamp = timestamp };
Assert.Equal(Hash(s =>
{
using var writer = new StreamWriter(s, leaveOpen: true);
originalIfc.Export(expected, writer);
}), Hash(s =>
{
using var writer = new StreamWriter(s, leaveOpen: true);
copyIfc.Export(actual, writer);
}));
Assert.True(originalIfc.ExportedProducts > 0);
Assert.Equal(originalIfc.ExportedProducts, copyIfc.ExportedProducts);

Parsing.ValidatedElementNames.Apply(expected, originalReader.LastParsingState!);
Parsing.ValidatedElementNames.Apply(actual, copyReader.LastParsingState!);
var expectedIdentity = new CompactIdentity(CompactUniverse.Select(expected));
var actualIdentity = new CompactIdentity(CompactUniverse.Select(actual));
var expectedSource = CompactPropertySources.Create(expected, originalReader.LastParsingState, true, identity: expectedIdentity);
var actualSource = CompactPropertySources.Create(actual, copyReader.LastParsingState, true, identity: actualIdentity);
Assert.Equal(Hash(s => CompactJsonExporter.Export(expected, s, expectedSource, identity: expectedIdentity)),
Hash(s => CompactJsonExporter.Export(actual, s, actualSource, identity: actualIdentity)));
}
finally { File.Delete(copy); }
}

private static string Hash(Action<Stream> write)
{
using var hash = SHA256.Create();
using (var stream = new CryptoStream(Stream.Null, hash, CryptoStreamMode.Write)) write(stream);
return Convert.ToHexString(hash.Hash!);
}
}
Loading
Loading