diff --git a/OpenNest.Core/Geometry/EntityChains.cs b/OpenNest.Core/Geometry/EntityChains.cs
new file mode 100644
index 00000000..770dd4f6
--- /dev/null
+++ b/OpenNest.Core/Geometry/EntityChains.cs
@@ -0,0 +1,87 @@
+using System.Collections.Generic;
+using System.Linq;
+using OpenNest.Math;
+
+namespace OpenNest.Geometry
+{
+ ///
+ /// Finds the geometry joined to a set of entities end to end, as the CAD Converter's
+ /// Select Chain does.
+ ///
+ public static class EntityChains
+ {
+ ///
+ /// Every entity of reachable from through
+ /// shared endpoints within (default
+ /// , the tolerance shapes are built with), including
+ /// the seeds themselves. Branches are followed, so a closed loop with a line running off it
+ /// comes back whole. Circles have no endpoints and join nothing. Seeds outside the pool are
+ /// returned but not grown from. Results keep the pool's order, seeds outside it last.
+ ///
+ public static List Connected(IEnumerable pool, IEnumerable seeds,
+ double tolerance = Tolerance.ChainTolerance)
+ {
+ var candidates = pool.Where(e => e is Line or Arc).Distinct().ToList();
+ var cellSize = System.Math.Max(tolerance, Tolerance.Epsilon) * 4;
+ var grid = new Dictionary<(long, long), List>();
+ foreach (var entity in candidates)
+ {
+ foreach (var end in Ends(entity))
+ {
+ var key = Cell(end, cellSize);
+ if (!grid.TryGetValue(key, out var bucket))
+ grid[key] = bucket = new List();
+ bucket.Add(entity);
+ }
+ }
+
+ var found = new HashSet();
+ var queue = new Queue();
+ var outside = new List();
+ foreach (var seed in seeds)
+ {
+ if (!found.Add(seed))
+ continue;
+ if (seed is Line or Arc)
+ queue.Enqueue(seed);
+ if (!candidates.Contains(seed))
+ outside.Add(seed);
+ }
+
+ while (queue.Count > 0)
+ {
+ foreach (var end in Ends(queue.Dequeue()))
+ {
+ var (cx, cy) = Cell(end, cellSize);
+ for (var dx = -1L; dx <= 1; dx++)
+ {
+ for (var dy = -1L; dy <= 1; dy++)
+ {
+ if (!grid.TryGetValue((cx + dx, cy + dy), out var bucket))
+ continue;
+ foreach (var other in bucket)
+ {
+ if (found.Contains(other) || !Ends(other).Any(p => p.DistanceTo(end) <= tolerance))
+ continue;
+ found.Add(other);
+ queue.Enqueue(other);
+ }
+ }
+ }
+ }
+ }
+
+ return candidates.Where(found.Contains).Concat(outside).ToList();
+ }
+
+ private static Vector[] Ends(Entity entity) => entity switch
+ {
+ Line line => new[] { line.StartPoint, line.EndPoint },
+ Arc arc => new[] { arc.StartPoint(), arc.EndPoint() },
+ _ => System.Array.Empty(),
+ };
+
+ private static (long, long) Cell(Vector point, double size) =>
+ ((long)System.Math.Floor(point.X / size), (long)System.Math.Floor(point.Y / size));
+ }
+}
diff --git a/OpenNest.Tests/Geometry/EntityChainsTests.cs b/OpenNest.Tests/Geometry/EntityChainsTests.cs
new file mode 100644
index 00000000..2dcda7dd
--- /dev/null
+++ b/OpenNest.Tests/Geometry/EntityChainsTests.cs
@@ -0,0 +1,55 @@
+using OpenNest.Geometry;
+using OpenNest.Math;
+
+namespace OpenNest.Tests.Geometry;
+
+public class EntityChainsTests
+{
+ [Fact]
+ public void ClosedLoop_ComesBackWhole()
+ {
+ var bottom = new Line(0, 0, 10, 0);
+ var right = new Line(10, 0, 10, 5);
+ var corner = new Arc(8, 5, 2, 0, System.Math.PI / 2); // (10,5) to (8,7)
+ var top = new Line(0, 7, 8, 7);
+ var left = new Line(0, 0, 0, 7);
+ var stray = new Line(20, 20, 30, 20);
+
+ var chain = EntityChains.Connected(
+ new Entity[] { bottom, right, corner, top, left, stray }, new[] { top });
+
+ Assert.Equal(new Entity[] { bottom, right, corner, top, left }, chain);
+ }
+
+ [Fact]
+ public void OpenChain_FollowsBranchesAndIgnoresReversedEnds()
+ {
+ var a = new Line(0, 0, 5, 0);
+ var b = new Line(10, 0, 5, 0); // joined at its end, not its start
+ var branch = new Line(5, 0, 5, 5);
+ var separate = new Line(5.01, 5.01, 9, 9);
+
+ var chain = EntityChains.Connected(new Entity[] { a, b, branch, separate }, new[] { a });
+
+ Assert.Equal(new Entity[] { a, b, branch }, chain);
+ }
+
+ [Fact]
+ public void EndsWithinToleranceJoin()
+ {
+ var a = new Line(0, 0, 5, 0);
+ var b = new Line(5 + Tolerance.ChainTolerance / 2, 0, 5, 5);
+
+ Assert.Equal(2, EntityChains.Connected(new Entity[] { a, b }, new[] { a }).Count);
+ }
+
+ [Fact]
+ public void Circle_JoinsNothing()
+ {
+ var circle = new Circle(0, 0, 5);
+ var line = new Line(5, 0, 10, 0);
+
+ Assert.Equal(new Entity[] { line }, EntityChains.Connected(new Entity[] { circle, line }, new[] { line }));
+ Assert.Equal(new Entity[] { circle }, EntityChains.Connected(new Entity[] { line }, new[] { circle }));
+ }
+}
diff --git a/OpenNest/Controls/EntityView.Selection.cs b/OpenNest/Controls/EntityView.Selection.cs
index 9ff15e9b..c98d0e19 100644
--- a/OpenNest/Controls/EntityView.Selection.cs
+++ b/OpenNest/Controls/EntityView.Selection.cs
@@ -191,6 +191,16 @@ namespace OpenNest.Controls
var menu = new ContextMenuStrip();
menu.Items.Add(new ToolStripLabel(noun) { ForeColor = SystemColors.GrayText });
+ if (selection.Any(en => en is Line or Arc))
+ {
+ var chain = new ToolStripMenuItem("Select Chain")
+ {
+ ToolTipText = "Select every line and arc joined end to end to the selection, the whole loop when it closes.",
+ };
+ chain.Click += (s, e) => SelectChain();
+ menu.Items.Add(chain);
+ menu.Items.Add(new ToolStripSeparator());
+ }
AddRoleItem(menu, "Cut", EntityAction.Cut, current == EntityRole.Cut, selection);
AddRoleItem(menu, "Etch", EntityAction.Etch, current == EntityRole.Etch, selection);
AddRoleItem(menu, "Display Only", EntityAction.Display, current == EntityRole.Display, selection);
@@ -236,6 +246,16 @@ namespace OpenNest.Controls
menu.Show(this, location);
}
+ /// Adds every selectable entity joined end to end to the current selection.
+ public void SelectChain()
+ {
+ var chain = EntityChains.Connected(Entities.Where(IsSelectable), selectedEntities.ToList());
+ var before = selectedEntities.Count;
+ selectedEntities.UnionWith(chain);
+ if (selectedEntities.Count != before)
+ Invalidate();
+ }
+
private void AddRoleItem(ContextMenuStrip menu, string text, EntityAction action, bool isChecked,
List selection)
{