diff --git a/OpenNest.Desktop/Interaction/PlateArrange.cs b/OpenNest.Desktop/Interaction/PlateArrange.cs
index 838cf672..2e20461c 100644
--- a/OpenNest.Desktop/Interaction/PlateArrange.cs
+++ b/OpenNest.Desktop/Interaction/PlateArrange.cs
@@ -224,6 +224,9 @@ public static class PlateArrange
? (result.PatternMoved
? $"Spread the repeating pattern - smallest gap {result.AchievedSpacing:0.###}"
: $"The repeating pattern has no room to spread - smallest gap {result.AchievedSpacing:0.###}")
+ : result.SpacingX != null || result.SpacingY != null
+ ? "Opened gaps: " + string.Join(", ", new[] { ("X", result.SpacingX), ("Y", result.SpacingY) }
+ .Select(a => a.Item2 is { } gap ? $"{gap:0.###} along {a.Item1}" : $"no room along {a.Item1}"))
: $"Expanded spacing to {result.AchievedSpacing:0.###}"
+ (result.Violations.Count > 0 ? $" - {result.Violations.Count} blocked pairs" : "");
diff --git a/OpenNest.Engine.Tests/Fill/ExpanderSweepTests.cs b/OpenNest.Engine.Tests/Fill/ExpanderSweepTests.cs
new file mode 100644
index 00000000..3d61e3ef
--- /dev/null
+++ b/OpenNest.Engine.Tests/Fill/ExpanderSweepTests.cs
@@ -0,0 +1,108 @@
+using System.Collections.Generic;
+using System.Linq;
+using OpenNest.Engine.Fill;
+using OpenNest.Engine.Tests.Jobs;
+using OpenNest.Geometry;
+using Xunit;
+
+namespace OpenNest.Engine.Tests.Fill;
+
+///
+/// Expand Spacing on a packed selection of two bar groups, which is no single
+/// repeating pattern: each axis with room opens on its own.
+///
+public class ExpanderSweepTests
+{
+ private const double Gap = 0.25;
+ private const double Edge = 0.25;
+
+ /// Two columns of flat bars with three upright bars at their end,
+ /// all at a 0.25 gap but for a wider one before the uprights.
+ private static (Plate Plate, List Parts) TwoBarGroups(double widthY)
+ {
+ const int rows = 8;
+ const double barHeight = 2.21875; // eight rows fill 19.5 exactly
+ var plate = new Plate(new Size(widthY, 30)) { EdgeSpacing = new Spacing(Edge, Edge) };
+ var flat = new Drawing("flat", TestDrawingFactory.Rectangle(10, barHeight));
+ var upright = new Drawing("upright", TestDrawingFactory.Rectangle(2, 19.5));
+ var parts = new List();
+ for (var row = 0; row < rows; row++)
+ for (var column = 0; column < 2; column++)
+ parts.Add(new Part(flat, new Vector(Edge + column * (10 + Gap), Edge + row * (barHeight + Gap))));
+ for (var i = 0; i < 3; i++)
+ parts.Add(new Part(upright, new Vector(21.5 + i * (2 + Gap), Edge)));
+ foreach (var part in parts)
+ plate.Parts.Add(part);
+ return (plate, parts);
+ }
+
+ private static double SmallestGap(List parts) => parts
+ .SelectMany((a, i) => parts.Skip(i + 1).Select(b => BoxGap(a.BoundingBox, b.BoundingBox)))
+ .Min();
+
+ private static double BoxGap(Box a, Box b)
+ {
+ var x = System.Math.Max(0, System.Math.Max(b.Left - a.Right, a.Left - b.Right));
+ var y = System.Math.Max(0, System.Math.Max(b.Bottom - a.Top, a.Bottom - b.Top));
+ return System.Math.Sqrt(x * x + y * y);
+ }
+
+ [Fact]
+ public void Expand_PinnedAlongY_OpensX()
+ {
+ var (plate, parts) = TwoBarGroups(widthY: 20);
+ var ys = parts.Select(p => p.Location.Y).ToList();
+
+ var result = Expander.Expand(parts, plate);
+
+ Assert.Null(result.SpacingY);
+ Assert.NotNull(result.SpacingX);
+ Assert.True(result.SpacingX > 0.5, $"X={result.SpacingX:R}");
+ Assert.Empty(result.Violations);
+ for (var i = 0; i < parts.Count; i++)
+ Assert.Equal(ys[i], parts[i].Location.Y, 9);
+
+ // Every gap along the chain opened to the X spacing.
+ var firstRow = parts.Take(2).Concat(parts.Skip(16)).Select(p => p.BoundingBox).ToList();
+ for (var i = 1; i < firstRow.Count; i++)
+ Assert.True(firstRow[i].Left - firstRow[i - 1].Right >= result.SpacingX - 1e-6);
+ var work = plate.WorkArea();
+ Assert.True(parts.Max(p => p.BoundingBox.Right) <= work.Right + 1e-9);
+ Assert.True(SmallestGap(parts) >= Gap - 1e-6);
+ }
+
+ [Fact]
+ public void Expand_RoomOnBothAxes_OpensBoth()
+ {
+ var (plate, parts) = TwoBarGroups(widthY: 21);
+
+ var result = Expander.Expand(parts, plate);
+
+ Assert.True(result.AchievedSpacing > Gap + 0.05, $"achieved={result.AchievedSpacing:R}");
+ Assert.Empty(result.Violations);
+ Assert.True(SmallestGap(parts) >= result.AchievedSpacing - 1e-6);
+ var work = plate.WorkArea();
+ Assert.All(parts, p =>
+ {
+ Assert.True(p.BoundingBox.Right <= work.Right + 1e-9);
+ Assert.True(p.BoundingBox.Top <= work.Top + 1e-9);
+ });
+ }
+
+ [Fact]
+ public void Expand_NoRoomOnEitherAxis_MovesNothing()
+ {
+ var (plate, parts) = TwoBarGroups(widthY: 20);
+ foreach (var part in parts.Skip(16))
+ part.Offset(-0.75, 0); // close the wide gap before the uprights
+ plate.Size = new Size(20, 27.5); // and end the last one on the work-area edge
+ var before = parts.Select(p => p.Location).ToList();
+
+ var result = Expander.Expand(parts, plate);
+
+ Assert.Null(result.SpacingX);
+ Assert.Null(result.SpacingY);
+ for (var i = 0; i < parts.Count; i++)
+ Assert.Equal(0, parts[i].Location.DistanceTo(before[i]), 6);
+ }
+}
diff --git a/OpenNest.Engine/Fill/Expander.cs b/OpenNest.Engine/Fill/Expander.cs
index a65cc122..c6366389 100644
--- a/OpenNest.Engine/Fill/Expander.cs
+++ b/OpenNest.Engine/Fill/Expander.cs
@@ -31,6 +31,10 @@ namespace OpenNest.Engine.Fill
/// A selection that repeats on a lattice skips the pair search: its lattice
/// is stretched per axis instead, so the pattern is kept (ExpanderPattern.cs).
///
+ ///
+ /// When the pair search gains nothing, the selection is opened axis by axis
+ /// where it has room (ExpanderSweep.cs).
+ ///
///
public static partial class Expander
{
@@ -87,6 +91,13 @@ namespace OpenNest.Engine.Fill
/// True when a kept pattern had room to spread and moved.
public bool PatternMoved;
+
+ /// The gap opened along X when the selection was opened axis
+ /// by axis; null when X did not open or another search ran.
+ public double? SpacingX;
+
+ /// As , along Y.
+ public double? SpacingY;
}
/// Per-call test checkpoints; no process-global mutable hook.
@@ -262,6 +273,16 @@ namespace OpenNest.Engine.Fill
sGood = lo;
}
+ // The pair search gained nothing on a packed selection: open whichever
+ // axis still has room.
+ if (sMin > 0 && sGood <= sMin + opt.Tolerance && IsPacked(plate, sMin)
+ && context.WorkAreaViolations(entry).Count == 0)
+ {
+ var swept = SweepAxes(context, entry, selected, opt, token);
+ if (swept != null)
+ return swept;
+ }
+
// Finalize: re-separate at the applied spacing from the entry state.
// Deterministic, so this reproduces any committed probe exactly; when
// sGood was never probed (overlapping entry) it still performs the
diff --git a/OpenNest.Engine/Fill/ExpanderSweep.cs b/OpenNest.Engine/Fill/ExpanderSweep.cs
new file mode 100644
index 00000000..b5b9b747
--- /dev/null
+++ b/OpenNest.Engine/Fill/ExpanderSweep.cs
@@ -0,0 +1,197 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading;
+using OpenNest.Geometry;
+using OpenNest.Math;
+
+namespace OpenNest.Engine.Fill
+{
+ public static partial class Expander
+ {
+ ///
+ /// Opens a tightly packed selection one axis at a time, for the layouts
+ /// the pair search cannot move: every pair there must reach one spacing,
+ /// so a single pinned direction (or a long stack it fails to ripple
+ /// through) leaves the other direction's room unused. Each axis takes the
+ /// widest gap its longest chain of parts fits, keeping their order.
+ /// Returns null when the selection has cut-offs or parts whose boxes
+ /// overlap, or when neither axis has room.
+ ///
+ private static Result SweepAxes(
+ SeparationContext context,
+ List entry,
+ List selected,
+ Options opt,
+ CancellationToken token
+ )
+ {
+ if (selected.Any(p => p.BaseDrawing.IsCutOff))
+ return null;
+
+ var boxes = selected.Select(p => p.BoundingBox).ToArray();
+ for (var i = 0; i < boxes.Length; i++)
+ for (var j = i + 1; j < boxes.Length; j++)
+ if (AxisGap(boxes[i], boxes[j], horizontal: true) < -Tolerance.Epsilon
+ && AxisGap(boxes[i], boxes[j], horizontal: false) < -Tolerance.Epsilon)
+ return null;
+
+ // As for a pattern, no selected gap closes below the tightest one.
+ var floor = double.MaxValue;
+ foreach (var pair in context.Pairs)
+ if (context.IsMover(pair.IndexB))
+ floor = System.Math.Min(floor, pair.EntryFloor);
+ if (!(floor > 0) || floor == double.MaxValue)
+ return null;
+
+ var positions = new List(entry);
+ var x = SweepAxis(context, entry, positions, boxes, horizontal: true, floor, opt, token);
+ var y = SweepAxis(context, entry, positions, boxes, horizontal: false, floor, opt, token);
+ if (token.IsCancellationRequested)
+ return new Result { Canceled = true };
+ if (x == null && y == null)
+ return null;
+
+ if (!context.MeetsEntryFloors(positions, entry, floor))
+ throw new InvalidOperationException("Axis sweep violates an entry clearance floor.");
+
+ double measured;
+ try
+ {
+ measured = MinimumPairClearance(context, positions, token);
+ }
+ catch (OperationCanceledException) when (token.IsCancellationRequested)
+ {
+ return new Result { Canceled = true };
+ }
+ if (token.IsCancellationRequested)
+ return new Result { Canceled = true };
+
+ Apply(selected, entry, positions);
+ return new Result
+ {
+ AchievedSpacing = System.Math.Max(0, measured),
+ SpacingX = x,
+ SpacingY = y,
+ };
+ }
+
+ /// True when the tightest gap is still near the plate's own
+ /// spacings, as in a nest; parts placed far apart are not opened up.
+ private static bool IsPacked(Plate plate, double spacing)
+ {
+ var edge = plate.EdgeSpacing;
+ var widest = System.Math.Max(plate.PartSpacing, System.Math.Max(
+ System.Math.Max(edge.Left, edge.Right), System.Math.Max(edge.Bottom, edge.Top)));
+ return spacing <= 2 * widest;
+ }
+
+ /// The gap from the lower box to the higher one along an axis;
+ /// negative when their extents on that axis overlap.
+ private static double AxisGap(Box a, Box b, bool horizontal) => horizontal
+ ? System.Math.Max(b.Left - a.Right, a.Left - b.Right)
+ : System.Math.Max(b.Bottom - a.Top, a.Bottom - b.Top);
+
+ ///
+ /// Moves parts toward the high side of one axis to the widest spacing that
+ /// fits, updating . Returns that spacing, or
+ /// null when the axis cannot open (or the run was canceled).
+ ///
+ private static double? SweepAxis(
+ SeparationContext context,
+ List entry,
+ List positions,
+ Box[] entryBoxes,
+ bool horizontal,
+ double floor,
+ Options opt,
+ CancellationToken token
+ )
+ {
+ var count = entryBoxes.Length;
+ var boxes = new Box[count];
+ for (var i = 0; i < count; i++)
+ boxes[i] = entryBoxes[i].Translate(positions[i] - entry[i]);
+
+ double LowOf(Box box) => horizontal ? box.Left : box.Bottom;
+ double HighOf(Box box) => horizontal ? box.Right : box.Top;
+ var order = Enumerable.Range(0, count).OrderBy(i => LowOf(boxes[i])).ToArray();
+
+ // The positions at a target spacing, each part pushed only as far as
+ // its edge strip and its lower neighbors need; null when they do not
+ // fit or break a floor.
+ (List Positions, bool Widens)? At(double spacing)
+ {
+ var limits = context.EdgeLimits(spacing);
+ var (lowLimit, highLimit) = horizontal
+ ? (limits.Left, limits.Right)
+ : (limits.Bottom, limits.Top);
+ var move = new double[count];
+ var widens = false;
+ for (var n = 0; n < count; n++)
+ {
+ var j = order[n];
+ move[j] = System.Math.Max(0, lowLimit - LowOf(boxes[j]));
+ for (var m = 0; m < n; m++)
+ {
+ var i = order[m];
+ var gap = LowOf(boxes[j]) - HighOf(boxes[i]);
+ if (gap < -Tolerance.Epsilon)
+ continue; // side by side on this axis, not in a chain
+ var cross = System.Math.Max(0, AxisGap(boxes[i], boxes[j], !horizontal));
+ if (cross >= spacing)
+ continue;
+ // The lower part's own push carries over, less any gap
+ // to spare.
+ var need = System.Math.Sqrt(spacing * spacing - cross * cross) - gap;
+ widens |= need > Tolerance.Epsilon;
+ move[j] = System.Math.Max(move[j], move[i] + need);
+ }
+ if (move[j] > 0 && HighOf(boxes[j]) + move[j] > highLimit + 1e-9)
+ return null;
+ if (move[j] == 0 && HighOf(boxes[j]) > highLimit + 1e-9)
+ return null;
+ }
+
+ var trial = new List(positions);
+ for (var i = 0; i < count; i++)
+ {
+ if (move[i] <= 0)
+ continue;
+ var delta = horizontal ? new Vector(move[i], 0) : new Vector(0, move[i]);
+ trial[i] = positions[i] + delta;
+ if (context.HasWalls
+ && context.ClipToMaterial(i, positions, delta / move[i], move[i], wallsOnly: true)
+ < move[i] - 1e-6)
+ return null;
+ }
+ return context.MeetsEntryFloors(trial, entry, floor) ? (trial, widens) : null;
+ }
+
+ var good = floor;
+ var bad = horizontal ? context.WorkArea.Length : context.WorkArea.Width;
+ List best = null;
+ while (bad - good > opt.Tolerance)
+ {
+ if (token.IsCancellationRequested)
+ return null;
+ var mid = (good + bad) / 2;
+ var probe = At(mid);
+ if (probe == null)
+ {
+ bad = mid;
+ continue;
+ }
+ good = mid;
+ // A probe that only pushes parts off an edge opens no gap.
+ best = probe.Value.Widens ? probe.Value.Positions : null;
+ }
+
+ if (best == null || good <= floor + opt.Tolerance)
+ return null;
+ for (var i = 0; i < count; i++)
+ positions[i] = best[i];
+ return good;
+ }
+ }
+}
diff --git a/docs/geometry/expand-spacing.md b/docs/geometry/expand-spacing.md
index b36d83e3..fbe55c5f 100644
--- a/docs/geometry/expand-spacing.md
+++ b/docs/geometry/expand-spacing.md
@@ -2,6 +2,10 @@
`Plate > Expand Spacing` moves selected parts by translation only. Any selected part may move: a pair of selected parts shares each push (the later part takes half, the earlier part the rest, and the later part whatever its partner has no room for), so the selection opens about its middle and a part on the edge leaves the push to its partner. A selected part pushes away from a nonselected part alone, and nonselected parts remain fixed. The search probes ever wider spacings (doubling, then bisecting between the last spacing that converged and the first that failed) and applies the widest one that converged. The command does not change `PartSpacing` or `EdgeSpacing`. To push apart only the parts the overlap check flags, use [Fix Spacing](fix-spacing.md).
+## Opening one axis at a time
+
+The pair search above asks every pair to reach one spacing, so one pinned direction, or a long stack it cannot ripple through, can leave the whole selection unmoved while the other direction has room. When it gains nothing on a packed selection (its smallest gap is at most twice the plate's part or widest edge spacing, and it did not start overlapping or outside the work area), the selection is opened along X, then along Y. This covers, for example, two groups of flat bars at different rotations, which are no single repeating pattern. Parts move only toward the high side of the axis and keep their order: each is pushed just far enough that its gap to every lower neighbor reaches the axis's spacing, which is the widest at which the longest chain of parts still fits with its edge strips. Wider gaps absorb the push first. As for a repeating pattern, no selected gap closes below the smallest selected gap at entry, gaps to nonselected parts close no further than that, no clamp is entered and no part passes through a fixed part. The status bar reads, for example, "Opened gaps: 0.55 along X, no room along Y". Selections with cut-offs or with parts whose bounding boxes overlap are left to the pair search.
+
## Edge strips
The strip of sheet between a part and the plate edge grows with the spacing: at a target spacing *s*, each selected part's box stays at least *s*/2 from every plate edge, and never inside that side's `EdgeSpacing`. A web between two parts takes heat from a cut on each side, while an edge strip takes it from one, so a strip half as wide holds about as much material per cut. Until *s* passes twice the edge spacing the strips are just the edge spacing, as before. Beyond that, a part closer to the edge than *s*/2 is pushed straight inward, as far as material and its pair floors allow. A probe converges only when every strip holds, so a part boxed in between the edge and a fixed part caps the spacing at the widest *s* its strip and gap can share. The strip never counts toward the reported spacing. When no wider spacing converges, the layout keeps its entry spacing, and edge parts move inward only as far as that spacing's floors allow. Cut-offs run to the edge by design and have no strip, and a part already outside the work area is reported as an edge violation instead. A clamp keep-out stays a hard boundary with no strip.