From 7d498f2b4165e84ba48a0164b9daf49192c4167c Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Fri, 6 Nov 2026 09:27:39 -0500 Subject: [PATCH] fix(common-line): keep copied joined parts joined when pasted Copy and Cut paste through ActionClone, which added each clone to the plate but never recorded the groups the sources were in, so a pasted common-line pair came out as separate parts held to the part spacing. ActionClone now reads the copied parts' groups when it starts (before Cut removes the sources and their groups) and joins each paste's clones the same way, at the source group's kerf. Co-Authored-By: Claude Opus 5.5 --- .../Controls/CommonLineSelectionTests.cs | 36 +++++++++++++++++++ OpenNest/Actions/ActionClone.cs | 26 +++++++++++++- docs/common-line-cutting.md | 2 ++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/OpenNest.WinForms.Tests/Controls/CommonLineSelectionTests.cs b/OpenNest.WinForms.Tests/Controls/CommonLineSelectionTests.cs index 577d86a3..9064c20e 100644 --- a/OpenNest.WinForms.Tests/Controls/CommonLineSelectionTests.cs +++ b/OpenNest.WinForms.Tests/Controls/CommonLineSelectionTests.cs @@ -1,3 +1,4 @@ +using OpenNest.Actions; using OpenNest.Controls; using OpenNest.Geometry; @@ -112,6 +113,41 @@ public class CommonLineSelectionTests Assert.Null(view.PartMenu.BuildMenu(view.PointWorldToControl(left.BoundingBox.Center))); }); + [Fact] + public void PastingACopiedJoinedPairKeepsTheCopiesJoined() => RunSta(() => + { + var (plate, left, right, loose) = JoinedPairWithLoosePart(); + using var view = new PlateView { Plate = plate }; + + view.SetAction(typeof(ActionClone), new List { left, right, loose }); + Assert.IsType(view.Actions.CurrentAction).Apply(); + + Assert.Equal(6, plate.Parts.Count); + Assert.Equal(2, plate.CommonLines.Count); + var pasted = plate.CommonLines[1]; + Assert.DoesNotContain(pasted.Members, p => p == left || p == right || p == loose); + Assert.Equal(Kerf, pasted.Kerf); + Assert.True(CommonLineGeometry.Check(pasted).IsIntact); + }); + + [Fact] + public void PastingACutJoinedPairKeepsItJoined() => RunSta(() => + { + var (plate, left, right, _) = JoinedPairWithLoosePart(); + using var view = new PlateView { Plate = plate }; + + // Edit > Cut starts the clone, then removes the sources and their group. + view.SetAction(typeof(ActionClone), new List { left, right }); + plate.Parts.Remove(left); + plate.Parts.Remove(right); + Assert.Empty(plate.CommonLines); + Assert.IsType(view.Actions.CurrentAction).Apply(); + + var group = Assert.Single(plate.CommonLines); + Assert.Equal(2, group.Members.Count); + Assert.True(CommonLineGeometry.Check(group).IsIntact); + }); + private static List MenuItems(System.Windows.Forms.ContextMenuStrip? menu) { Assert.NotNull(menu); diff --git a/OpenNest/Actions/ActionClone.cs b/OpenNest/Actions/ActionClone.cs index 543caa06..f1483c5f 100644 --- a/OpenNest/Actions/ActionClone.cs +++ b/OpenNest/Actions/ActionClone.cs @@ -14,6 +14,7 @@ namespace OpenNest.Actions { private readonly List parts; private readonly List sourceParts; + private readonly List<(int[] Members, double Kerf)> commonLines; private double lastScale; private System.Drawing.Point? restingCursor; @@ -38,9 +39,29 @@ namespace OpenNest.Actions for (int i = 0; i < partsToClone.Count; i++) parts.Add(LayoutPart.Create(partsToClone[i].Clone() as Part, plateView)); + commonLines = CopiedCommonLines(plateView.Plate, partsToClone); + SelectPreviews(); } + // The common-line groups among the copied parts, by index into the copy, so each paste + // joins its clones the same way. Read now: Cut removes the sources, and their groups, next. + private static List<(int[] Members, double Kerf)> CopiedCommonLines(Plate plate, List copied) + { + var groups = new List<(int[] Members, double Kerf)>(); + if (plate == null) + return groups; + + foreach (var group in plate.CommonLines) + { + var members = Enumerable.Range(0, copied.Count).Where(i => group.Contains(copied[i])).ToArray(); + if (members.Length >= 2) + groups.Add((members, group.Kerf)); + } + + return groups; + } + // Re-flag on every reselect: a deselect elsewhere would otherwise leave the // previews drawn with the solid fill of placed parts. private void SelectPreviews() @@ -219,7 +240,10 @@ namespace OpenNest.Actions plateView.Invalidate(); } - parts.ForEach(p => plateView.Plate.Parts.Add(p.BasePart.Clone() as Part)); + var placed = parts.Select(p => p.BasePart.Clone() as Part).ToList(); + placed.ForEach(plateView.Plate.Parts.Add); + foreach (var (members, kerf) in commonLines) + plateView.Plate.JoinCommonLine(members.Select(i => placed[i]), kerf); if (plateView.Plate.CutOffs.Count > 0) plateView.Plate.RegenerateCutOffs(plateView.CutOffSettings); diff --git a/docs/common-line-cutting.md b/docs/common-line-cutting.md index b3a862c0..c04432cd 100644 --- a/docs/common-line-cutting.md +++ b/docs/common-line-cutting.md @@ -101,6 +101,8 @@ head on the second. Changing the setting after planning makes the plan stale. - Selecting a member, by click or window, selects its whole group, so dragging, nudging, pushing, rotating and aligning move the joined parts as one and keep their shared spans. Deleting the selection deletes the whole group. To move one member alone, remove the group first. +- Copying or cutting joined parts and pasting them keeps each pasted copy joined at the source + group's kerf; members copied without their partners paste as loose parts. - Join, Remove and Cut Common Lines Twice are undoable like any other edit. ## Automatic placement