feat(io): add TitleBlockDetector with layer name heuristic (phase 1)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ACadSharp;
|
||||
using OpenNest.Geometry;
|
||||
|
||||
namespace OpenNest.IO
|
||||
{
|
||||
public static class TitleBlockDetector
|
||||
{
|
||||
private static readonly HashSet<string> TitleBlockLayerNames = new(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
"TITLE", "TITLEBLOCK", "TITLE_BLOCK", "BORDER", "FRAME",
|
||||
"TB", "INFO", "SHEET", "ANNOTATION"
|
||||
};
|
||||
|
||||
public static HashSet<Guid> Detect(List<Entity> entities, CadDocument document)
|
||||
{
|
||||
var flagged = new HashSet<Guid>();
|
||||
DetectByLayerName(entities, flagged);
|
||||
return flagged;
|
||||
}
|
||||
|
||||
private static void DetectByLayerName(List<Entity> entities, HashSet<Guid> flagged)
|
||||
{
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
if (entity.Layer?.Name != null && TitleBlockLayerNames.Contains(entity.Layer.Name))
|
||||
flagged.Add(entity.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
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<Entity> { 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<Entity> { 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<Entity> { 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<Entity> { line };
|
||||
|
||||
var result = TitleBlockDetector.Detect(entities, null);
|
||||
|
||||
Assert.Contains(line.Id, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user