From bf3e3e1f424bc71e4ec5130dfd375f90a393bab4 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Fri, 10 Apr 2026 12:37:12 -0400 Subject: [PATCH] feat: add CadImporter.Import stage with bend detection Co-Authored-By: Claude Opus 4.6 (1M context) --- OpenNest.IO/CadImporter.cs | 49 +++++++++++++++++++++++++++ OpenNest.Tests/IO/CadImporterTests.cs | 42 +++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 OpenNest.IO/CadImporter.cs create mode 100644 OpenNest.Tests/IO/CadImporterTests.cs diff --git a/OpenNest.IO/CadImporter.cs b/OpenNest.IO/CadImporter.cs new file mode 100644 index 0000000..7ce0d27 --- /dev/null +++ b/OpenNest.IO/CadImporter.cs @@ -0,0 +1,49 @@ +using System.Collections.Generic; +using System.IO; +using OpenNest.Bending; +using OpenNest.Geometry; +using OpenNest.IO.Bending; + +namespace OpenNest.IO +{ + /// + /// Shared service that converts a CAD source file into a fully-populated + /// . Used by the UI, console, MCP, API, and training + /// tools so all code paths produce identical drawings. + /// + public static class CadImporter + { + /// + /// Load a DXF file, run bend detection, and return a mutable result + /// ready for interactive editing or direct conversion to a Drawing. + /// + public static CadImportResult Import(string path, CadImportOptions options = null) + { + options ??= CadImportOptions.Default; + + var dxf = Dxf.Import(path); + + var bends = new List(); + if (options.DetectBends && dxf.Document != null) + { + bends = options.BendDetectorName == null + ? BendDetectorRegistry.AutoDetect(dxf.Document) + : BendDetectorRegistry.GetByName(options.BendDetectorName) + ?.DetectBends(dxf.Document) + ?? new List(); + } + + Bend.UpdateEtchEntities(dxf.Entities, bends); + + return new CadImportResult + { + Entities = dxf.Entities, + Bends = bends, + Bounds = dxf.Entities.GetBoundingBox(), + Document = dxf.Document, + SourcePath = path, + Name = options.Name ?? Path.GetFileNameWithoutExtension(path), + }; + } + } +} diff --git a/OpenNest.Tests/IO/CadImporterTests.cs b/OpenNest.Tests/IO/CadImporterTests.cs new file mode 100644 index 0000000..b265d4e --- /dev/null +++ b/OpenNest.Tests/IO/CadImporterTests.cs @@ -0,0 +1,42 @@ +using System.IO; +using System.Linq; +using OpenNest.IO; +using Xunit; + +namespace OpenNest.Tests.IO +{ + public class CadImporterTests + { + private static string TestDxf => + Path.Combine("Bending", "TestData", "4526 A14 PT11.dxf"); + + [Fact] + public void Import_LoadsEntitiesAndDetectsBends() + { + var result = CadImporter.Import(TestDxf); + + Assert.NotNull(result); + Assert.NotEmpty(result.Entities); + Assert.NotNull(result.Bends); + Assert.NotNull(result.Bounds); + Assert.Equal(TestDxf, result.SourcePath); + Assert.Equal("4526 A14 PT11", result.Name); + } + + [Fact] + public void Import_WhenDetectBendsFalse_ReturnsEmptyBends() + { + var result = CadImporter.Import(TestDxf, new CadImportOptions { DetectBends = false }); + + Assert.Empty(result.Bends); + } + + [Fact] + public void Import_WhenNameOverrideProvided_UsesOverride() + { + var result = CadImporter.Import(TestDxf, new CadImportOptions { Name = "custom" }); + + Assert.Equal("custom", result.Name); + } + } +}