using System.Linq; using System.Threading; using System.Threading.Tasks; using OpenNest.Desktop.Interaction; using OpenNest.Engine.Fill; using OpenNest.Geometry; namespace OpenNest.Desktop.Tests.Interaction; public class PlateArrangeTests { private readonly Plate plate = InteractionFixtures.Plate(); private readonly TestToolHost host; public PlateArrangeTests() { host = new TestToolHost(plate); } private PlateSelection Selection => host.Selection; private static void AssertNear(double expected, double actual) => Assert.True(System.Math.Abs(expected - actual) < 1e-9, $"expected {expected}, was {actual}"); [Fact] public void Align_left_lines_the_selection_up_with_the_first_selected_part_as_one_step() { var recorder = host.RecordHistory(); var fixedPart = plate.Parts[1]; var moving = plate.Parts[4]; moving.Offset(7, 0); recorder.Reset(); Selection.Select(fixedPart); Selection.Select(moving); Assert.True(PlateArrange.Align(host, AlignType.Left)); AssertNear(fixedPart.BoundingBox.Left, moving.BoundingBox.Left); AssertNear(20, fixedPart.BoundingBox.Left); Assert.Equal("Align", recorder.History.UndoLabel); Assert.Equal(1, recorder.History.UndoCount); Assert.True(host.Invalidations > 0); } [Theory] [InlineData(AlignType.Right)] [InlineData(AlignType.Top)] [InlineData(AlignType.Bottom)] [InlineData(AlignType.Horizontally)] [InlineData(AlignType.Vertically)] public void Each_alignment_matches_the_core_alignment(AlignType type) { var fixedPart = plate.Parts[1]; var moving = plate.Parts[4]; moving.Offset(3, 4); Selection.Select(fixedPart); Selection.Select(moving); var expected = new Part(moving.BaseDrawing, moving.Location); switch (type) { case AlignType.Right: Align.Right(fixedPart, expected); break; case AlignType.Top: Align.Top(fixedPart, expected); break; case AlignType.Bottom: Align.Bottom(fixedPart, expected); break; case AlignType.Horizontally: Align.Horizontally(fixedPart, expected); break; case AlignType.Vertically: Align.Vertically(fixedPart, expected); break; } Assert.True(PlateArrange.Align(host, type)); Assert.True(expected.Location.DistanceTo(moving.Location) < 1e-9); Assert.Equal(new[] { "Align" }, host.ChangeLabels); } [Fact] public void Evenly_space_horizontally_spreads_the_middle_part_between_the_outer_ones() { var square = plate.Parts[1].BaseDrawing; var a = new Part(square, new Vector(0, 50)); var b = new Part(square, new Vector(13, 50)); var c = new Part(square, new Vector(40, 50)); plate.Parts.Add(a); plate.Parts.Add(b); plate.Parts.Add(c); Selection.Select(a); Selection.Select(b); Selection.Select(c); Assert.True(PlateArrange.Align(host, AlignType.EvenlySpaceHorizontally)); AssertNear(b.BoundingBox.Left - a.BoundingBox.Right, c.BoundingBox.Left - b.BoundingBox.Right); AssertNear(0, a.BoundingBox.Left); AssertNear(40, c.BoundingBox.Left); } [Fact] public void Arrange_commands_do_nothing_without_a_selection_or_while_edits_are_locked() { Assert.False(PlateArrange.Align(host, AlignType.Left)); Selection.Select(plate.Parts[1]); host.EditsLocked = true; Assert.False(PlateArrange.Align(host, AlignType.Left)); Assert.False(PlateArrange.CenterParts(host)); Assert.Null(PlateArrange.JoinCommonLine(host, 0.05)); Assert.Null(PlateArrange.RemoveCommonLines(host)); Assert.False(PlateArrange.ToggleCutCommonLinesTwice(host)); Assert.Empty(host.ChangeLabels); } [Fact] public void Center_parts_centers_every_part_in_the_work_area() { var recorder = host.RecordHistory(); Assert.True(PlateArrange.CenterParts(host)); var center = plate.Parts.GetBoundingBox().Center; var work = plate.WorkArea().Center; Assert.True(center.DistanceTo(work) < 1e-9, $"{center} vs {work}"); Assert.Equal("Center Parts", recorder.History.UndoLabel); // Already centered: the step records nothing. Assert.True(PlateArrange.CenterParts(host)); Assert.Equal(1, recorder.History.UndoCount); } [Fact] public void Join_common_line_slides_two_parts_one_kerf_apart_and_groups_them() { var recorder = host.RecordHistory(); var square = plate.Parts[1].BaseDrawing; var a = new Part(square, new Vector(0, 60)); var b = new Part(square, new Vector(10.5, 60)); using (var add = recorder.BeginChange("Add")) { add.MarkChanged(); plate.Parts.Add(a); plate.Parts.Add(b); add.Complete(); } Selection.Select(a); Selection.Select(b); Assert.Equal((true, false), PlateArrange.CommonLineCommands(plate, Selection.Parts)); var result = PlateArrange.JoinCommonLine(host, 0.05); Assert.True(result.Succeeded, result.Failure); Assert.Same(result.Group, plate.CommonLineGroupOf(a)); Assert.Same(result.Group, plate.CommonLineGroupOf(b)); AssertNear(0.05, System.Math.Abs(b.BoundingBox.Left - a.BoundingBox.Right)); Assert.Equal("Join Common Line", recorder.History.UndoLabel); Assert.Equal((false, true), PlateArrange.CommonLineCommands(plate, Selection.Parts)); } [Fact] public void A_failed_join_moves_nothing_and_records_nothing() { var recorder = host.RecordHistory(); Selection.Select(plate.Parts[1]); var before = plate.Parts[1].Location; var result = PlateArrange.JoinCommonLine(host, 0.05); Assert.False(result.Succeeded); Assert.Equal("Select at least two parts to share a common line.", result.Failure); Assert.Equal(before, plate.Parts[1].Location); Assert.False(recorder.CanUndo); } [Fact] public void Remove_common_line_removes_the_selected_parts_group_and_keeps_them_in_place() { var recorder = host.RecordHistory(); var member = plate.Parts[2]; var location = member.Location; Selection.Select(member); Assert.Equal((false, true), PlateArrange.CommonLineCommands(plate, Selection.Parts)); Assert.True(PlateArrange.RemoveCommonLines(host)); Assert.Empty(plate.CommonLines); Assert.Equal(location, member.Location); Assert.Equal("Remove Common Line", recorder.History.UndoLabel); Assert.False(PlateArrange.RemoveCommonLines(host)); Assert.Equal(1, recorder.History.UndoCount); } [Fact] public void Join_is_offered_for_a_group_and_a_loose_part() { Selection.Select(plate.Parts[2]); // brings its partner Selection.Select(plate.Parts[1]); Assert.Equal((true, true), PlateArrange.CommonLineCommands(plate, Selection.Parts)); } [Fact] public void Cut_common_lines_twice_toggles_as_an_undoable_edit_named_for_the_new_state() { var recorder = host.RecordHistory(); Assert.True(PlateArrange.ToggleCutCommonLinesTwice(host)); Assert.True(plate.CutCommonLinesTwice); Assert.Equal("Cut Common Lines Twice", recorder.History.UndoLabel); Assert.True(PlateArrange.ToggleCutCommonLinesTwice(host)); Assert.False(plate.CutCommonLinesTwice); Assert.Equal("Cut Common Lines Once", recorder.History.UndoLabel); Assert.False(PlateArrange.SetCutCommonLinesTwice(plate, false, label => host.BeginChange(label))); } [Fact] public async Task Expand_spacing_needs_two_parts_and_opens_no_step_without_them() { var outcome = await PlateArrange.ExpandSpacingAsync(plate, new[] { plate.Parts[1] }, host.CutOffSettings, label => host.BeginChange(label), work => Task.FromResult(work(default))); Assert.Null(outcome); Assert.Empty(host.ChangeLabels); } [Fact] public async Task Expand_spacing_pushes_the_selection_apart_as_one_step() { var recorder = host.RecordHistory(); var parts = new[] { plate.Parts[1], plate.Parts[4] }; var outcome = await PlateArrange.ExpandSpacingAsync(plate, parts, host.CutOffSettings, label => host.BeginChange(label), work => Task.Run(() => work(CancellationToken.None))); Assert.False(outcome.Result.Canceled); Assert.Equal(parts, outcome.MovedParts); Assert.StartsWith("Expanded spacing to ", outcome.Status); Assert.Equal("Expand Spacing", recorder.History.UndoLabel); } [Fact] public async Task A_canceled_expand_moves_nothing_and_records_nothing() { var recorder = host.RecordHistory(); var parts = new[] { plate.Parts[1], plate.Parts[4] }; var before = parts.Select(p => p.Location).ToList(); using var canceled = new CancellationTokenSource(); canceled.Cancel(); var outcome = await PlateArrange.ExpandSpacingAsync(plate, parts, host.CutOffSettings, label => host.BeginChange(label), work => Task.FromResult(work(canceled.Token))); Assert.True(outcome.Result.Canceled); Assert.Empty(outcome.MovedParts); Assert.Equal("Expand Spacing canceled - no parts were moved", outcome.Status); Assert.Equal(before, parts.Select(p => p.Location)); Assert.False(recorder.CanUndo); } [Fact] public async Task Fix_spacing_reports_a_clear_plate_without_recording_a_step() { var recorder = host.RecordHistory(); var clear = new Plate(new Size(100, 100)); var square = plate.Parts[1].BaseDrawing; clear.Parts.Add(new Part(square, new Vector(10, 10))); clear.Parts.Add(new Part(square, new Vector(40, 10))); var outcome = await PlateArrange.FixSpacingAsync(clear, System.Array.Empty(), host.CutOffSettings, label => host.BeginChange(label), PlateArrange.Synchronous(null)); Assert.Equal(0, outcome.Result.FlaggedPairs); Assert.Empty(outcome.MovedParts); Assert.Equal("No overlapping or too-close parts to fix", outcome.Status); Assert.False(recorder.CanUndo); } [Fact] public async Task Fix_spacing_pushes_apart_parts_closer_than_the_part_spacing_as_one_step() { var recorder = host.RecordHistory(); var square = plate.Parts[1].BaseDrawing; var a = new Part(square, new Vector(20, 60)); var b = new Part(square, new Vector(30.5, 60)); using (var add = recorder.BeginChange("Add")) { add.MarkChanged(); plate.PartSpacing = 1; plate.Parts.Add(a); plate.Parts.Add(b); add.Complete(); } var outcome = await PlateArrange.FixSpacingAsync(plate, new[] { a }, host.CutOffSettings, label => host.BeginChange(label), work => Task.Run(() => work(CancellationToken.None))); Assert.True(outcome.Result.FlaggedPairs > 0); Assert.NotEmpty(outcome.MovedParts); Assert.StartsWith("Fixed ", outcome.Status); Assert.Equal("Fix Spacing", recorder.History.UndoLabel); } }