mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-02 08:41:37 -05:00
Two nests that should have been one (a thickness saved as 0.1196 instead of 0.12 kept N26-2CN and N26-2CH apart in Auto Nest) could only be joined by re-importing drawings and re-nesting. Copy and Cut now also remember the selected parts, and Paste (Ctrl+V) and Paste in Place (Ctrl+Shift+V) put them into whichever nest is active. A paste adds an exact copy of each drawing the target lacks, with its required quantity, uses a same-named drawing with the same geometry, and refuses a different drawing with the same name, since a nest cannot hold two. Drawings are captured at copy time, so later edits or undo in the source do not change what is pasted. Clipboard keys in text boxes now edit the text instead of acting on parts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
235 lines
10 KiB
C#
235 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using OpenNest.CommonLines;
|
|
|
|
namespace OpenNest.IO
|
|
{
|
|
/// <summary>
|
|
/// Parts copied from a plate (Edit > Copy or Cut) for pasting into any open nest, the one they
|
|
/// came from or another. Each part is cloned as it was copied, with its placed program, lead-ins
|
|
/// and the common-line groups among the copied parts; each drawing is kept as the nest file saves
|
|
/// it, so editing or closing the source nest afterwards changes nothing here. Cut-off parts are
|
|
/// plate definitions, not parts, and are never copied. See docs/copy-paste-parts.md.
|
|
/// </summary>
|
|
public sealed class CopiedParts
|
|
{
|
|
// A .nest archive holding the copied drawings and no plates.
|
|
private readonly byte[] drawingsArchive;
|
|
private readonly List<(Part Part, string DrawingName)> parts;
|
|
// Drawing name -> DrawingPackage.GeometryKey, in order of first use.
|
|
private readonly List<(string Name, string GeometryKey)> drawings;
|
|
|
|
private CopiedParts(byte[] drawingsArchive, List<(Part, string)> parts,
|
|
List<(string, string)> drawings, List<(int[] Members, double Kerf)> commonLines, Units units)
|
|
{
|
|
this.drawingsArchive = drawingsArchive;
|
|
this.parts = parts;
|
|
this.drawings = drawings;
|
|
CommonLines = commonLines;
|
|
Units = units;
|
|
}
|
|
|
|
/// <summary>The units the copied drawings are drawn in.</summary>
|
|
public Units Units { get; }
|
|
|
|
public int Count => parts.Count;
|
|
|
|
/// <summary>The names of the copied parts' drawings, each once, in the order the parts use them.</summary>
|
|
public IReadOnlyList<string> DrawingNames => drawings.Select(d => d.Name).ToList();
|
|
|
|
/// <summary>The common-line groups among the copied parts, by index into the copy.</summary>
|
|
public IReadOnlyList<(int[] Members, double Kerf)> CommonLines { get; }
|
|
|
|
/// <summary>
|
|
/// Copies <paramref name="selected"/> from <paramref name="plate"/> of <paramref name="nest"/>.
|
|
/// Cut-off parts are left out; null when nothing else was selected.
|
|
/// </summary>
|
|
public static CopiedParts Capture(Nest nest, Plate plate, IEnumerable<Part> selected)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(nest);
|
|
ArgumentNullException.ThrowIfNull(selected);
|
|
|
|
var sources = selected.Where(p => p != null && !p.BaseDrawing.IsCutOff).ToList();
|
|
if (sources.Count == 0)
|
|
return null;
|
|
|
|
var byName = new Dictionary<string, Drawing>(StringComparer.Ordinal);
|
|
var order = new List<Drawing>();
|
|
foreach (var part in sources)
|
|
{
|
|
var name = part.BaseDrawing.Name ?? "";
|
|
if (!byName.TryGetValue(name, out var known))
|
|
{
|
|
byName.Add(name, part.BaseDrawing);
|
|
order.Add(part.BaseDrawing);
|
|
}
|
|
else if (!ReferenceEquals(known, part.BaseDrawing))
|
|
{
|
|
throw new InvalidOperationException($"Two different drawings are named '{name}'.");
|
|
}
|
|
}
|
|
|
|
var holder = new Nest(nest.Name) { Units = nest.Units };
|
|
foreach (var drawing in order)
|
|
holder.Drawings.Add(drawing);
|
|
using var stream = new MemoryStream();
|
|
new NestWriter(holder).Write(stream);
|
|
|
|
var keyed = order.Select(d => (d.Name ?? "", GeometryKey(d, nest.Units))).ToList();
|
|
var copies = sources.Select(p => ((Part)p.Clone(), p.BaseDrawing.Name ?? "")).ToList();
|
|
var commonLines = CommonLineGroup.AmongParts(plate, sources);
|
|
return new CopiedParts(stream.ToArray(), copies, keyed, commonLines, nest.Units);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Plans pasting the copy into <paramref name="target"/> without changing it. A drawing the
|
|
/// nest already has under the same name is used when it is the same drawing (same geometry,
|
|
/// whatever its quantities, color or nesting settings); otherwise the paste is refused, since
|
|
/// a nest cannot hold two drawings with one name. A drawing the nest lacks is added as an
|
|
/// exact copy, with the required quantity it had where it was copied from.
|
|
/// </summary>
|
|
public PartsPaste PlanPaste(Nest target)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(target);
|
|
if (target.Units != Units)
|
|
return PartsPaste.Refuse(target, $"The copied parts are drawn in {UnitName(Units)} and this nest "
|
|
+ $"is in {UnitName(target.Units)}. Converting units is not supported.");
|
|
|
|
var conflicts = new List<string>();
|
|
var map = new Dictionary<string, Drawing>(StringComparer.Ordinal);
|
|
var matched = new List<Drawing>();
|
|
var missing = new List<string>();
|
|
foreach (var (name, key) in drawings)
|
|
{
|
|
var existing = target.Drawings.FirstOrDefault(d => string.Equals(d.Name ?? "", name, StringComparison.Ordinal));
|
|
if (existing == null)
|
|
missing.Add(name);
|
|
else if (existing.IsCutOff || GeometryKey(existing, target.Units) != key)
|
|
conflicts.Add(name);
|
|
else
|
|
{
|
|
map[name] = existing;
|
|
matched.Add(existing);
|
|
}
|
|
}
|
|
|
|
if (conflicts.Count > 0)
|
|
return PartsPaste.Refuse(target, conflicts.Count == 1
|
|
? $"This nest already has a different drawing named '{conflicts[0]}'. Rename one of them first."
|
|
: "This nest already has different drawings with these names; rename them first:"
|
|
+ Environment.NewLine + string.Join(Environment.NewLine, conflicts.Select(n => "- " + n)));
|
|
|
|
var added = new List<Drawing>();
|
|
if (missing.Count > 0)
|
|
{
|
|
var copies = new NestReader(new MemoryStream(drawingsArchive, writable: false)).Read().Drawings
|
|
.ToDictionary(d => d.Name ?? "", StringComparer.Ordinal);
|
|
// The next palette slots, as reopening the nest would give them.
|
|
var slot = target.Drawings.Count(d => !d.IsCutOff);
|
|
foreach (var name in missing)
|
|
{
|
|
// Read from an archive with no plates: nothing is nested yet.
|
|
var copy = copies[name];
|
|
if (!copy.HasCustomColor)
|
|
copy.Color = Drawing.GetPartColor(slot);
|
|
slot++;
|
|
map[name] = copy;
|
|
added.Add(copy);
|
|
}
|
|
}
|
|
|
|
var placed = parts.Select(p => p.Part.CloneFor(map[p.DrawingName])).ToList();
|
|
return new PartsPaste(target, added, matched, placed, CommonLines);
|
|
}
|
|
|
|
private static string GeometryKey(Drawing drawing, Units units) =>
|
|
DrawingPackage.GeometryKey(DrawingPackage.Write(drawing, units));
|
|
|
|
private static string UnitName(Units units) => units.ToString().ToLowerInvariant();
|
|
}
|
|
|
|
/// <summary>
|
|
/// How <see cref="CopiedParts"/> enter one nest: the drawings to add, the drawings it already
|
|
/// has, and the parts bound to them at the positions they were copied from; or a refusal.
|
|
/// </summary>
|
|
public sealed class PartsPaste
|
|
{
|
|
private bool drawingsAdded;
|
|
|
|
internal PartsPaste(Nest nest, IReadOnlyList<Drawing> added, IReadOnlyList<Drawing> matched,
|
|
IReadOnlyList<Part> parts, IReadOnlyList<(int[] Members, double Kerf)> commonLines)
|
|
{
|
|
Nest = nest;
|
|
AddedDrawings = added;
|
|
MatchedDrawings = matched;
|
|
Parts = parts;
|
|
CommonLines = commonLines;
|
|
Refusal = "";
|
|
}
|
|
|
|
private PartsPaste(Nest nest, string refusal)
|
|
{
|
|
Nest = nest;
|
|
AddedDrawings = Array.Empty<Drawing>();
|
|
MatchedDrawings = Array.Empty<Drawing>();
|
|
Parts = Array.Empty<Part>();
|
|
CommonLines = Array.Empty<(int[], double)>();
|
|
Refusal = refusal;
|
|
}
|
|
|
|
internal static PartsPaste Refuse(Nest nest, string refusal) => new(nest, refusal);
|
|
|
|
public Nest Nest { get; }
|
|
|
|
/// <summary>Exact copies of drawings the nest lacks, which <see cref="AddDrawings"/> adds.</summary>
|
|
public IReadOnlyList<Drawing> AddedDrawings { get; }
|
|
|
|
/// <summary>The nest's own drawings the copied parts turned out to be.</summary>
|
|
public IReadOnlyList<Drawing> MatchedDrawings { get; }
|
|
|
|
/// <summary>The copied parts bound to this nest's drawings, where they were copied from; not on any plate.</summary>
|
|
public IReadOnlyList<Part> Parts { get; }
|
|
|
|
/// <summary>The common-line groups among <see cref="Parts"/>, by index.</summary>
|
|
public IReadOnlyList<(int[] Members, double Kerf)> CommonLines { get; }
|
|
|
|
/// <summary>Why nothing may be pasted; empty unless <see cref="IsRefused"/>.</summary>
|
|
public string Refusal { get; }
|
|
|
|
public bool IsRefused => Refusal.Length > 0;
|
|
|
|
/// <summary>Adds <see cref="AddedDrawings"/> to the nest; call before placing any part, once.</summary>
|
|
public void AddDrawings()
|
|
{
|
|
if (IsRefused)
|
|
throw new InvalidOperationException(Refusal);
|
|
if (drawingsAdded)
|
|
return;
|
|
foreach (var drawing in AddedDrawings)
|
|
{
|
|
if (!Nest.Drawings.Add(drawing))
|
|
throw new InvalidOperationException($"This nest already has a drawing named '{drawing.Name}'.");
|
|
}
|
|
drawingsAdded = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds the drawings when they are not yet added and places a copy of every part on
|
|
/// <paramref name="plate"/> exactly where it was copied from, joined in its common-line
|
|
/// groups again. Returns the placed parts.
|
|
/// </summary>
|
|
public IReadOnlyList<Part> PlaceInPlace(Plate plate)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(plate);
|
|
AddDrawings();
|
|
var placed = Parts.Select(p => (Part)p.Clone()).ToList();
|
|
placed.ForEach(plate.Parts.Add);
|
|
foreach (var (members, kerf) in CommonLines)
|
|
plate.JoinCommonLine(members.Select(i => placed[i]), kerf);
|
|
return placed;
|
|
}
|
|
}
|
|
}
|