feat(cad): preview which part each entity separates into

Separate gave no way to see what each part would take before
splitting the file. Preview Parts, joined to the front of the
Separate button, draws every entity in its part's color and labels
each part with the name it would get.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-29 18:41:55 -05:00
1 parent d09840e788
commit 3b19fda767
7 files changed
+251 -15

No files matched your search

@@ -74,6 +74,42 @@ public class CadConverterSeparateTests
Assert.Equal(1, form.CurrentBodies!.Count);
}));
[Fact]
public void Preview_ColorsEachPartAndNamesIt_UntilTurnedOff() => ArchUnitsTests.RunSta(() =>
WithFile(form =>
{
var list = Field<FileListControl>(form, "fileList");
var view = Field<EntityView>(form, "entityView1");
var preview = Field<CheckBox>(form, "chkPreviewParts");
Assert.True(preview.Visible);
// Preview sits just before Separate, joined to it, so they read as one control.
var separate = Field<Button>(form, "btnSeparate");
Assert.Same(preview.Parent, separate.Parent);
Assert.Equal(separate.Left, preview.Right);
Assert.Equal(separate.Bounds.Top, preview.Bounds.Top);
Assert.Equal(separate.Height, preview.Height);
Assert.Null(view.PartPreview);
preview.Checked = true;
var shown = view.PartPreview!;
var bodies = form.CurrentBodies!;
var entities = list.SelectedItem!.Entities;
Assert.All(entities, e => Assert.Equal(HoleTableForm.ColorFor(bodies.IndexOf(e)), shown.Colors[e.Id]));
Assert.Equal(4, shown.Colors.Values.Distinct().Count());
Assert.Equal(new[] { "tag", "bauble", "parts-3", "parts-4" }, shown.Labels.Select(l => l.Name));
preview.Checked = false;
Assert.Null(view.PartPreview);
// The toggle stays on across items; an item with one part shows no preview.
preview.Checked = true;
Assert.True(form.SeparateParts(new[] { 1 }));
WaitForParts(form);
Assert.Null(view.PartPreview);
Assert.False(preview.Visible);
Assert.True(preview.Checked);
}));
[Fact]
public void OnePart_OffersNoSeparation() => ArchUnitsTests.RunSta(() =>
WithFile(form =>
@@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using OpenNest.Geometry;
namespace OpenNest.Controls
{
/// <summary>
/// A part's name and color in the Separate preview, shown just above <paramref name="Anchor"/>, the top
/// center of its outline's bounds, so a part sitting in another's window does not hide its label.
/// </summary>
public sealed record PartPreviewLabel(string Name, Vector Anchor, Color Color);
/// <summary>
/// What the Separate preview shows: each entity's part color by entity ID, and each part's name.
/// </summary>
public sealed record PartPreview(IReadOnlyDictionary<Guid, Color> Colors, IReadOnlyList<PartPreviewLabel> Labels);
public partial class EntityView
{
private const float PartPenWidth = 2f;
private readonly Dictionary<int, Pen> partPenCache = new Dictionary<int, Pen>();
private readonly Dictionary<int, Pen> partDisplayPenCache = new Dictionary<int, Pen>();
private readonly Font partLabelFont = new Font("Segoe UI", 8.5f, FontStyle.Bold);
private PartPreview partPreview;
/// <summary>
/// While the Separate preview is on, the parts separating would make: every entity is drawn in
/// its part's color and each part is labeled with the name it would get. Null draws normally.
/// </summary>
public PartPreview PartPreview
{
get => partPreview;
set
{
partPreview = value;
Invalidate();
}
}
private bool TryGetPartColor(Entity entity, out Color color)
{
color = default;
return partPreview != null && partPreview.Colors.TryGetValue(entity.Id, out color);
}
private Pen PartPen(Entity entity) =>
TryGetPartColor(entity, out var color) ? CachedPen(partPenCache, color, dashed: false) : null;
private Pen PartDisplayPen(Entity entity) =>
TryGetPartColor(entity, out var color) ? CachedPen(partDisplayPenCache, color, dashed: true) : null;
private static Pen CachedPen(Dictionary<int, Pen> cache, Color color, bool dashed)
{
var argb = color.ToArgb();
if (!cache.TryGetValue(argb, out var pen))
{
pen = new Pen(color, PartPenWidth);
if (dashed)
pen.DashPattern = new float[] { 6, 3 };
cache[argb] = pen;
}
return pen;
}
private void DrawPartPreviewLabels(Graphics g)
{
if (partPreview == null)
return;
var smoothing = g.SmoothingMode;
g.SmoothingMode = SmoothingMode.AntiAlias;
using var textBrush = new SolidBrush(Color.FromArgb(25, 30, 36));
foreach (var label in partPreview.Labels)
{
var size = g.MeasureString(label.Name, partLabelFont);
var at = PointWorldToGraph(label.Anchor);
var box = new RectangleF(at.X - size.Width / 2f - 3f, at.Y - size.Height - 5f,
size.Width + 6f, size.Height + 2f);
using var back = new SolidBrush(Color.FromArgb(220, label.Color));
g.FillRectangle(back, box);
g.DrawString(label.Name, partLabelFont, textBrush, box.X + 3f, box.Y + 1f);
}
g.SmoothingMode = smoothing;
}
private void DisposePartPreview()
{
foreach (var pen in partPenCache.Values)
pen.Dispose();
partPenCache.Clear();
foreach (var pen in partDisplayPenCache.Values)
pen.Dispose();
partDisplayPenCache.Clear();
partLabelFont.Dispose();
}
}
}
+8 -5
View File
@@ -133,14 +133,14 @@ namespace OpenNest.Controls
continue;
if (role == Converters.EntityRole.Display)
{
DrawEntity(e.Graphics, entity, displayPen);
DrawEntity(e.Graphics, entity, PartDisplayPen(entity) ?? displayPen);
continue;
}
var isHighlighted =
simplifierHighlightSet != null && simplifierHighlightSet.Contains(entity);
var pen = isHighlighted
? GetEntityPen(Color.FromArgb(60, entity.Color))
: HolePen(entity) ?? GetEntityPen(entity.Color);
: PartPen(entity) ?? HolePen(entity) ?? GetEntityPen(entity.Color);
DrawEntity(e.Graphics, entity, pen);
}
@@ -154,9 +154,10 @@ namespace OpenNest.Controls
{
if (RoleOf(entity) != Converters.EntityRole.Etch)
continue;
var pen = Converters.EntityRoles.Default(entity) == Converters.EntityRole.Etch
? GetEntityPen(entity.Color)
: GetEntityPen(EtchRoleColor);
var pen = PartPen(entity)
?? (Converters.EntityRoles.Default(entity) == Converters.EntityRole.Etch
? GetEntityPen(entity.Color)
: GetEntityPen(EtchRoleColor));
DrawEntity(e.Graphics, entity, pen);
}
@@ -165,6 +166,7 @@ namespace OpenNest.Controls
DrawDimensions(e.Graphics);
DrawBendLabels(e.Graphics);
DrawLabelBox(e.Graphics);
DrawPartPreviewLabels(e.Graphics);
if (ShowEntityLabels)
DrawEntityLabels(e.Graphics);
@@ -523,6 +525,7 @@ namespace OpenNest.Controls
{
ClearPenCache();
DisposeHolePens();
DisposePartPreview();
DisposeRulers();
gridPen.Dispose();
labelFont.Dispose();
+43 -4
View File
@@ -32,6 +32,8 @@ namespace OpenNest.Forms
btnSplit = new System.Windows.Forms.Button();
btnSimplify = new System.Windows.Forms.Button();
btnSeparate = new System.Windows.Forms.Button();
chkPreviewParts = new System.Windows.Forms.CheckBox();
separateGroup = new System.Windows.Forms.FlowLayoutPanel();
btnExport = new System.Windows.Forms.Button();
btnHoleTable = new System.Windows.Forms.Button();
btnLabelBox = new System.Windows.Forms.Button();
@@ -55,6 +57,7 @@ namespace OpenNest.Forms
cadViewSplit.Panel2.SuspendLayout();
cadViewSplit.SuspendLayout();
detailBar.SuspendLayout();
separateGroup.SuspendLayout();
((System.ComponentModel.ISupportInitialize)numQuantity).BeginInit();
tabProgram.SuspendLayout();
bottomPanel1.SuspendLayout();
@@ -171,7 +174,7 @@ namespace OpenNest.Forms
detailBar.Controls.Add(txtCustomer);
detailBar.Controls.Add(lblDimensions);
detailBar.Controls.Add(lblEntityCount);
detailBar.Controls.Add(btnSeparate);
detailBar.Controls.Add(separateGroup);
detailBar.Controls.Add(btnSplit);
detailBar.Controls.Add(btnSimplify);
detailBar.Controls.Add(btnExport);
@@ -259,16 +262,48 @@ namespace OpenNest.Forms
//
btnSeparate.AutoSize = true;
btnSeparate.BackColor = System.Drawing.Color.FromArgb(60, 120, 60);
btnSeparate.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(60, 120, 60);
btnSeparate.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
btnSeparate.Font = new System.Drawing.Font("Segoe UI", 9F);
btnSeparate.ForeColor = System.Drawing.Color.White;
btnSeparate.Margin = new System.Windows.Forms.Padding(0, 0, 10, 0);
btnSeparate.Margin = new System.Windows.Forms.Padding(0);
btnSeparate.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom;
btnSeparate.Name = "btnSeparate";
btnSeparate.Size = new System.Drawing.Size(85, 27);
btnSeparate.TabIndex = 6;
btnSeparate.TabIndex = 1;
btnSeparate.Text = "Separate...";
btnSeparate.UseVisualStyleBackColor = false;
btnSeparate.Visible = false;
//
// separateGroup
//
separateGroup.AutoSize = true;
separateGroup.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
separateGroup.Controls.Add(chkPreviewParts);
separateGroup.Controls.Add(btnSeparate);
separateGroup.Margin = new System.Windows.Forms.Padding(0, 0, 10, 0);
separateGroup.Name = "separateGroup";
separateGroup.TabIndex = 6;
separateGroup.Visible = false;
separateGroup.WrapContents = false;
//
// chkPreviewParts
//
chkPreviewParts.Appearance = System.Windows.Forms.Appearance.Button;
chkPreviewParts.AutoSize = true;
chkPreviewParts.BackColor = System.Drawing.Color.White;
chkPreviewParts.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(60, 120, 60);
chkPreviewParts.FlatAppearance.CheckedBackColor = System.Drawing.Color.FromArgb(200, 228, 200);
chkPreviewParts.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
chkPreviewParts.Font = new System.Drawing.Font("Segoe UI", 9F);
chkPreviewParts.ForeColor = System.Drawing.Color.FromArgb(40, 95, 40);
chkPreviewParts.Margin = new System.Windows.Forms.Padding(0);
chkPreviewParts.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom;
chkPreviewParts.Name = "chkPreviewParts";
chkPreviewParts.Size = new System.Drawing.Size(60, 27);
chkPreviewParts.TabIndex = 0;
chkPreviewParts.Text = "Preview Parts";
chkPreviewParts.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
chkPreviewParts.CheckedChanged += OnPreviewPartsChanged;
//
// btnSplit
//
@@ -455,6 +490,8 @@ namespace OpenNest.Forms
cadViewSplit.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)cadViewSplit).EndInit();
cadViewSplit.ResumeLayout(false);
separateGroup.ResumeLayout(false);
separateGroup.PerformLayout();
detailBar.ResumeLayout(false);
detailBar.PerformLayout();
((System.ComponentModel.ISupportInitialize)numQuantity).EndInit();
@@ -477,6 +514,8 @@ namespace OpenNest.Forms
private System.Windows.Forms.TextBox txtCustomer;
private System.Windows.Forms.Button btnSplit;
private System.Windows.Forms.Button btnSeparate;
private System.Windows.Forms.CheckBox chkPreviewParts;
private System.Windows.Forms.FlowLayoutPanel separateGroup;
private System.Windows.Forms.Button btnSimplify;
private System.Windows.Forms.Button btnExport;
private System.Windows.Forms.Button btnHoleTable;
+59 -6
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@@ -14,6 +15,7 @@ namespace OpenNest.Forms
public partial class CadConverterForm
{
private PartBodies currentBodies;
private Dictionary<Guid, int> currentOwners;
private FileListItem bodiesItem;
private int bodiesVersion;
@@ -37,6 +39,7 @@ namespace OpenNest.Forms
{
var version = ++bodiesVersion;
currentBodies = null;
currentOwners = null;
bodiesItem = null;
ShowSeparable(0);
if (item == null)
@@ -46,15 +49,25 @@ namespace OpenNest.Forms
}
var outlines = Outlines(item);
BodiesSearch = Task.Run(() => PartBodies.Find(outlines))
var entities = item.Entities.ToList();
BodiesSearch = Task.Run(() =>
{
var bodies = PartBodies.Find(outlines);
var owners = new Dictionary<Guid, int>();
if (bodies.Count >= 2)
foreach (var entity in entities)
owners.TryAdd(entity.Id, bodies.IndexOf(entity));
return (bodies, owners);
})
.ContinueWith(
t =>
{
if (version != bodiesVersion || item != CurrentItem || !t.IsCompletedSuccessfully)
return;
currentBodies = t.Result;
currentBodies = t.Result.bodies;
currentOwners = t.Result.owners;
bodiesItem = item;
ShowSeparable(t.Result.Count);
ShowSeparable(currentBodies.Count);
},
CancellationToken.None,
TaskContinuationOptions.None,
@@ -64,9 +77,46 @@ namespace OpenNest.Forms
private void ShowSeparable(int count)
{
var separable = count >= 2;
btnSeparate.Visible = separable;
separateGroup.Visible = separable;
btnSeparate.Text = separable ? $"Separate ({count})" : "Separate...";
entityView1.SeparablePartCount = separable ? count : 0;
RefreshPartPreview();
}
private void OnPreviewPartsChanged(object sender, EventArgs e) => RefreshPartPreview();
// While Preview is checked, draws each part separating would make in its own color, labeled
// with the name it would get. The toggle stays on across items, so each multi-part item shows.
private void RefreshPartPreview()
{
var item = CurrentItem;
var bodies = CurrentBodies;
entityView1.PartPreview = chkPreviewParts.Checked && item != null && bodies is { Count: >= 2 }
? BuildPartPreview(item, bodies, currentOwners, TakenNames(item))
: null;
}
internal static PartPreview BuildPartPreview(
FileListItem item, PartBodies bodies, IReadOnlyDictionary<Guid, int> owners, HashSet<string> taken)
{
var colors = new Dictionary<Guid, Color>();
foreach (var entity in item.Entities)
{
var index = owners != null && owners.TryGetValue(entity.Id, out var owner)
? owner
: bodies.IndexOf(entity);
if (index >= 0)
colors.TryAdd(entity.Id, HoleTableForm.ColorFor(index));
}
var labels = new List<PartPreviewLabel>();
for (var i = 0; i < bodies.Count; i++)
{
var bounds = bodies.Bounds(i);
var name = UniqueName(bodies.NameOf(i, item.Labels) ?? $"{item.Name}-{i + 1}", taken);
labels.Add(new PartPreviewLabel(name, new Vector(bounds.Center.X, bounds.Top), HoleTableForm.ColorFor(i)));
}
return new PartPreview(colors, labels);
}
private void OnSeparateClicked(object sender, EventArgs e) => SeparateParts(null);
@@ -95,8 +145,7 @@ namespace OpenNest.Forms
var leaving = (chosen ?? Enumerable.Range(0, bodies.Count)).Distinct().OrderBy(i => i).ToList();
var staying = Enumerable.Range(0, bodies.Count).Except(leaving).ToList();
var taken = new HashSet<string>(
fileList.Items.Where(i => i != item).Select(i => i.Name), StringComparer.OrdinalIgnoreCase);
var taken = TakenNames(item);
var replacements = new List<FileListItem>();
foreach (var index in leaving)
@@ -183,6 +232,10 @@ namespace OpenNest.Forms
};
}
// The names of the other items in the list, which a separated part's name must not repeat.
private HashSet<string> TakenNames(FileListItem item) =>
new HashSet<string>(fileList.Items.Where(i => i != item).Select(i => i.Name), StringComparer.OrdinalIgnoreCase);
private static string UniqueName(string name, HashSet<string> taken)
{
var unique = name;
+4
View File
@@ -79,6 +79,10 @@ namespace OpenNest.Forms
entityView1.EntityActionRequested += OnEntityActionRequested;
btnSplit.Click += OnSplitClicked;
btnSeparate.Click += OnSeparateClicked;
components ??= new System.ComponentModel.Container();
var separateTips = new ToolTip(components);
separateTips.SetToolTip(chkPreviewParts, "Color each part Separate would make and show its name");
separateTips.SetToolTip(btnSeparate, "Make each part its own drawing");
numQuantity.ValueChanged += OnQuantityChanged;
txtCustomer.TextChanged += OnCustomerChanged;
cboBendDetector.SelectedIndexChanged += OnBendDetectorChanged;
+2
View File
@@ -14,6 +14,8 @@ A file can draw several parts, such as a sheet of ornaments. Import still makes
The search runs off the UI thread whenever an item loads or its filters change. With two or more parts, the **Separate (N)** button shows in the detail bar, and the entity right-click menu adds **Separate This Part** and **Separate All N Parts**.
**Preview Parts**, joined to the front of the Separate button, draws every entity in its part's color (the Hole Table palette) and labels each part above its outline with the name it would get, so you can check what each part takes before separating. While on, it overrides the Hole Table's colors; it changes nothing in the item and stays on as you move between items, showing for each one with two or more parts.
## What a separated item keeps
Each new item takes the entities of its part, with their IDs, so entity roles, exclusions, suppression, breakoffs, tabbed cutouts and dimensions follow them. Bends go with their source entity or, without one, their midpoint; text goes by its position. Quantity, customer and source path are copied, so every drawing records the same file. `BuildDrawing` then gives each its own source offset.