feat: add Description/ShortName attributes to NestPhase with extension methods
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,16 +1,52 @@
|
|||||||
using OpenNest.Geometry;
|
using OpenNest.Geometry;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace OpenNest
|
namespace OpenNest
|
||||||
{
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Field)]
|
||||||
|
internal class ShortNameAttribute(string name) : Attribute
|
||||||
|
{
|
||||||
|
public string Name { get; } = name;
|
||||||
|
}
|
||||||
|
|
||||||
public enum NestPhase
|
public enum NestPhase
|
||||||
{
|
{
|
||||||
Linear,
|
[Description("Trying rotations..."), ShortName("Linear")] Linear,
|
||||||
RectBestFit,
|
[Description("Trying best fit..."), ShortName("BestFit")] RectBestFit,
|
||||||
Pairs,
|
[Description("Trying pairs..."), ShortName("Pairs")] Pairs,
|
||||||
Nfp,
|
[Description("Trying NFP..."), ShortName("NFP")] Nfp,
|
||||||
Extents,
|
[Description("Trying extents..."), ShortName("Extents")] Extents,
|
||||||
Custom
|
[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)
|
||||||
|
{
|
||||||
|
return DisplayNames.GetOrAdd(phase, p =>
|
||||||
|
{
|
||||||
|
var field = typeof(NestPhase).GetField(p.ToString());
|
||||||
|
var attr = field?.GetCustomAttribute<DescriptionAttribute>();
|
||||||
|
return attr?.Description ?? p.ToString();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ShortName(this NestPhase phase)
|
||||||
|
{
|
||||||
|
return ShortNames.GetOrAdd(phase, p =>
|
||||||
|
{
|
||||||
|
var field = typeof(NestPhase).GetField(p.ToString());
|
||||||
|
var attr = field?.GetCustomAttribute<ShortNameAttribute>();
|
||||||
|
return attr?.Name ?? p.ToString();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PhaseResult
|
public class PhaseResult
|
||||||
|
|||||||
28
OpenNest.Tests/NestPhaseExtensionsTests.cs
Normal file
28
OpenNest.Tests/NestPhaseExtensionsTests.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
namespace OpenNest.Tests;
|
||||||
|
|
||||||
|
public class NestPhaseExtensionsTests
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[InlineData(NestPhase.Linear, "Trying rotations...")]
|
||||||
|
[InlineData(NestPhase.RectBestFit, "Trying best fit...")]
|
||||||
|
[InlineData(NestPhase.Pairs, "Trying pairs...")]
|
||||||
|
[InlineData(NestPhase.Nfp, "Trying NFP...")]
|
||||||
|
[InlineData(NestPhase.Extents, "Trying extents...")]
|
||||||
|
[InlineData(NestPhase.Custom, "Custom")]
|
||||||
|
public void DisplayName_ReturnsDescription(NestPhase phase, string expected)
|
||||||
|
{
|
||||||
|
Assert.Equal(expected, phase.DisplayName());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(NestPhase.Linear, "Linear")]
|
||||||
|
[InlineData(NestPhase.RectBestFit, "BestFit")]
|
||||||
|
[InlineData(NestPhase.Pairs, "Pairs")]
|
||||||
|
[InlineData(NestPhase.Nfp, "NFP")]
|
||||||
|
[InlineData(NestPhase.Extents, "Extents")]
|
||||||
|
[InlineData(NestPhase.Custom, "Custom")]
|
||||||
|
public void ShortName_ReturnsShortLabel(NestPhase phase, string expected)
|
||||||
|
{
|
||||||
|
Assert.Equal(expected, phase.ShortName());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user