using System.Collections.Generic; using System.Linq; using System.Text; using OpenNest.Bending; using OpenNest.CNC; using OpenNest.Geometry; using OpenNest.Math; namespace OpenNest.Labels { /// /// The text etched in a part's : its drawing name and the bend groups /// its bends fall in for the way it is placed against the sheet grain. /// /// /// A bend group is the bend line's direction relative to the grain (), /// named by a fixed letter so a group has the same letter on every part and sheet of a nest: /// A across the grain, B with it, then C and on for the 15° steps between, in bucket order. /// Bends sharing a letter can be formed in one setup. Like a dimension, the box only stores /// placement; the text is made when the part is planned, because the letters depend on how the /// part was rotated. /// public static class PartLabels { /// The smallest share of the box height the text may be shrunk to; a longer name is not etched. public const double MinimumHeightShare = 0.5; // Letter per bucket, indexed by BendGrain.Bucket: with the grain B, across it A, the rest C on. private static readonly char[] BucketLetters = BuildBucketLetters(); private static char[] BuildBucketLetters() { var letters = new char[BendGrain.BucketCount]; letters[0] = 'B'; letters[BendGrain.BucketCount / 2] = 'A'; var next = 'C'; for (var bucket = 0; bucket < letters.Length; bucket++) if (letters[bucket] == 0) letters[bucket] = next++; return letters; } /// The bend group letter of a bend line (already rotated into the plate) against the grain. public static char GroupLetter(double lineAngle, double grainAngle) => BucketLetters[BendGrain.Bucket(lineAngle, grainAngle)]; /// /// The distinct bend group letters of for a part placed at /// , in alphabetical order. Empty without bends. /// public static string GroupLetters(IEnumerable bends, double partRotation, double grainAngle) { if (bends == null) return string.Empty; var letters = new SortedSet(); foreach (var bend in bends) { if (bend == null || bend.Length <= Tolerance.Epsilon) continue; letters.Add(GroupLetter(bend.LineAngle + partRotation, grainAngle)); } return new string(letters.ToArray()); } /// /// The label text for a drawing placed at : its name, then a /// dash and its bend groups when it has bends, for example 1234-AB. Characters the /// stroke font lacks are dropped from the name. Empty when nothing is left to etch. /// public static string Text(Drawing drawing, double partRotation, double grainAngle) { if (drawing == null) return string.Empty; var name = new StringBuilder(); foreach (var character in drawing.Name ?? string.Empty) if (StrokeFont.Has(character) && !char.IsWhiteSpace(character)) name.Append(char.ToUpperInvariant(character)); var groups = GroupLetters(drawing.Bends, partRotation, grainAngle); if (groups.Length == 0) return name.ToString(); return name.Length == 0 ? groups : name + "-" + groups; } /// /// The etch strokes of 's label for a part placed at /// , as polylines in the drawing's program frame turned with /// the part (the frame Part.Program is in before it is located). Null when the drawing /// has no valid box, no text, or the text cannot be shrunk to fit the box legibly. /// /// /// The text is centered in the box and reads along it. Its height is the box height, or /// less when the text is longer than the box: it is shrunk to fit, to /// of the box height at the least. /// public static List> Strokes(Drawing drawing, double partRotation, double grainAngle) { var box = drawing?.LabelBox; if (box == null || !box.IsValid || !double.IsFinite(partRotation) || !double.IsFinite(grainAngle)) return null; var text = Text(drawing, partRotation, grainAngle); if (text.Length == 0) return null; var height = box.Height; var width = StrokeFont.Measure(text) * height; if (width > box.Width) { height *= box.Width / width; width = box.Width; } if (height < box.Height * MinimumHeightShare - Tolerance.Epsilon) return null; var offset = drawing.Source?.Offset ?? Vector.Zero; var strokes = StrokeFont.Strokes(text, height); foreach (var stroke in strokes) for (var i = 0; i < stroke.Count; i++) { var local = new Vector(stroke[i].X - width / 2, stroke[i].Y - height / 2); // The source frame loses the program's offset, then turns with the part, as bends do. var point = box.ToWorld(local) - offset; stroke[i] = partRotation == 0 ? point : point.Rotate(partRotation); } return strokes; } /// /// Appends 's label to as scribe /// (etch) moves, each stroke started by a rapid. Returns false and leaves the program /// unchanged when has nothing to etch. /// public static bool AddTo(Program program, Drawing drawing, double partRotation, double grainAngle) { var strokes = Strokes(drawing, partRotation, grainAngle); if (program == null || strokes == null || strokes.Count == 0) return false; foreach (var stroke in strokes) { if (stroke.Count < 2) continue; program.Codes.Add(new RapidMove(stroke[0])); for (var i = 1; i < stroke.Count; i++) program.Codes.Add(new LinearMove(stroke[i]) { Layer = LayerType.Scribe }); } return true; } } }