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