using System; using System.Collections.Generic; using System.Linq; using OpenNest.Geometry; namespace OpenNest.Engine.Jobs.Adapters; /// /// Binds one result sheet's poses to the caller's own drawings. The pose semantics match /// : 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. /// public static class NestResultBinder { public static IReadOnlyList Bind( NestJobPlateResult sheet, IReadOnlyDictionary 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(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; } /// The sheet's common-line groups over , which /// returned for that sheet (same order). public static IReadOnlyList BindCommonLines(NestJobPlateResult sheet, IReadOnlyList parts) { ArgumentNullException.ThrowIfNull(sheet); ArgumentNullException.ThrowIfNull(parts); var groups = new List(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; } }