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 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-06 09:27:39 -05:00
1 parent f09a9ad705
commit 7d498f2b41
3 files changed
+63 -1

No files matched your search

@@ -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<Part> { left, right, loose });
Assert.IsType<ActionClone>(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<Part> { left, right });
plate.Parts.Remove(left);
plate.Parts.Remove(right);
Assert.Empty(plate.CommonLines);
Assert.IsType<ActionClone>(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<System.Windows.Forms.ToolStripItem> MenuItems(System.Windows.Forms.ContextMenuStrip? menu)
{
Assert.NotNull(menu);
+25 -1
View File
@@ -14,6 +14,7 @@ namespace OpenNest.Actions
{
private readonly List<LayoutPart> parts;
private readonly List<Part> 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<Part> 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);
+2
View File
@@ -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