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 @@ -141,6 +141,7 @@ The text of a sheet prints too: a text note's RTF string (class 0x0F83 block) is
- Adds `--ifc-timestamp <timestamp>` (an ISO-8601 timestamp or Unix epoch seconds; `IfcExporter.Timestamp` in the library) to write a fixed UTC timestamp into the STEP `FILE_NAME` header so repeated IFC exports of the same input are byte-for-byte identical, and adds CI-runnable corruption and fuzz tests over synthetic `.rvt` containers and inner streams with execution-time and memory-allocation bounds alongside full Windows, Linux and macOS CI test matrices. (Closes #39)

- IFC export, current profile: a ceiling in pieces takes the area of the piece Revit's export names (rvt-rs RE-486, #1209): where a ceiling has no computed host area, `GrossCeilingArea` is the area of the piece holding its first top face (faces grouped by serialized `SourceFace`/`MeshMarker` within their geometry node), required to face a bottom piece of the same area within 0.1%, instead of the whole upper surface. With a sketch area or a single piece nothing changes. New tests: `CoveringFirstPieceTests`.
- Printing: a plan stops a wall flat at the near face of the wall it joins (rvt-rs #1474, RE-488): a butt join (JoinType 0) to another wall of the level cuts back the overshoot past the partner's near face, orthogonal joins only, mutual end-to-end L corners, mitre joins, roof-joined walls and ambiguous ends keep the stored line. New tests: `PlanWallJoinCutbackTests` (5, synthetic T/L/mitre/roof-joined/start-end); on the 2022 and 2025 fixture projects every joined end is a mutual L corner, so no printed plan changes there.

### Changed

Expand Down
2 changes: 1 addition & 1 deletion docs/printing.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ stays empty.

| Content | Source |
|---|---|
| Walls | the stored location line, drawn as two sides its stored width apart, the stretch between them poche (filled grey) with end caps (`WALL_ATTR_WIDTH_PARAM` of the wall or its type; 100 mm when neither stores one) |
| Walls | the stored location line, drawn as two sides its stored width apart, the stretch between them poche (filled grey) with end caps (`WALL_ATTR_WIDTH_PARAM` of the wall or its type; 100 mm when neither stores one); a stopping end joined to another wall (butt join) is cut back flat at the partner's near face instead of running through it, as in rvt-rs #1474 and RE-488 (orthogonal joins only; mutual end-to-end L corners, mitre joins, roof-joined walls and ambiguous ends keep the stored line) |
| Openings | the cut loop of a hosted door or window, transformed to the model, leaves its stretch of wall out; the wall is drawn in the pieces between its openings, with a jamb at every cut, and a window gets a light line across the opening |
| Columns | the stored bounding box footprint, as a filled rectangle |
| Grids | the stored location lines, clipped to the extent of the level (a grid line can run far beyond the building), with a white bubble and the grid name at both ends |
Expand Down
90 changes: 88 additions & 2 deletions src/RvtIO/Print/PlanPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,94 @@ public static PdfDocument RenderViews(RvtDocument document, IReadOnlyList<int> v
return pdf;
}

/// <summary>Shortens the ends of walls that stop at a joined wall, drawing them flat at the partner's near face
/// instead of through it (rvt-rs #1474 and RE-488): a butt join (JoinType 0, m_wallJoinType) whose end is not shared
/// with the partner's own end (a T stop, not an L corner both walls end at), on a wall with no top attachment and a
/// partner drawn on the same level with a straight location line. Only orthogonal joins move, only the overshoot past
/// the near face is taken back (never extended), and at most to a stub of the wall's own width. Mitre joins and
/// ambiguous ends (no single butt join, a partner off the level or without a line) keep the stored location line.</summary>
internal static List<(int Id, double X1, double Y1, double X2, double Y2, double Width)> CutBackJoinedEnds(
List<(int Id, double X1, double Y1, double X2, double Y2, double Width)> walls,
IReadOnlyDictionary<int, Element> elements)
{
if (walls.Count == 0) return walls;
var byId = new Dictionary<int, int>();
for (int i = 0; i < walls.Count; i++) byId[walls[i].Id] = i;
var result = walls.ToList();
for (int i = 0; i < result.Count; i++)
{
var wall = result[i];
if (!elements.TryGetValue(wall.Id, out var element)) continue;
if (element.SerializedWallJoinedToTop == true) continue;
if (element.SerializedWallEndJoins is not { Count: > 0 } joins) continue;
double dx = wall.X2 - wall.X1, dy = wall.Y2 - wall.Y1;
double length = Math.Sqrt(dx * dx + dy * dy);
if (length < 1e-6) continue;
double ux = dx / length, uy = dy / length;
for (int end = 0; end < 2; end++)
{
var candidates = joins.Where(join => join.End == end && join.JoinType == 0).ToList();
if (candidates.Count != 1) continue;
var join = candidates[0];
if (!byId.TryGetValue(join.ElementId, out int partnerIndex) || partnerIndex == i) continue;
var partner = result[partnerIndex];
if (!elements.TryGetValue(partner.Id, out var partnerElement)) continue;
double pdx = partner.X2 - partner.X1, pdy = partner.Y2 - partner.Y1;
double partnerLength = Math.Sqrt(pdx * pdx + pdy * pdy);
if (partnerLength < 1e-6) continue;
double vx = pdx / partnerLength, vy = pdy / partnerLength;
// Orthogonal butt joins only: an angled join keeps the stored line.
if (Math.Abs(ux * vx + uy * vy) > 0.02) continue;
// An L corner both walls end at keeps the overlap: the partner names this wall back at one of its ends.
bool mutualEnd = partnerElement.SerializedWallEndJoins?.Any(partnerJoin =>
partnerJoin.ElementId == wall.Id && partnerJoin.JoinedEnd is 0 or 1 && partnerJoin.JoinType == 0) == true;
if (mutualEnd && join.JoinedEnd is 0 or 1) continue;
// The near face of the partner on the side the wall comes from: travelling along
// the wall into the join first meets that face. For the end (tip in +u) the wall
// approaches along +u, so the retained interior lies behind (-denom side); for the
// start the wall leaves along +u, so the interior lies ahead (+denom side). A probe
// just inside the tip fails once the tip has crossed the partner axis, choosing the
// far face instead.
double nx = -vy, ny = vx;
double ex = end == 0 ? wall.X1 : wall.X2, ey = end == 0 ? wall.Y1 : wall.Y2;
double s = (ex - partner.X1) * nx + (ey - partner.Y1) * ny;
double denom = ux * nx + uy * ny;
if (Math.Abs(denom) < 0.98) continue;
double side = end == 0 ? Math.Sign(denom) : -Math.Sign(denom);
if (side == 0) continue;
double facing = side * partner.Width / 2;
// Displacement along +u that plants the tip on the face. The start moves forward
// (+t shortens), the end moves back (-t shortens); only an overshoot past the face
// is taken back, never an extension, and at least a millimetre.
double t = (facing - s) / denom;
// The tip must stand on the partner's span, and only an overshoot past the face
// is taken back, never an extension, and at least a millimetre.
double along = (ex - partner.X1) * vx + (ey - partner.Y1) * vy;
if (along < -partner.Width / 2 - 1 || along > partnerLength + partner.Width / 2 + 1) continue;
bool overshoot = end == 0 ? t >= 1 : t <= -1;
if (!overshoot) continue;
double newLength = end == 0 ? length - t : length + t;
if (newLength < Math.Max(wall.Width, 10)) continue;
double newX = ex + ux * t;
double newY = ey + uy * t;
result[i] = end == 0 ? (wall.Id, newX, newY, wall.X2, wall.Y2, wall.Width)
: (wall.Id, wall.X1, wall.Y1, newX, newY, wall.Width);
wall = result[i];
dx = wall.X2 - wall.X1; dy = wall.Y2 - wall.Y1;
length = Math.Sqrt(dx * dx + dy * dy);
if (length < 1e-6) break;
ux = dx / length; uy = dy / length;
}
}
return result;
}

/// <summary>Collects what the plan of a level draws: its walls (cut at the openings of their doors and windows),
/// columns and grids, and the extents they span in model millimetres. Null when the level has nothing to draw.</summary>
internal static PlanContent? CollectLevel(RvtDocument document, int levelId)
{
var walls = new List<(int Id, double X1, double Y1, double X2, double Y2, double Width)>();
var wallElements = new Dictionary<int, Element>();
var columns = new List<(double X, double Y, double Width, double Height)>();
double minX = double.MaxValue, minY = double.MaxValue, maxX = double.MinValue, maxY = double.MinValue;
void Grow(double x, double y)
Expand All @@ -232,7 +315,7 @@ void Grow(double x, double y)
double x2 = line.EndPoint[0] * FeetToMillimetres, y2 = line.EndPoint[1] * FeetToMillimetres;
double width = WidthOf(element) ?? 100;
walls.Add((element.Id, x1, y1, x2, y2, width));
Grow(x1, y1); Grow(x2, y2);
wallElements[element.Id] = element;
continue;
}

Expand All @@ -243,10 +326,13 @@ void Grow(double x, double y)
double x = box[0] * FeetToMillimetres, y = box[1] * FeetToMillimetres;
double width = (box[3] - box[0]) * FeetToMillimetres, height = (box[4] - box[1]) * FeetToMillimetres;
columns.Add((x, y, width, height));
Grow(x, y); Grow(x + width, y + height);
}
}

