mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-22 04:17:40 -05:00
feat(cad-converter): add Select Chain to the entity right-click menu
Picking every piece of a contour one click at a time is slow. Select Chain grows the selection through every line and arc joined end to end (at the shape-building chain tolerance), so a closed loop, and anything branching off it, is selected whole. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
692fc280f6
commit
be0fc8dfca
3 files changed
+162
No files matched your search
@@ -0,0 +1,87 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenNest.Math;
|
||||
|
||||
namespace OpenNest.Geometry
|
||||
{
|
||||
/// <summary>
|
||||
/// Finds the geometry joined to a set of entities end to end, as the CAD Converter's
|
||||
/// Select Chain does.
|
||||
/// </summary>
|
||||
public static class EntityChains
|
||||
{
|
||||
/// <summary>
|
||||
/// Every entity of <paramref name="pool"/> reachable from <paramref name="seeds"/> through
|
||||
/// shared endpoints within <paramref name="tolerance"/> (default
|
||||
/// <see cref="Tolerance.ChainTolerance"/>, 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.
|
||||
/// </summary>
|
||||
public static List<Entity> Connected(IEnumerable<Entity> pool, IEnumerable<Entity> 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<Entity>>();
|
||||
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<Entity>();
|
||||
bucket.Add(entity);
|
||||
}
|
||||
}
|
||||
|
||||
var found = new HashSet<Entity>();
|
||||
var queue = new Queue<Entity>();
|
||||
var outside = new List<Entity>();
|
||||
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<Vector>(),
|
||||
};
|
||||
|
||||
private static (long, long) Cell(Vector point, double size) =>
|
||||
((long)System.Math.Floor(point.X / size), (long)System.Math.Floor(point.Y / size));
|
||||
}
|
||||
}
|
||||
@@ -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 }));
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/// <summary>Adds every selectable entity joined end to end to the current selection.</summary>
|
||||
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<Entity> selection)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user