mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 11:01:36 -05:00
- CuttingPlanSnapshot: one private Copy carries every value and init property for WithSearchLimits, WithTabLeadOuts, WithPreservedOrder and WithTabbedPartsAvoided, keeping the defensive CrossableTabbedParts copy. - NestPhaseExtensions: DisplayName and ShortName share one attribute lookup and keep their own caches. - NestDatabase: History, DrawingHistory and DrawingSetHistory share ReadRevisionPage (count, page query, read rows) under the same lock. - NestServerClient.GetOrNullAsync: the GET, 404 check-then-null, ensure success and read flow used by the Remote* clients. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
174 lines
4.9 KiB
C#
174 lines
4.9 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Reflection;
|
|
using OpenNest.Geometry;
|
|
|
|
namespace OpenNest.Engine
|
|
{
|
|
[AttributeUsage(AttributeTargets.Field)]
|
|
internal class ShortNameAttribute(string name) : Attribute
|
|
{
|
|
public string Name { get; } = name;
|
|
}
|
|
|
|
public enum NestPhase
|
|
{
|
|
[Description("Trying rotations..."), ShortName("Linear")]
|
|
Linear,
|
|
|
|
[Description("Trying best fit..."), ShortName("BestFit")]
|
|
RectBestFit,
|
|
|
|
[Description("Trying pairs..."), ShortName("Pairs")]
|
|
Pairs,
|
|
|
|
[Description("Trying extents..."), ShortName("Extents")]
|
|
Extents,
|
|
|
|
[Description("Custom"), ShortName("Custom")]
|
|
Custom,
|
|
}
|
|
|
|
public static class NestPhaseExtensions
|
|
{
|
|
private static readonly ConcurrentDictionary<NestPhase, string> DisplayNames = new();
|
|
private static readonly ConcurrentDictionary<NestPhase, string> ShortNames = new();
|
|
|
|
public static string DisplayName(this NestPhase phase) =>
|
|
DisplayNames.GetOrAdd(phase, p => AttributeName<DescriptionAttribute>(p, attr => attr.Description));
|
|
|
|
public static string ShortName(this NestPhase phase) =>
|
|
ShortNames.GetOrAdd(phase, p => AttributeName<ShortNameAttribute>(p, attr => attr.Name));
|
|
|
|
// The name the phase's TAttribute gives, or the enum name without one.
|
|
private static string AttributeName<TAttribute>(NestPhase phase, Func<TAttribute, string> name)
|
|
where TAttribute : Attribute
|
|
{
|
|
var attr = typeof(NestPhase).GetField(phase.ToString())?.GetCustomAttribute<TAttribute>();
|
|
return (attr == null ? null : name(attr)) ?? phase.ToString();
|
|
}
|
|
}
|
|
|
|
public class PhaseResult
|
|
{
|
|
public NestPhase Phase { get; set; }
|
|
public int PartCount { get; set; }
|
|
public long TimeMs { get; set; }
|
|
|
|
public PhaseResult(NestPhase phase, int partCount, long timeMs)
|
|
{
|
|
Phase = phase;
|
|
PartCount = partCount;
|
|
TimeMs = timeMs;
|
|
}
|
|
}
|
|
|
|
public class AngleResult
|
|
{
|
|
public double AngleDeg { get; set; }
|
|
public NestDirection Direction { get; set; }
|
|
public int PartCount { get; set; }
|
|
}
|
|
|
|
internal readonly struct ProgressReport
|
|
{
|
|
public NestPhase Phase { get; init; }
|
|
public int PlateNumber { get; init; }
|
|
public List<Part> Parts { get; init; }
|
|
public Box WorkArea { get; init; }
|
|
public string Description { get; init; }
|
|
public bool IsOverallBest { get; init; }
|
|
}
|
|
|
|
public class NestProgress
|
|
{
|
|
public NestPhase Phase { get; set; }
|
|
public int PlateNumber { get; set; }
|
|
|
|
private List<Part> bestParts;
|
|
public List<Part> BestParts
|
|
{
|
|
get => bestParts;
|
|
set
|
|
{
|
|
bestParts = value;
|
|
cachedParts = null;
|
|
}
|
|
}
|
|
|
|
public string Description { get; set; }
|
|
public Box ActiveWorkArea { get; set; }
|
|
public bool IsOverallBest { get; set; }
|
|
|
|
public int BestPartCount => BestParts?.Count ?? 0;
|
|
|
|
private List<Part> cachedParts;
|
|
private Box cachedBounds;
|
|
private double cachedPartArea;
|
|
|
|
private void EnsureCache()
|
|
{
|
|
if (cachedParts == bestParts)
|
|
return;
|
|
cachedParts = bestParts;
|
|
if (bestParts == null || bestParts.Count == 0)
|
|
{
|
|
cachedBounds = default;
|
|
cachedPartArea = 0;
|
|
return;
|
|
}
|
|
cachedBounds = bestParts.GetBoundingBox();
|
|
cachedPartArea = 0;
|
|
foreach (var p in bestParts)
|
|
cachedPartArea += p.BaseDrawing.Area;
|
|
}
|
|
|
|
public double BestDensity
|
|
{
|
|
get
|
|
{
|
|
if (BestParts == null || BestParts.Count == 0)
|
|
return 0;
|
|
EnsureCache();
|
|
var bboxArea = cachedBounds.Width * cachedBounds.Length;
|
|
return bboxArea > 0 ? cachedPartArea / bboxArea : 0;
|
|
}
|
|
}
|
|
|
|
public double NestedWidth
|
|
{
|
|
get
|
|
{
|
|
if (BestParts == null || BestParts.Count == 0)
|
|
return 0;
|
|
EnsureCache();
|
|
return cachedBounds.Width;
|
|
}
|
|
}
|
|
|
|
public double NestedLength
|
|
{
|
|
get
|
|
{
|
|
if (BestParts == null || BestParts.Count == 0)
|
|
return 0;
|
|
EnsureCache();
|
|
return cachedBounds.Length;
|
|
}
|
|
}
|
|
|
|
public double NestedArea
|
|
{
|
|
get
|
|
{
|
|
if (BestParts == null || BestParts.Count == 0)
|
|
return 0;
|
|
EnsureCache();
|
|
return cachedPartArea;
|
|
}
|
|
}
|
|
}
|
|
}
|