mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-18 07:07:40 -05:00
Ctrl+F while pasting one common-line group fills the area with the group's single merged outline as one part, then turns each placed copy back into its parts joined at the source kerf. The old pattern fill tiled the members as loose parts, added lone parts and joined nothing. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
179 lines
7.6 KiB
C#
179 lines
7.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using OpenNest.CommonLines;
|
|
using OpenNest.Engine.Jobs.Adapters;
|
|
using OpenNest.Engine.Jobs.CommonLines;
|
|
using OpenNest.Geometry;
|
|
using OpenNest.Math;
|
|
|
|
namespace OpenNest.Engine.Jobs.Placement;
|
|
|
|
/// <summary>A proposed fill: new parts and the common-line groups among them (empty when none).</summary>
|
|
public sealed record PlateFillProposal(List<Part> Parts, IReadOnlyList<CommonLineGroup> CommonLines);
|
|
|
|
public static partial class PlateFillService
|
|
{
|
|
/// <summary>
|
|
/// <see cref="FillItem(string, Plate, NestItem, Box, IProgress{NestProgress}, CancellationToken)"/>
|
|
/// at a common-line usage level. With the level off, a drawing that does not allow common lines,
|
|
/// or no positive kerf, it is exactly that fill. Otherwise the area is also filled with kerf-gap
|
|
/// pairs of the drawing (<see cref="CommonLinePair"/>), each expanded into its two parts and a
|
|
/// group, and the pair fill replaces the plain one when it places more parts, or (at
|
|
/// <see cref="CommonLineUsage.Whenever"/>) the same number and the strategy's own ranking does
|
|
/// not prefer the plain fill. Ties at <see cref="CommonLineUsage.WhenItAddsParts"/> keep the
|
|
/// plain fill. The proposal is not committed: the caller adds the parts, then the groups.
|
|
/// </summary>
|
|
public static PlateFillProposal FillItem(
|
|
string strategy,
|
|
Plate plate,
|
|
NestItem item,
|
|
Box workArea,
|
|
CommonLineUsage usage,
|
|
double kerf,
|
|
IProgress<NestProgress> progress,
|
|
CancellationToken token
|
|
)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(item);
|
|
var plain = FillItem(strategy, plate, item, workArea, progress, token);
|
|
var none = new PlateFillProposal(plain, Array.Empty<CommonLineGroup>());
|
|
if (usage == CommonLineUsage.Off || item.Drawing?.AllowCommonLine != true
|
|
|| !double.IsFinite(kerf) || kerf <= 0 || !(kerf < plate.PartSpacing)
|
|
|| item.Quantity is > 0 and < 2 || token.IsCancellationRequested)
|
|
return none;
|
|
var constraints = item.Drawing.Constraints;
|
|
// The second copy turns half a turn, so a fixed rotation must allow its 180° equivalent.
|
|
if (item.StepAngle > 0 && constraints?.Allow180Equivalent != true)
|
|
return none;
|
|
|
|
CommonLinePair pair;
|
|
try
|
|
{
|
|
pair = CommonLinePair.Build(PartGeometrySnapshot.FromProgram(item.Drawing.Program), kerf);
|
|
}
|
|
catch (NotSupportedException)
|
|
{
|
|
return none;
|
|
}
|
|
if (pair == null)
|
|
return none;
|
|
|
|
var proxy = new Drawing(item.Drawing.Name, DrawingJobMapper.ToProgram(pair.Geometry))
|
|
{
|
|
Priority = item.Drawing.Priority,
|
|
Constraints = constraints,
|
|
};
|
|
var proxyItem = new NestItem
|
|
{
|
|
Drawing = proxy,
|
|
Priority = item.Priority,
|
|
Quantity = item.Quantity > 0 ? item.Quantity / 2 : item.Quantity,
|
|
StepAngle = item.StepAngle,
|
|
RotationStart = item.RotationStart,
|
|
RotationEnd = item.RotationEnd,
|
|
};
|
|
// Previews show the plain fill; the pair fill is compared once both are done.
|
|
var pairs = FillItem(strategy, plate, proxyItem, workArea, null, token);
|
|
if (pairs.Count == 0 || token.IsCancellationRequested)
|
|
return none;
|
|
|
|
var parts = new List<Part>(2 * pairs.Count);
|
|
var groups = new List<CommonLineGroup>(pairs.Count);
|
|
foreach (var placed in pairs)
|
|
{
|
|
var rotation = placed.Rotation;
|
|
var first = Place(item.Drawing, rotation, placed.Location);
|
|
var sine = PortableMath.Sin(rotation);
|
|
var cosine = PortableMath.Cos(rotation);
|
|
var second = Place(item.Drawing, rotation + CommonLinePair.SecondRotation,
|
|
new Vector(placed.Location.X + pair.SecondX * cosine - pair.SecondY * sine,
|
|
placed.Location.Y + pair.SecondX * sine + pair.SecondY * cosine));
|
|
parts.Add(first);
|
|
parts.Add(second);
|
|
groups.Add(new CommonLineGroup(new[] { first, second }, kerf));
|
|
}
|
|
|
|
if (parts.Count < plain.Count)
|
|
return none;
|
|
if (parts.Count == plain.Count)
|
|
{
|
|
if (usage != CommonLineUsage.Whenever
|
|
|| RequireFiller(strategy, plate).IsBetterFill(plain, parts, workArea))
|
|
return none;
|
|
}
|
|
return new PlateFillProposal(parts, groups);
|
|
}
|
|
|
|
/// <summary>
|
|
/// <see cref="FillGroup(string, Plate, List{Part}, Box, IProgress{NestProgress}, CancellationToken)"/>
|
|
/// for copied parts that may be one common-line group. With <paramref name="commonLineKerf"/>
|
|
/// (the kerf every part in <paramref name="groupParts"/> is joined at) the area is filled with
|
|
/// the group's single outline (<see cref="CommonLineCompound"/>) as one part, so only whole
|
|
/// groups are placed, and each placed copy becomes its parts and a group. Without a kerf, or
|
|
/// when the parts are not one intact group with a single outline, it is the plain group fill
|
|
/// and proposes no groups. <paramref name="settle"/> compacts the placed units (whole groups,
|
|
/// or the plain fill's parts) before the proposal is built. Not committed: the caller adds the
|
|
/// parts, then the groups.
|
|
/// </summary>
|
|
public static PlateFillProposal FillGroup(
|
|
string strategy,
|
|
Plate plate,
|
|
List<Part> groupParts,
|
|
double? commonLineKerf,
|
|
Box workArea,
|
|
Action<List<Part>> settle,
|
|
IProgress<NestProgress> progress,
|
|
CancellationToken token
|
|
)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(groupParts);
|
|
var compound = commonLineKerf is { } kerf ? CommonLineCompound.Build(groupParts, kerf) : null;
|
|
if (compound == null)
|
|
{
|
|
var plain = FillGroup(strategy, plate, groupParts, workArea, progress, token);
|
|
settle?.Invoke(plain);
|
|
return new PlateFillProposal(plain, Array.Empty<CommonLineGroup>());
|
|
}
|
|
|
|
var units = FillItem(strategy, plate, new NestItem { Drawing = compound.Proxy }, workArea,
|
|
progress == null ? null : new ExpandedProgress(progress, compound), token);
|
|
settle?.Invoke(units);
|
|
var parts = new List<Part>(units.Count * compound.MemberCount);
|
|
var groups = new List<CommonLineGroup>(units.Count);
|
|
foreach (var unit in units)
|
|
{
|
|
var (members, group) = compound.Expand(unit);
|
|
parts.AddRange(members);
|
|
groups.Add(group);
|
|
}
|
|
return new PlateFillProposal(parts, groups);
|
|
}
|
|
|
|
/// <summary>Forwards a compound fill's previews as the member parts they stand for.</summary>
|
|
private sealed class ExpandedProgress(IProgress<NestProgress> inner, CommonLineCompound compound)
|
|
: IProgress<NestProgress>
|
|
{
|
|
public void Report(NestProgress value) => inner.Report(new NestProgress
|
|
{
|
|
Phase = value.Phase,
|
|
PlateNumber = value.PlateNumber,
|
|
BestParts = value.BestParts?.SelectMany(unit => compound.Expand(unit).Parts).ToList(),
|
|
Description = value.Description,
|
|
ActiveWorkArea = value.ActiveWorkArea,
|
|
IsOverallBest = value.IsOverallBest,
|
|
});
|
|
}
|
|
|
|
// The proxy's frame is the drawing's own: rotate about the program origin, then move.
|
|
private static Part Place(Drawing drawing, double rotation, Vector location)
|
|
{
|
|
var part = new Part(drawing);
|
|
part.Rotate(rotation);
|
|
part.Location = location;
|
|
part.UpdateBounds();
|
|
return part;
|
|
}
|
|
}
|