mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-29 09:01:36 -05:00
feat(library): add a Drawing Library Grid that checks every drawing
File > Drawing Library Grid shows a page of library drawings side by side. Check All Pages walks every page, downloading each drawing, timing its label search without the paint's cell budget on a worker with a 10 s limit, then timing its paint, and lists drawings that fail, run slow or would freeze a preview. It was added to confirm no other library drawing hangs on load the way the twin-outline drawing did: a run over all 40,757 drawings on the work server found no hangs or failures. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f9b718e890
commit
b703abe8dc
6 files changed
+727
-5
No files matched your search
@@ -0,0 +1,93 @@
|
||||
using System.Windows.Forms;
|
||||
using OpenNest.CNC;
|
||||
using OpenNest.Data;
|
||||
using OpenNest.Forms;
|
||||
using OpenNest.IO;
|
||||
using static OpenNest.WinForms.Tests.FormTesting;
|
||||
|
||||
namespace OpenNest.WinForms.Tests.Forms;
|
||||
|
||||
public class DrawingLibraryGridFormTests
|
||||
{
|
||||
[Fact]
|
||||
public void CheckAllPages_ShowsEveryDrawing_AndListsTheOnesThatFail()
|
||||
{
|
||||
StaTestThread.Run(
|
||||
() =>
|
||||
{
|
||||
var library = new PagedLibrary();
|
||||
for (var i = 0; i < DrawingLibraryGridForm.PageSize; i++)
|
||||
library.Add($"RECT-{i:00}", Rectangles(1));
|
||||
// Two equal outlines once froze the preview's label search.
|
||||
library.Add("TWIN", Rectangles(2));
|
||||
var missing = library.Add("MISSING", null);
|
||||
|
||||
using var form = new DrawingLibraryGridForm(library, "http://library.test:8090");
|
||||
Show(form);
|
||||
Run(form.CheckAllPagesAsync());
|
||||
|
||||
Assert.Equal(DrawingLibraryGridForm.PageSize + 1, form.DrawingsShown);
|
||||
var problem = Assert.Single(form.Problems);
|
||||
Assert.Same(missing, problem.Record);
|
||||
Assert.Equal("Revision not on the server", problem.Problem);
|
||||
Assert.Equal(new[] { 0, DrawingLibraryGridForm.PageSize }, library.Offsets.Skip(library.Offsets.Count - 2));
|
||||
},
|
||||
TimeSpan.FromSeconds(60),
|
||||
"The drawing library grid check did not complete.");
|
||||
}
|
||||
|
||||
// A faulted task rethrows here, so a failed check reports its own error instead of a timeout.
|
||||
private static void Run(Task task)
|
||||
{
|
||||
PumpUntil(() => task.IsCompleted, "the check");
|
||||
task.GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
// Side-by-side 6 x 4 rectangles: a ridge, not a point, is clearest of their edges.
|
||||
private static Drawing Rectangles(int count)
|
||||
{
|
||||
var program = new Program();
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var x = i * 7;
|
||||
program.Codes.Add(new RapidMove(x, 0));
|
||||
program.Codes.Add(new LinearMove(x + 6, 0));
|
||||
program.Codes.Add(new LinearMove(x + 6, 4));
|
||||
program.Codes.Add(new LinearMove(x, 4));
|
||||
program.Codes.Add(new LinearMove(x, 0));
|
||||
}
|
||||
return new Drawing("rectangles", program);
|
||||
}
|
||||
|
||||
private sealed class PagedLibrary : IDrawingLibrary
|
||||
{
|
||||
private readonly List<DrawingLibraryRecord> records = new();
|
||||
private readonly Dictionary<Guid, byte[]> packages = new();
|
||||
|
||||
public List<int> Offsets { get; } = new();
|
||||
|
||||
public DrawingLibraryRecord Add(string name, Drawing drawing)
|
||||
{
|
||||
var record = new DrawingLibraryRecord { Id = Guid.NewGuid(), Revision = 1, Name = name };
|
||||
records.Add(record);
|
||||
if (drawing != null)
|
||||
packages[record.Id] = DrawingPackage.Write(drawing, Units.Inches);
|
||||
return record;
|
||||
}
|
||||
|
||||
public Task<DrawingLibraryPage> QueryAsync(DrawingLibraryQuery query, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Offsets.Add(query.Offset);
|
||||
return Task.FromResult(new DrawingLibraryPage
|
||||
{
|
||||
Items = records.Skip(query.Offset).Take(query.Limit).ToArray(),
|
||||
Total = records.Count,
|
||||
Offset = query.Offset,
|
||||
Limit = query.Limit,
|
||||
});
|
||||
}
|
||||
|
||||
public Task<byte[]?> GetRevisionPackageAsync(Guid id, int revision, CancellationToken cancellationToken = default) =>
|
||||
Task.FromResult(packages.TryGetValue(id, out var package) ? package : null);
|
||||
}
|
||||
}
|
||||
+249
@@ -0,0 +1,249 @@
|
||||
namespace OpenNest.Forms
|
||||
{
|
||||
partial class DrawingLibraryGridForm
|
||||
{
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Windows Form Designer generated code
|
||||
|
||||
private void InitializeComponent()
|
||||
{
|
||||
toolPanel = new System.Windows.Forms.FlowLayoutPanel();
|
||||
searchLabel = new System.Windows.Forms.Label();
|
||||
searchBox = new System.Windows.Forms.TextBox();
|
||||
previousButton = new System.Windows.Forms.Button();
|
||||
pageLabel = new System.Windows.Forms.Label();
|
||||
nextButton = new System.Windows.Forms.Button();
|
||||
checkAllButton = new System.Windows.Forms.Button();
|
||||
stopButton = new System.Windows.Forms.Button();
|
||||
copyButton = new System.Windows.Forms.Button();
|
||||
splitContainer = new System.Windows.Forms.SplitContainer();
|
||||
tilePanel = new System.Windows.Forms.FlowLayoutPanel();
|
||||
problemList = new System.Windows.Forms.ListView();
|
||||
problemNameColumn = new System.Windows.Forms.ColumnHeader();
|
||||
problemRevisionColumn = new System.Windows.Forms.ColumnHeader();
|
||||
problemColumn = new System.Windows.Forms.ColumnHeader();
|
||||
problemTimeColumn = new System.Windows.Forms.ColumnHeader();
|
||||
statusStrip = new System.Windows.Forms.StatusStrip();
|
||||
statusLabel = new System.Windows.Forms.ToolStripStatusLabel();
|
||||
toolPanel.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)splitContainer).BeginInit();
|
||||
splitContainer.Panel1.SuspendLayout();
|
||||
splitContainer.Panel2.SuspendLayout();
|
||||
splitContainer.SuspendLayout();
|
||||
statusStrip.SuspendLayout();
|
||||
SuspendLayout();
|
||||
//
|
||||
// toolPanel
|
||||
//
|
||||
toolPanel.AutoSize = true;
|
||||
toolPanel.Controls.Add(searchLabel);
|
||||
toolPanel.Controls.Add(searchBox);
|
||||
toolPanel.Controls.Add(previousButton);
|
||||
toolPanel.Controls.Add(pageLabel);
|
||||
toolPanel.Controls.Add(nextButton);
|
||||
toolPanel.Controls.Add(checkAllButton);
|
||||
toolPanel.Controls.Add(stopButton);
|
||||
toolPanel.Controls.Add(copyButton);
|
||||
toolPanel.Dock = System.Windows.Forms.DockStyle.Top;
|
||||
toolPanel.Name = "toolPanel";
|
||||
toolPanel.Padding = new System.Windows.Forms.Padding(6);
|
||||
toolPanel.TabIndex = 0;
|
||||
toolPanel.WrapContents = false;
|
||||
//
|
||||
// searchLabel
|
||||
//
|
||||
searchLabel.Anchor = System.Windows.Forms.AnchorStyles.Left;
|
||||
searchLabel.AutoSize = true;
|
||||
searchLabel.Name = "searchLabel";
|
||||
searchLabel.TabIndex = 0;
|
||||
searchLabel.Text = "Search:";
|
||||
//
|
||||
// searchBox
|
||||
//
|
||||
searchBox.Anchor = System.Windows.Forms.AnchorStyles.Left;
|
||||
searchBox.Name = "searchBox";
|
||||
searchBox.Size = new System.Drawing.Size(260, 25);
|
||||
searchBox.TabIndex = 1;
|
||||
searchBox.KeyDown += SearchBox_KeyDown;
|
||||
//
|
||||
// previousButton
|
||||
//
|
||||
previousButton.AutoSize = true;
|
||||
previousButton.Enabled = false;
|
||||
previousButton.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
|
||||
previousButton.Name = "previousButton";
|
||||
previousButton.TabIndex = 2;
|
||||
previousButton.Text = "< Previous";
|
||||
previousButton.UseVisualStyleBackColor = true;
|
||||
previousButton.Click += PreviousButton_Click;
|
||||
//
|
||||
// pageLabel
|
||||
//
|
||||
pageLabel.Anchor = System.Windows.Forms.AnchorStyles.Left;
|
||||
pageLabel.AutoSize = true;
|
||||
pageLabel.MinimumSize = new System.Drawing.Size(140, 0);
|
||||
pageLabel.Name = "pageLabel";
|
||||
pageLabel.TabIndex = 3;
|
||||
pageLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
|
||||
//
|
||||
// nextButton
|
||||
//
|
||||
nextButton.AutoSize = true;
|
||||
nextButton.Enabled = false;
|
||||
nextButton.Name = "nextButton";
|
||||
nextButton.TabIndex = 4;
|
||||
nextButton.Text = "Next >";
|
||||
nextButton.UseVisualStyleBackColor = true;
|
||||
nextButton.Click += NextButton_Click;
|
||||
//
|
||||
// checkAllButton
|
||||
//
|
||||
checkAllButton.AutoSize = true;
|
||||
checkAllButton.Margin = new System.Windows.Forms.Padding(24, 3, 3, 3);
|
||||
checkAllButton.Name = "checkAllButton";
|
||||
checkAllButton.TabIndex = 5;
|
||||
checkAllButton.Text = "Check All Pages";
|
||||
checkAllButton.UseVisualStyleBackColor = true;
|
||||
checkAllButton.Click += CheckAllButton_Click;
|
||||
//
|
||||
// stopButton
|
||||
//
|
||||
stopButton.AutoSize = true;
|
||||
stopButton.Enabled = false;
|
||||
stopButton.Name = "stopButton";
|
||||
stopButton.TabIndex = 6;
|
||||
stopButton.Text = "Stop";
|
||||
stopButton.UseVisualStyleBackColor = true;
|
||||
stopButton.Click += StopButton_Click;
|
||||
//
|
||||
// copyButton
|
||||
//
|
||||
copyButton.AutoSize = true;
|
||||
copyButton.Name = "copyButton";
|
||||
copyButton.TabIndex = 7;
|
||||
copyButton.Text = "Copy Problems";
|
||||
copyButton.UseVisualStyleBackColor = true;
|
||||
copyButton.Click += CopyButton_Click;
|
||||
//
|
||||
// splitContainer
|
||||
//
|
||||
splitContainer.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
splitContainer.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
|
||||
splitContainer.Name = "splitContainer";
|
||||
splitContainer.Orientation = System.Windows.Forms.Orientation.Horizontal;
|
||||
splitContainer.Panel1.Controls.Add(tilePanel);
|
||||
splitContainer.Panel2.Controls.Add(problemList);
|
||||
splitContainer.Size = new System.Drawing.Size(1184, 650);
|
||||
splitContainer.SplitterDistance = 480;
|
||||
splitContainer.TabIndex = 1;
|
||||
//
|
||||
// tilePanel
|
||||
//
|
||||
tilePanel.AutoScroll = true;
|
||||
tilePanel.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
tilePanel.Name = "tilePanel";
|
||||
tilePanel.Padding = new System.Windows.Forms.Padding(4);
|
||||
tilePanel.TabIndex = 0;
|
||||
//
|
||||
// problemList
|
||||
//
|
||||
problemList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { problemNameColumn, problemRevisionColumn, problemColumn, problemTimeColumn });
|
||||
problemList.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
problemList.FullRowSelect = true;
|
||||
problemList.Name = "problemList";
|
||||
problemList.TabIndex = 0;
|
||||
problemList.UseCompatibleStateImageBehavior = false;
|
||||
problemList.View = System.Windows.Forms.View.Details;
|
||||
problemList.DoubleClick += ProblemList_DoubleClick;
|
||||
//
|
||||
// problemNameColumn
|
||||
//
|
||||
problemNameColumn.Text = "Drawing";
|
||||
problemNameColumn.Width = 260;
|
||||
//
|
||||
// problemRevisionColumn
|
||||
//
|
||||
problemRevisionColumn.Text = "Rev";
|
||||
problemRevisionColumn.Width = 50;
|
||||
//
|
||||
// problemColumn
|
||||
//
|
||||
problemColumn.Text = "Problem";
|
||||
problemColumn.Width = 420;
|
||||
//
|
||||
// problemTimeColumn
|
||||
//
|
||||
problemTimeColumn.Text = "ms";
|
||||
problemTimeColumn.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
|
||||
problemTimeColumn.Width = 80;
|
||||
//
|
||||
// statusStrip
|
||||
//
|
||||
statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { statusLabel });
|
||||
statusStrip.Name = "statusStrip";
|
||||
statusStrip.TabIndex = 2;
|
||||
//
|
||||
// statusLabel
|
||||
//
|
||||
statusLabel.Name = "statusLabel";
|
||||
statusLabel.Spring = true;
|
||||
statusLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
||||
//
|
||||
// DrawingLibraryGridForm
|
||||
//
|
||||
AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
|
||||
ClientSize = new System.Drawing.Size(1184, 721);
|
||||
Controls.Add(splitContainer);
|
||||
Controls.Add(statusStrip);
|
||||
Controls.Add(toolPanel);
|
||||
Font = new System.Drawing.Font("Segoe UI", 9.75F);
|
||||
MinimumSize = new System.Drawing.Size(860, 520);
|
||||
Name = "DrawingLibraryGridForm";
|
||||
ShowIcon = false;
|
||||
StartPosition = System.Windows.Forms.FormStartPosition.Manual;
|
||||
Text = "Drawing Library Grid";
|
||||
Shown += DrawingLibraryGridForm_Shown;
|
||||
toolPanel.ResumeLayout(false);
|
||||
toolPanel.PerformLayout();
|
||||
splitContainer.Panel1.ResumeLayout(false);
|
||||
splitContainer.Panel2.ResumeLayout(false);
|
||||
((System.ComponentModel.ISupportInitialize)splitContainer).EndInit();
|
||||
splitContainer.ResumeLayout(false);
|
||||
statusStrip.ResumeLayout(false);
|
||||
statusStrip.PerformLayout();
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private System.Windows.Forms.FlowLayoutPanel toolPanel;
|
||||
private System.Windows.Forms.Label searchLabel;
|
||||
private System.Windows.Forms.TextBox searchBox;
|
||||
private System.Windows.Forms.Button previousButton;
|
||||
private System.Windows.Forms.Label pageLabel;
|
||||
private System.Windows.Forms.Button nextButton;
|
||||
private System.Windows.Forms.Button checkAllButton;
|
||||
private System.Windows.Forms.Button stopButton;
|
||||
private System.Windows.Forms.Button copyButton;
|
||||
private System.Windows.Forms.SplitContainer splitContainer;
|
||||
private System.Windows.Forms.FlowLayoutPanel tilePanel;
|
||||
private System.Windows.Forms.ListView problemList;
|
||||
private System.Windows.Forms.ColumnHeader problemNameColumn;
|
||||
private System.Windows.Forms.ColumnHeader problemRevisionColumn;
|
||||
private System.Windows.Forms.ColumnHeader problemColumn;
|
||||
private System.Windows.Forms.ColumnHeader problemTimeColumn;
|
||||
private System.Windows.Forms.StatusStrip statusStrip;
|
||||
private System.Windows.Forms.ToolStripStatusLabel statusLabel;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using OpenNest.Controls;
|
||||
using OpenNest.Data;
|
||||
using OpenNest.IO;
|
||||
|
||||
namespace OpenNest.Forms
|
||||
{
|
||||
/// <summary>
|
||||
/// File > Drawing Library Grid: a page of library drawings drawn side by side. Check All
|
||||
/// Pages walks every page, timing each drawing's download, label search and paint, and lists
|
||||
/// the drawings that fail, run slow or would freeze a preview. The label search is timed
|
||||
/// without the paint's cell budget, on a worker with a time limit, so a shape the budget hides
|
||||
/// still shows up here.
|
||||
/// </summary>
|
||||
public partial class DrawingLibraryGridForm : Form
|
||||
{
|
||||
internal const int PageSize = 48;
|
||||
private const int ParallelDownloads = 6;
|
||||
private static readonly TimeSpan PrepareTimeout = TimeSpan.FromSeconds(10);
|
||||
private const long SlowPrepareMs = 1000;
|
||||
private const long SlowPaintMs = 500;
|
||||
|
||||
private readonly IDrawingLibrary library;
|
||||
private CancellationTokenSource pageCancel;
|
||||
private CancellationTokenSource checkCancel;
|
||||
private int offset;
|
||||
private int total;
|
||||
|
||||
/// <summary>The problems found, in the order they were listed; read by tests.</summary>
|
||||
internal List<GridProblem> Problems { get; } = new();
|
||||
|
||||
/// <summary>Drawings shown since the form opened; read by tests.</summary>
|
||||
internal int DrawingsShown { get; private set; }
|
||||
|
||||
public DrawingLibraryGridForm(IDrawingLibrary library, string serverUrl)
|
||||
{
|
||||
this.library = library ?? throw new ArgumentNullException(nameof(library));
|
||||
InitializeComponent();
|
||||
Text = $"Drawing Library Grid — {serverUrl}";
|
||||
Load += (_, _) => DialogBounds.FitToOwnerScreen(this);
|
||||
FormClosed += (_, _) =>
|
||||
{
|
||||
checkCancel?.Cancel();
|
||||
pageCancel?.Cancel();
|
||||
};
|
||||
}
|
||||
|
||||
// A check started first owns the page; loading the first page now would cancel its own.
|
||||
private async void DrawingLibraryGridForm_Shown(object sender, EventArgs e)
|
||||
{
|
||||
if (checkCancel == null)
|
||||
await ShowPageAsync(0);
|
||||
}
|
||||
|
||||
private async void SearchBox_KeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (e.KeyCode != Keys.Enter)
|
||||
return;
|
||||
e.SuppressKeyPress = true;
|
||||
await ShowPageAsync(0);
|
||||
}
|
||||
|
||||
private async void PreviousButton_Click(object sender, EventArgs e) => await ShowPageAsync(System.Math.Max(0, offset - PageSize));
|
||||
|
||||
private async void NextButton_Click(object sender, EventArgs e) => await ShowPageAsync(offset + PageSize);
|
||||
|
||||
private async void CheckAllButton_Click(object sender, EventArgs e) => await CheckAllPagesAsync();
|
||||
|
||||
private void StopButton_Click(object sender, EventArgs e) => checkCancel?.Cancel();
|
||||
|
||||
private void CopyButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (Problems.Count == 0)
|
||||
return;
|
||||
var text = new StringBuilder("Name\tRevision\tId\tProblem\tms\r\n");
|
||||
foreach (var p in Problems)
|
||||
text.Append($"{p.Record.Name}\t{p.Record.Revision}\t{p.Record.Id}\t{p.Problem}\t{p.Milliseconds}\r\n");
|
||||
Clipboard.SetText(text.ToString());
|
||||
}
|
||||
|
||||
// Searching for a listed drawing shows it on its own.
|
||||
private async void ProblemList_DoubleClick(object sender, EventArgs e)
|
||||
{
|
||||
if (problemList.SelectedItems.Count == 0 || checkCancel != null)
|
||||
return;
|
||||
searchBox.Text = ((GridProblem)problemList.SelectedItems[0].Tag).Record.Name;
|
||||
await ShowPageAsync(0);
|
||||
}
|
||||
|
||||
/// <summary>Shows one page; false when it was canceled or could not be read.</summary>
|
||||
internal async Task<bool> ShowPageAsync(int pageOffset)
|
||||
{
|
||||
pageCancel?.Cancel();
|
||||
var cancel = pageCancel = CancellationTokenSource.CreateLinkedTokenSource(checkCancel?.Token ?? CancellationToken.None);
|
||||
var token = cancel.Token;
|
||||
|
||||
DrawingLibraryPage page;
|
||||
try
|
||||
{
|
||||
page = await library.QueryAsync(new DrawingLibraryQuery
|
||||
{
|
||||
Search = searchBox.Text,
|
||||
Sort = DrawingSortField.Name,
|
||||
Descending = false,
|
||||
Offset = pageOffset,
|
||||
Limit = PageSize,
|
||||
}, token);
|
||||
}
|
||||
catch (OperationCanceledException) when (token.IsCancellationRequested)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
statusLabel.Text = $"Could not read the library: {ex.Message}";
|
||||
return false;
|
||||
}
|
||||
if (token.IsCancellationRequested || IsDisposed)
|
||||
return false;
|
||||
|
||||
offset = page.Offset;
|
||||
total = page.Total;
|
||||
pageLabel.Text = page.Items.Count == 0 ? "No drawings" : $"{offset + 1}-{offset + page.Items.Count} of {total}";
|
||||
previousButton.Enabled = offset > 0 && checkCancel == null;
|
||||
nextButton.Enabled = offset + page.Items.Count < total && checkCancel == null;
|
||||
|
||||
var tiles = page.Items.Select(record => new DrawingTile(record)).ToList();
|
||||
tilePanel.SuspendLayout();
|
||||
foreach (Control old in tilePanel.Controls.Cast<Control>().ToList())
|
||||
old.Dispose();
|
||||
tilePanel.Controls.AddRange(tiles.ToArray());
|
||||
tilePanel.ResumeLayout();
|
||||
tilePanel.Update();
|
||||
|
||||
// Downloads and label searches overlap; each tile paints on this thread as its drawing arrives.
|
||||
// Not disposed: loads of a superseded page still release it.
|
||||
var gate = new SemaphoreSlim(ParallelDownloads);
|
||||
var loads = tiles.Select(tile => PrepareAsync(tile.Record, gate, token)).ToList();
|
||||
var pending = loads.ToList();
|
||||
while (pending.Count > 0)
|
||||
{
|
||||
var done = await Task.WhenAny(pending);
|
||||
pending.Remove(done);
|
||||
if (token.IsCancellationRequested || IsDisposed)
|
||||
return false;
|
||||
Show(tiles[loads.IndexOf(done)], done.Result);
|
||||
}
|
||||
return !token.IsCancellationRequested;
|
||||
}
|
||||
|
||||
private void Show(DrawingTile tile, Prepared prepared)
|
||||
{
|
||||
if (prepared.Problem != null)
|
||||
{
|
||||
tile.ShowMessage(prepared.Problem);
|
||||
Report(tile.Record, prepared.Problem, prepared.Milliseconds);
|
||||
return;
|
||||
}
|
||||
if (prepared.Milliseconds > SlowPrepareMs)
|
||||
Report(tile.Record, "Slow label search", prepared.Milliseconds);
|
||||
|
||||
var watch = Stopwatch.StartNew();
|
||||
try
|
||||
{
|
||||
tile.ShowDrawing(prepared.Drawing);
|
||||
// Paint now, so the time is this drawing's.
|
||||
tile.Refresh();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tile.ShowMessage("Paint failed");
|
||||
Report(tile.Record, $"Paint failed: {ex.Message}", watch.ElapsedMilliseconds);
|
||||
return;
|
||||
}
|
||||
if (watch.ElapsedMilliseconds > SlowPaintMs)
|
||||
Report(tile.Record, "Slow paint", watch.ElapsedMilliseconds);
|
||||
DrawingsShown++;
|
||||
}
|
||||
|
||||
private async Task<Prepared> PrepareAsync(DrawingLibraryRecord record, SemaphoreSlim gate, CancellationToken token)
|
||||
{
|
||||
try
|
||||
{
|
||||
await gate.WaitAsync(token);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
return new Prepared(null, "Canceled", 0);
|
||||
}
|
||||
|
||||
var watch = Stopwatch.StartNew();
|
||||
try
|
||||
{
|
||||
var bytes = await library.GetRevisionPackageAsync(record.Id, record.Revision, token);
|
||||
if (bytes == null)
|
||||
return new Prepared(null, "Revision not on the server", 0);
|
||||
|
||||
watch.Restart();
|
||||
// A search that never ends keeps its worker; the check moves on without it.
|
||||
var drawing = await Task.Run(() =>
|
||||
{
|
||||
var read = DrawingPackage.Read(bytes).Drawing;
|
||||
if (read?.Program != null)
|
||||
LayoutPart.ComputeLabelPoint(read, int.MaxValue);
|
||||
return read;
|
||||
}).WaitAsync(PrepareTimeout, token);
|
||||
|
||||
if (drawing?.Program == null)
|
||||
return new Prepared(null, "No program", watch.ElapsedMilliseconds);
|
||||
var color = Color.FromArgb(record.Settings?.ColorArgb ?? 0);
|
||||
drawing.Color = color.A != 0 ? color : Color.SteelBlue;
|
||||
return new Prepared(drawing, null, watch.ElapsedMilliseconds);
|
||||
}
|
||||
catch (TimeoutException)
|
||||
{
|
||||
return new Prepared(null, $"Hangs: label search over {PrepareTimeout.TotalSeconds:0} s", watch.ElapsedMilliseconds);
|
||||
}
|
||||
catch (OperationCanceledException) when (token.IsCancellationRequested)
|
||||
{
|
||||
return new Prepared(null, "Canceled", 0);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new Prepared(null, $"Load failed: {ex.Message}", watch.ElapsedMilliseconds);
|
||||
}
|
||||
finally
|
||||
{
|
||||
gate.Release();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Shows every page in turn from the first, listing each problem found.</summary>
|
||||
internal async Task CheckAllPagesAsync()
|
||||
{
|
||||
if (checkCancel != null)
|
||||
return;
|
||||
checkCancel = new CancellationTokenSource();
|
||||
Problems.Clear();
|
||||
problemList.Items.Clear();
|
||||
DrawingsShown = 0;
|
||||
checkAllButton.Enabled = false;
|
||||
stopButton.Enabled = true;
|
||||
searchBox.Enabled = false;
|
||||
var watch = Stopwatch.StartNew();
|
||||
try
|
||||
{
|
||||
var next = 0;
|
||||
do
|
||||
{
|
||||
if (!await ShowPageAsync(next))
|
||||
{
|
||||
// A page the server would not list ends the check with its error shown.
|
||||
if (!checkCancel.IsCancellationRequested)
|
||||
return;
|
||||
break;
|
||||
}
|
||||
next = offset + PageSize;
|
||||
statusLabel.Text = $"Checked {System.Math.Min(next, total)} of {total} drawings, {Problems.Count} problem(s), {watch.Elapsed:h\\:mm\\:ss}";
|
||||
} while (next < total && !checkCancel.IsCancellationRequested);
|
||||
|
||||
statusLabel.Text = (checkCancel.IsCancellationRequested ? "Stopped. " : "Done. ")
|
||||
+ $"{DrawingsShown} drawing(s) shown, {Problems.Count} problem(s), {watch.Elapsed:h\\:mm\\:ss}";
|
||||
}
|
||||
finally
|
||||
{
|
||||
checkCancel.Dispose();
|
||||
checkCancel = null;
|
||||
if (!IsDisposed)
|
||||
{
|
||||
checkAllButton.Enabled = true;
|
||||
stopButton.Enabled = false;
|
||||
searchBox.Enabled = true;
|
||||
previousButton.Enabled = offset > 0;
|
||||
nextButton.Enabled = offset + PageSize < total;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Report(DrawingLibraryRecord record, string problem, long milliseconds)
|
||||
{
|
||||
var found = new GridProblem(record, problem, milliseconds);
|
||||
Problems.Add(found);
|
||||
problemList.Items.Add(new ListViewItem(new[]
|
||||
{
|
||||
record.Name, record.Revision.ToString(), problem, milliseconds.ToString(),
|
||||
})
|
||||
{ Tag = found });
|
||||
}
|
||||
|
||||
internal sealed record GridProblem(DrawingLibraryRecord Record, string Problem, long Milliseconds);
|
||||
|
||||
private sealed record Prepared(Drawing Drawing, string Problem, long Milliseconds);
|
||||
|
||||
/// <summary>One drawing: its preview over its name.</summary>
|
||||
private sealed class DrawingTile : Panel
|
||||
{
|
||||
private readonly ShapePreviewControl preview;
|
||||
|
||||
public DrawingLibraryRecord Record { get; }
|
||||
|
||||
public DrawingTile(DrawingLibraryRecord record)
|
||||
{
|
||||
Record = record;
|
||||
Size = new Size(180, 190);
|
||||
Margin = new Padding(4);
|
||||
BorderStyle = BorderStyle.FixedSingle;
|
||||
|
||||
preview = new ShapePreviewControl { Dock = DockStyle.Fill, TabStop = false };
|
||||
preview.SetInfo("Loading...");
|
||||
var name = new Label
|
||||
{
|
||||
Dock = DockStyle.Bottom,
|
||||
Height = 20,
|
||||
Text = record.Name,
|
||||
TextAlign = ContentAlignment.MiddleCenter,
|
||||
AutoEllipsis = true,
|
||||
};
|
||||
Controls.Add(preview);
|
||||
Controls.Add(name);
|
||||
}
|
||||
|
||||
public void ShowDrawing(Drawing drawing)
|
||||
{
|
||||
preview.ShowDrawing(drawing);
|
||||
preview.SetInfo();
|
||||
}
|
||||
|
||||
public void ShowMessage(string message)
|
||||
{
|
||||
preview.ShowDrawing(null);
|
||||
preview.SetInfo(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Generated
+10
-1
@@ -42,6 +42,7 @@ namespace OpenNest.Forms
|
||||
mnuFileOpen = new System.Windows.Forms.ToolStripMenuItem();
|
||||
mnuFileImportBom = new System.Windows.Forms.ToolStripMenuItem();
|
||||
mnuFileDrawingLibrary = new System.Windows.Forms.ToolStripMenuItem();
|
||||
mnuFileDrawingLibraryGrid = new System.Windows.Forms.ToolStripMenuItem();
|
||||
mnuFileDrawingSets = new System.Windows.Forms.ToolStripMenuItem();
|
||||
mnuFileReleases = new System.Windows.Forms.ToolStripMenuItem();
|
||||
toolStripMenuItem1 = new System.Windows.Forms.ToolStripSeparator();
|
||||
@@ -216,7 +217,7 @@ namespace OpenNest.Forms
|
||||
//
|
||||
// mnuFile
|
||||
//
|
||||
mnuFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { mnuFileNew, mnuFileOpen, mnuFileImportBom, mnuFileDrawingLibrary, mnuFileDrawingSets, mnuFileReleases, toolStripMenuItem1, mnuFileSave, mnuFileSaveAs, toolStripMenuItem2, mnuFileExport, mnuFileExportAll, mnuFileExportNestReport, toolStripMenuItem3, mnuFileExit });
|
||||
mnuFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { mnuFileNew, mnuFileOpen, mnuFileImportBom, mnuFileDrawingLibrary, mnuFileDrawingLibraryGrid, mnuFileDrawingSets, mnuFileReleases, toolStripMenuItem1, mnuFileSave, mnuFileSaveAs, toolStripMenuItem2, mnuFileExport, mnuFileExportAll, mnuFileExportNestReport, toolStripMenuItem3, mnuFileExit });
|
||||
mnuFile.Name = "mnuFile";
|
||||
mnuFile.DropDownOpening += FileMenu_Opening;
|
||||
mnuFile.Size = new System.Drawing.Size(37, 20);
|
||||
@@ -254,6 +255,13 @@ namespace OpenNest.Forms
|
||||
mnuFileDrawingLibrary.Text = "Drawing Library...";
|
||||
mnuFileDrawingLibrary.Click += FileDrawingLibrary_Click;
|
||||
//
|
||||
// mnuFileDrawingLibraryGrid
|
||||
//
|
||||
mnuFileDrawingLibraryGrid.Name = "mnuFileDrawingLibraryGrid";
|
||||
mnuFileDrawingLibraryGrid.Size = new System.Drawing.Size(180, 22);
|
||||
mnuFileDrawingLibraryGrid.Text = "Drawing Library Grid...";
|
||||
mnuFileDrawingLibraryGrid.Click += FileDrawingLibraryGrid_Click;
|
||||
//
|
||||
// mnuFileDrawingSets
|
||||
//
|
||||
mnuFileDrawingSets.Name = "mnuFileDrawingSets";
|
||||
@@ -1416,6 +1424,7 @@ namespace OpenNest.Forms
|
||||
private System.Windows.Forms.ToolStripMenuItem mnuFileOpen;
|
||||
private System.Windows.Forms.ToolStripMenuItem mnuFileImportBom;
|
||||
private System.Windows.Forms.ToolStripMenuItem mnuFileDrawingLibrary;
|
||||
private System.Windows.Forms.ToolStripMenuItem mnuFileDrawingLibraryGrid;
|
||||
private System.Windows.Forms.ToolStripMenuItem mnuFileDrawingSets;
|
||||
private System.Windows.Forms.ToolStripMenuItem mnuFileReleases;
|
||||
private System.Windows.Forms.ToolStripSeparator toolStripMenuItem1;
|
||||
|
||||
@@ -135,6 +135,10 @@ namespace OpenNest.Forms
|
||||
mnuFile.DropDown.ShowItemToolTips = true;
|
||||
mnuFileDrawingLibrary.Enabled = databaseMode;
|
||||
mnuFileDrawingLibrary.ToolTipText = databaseMode ? "" : NestStorage.DatabaseModeOnly;
|
||||
mnuFileDrawingLibraryGrid.Enabled = databaseMode;
|
||||
mnuFileDrawingLibraryGrid.ToolTipText = databaseMode
|
||||
? "Shows library drawings side by side; Check All Pages finds drawings that fail or freeze a preview."
|
||||
: NestStorage.DatabaseModeOnly;
|
||||
mnuFileDrawingSets.Enabled = databaseMode;
|
||||
mnuFileDrawingSets.ToolTipText = databaseMode ? "" : NestStorage.DatabaseModeOnly;
|
||||
mnuFileReleases.Enabled = databaseMode;
|
||||
@@ -150,6 +154,24 @@ namespace OpenNest.Forms
|
||||
/// </summary>
|
||||
private void FileDrawingLibrary_Click(object sender, EventArgs e) => ShowDrawingSets(releases: false, libraryTab: true);
|
||||
|
||||
/// <summary>Opens the Drawing Library Grid, a window of its own like Drawing Sets.</summary>
|
||||
private void FileDrawingLibraryGrid_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!NestStorage.Settings.IsDatabaseMode)
|
||||
return;
|
||||
IDrawingLibraryStore library;
|
||||
try
|
||||
{
|
||||
library = NestStorage.CreateDrawingLibrary();
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
MessageBox.Show(this, ex.Message, "Drawing Library Grid", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
new DrawingLibraryGridForm(library, NestStorage.Settings.ServerUrl).Show();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Browses, edits and releases drawing sets; needs no open nest, since a release
|
||||
/// creates or merges into nests and opens each in its own tab.
|
||||
|
||||
+10
-4
@@ -308,16 +308,22 @@ namespace OpenNest
|
||||
return path;
|
||||
}
|
||||
|
||||
private Vector ComputeLabelPoint()
|
||||
private Vector ComputeLabelPoint() => ComputeLabelPoint(BasePart.BaseDrawing);
|
||||
|
||||
/// <summary>
|
||||
/// The drawing's label point in its program frame; uses no GDI+ objects. The Drawing
|
||||
/// Library grid's check passes an unlimited <paramref name="maxCells"/> to time the search.
|
||||
/// </summary>
|
||||
internal static Vector ComputeLabelPoint(Drawing drawing, int maxCells = LabelSearchCells)
|
||||
{
|
||||
var entities = ConvertProgram.ToGeometry(BasePart.BaseDrawing.Program);
|
||||
var entities = ConvertProgram.ToGeometry(drawing.Program);
|
||||
var nonRapid = entities.Where(e => SpecialLayers.IsMaterial(e.Layer)).ToList();
|
||||
|
||||
var shapes = ShapeBuilder.GetShapes(nonRapid);
|
||||
|
||||
if (shapes.Count == 0)
|
||||
{
|
||||
var bbox = BasePart.BaseDrawing.Program.BoundingBox();
|
||||
var bbox = drawing.Program.BoundingBox();
|
||||
return new Vector(
|
||||
bbox.Location.X + bbox.Length / 2,
|
||||
bbox.Location.Y + bbox.Width / 2
|
||||
@@ -337,7 +343,7 @@ namespace OpenNest
|
||||
}
|
||||
|
||||
// The label is cosmetic: a budget keeps any shape from stalling the paint.
|
||||
return PolyLabel.Find(outer, holes, PolyLabel.ThicknessPrecision(outer, holes), LabelSearchCells);
|
||||
return PolyLabel.Find(outer, holes, PolyLabel.ThicknessPrecision(outer, holes), maxCells);
|
||||
}
|
||||
|
||||
/// <summary>Rebuilds the part's paths from its program and maps them to the view.</summary>
|
||||
|
||||
Reference in new issue
Block a user