Files
OpenNest/OpenNest.WinForms.Tests/Forms/CadConverterSeparateTests.cs
T
ajandClaude Opus 5.5 c8ba250adc feat(cad-converter): separate a file that draws several parts
A file such as a sheet of ornaments imported as one drawing, with every
part but the largest treated as a cutout. PartBodies (Core) now finds the
separate parts by contour nesting, and the CAD Converter offers a
Separate (N) button plus Separate This Part / Separate All Parts in the
entity menu. Each part keeps its entity IDs, so roles, exclusions,
breakoffs, tabbed cutouts and dimensions follow it; separation is refused
when a breakoff or tabbed cutout spans parts.

SVG import keeps element titles as labeled regions, which name the
separated parts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 20:53:08 -05:00

126 lines
5.3 KiB
C#

using System.IO;
using System.Reflection;
using System.Windows.Forms;
using OpenNest.Controls;
using OpenNest.Forms;
using OpenNest.Geometry;
using static OpenNest.WinForms.Tests.FormTesting;
namespace OpenNest.WinForms.Tests.Forms;
/// <summary>
/// A file that draws several parts separates into one drawing per part, named from the file's
/// titles where it has them, each keeping its holes, IDs and settings.
/// </summary>
public class CadConverterSeparateTests
{
// Three parts in a row: a titled square with a hole, a titled disc with a hole, and an
// untitled frame with a loose part sitting in its window.
private const string ThreeParts =
"<svg xmlns='http://www.w3.org/2000/svg' width='100mm' height='30mm' viewBox='0 0 100 30'>"
+ "<title>sheet</title>"
+ "<g><title>tag</title><path d='M0 0H20V20H0Z M8 4A2 2 0 1 0 12 4A2 2 0 1 0 8 4Z'/></g>"
+ "<g><title>bauble</title><circle cx='40' cy='10' r='10'/><circle cx='40' cy='3' r='1'/></g>"
+ "<path d='M60 0H90V30H60Z M65 5H85V25H65Z'/>"
+ "<path d='M70 10H80V20H70Z'/>"
+ "</svg>";
[Fact]
public void SeparateAll_GivesEachPartItsOwnDrawing() => ArchUnitsTests.RunSta(() =>
WithFile(form =>
{
var list = Field<FileListControl>(form, "fileList");
var source = list.SelectedItem!;
source.Quantity = 3;
source.Customer = "Acme";
var ids = source.Entities.Select(e => e.Id).ToHashSet();
Assert.True(form.SeparateParts(null));
Assert.Equal(new[] { "bauble", "parts-3", "parts-4", "tag" }, list.Items.Select(i => i.Name));
var drawings = form.GetDrawings();
Assert.Equal(4, drawings.Count);
Assert.All(drawings, d =>
{
Assert.Equal(3, d.Quantity.Required);
Assert.Equal("Acme", d.Customer);
Assert.Equal(source.Path, d.Source.Path);
Assert.All(d.SourceEntities, e => Assert.Contains(e.Id, ids));
});
Assert.Equal(ids.Count, drawings.Sum(d => d.SourceEntities.Count));
var bauble = drawings.Single(d => d.Name == "bauble");
Assert.Equal(2, bauble.SourceEntities.Count);
// The frame keeps its window; the loose square inside it is a part of its own.
Assert.Equal(8, drawings.Single(d => d.Name == "parts-3").SourceEntities.Count);
Assert.Equal(4, drawings.Single(d => d.Name == "parts-4").SourceEntities.Count);
}));
[Fact]
public void SeparateOne_LeavesTheRestTogether_UnderTheFileName() => ArchUnitsTests.RunSta(() =>
WithFile(form =>
{
var list = Field<FileListControl>(form, "fileList");
var excluded = list.SelectedItem!.Entities.OfType<Circle>().Single(c => c.Radius < 5 && c.Center.X > 30);
list.SelectedItem.ExcludedEntityIds.Add(excluded.Id);
Assert.True(form.SeparateParts(new[] { 1 }));
Assert.Equal(new[] { "bauble", "parts" }, list.Items.Select(i => i.Name));
Assert.Equal("bauble", list.SelectedItem!.Name);
Assert.Equal(new[] { excluded.Id }, list.SelectedItem.ExcludedEntityIds);
Assert.Empty(list.Items.Single(i => i.Name == "parts").ExcludedEntityIds);
WaitFor(form.BodiesSearch);
Assert.Equal(1, form.CurrentBodies!.Count);
}));
[Fact]
public void OnePart_OffersNoSeparation() => ArchUnitsTests.RunSta(() =>
WithFile(form =>
{
Assert.Equal(1, form.CurrentBodies!.Count);
Assert.False(Field<Button>(form, "btnSeparate").Visible);
Assert.False(form.SeparateParts(null));
}, "<svg xmlns='http://www.w3.org/2000/svg' width='20mm' height='20mm' viewBox='0 0 20 20'>"
+ "<path d='M0 0H20V20H0Z M8 4A2 2 0 1 0 12 4A2 2 0 1 0 8 4Z'/></svg>"));
private static void WithFile(Action<CadConverterForm> test, string svg = ThreeParts)
{
var directory = Directory.CreateTempSubdirectory("opennest-separate-").FullName;
try
{
var path = Path.Combine(directory, "parts.svg");
File.WriteAllText(path, svg);
using var form = new CadConverterForm { DrawingUnits = Units.Millimeters };
Show(form);
form.AddFile(path);
WaitFor(form.BodiesSearch);
if (svg == ThreeParts)
{
Assert.Equal(4, form.CurrentBodies!.Count);
var button = Field<Button>(form, "btnSeparate");
Assert.True(button.Visible);
Assert.Equal("Separate (4)", button.Text);
Assert.Equal(4, Field<EntityView>(form, "entityView1").SeparablePartCount);
}
test(form);
form.Close();
}
finally
{
Directory.Delete(directory, true);
}
}
private static void WaitFor(Task task)
{
var deadline = DateTime.UtcNow.AddSeconds(30);
while (!task.IsCompleted && DateTime.UtcNow < deadline)
Application.DoEvents();
Assert.True(task.IsCompleted, "The parts search did not finish.");
}
private static T Field<T>(object owner, string name) =>
(T)typeof(CadConverterForm).GetField(name, BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(owner)!;
}