walls = CutBackJoinedEnds(walls, wallElements);
foreach (var wall in walls) { Grow(wall.X1, wall.Y1); Grow(wall.X2, wall.Y2); }
foreach (var column in columns) { Grow(column.X, column.Y); Grow(column.X + column.Width, column.Y + column.Height); }

if (walls.Count == 0 && columns.Count == 0) return null;

// Openings: the cut loop of a hosted door or window, transformed to the model, gives the stretch of its wall to
Expand Down
188 changes: 188 additions & 0 deletions tests/RvtIO.Tests/PlanWallJoinCutbackTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
using RvtIO.BuiltIns;
using RvtIO.Model;
using RvtIO.Parsing;
using RvtIO.Print;
using Xunit;

namespace RvtIO.Tests;

/// <summary>The plan stops a wall flat at the near face of the wall it joins (rvt-rs #1474, RE-488):
/// a butt join (JoinType 0) to another wall of the level, orthogonal, mutual end-to-end L corners and
/// roof-joined, mitre and ambiguous ends left out.</summary>
public class PlanWallJoinCutbackTests
{
private static void Add(Element element, BuiltInParameter id, StorageType storage, object value) =>
element.Parameters.Add(BipHelpers.MakeBipParam((int)id, id.ToString(), storage, value));

private static Element Level(int id, string name, double elevationFeet)
{
var level = new Element { Id = id, Name = name, CategoryId = (int)BuiltInCategory.OST_Levels };
Add(level, BuiltInParameter.LEVEL_ELEV, StorageType.Double, elevationFeet);
return level;
}

private static Element Wall(int id, int levelId, double x1, double y1, double x2, double y2,
double widthFeet = 0.656168, bool roofJoined = false,
IReadOnlyList<TfBlockDeserializer.WallEndJoin>? joins = null)
{
var wall = new Element
{
Id = id,
CategoryId = (int)BuiltInCategory.OST_Walls,
Location = new LocationCurve
{
CurveType = "Line",
StartPoint = [x1, y1, 0],
EndPoint = [x2, y2, 0],
},
};
Add(wall, BuiltInParameter.LEVEL_PARAM, StorageType.ElementId, levelId);
Add(wall, BuiltInParameter.WALL_ATTR_WIDTH_PARAM, StorageType.Double, widthFeet);
if (roofJoined) wall.SerializedWallJoinedToTop = true;
if (joins != null) wall.SerializedWallEndJoins = joins;
return wall;
}

private static double Feet(double feet) => feet * 304.8;

[Fact]
public void StoppingEnd_IsCutAtThePartnerNearFace()
{
// Wall 600 runs into the middle of the vertical wall 601 (a T stop).
var document = RvtDocument.FromInfo(new RevitFileInfo
{
Elements =
[
Level(500, "Level 1", 0),
Wall(600, 500, 0, 0, 10, 0, joins: [new TfBlockDeserializer.WallEndJoin(1, 601, 2, 0)]),
Wall(601, 500, 10, -5, 10, 5),
],
});

var content = PlanPrinter.CollectLevel(document, 500);

Assert.NotNull(content);
var wall = Assert.Single(content!.Walls, w => w.Id == 600);
// Partner half width 100 mm back from x = 10 ft.
Assert.Equal(Feet(0), wall.X1, 3);
Assert.Equal(Feet(10) - 100, wall.X2, 3);
Assert.Equal(Feet(0), wall.Y1, 3);
Assert.Equal(Feet(0), wall.Y2, 3);
}

[Fact]
public void MutualEndJoin_KeepsTheOverlap()
{
// An L corner both walls end at: each names the other's end.
var document = RvtDocument.FromInfo(new RevitFileInfo
{
Elements =
[
Level(500, "Level 1", 0),
Wall(600, 500, 0, 0, 10, 0, joins: [new TfBlockDeserializer.WallEndJoin(1, 601, 0, 0)]),
Wall(601, 500, 10, 0, 10, 10, joins: [new TfBlockDeserializer.WallEndJoin(0, 600, 1, 0)]),
],
});

var content = PlanPrinter.CollectLevel(document, 500);

Assert.NotNull(content);
var wall = Assert.Single(content!.Walls, w => w.Id == 600);
Assert.Equal(Feet(10), wall.X2, 3);
}

[Fact]
public void MitreJoin_KeepsTheStoredLine()
{
var document = RvtDocument.FromInfo(new RevitFileInfo
{
Elements =
[
Level(500, "Level 1", 0),
Wall(600, 500, 0, 0, 10, 0, joins: [new TfBlockDeserializer.WallEndJoin(1, 601, 2, 1)]),
Wall(601, 500, 10, -5, 10, 5),
],
});

var content = PlanPrinter.CollectLevel(document, 500);

Assert.NotNull(content);
var wall = Assert.Single(content!.Walls, w => w.Id == 600);
Assert.Equal(Feet(10), wall.X2, 3);
}

[Fact]
public void RoofJoinedWall_KeepsTheStoredLine()
{
var document = RvtDocument.FromInfo(new RevitFileInfo
{
Elements =
[
Level(500, "Level 1", 0),
Wall(600, 500, 0, 0, 10, 0, roofJoined: true,
joins: [new TfBlockDeserializer.WallEndJoin(1, 601, 2, 0)]),
Wall(601, 500, 10, -5, 10, 5),
],
});

var content = PlanPrinter.CollectLevel(document, 500);

Assert.NotNull(content);
var wall = Assert.Single(content!.Walls, w => w.Id == 600);
Assert.Equal(Feet(10), wall.X2, 3);
}

[Fact]
public void StartEnd_IsCutToo()
{
// The wall runs from inside the partner outwards: its start is the stopping end.
var document = RvtDocument.FromInfo(new RevitFileInfo
{
Elements =
[
Level(500, "Level 1", 0),
Wall(600, 500, 10, 0, 20, 0, joins: [new TfBlockDeserializer.WallEndJoin(0, 601, 2, 0)]),
Wall(601, 500, 10, -5, 10, 5),
],
});

var content = PlanPrinter.CollectLevel(document, 500);

Assert.NotNull(content);
var wall = Assert.Single(content!.Walls, w => w.Id == 600);
Assert.Equal(Feet(10) + 100, wall.X1, 3);
Assert.Equal(Feet(20), wall.X2, 3);
}

private static double CutEnd(double x1, double x2, int end, bool checkStart)
{
var source = new Element
{
Id = 600,
SerializedWallEndJoins = [new TfBlockDeserializer.WallEndJoin(end, 601, 2, 0)],
};
var partner = new Element { Id = 601 };
var walls = new List<(int Id, double X1, double Y1, double X2, double Y2, double Width)>
{
(600, x1, 0, x2, 0, 200),
(601, 3000, -1000, 3000, 1000, 200),
};
var result = PlanPrinter.CutBackJoinedEnds(
walls, new Dictionary<int, Element> { [600] = source, [601] = partner })[0];
return checkStart ? result.X1 : result.X2;
}

[Fact]
public void OffsetEnds_AreCutToTheNearFaceOnBothSides()
{
// Vertical partner centred at x=3000, width 200: the near face is 2900 for a wall
// ending from the west and 3100 for a wall starting to the east. The on-axis
// controls already pass through CollectLevel above.
Assert.Equal(2900, CutEnd(0, 3000, 1, checkStart: false), 3);
Assert.Equal(2900, CutEnd(0, 3050, 1, checkStart: false), 3);
Assert.Equal(2900, CutEnd(0, 2950, 1, checkStart: false), 3);
Assert.Equal(3100, CutEnd(3000, 6000, 0, checkStart: true), 3);
Assert.Equal(3100, CutEnd(2950, 6000, 0, checkStart: true), 3);
Assert.Equal(3100, CutEnd(3050, 6000, 0, checkStart: true), 3);
}
}
Loading