Files
OpenNest/OpenNest.Core/CommonLines/CommonLineGroup.cs
T
ajandClaude Opus 5.5 e011fe8290 refactor: fold duplicated helpers into one shared copy each
Roslyn Bridge's duplicate scan found helpers copied verbatim between
projects, so a fix to one copy could silently miss the other. Each copy
now calls a single implementation with the same body:

- boundary nearest point: ClipperBridge.NearestBoundaryPoint (ErodedPierce,
  ScrapPierceSearch)
- nesting profile of a part: NestLayoutCheck.Profile (CommonLinePair)
- part label points: PartLabels in OpenNest.Desktop (WinForms LayoutPart)
- copied common-line groups: CommonLineGroup.AmongParts (ActionClone,
  PlaceTool)
- shared saved cutting parameters: CuttingParametersSerializer.SharedBy
  (MCP, Console)
- bounding box of boxes: generic BoundingBox.GetBoundingBox<T> (rectangle
  packing items)
- box proximity: Clearance.BoxesWithin (Expander)

Behavior is unchanged: every moved body is identical to the copies it
replaces, so the fill change in Expander and rectangle packing skips the
problem-nest corpus. Core, IO, Desktop and Engine suites and the label,
clone and common-line WinForms tests pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 17:50:53 -05:00

74 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace OpenNest.CommonLines
{
/// <summary>
/// Parts placed exactly one kerf apart along collinear straight edges, so one centerline cut
/// makes both edges (see docs/common-line-cutting.md). Membership is by reference; the shared
/// spans are derived from the parts' placed geometry (<see cref="CommonLineGeometry"/>), never
/// stored, so a moved member breaks the group rather than leaving a stale span behind.
/// </summary>
public sealed class CommonLineGroup
{
private readonly List<Part> members;
/// <exception cref="ArgumentOutOfRangeException">The kerf is not finite and positive.</exception>
/// <exception cref="ArgumentException">Fewer than two distinct parts, a null part or a cut-off.</exception>
public CommonLineGroup(IEnumerable<Part> members, double kerf)
{
ArgumentNullException.ThrowIfNull(members);
if (!double.IsFinite(kerf) || kerf <= 0)
throw new ArgumentOutOfRangeException(nameof(kerf), kerf, "A common line needs a finite, positive kerf.");
var distinct = new List<Part>();
foreach (var part in members)
{
if (part == null)
throw new ArgumentException("A common-line group cannot hold a null part.", nameof(members));
if (part.BaseDrawing?.IsCutOff == true)
throw new ArgumentException("A cut-off cannot share a common line.", nameof(members));
if (!distinct.Any(existing => ReferenceEquals(existing, part)))
distinct.Add(part);
}
if (distinct.Count < 2)
throw new ArgumentException("A common-line group needs at least two parts.", nameof(members));
this.members = distinct;
Kerf = kerf;
}
/// <summary>The parts in the group, in the order they were joined.</summary>
public IReadOnlyList<Part> Members => members;
/// <summary>The gap the members were placed at: the kerf width when the group was made.</summary>
public double Kerf { get; }
public bool Contains(Part part) => members.Any(member => ReferenceEquals(member, part));
/// <summary>
/// The plate's groups that join two or more of <paramref name="parts"/>, as indexes into
/// <paramref name="parts"/>, so copies of those parts can be joined the same way. Empty when
/// <paramref name="plate"/> is null.
/// </summary>
public static List<(int[] Members, double Kerf)> AmongParts(Plate plate, IReadOnlyList<Part> parts)
{
ArgumentNullException.ThrowIfNull(parts);
var groups = new List<(int[] Members, double Kerf)>();
if (plate == null)
return groups;
foreach (var group in plate.CommonLines)
{
var indexes = Enumerable.Range(0, parts.Count).Where(i => group.Contains(parts[i])).ToArray();
if (indexes.Length >= 2)
groups.Add((indexes, group.Kerf));
}
return groups;
}
/// <summary>Drops a member (a part removed from the plate); true when it was one.</summary>
internal bool Remove(Part part) => members.RemoveAll(member => ReferenceEquals(member, part)) > 0;
}
}