Files
OpenNest/OpenNest.Engine/Jobs/Adapters/NestResultBinder.cs
T
2026-11-12 19:40:26 -05:00

59 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using OpenNest.CommonLines;
using OpenNest.Geometry;
namespace OpenNest.Engine.Jobs.Adapters;
/// <summary>
/// Binds one result sheet's poses to the caller's own drawings. The pose semantics match
/// <see cref="NestResultMaterializer"/>: rotate about the snapshot origin, then translate.
/// Every pose must be finite and refer to a mapped drawing. Malformed sheets are rejected
/// before binding any parts; the pipeline reports these violations without invoking binding.
/// </summary>
public static class NestResultBinder
{
public static IReadOnlyList<Part> Bind(
NestJobPlateResult sheet,
IReadOnlyDictionary<string, Drawing> drawingsByPartId
)
{
ArgumentNullException.ThrowIfNull(sheet);
ArgumentNullException.ThrowIfNull(drawingsByPartId);
foreach (var pose in sheet.Placements)
{
if (pose.PartId == null || !drawingsByPartId.TryGetValue(pose.PartId, out var drawing) || drawing == null)
throw new ArgumentException("Result contains a requirement not present in the drawing map.", nameof(sheet));
if (!double.IsFinite(pose.X) || !double.IsFinite(pose.Y) || !double.IsFinite(pose.Rotation))
throw new ArgumentException("Result contains a nonfinite placement pose.", nameof(sheet));
}
var parts = new List<Part>(sheet.Placements.Count);
foreach (var pose in sheet.Placements)
{
var part = new Part(drawingsByPartId[pose.PartId]);
part.Rotate(pose.Rotation);
part.Location = new Vector(pose.X, pose.Y);
part.UpdateBounds();
parts.Add(part);
}
return parts;
}
/// <summary>The sheet's common-line groups over <paramref name="parts"/>, which
/// <see cref="Bind"/> returned for that sheet (same order).</summary>
public static IReadOnlyList<CommonLineGroup> BindCommonLines(NestJobPlateResult sheet, IReadOnlyList<Part> parts)
{
ArgumentNullException.ThrowIfNull(sheet);
ArgumentNullException.ThrowIfNull(parts);
var groups = new List<CommonLineGroup>(sheet.CommonLines.Count);
foreach (var group in sheet.CommonLines)
{
if (group.Placements.Count < 2 || group.Placements.Any(i => i < 0 || i >= parts.Count))
throw new ArgumentException("A common-line group names a placement the sheet does not have.", nameof(sheet));
groups.Add(new CommonLineGroup(group.Placements.Select(i => parts[i]), group.Kerf));
}
return groups;
}
}