using System.Collections.Generic; using OpenNest.Geometry; using OpenNest.IO; using Xunit; namespace OpenNest.Tests.IO { public class TitleBlockDetectorTests { private static Line MakeLine(double x1, double y1, double x2, double y2) => new Line(x1, y1, x2, y2); [Fact] public void DetectByLayerName_FlagsTitleLayer() { var line = MakeLine(0, 0, 10, 0); line.Layer = new Layer("TITLE"); var entities = new List { line }; var result = TitleBlockDetector.Detect(entities, null); Assert.Contains(line.Id, result); } [Fact] public void DetectByLayerName_CaseInsensitive() { var line = MakeLine(0, 0, 10, 0); line.Layer = new Layer("border"); var entities = new List { line }; var result = TitleBlockDetector.Detect(entities, null); Assert.Contains(line.Id, result); } [Fact] public void DetectByLayerName_IgnoresNonMatchingLayers() { var line = MakeLine(0, 0, 10, 0); line.Layer = new Layer("0"); var entities = new List { line }; var result = TitleBlockDetector.Detect(entities, null); Assert.DoesNotContain(line.Id, result); } [Theory] [InlineData("TITLE")] [InlineData("TITLEBLOCK")] [InlineData("TITLE_BLOCK")] [InlineData("BORDER")] [InlineData("FRAME")] [InlineData("TB")] [InlineData("INFO")] [InlineData("SHEET")] [InlineData("ANNOTATION")] public void DetectByLayerName_AllKnownNames(string layerName) { var line = MakeLine(0, 0, 10, 0); line.Layer = new Layer(layerName); var entities = new List { line }; var result = TitleBlockDetector.Detect(entities, null); Assert.Contains(line.Id, result); } [Fact] public void DetectBorder_FlagsLinesOnBoundingBoxEdges() { var entities = new List { new Line(0, 0, 86, 0) { Layer = new Layer("0") }, new Line(86, 0, 86, 134) { Layer = new Layer("0") }, new Line(86, 134, 0, 134) { Layer = new Layer("0") }, new Line(0, 134, 0, 0) { Layer = new Layer("0") }, new Line(30, 40, 50, 90) { Layer = new Layer("0") }, new Line(50, 90, 70, 40) { Layer = new Layer("0") }, new Line(70, 40, 30, 40) { Layer = new Layer("0") }, }; var result = TitleBlockDetector.Detect(entities, null); Assert.Contains(entities[0].Id, result); Assert.Contains(entities[1].Id, result); Assert.Contains(entities[2].Id, result); Assert.Contains(entities[3].Id, result); Assert.DoesNotContain(entities[4].Id, result); Assert.DoesNotContain(entities[5].Id, result); Assert.DoesNotContain(entities[6].Id, result); } [Fact] public void DetectBorder_FlagsZoneMarkerTicks() { var entities = new List { new Line(0, 0, 100, 0) { Layer = new Layer("0") }, new Line(100, 0, 100, 80) { Layer = new Layer("0") }, new Line(100, 80, 0, 80) { Layer = new Layer("0") }, new Line(0, 80, 0, 0) { Layer = new Layer("0") }, new Line(25, 80, 25, 77) { Layer = new Layer("0") }, new Line(50, 80, 50, 77) { Layer = new Layer("0") }, new Line(75, 80, 75, 77) { Layer = new Layer("0") }, new Line(40, 30, 60, 30) { Layer = new Layer("0") }, }; var result = TitleBlockDetector.Detect(entities, null); Assert.Contains(entities[4].Id, result); Assert.Contains(entities[5].Id, result); Assert.Contains(entities[6].Id, result); Assert.DoesNotContain(entities[7].Id, result); } [Fact] public void DetectBorder_IgnoresWhenNoBorderPresent() { var entities = new List { new Line(30, 40, 50, 90) { Layer = new Layer("0") }, new Line(50, 90, 70, 40) { Layer = new Layer("0") }, new Line(70, 40, 30, 40) { Layer = new Layer("0") }, }; var result = TitleBlockDetector.Detect(entities, null); Assert.Empty(result); } [Fact] public void DetectBorder_ToleratesSlightRotation() { var angleRad = OpenNest.Math.Angle.ToRadians(0.5); var endY = 86 * System.Math.Sin(angleRad); var entities = new List { new Line(0, 0, 86, endY) { Layer = new Layer("0") }, new Line(86, endY, 86, 134) { Layer = new Layer("0") }, new Line(86, 134, 0, 134) { Layer = new Layer("0") }, new Line(0, 134, 0, 0) { Layer = new Layer("0") }, new Line(30, 40, 50, 90) { Layer = new Layer("0") }, }; var result = TitleBlockDetector.Detect(entities, null); Assert.Contains(entities[0].Id, result); } } }