mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 06:41:42 -05:00
PlateCommands ports the WinForms nest window's plate commands, each one undo step under its WinForms label: Remove Plate, Remove Empty Plates, Duplicate Plate (the copy current, with its own cutting parameters), Move Plate and its Alt+Up/Alt+Down target, Rotate Plate (90 degrees either way or 180), Resize Plate to its parts by the auto-size factor, Set Nest Default Plate, and Edit Plate's commit, where an empty plate's new settings become the nest's defaults and the nested quantities are counted again. NewNestDefaults is what Set as Default saves for new nests: the plate, the saved common-line usage and the nest's machine. PlateEditValues is what Edit Plate edits (quantity, size, part and edge spacing, quadrant, outline), when Done applies and how it writes the plate; SheetOutlineImport reads an outline from a CAD file, and SvgScale holds the scale prompt for an SVG that does not state its size. PlateListRow is the Plates list's row text and its drag drop slots; QuadrantPick the quadrant picker's hit test and cells. The WinForms nest window, plate list, Edit Plate dialog, quadrant picker, SVG scale prompt and Save Current as Defaults delegate to them with unchanged behavior. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
88 lines
3.7 KiB
C#
88 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using OpenNest.IO;
|
|
|
|
namespace OpenNest.Desktop;
|
|
|
|
/// <summary>
|
|
/// Edit Plate's Outline button: a sheet outline read from a DXF, DWG or SVG file, the largest
|
|
/// closed contour being the sheet and every other closed contour an obstacle, in the file's
|
|
/// units as drawings import. An SVG that does not state its real-world size needs the user to
|
|
/// say what its units are first (<see cref="SvgScale"/>); import never guesses a scale.
|
|
/// </summary>
|
|
public static class SheetOutlineImport
|
|
{
|
|
/// <summary>
|
|
/// Reads the outline. <paramref name="svgUserUnitsPerInch"/> is the scale the user chose
|
|
/// for an SVG without a stated size, null otherwise.
|
|
/// </summary>
|
|
/// <exception cref="ArgumentException">The file holds no closed contour with an area.</exception>
|
|
/// <exception cref="IOException">The file cannot be read.</exception>
|
|
public static SheetOutline Read(string path, Units units, double? svgUserUnitsPerInch = null)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(path);
|
|
var options = new CadImportOptions { DrawingUnits = units, SvgUserUnitsPerInch = svgUserUnitsPerInch };
|
|
return SheetOutline.FromGeometry(CadImporter.Import(path, options).Entities);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Whether a failed read is the file not holding an outline (told to the user), rather than
|
|
/// a fault in the app.
|
|
/// </summary>
|
|
public static bool IsReadFailure(Exception exception) =>
|
|
exception is ArgumentException or IOException or InvalidOperationException or InvalidDataException;
|
|
|
|
/// <summary>What the user is told when <see cref="Read"/> fails.</summary>
|
|
public static string FailureText(Exception exception) =>
|
|
$"The file does not hold a sheet outline: {exception?.Message}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// The scale prompt for an SVG that does not state its real-world size: what one of its units
|
|
/// is, with the size each choice gives, so a wrong one is obvious before importing.
|
|
/// </summary>
|
|
public static class SvgScale
|
|
{
|
|
/// <summary>The choices, in the order offered; the first (CSS pixels) is the default.</summary>
|
|
public static IReadOnlyList<(string Text, double UserUnitsPerInch)> Choices { get; } = new[]
|
|
{
|
|
("1/96 inch (web and Inkscape pixels)", 96.0),
|
|
("1/72 inch (Illustrator points)", 72.0),
|
|
("1 millimeter", 25.4),
|
|
("1 inch", 1.0),
|
|
};
|
|
|
|
/// <summary>
|
|
/// The SVG's width and height in its own units when the scale must be asked; null when the
|
|
/// file is not an SVG, states its size, or cannot be read (the import then reports it).
|
|
/// </summary>
|
|
public static (double Width, double Height)? SizeToAsk(string path)
|
|
{
|
|
if (!Svg.IsSvg(path))
|
|
return null;
|
|
try
|
|
{
|
|
return Svg.UndeclaredSize(path);
|
|
}
|
|
catch (Exception exception) when (exception is IOException or UnauthorizedAccessException)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>The prompt's question.</summary>
|
|
public static string Question(string fileName) =>
|
|
$"\"{fileName}\" does not state its real-world size. What is one of its units?";
|
|
|
|
/// <summary>The size the file imports at with <paramref name="userUnitsPerInch"/>, in <paramref name="units"/>.</summary>
|
|
public static string ImportedSizeText(double width, double height, double userUnitsPerInch, Units units)
|
|
{
|
|
var perInch = units == Units.Millimeters ? 25.4 : 1;
|
|
var scale = perInch / userUnitsPerInch;
|
|
var format = units == Units.Millimeters ? "F1" : "F3";
|
|
var unit = UnitsHelper.GetShortString(units);
|
|
return $"Imported size: {(width * scale).ToString(format)} x {(height * scale).ToString(format)} {unit}";
|
|
}
|
|
}
|