using System; using System.Collections.Generic; using System.IO; using OpenNest.IO; namespace OpenNest.Desktop; /// /// 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 (); import never guesses a scale. /// public static class SheetOutlineImport { /// /// Reads the outline. is the scale the user chose /// for an SVG without a stated size, null otherwise. /// /// The file holds no closed contour with an area. /// The file cannot be read. 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); } /// /// Whether a failed read is the file not holding an outline (told to the user), rather than /// a fault in the app. /// public static bool IsReadFailure(Exception exception) => exception is ArgumentException or IOException or InvalidOperationException or InvalidDataException; /// What the user is told when fails. public static string FailureText(Exception exception) => $"The file does not hold a sheet outline: {exception?.Message}"; } /// /// 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. /// public static class SvgScale { /// The choices, in the order offered; the first (CSS pixels) is the default. 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), }; /// /// 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). /// 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; } } /// The prompt's question. public static string Question(string fileName) => $"\"{fileName}\" does not state its real-world size. What is one of its units?"; /// The size the file imports at with , in . 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}"; } }