feat: add DrawingDxfExporter to convert PEP Drawing to DXF stream

This commit is contained in:
aj
2026-06-23 06:49:50 -04:00
parent 617de2e854
commit af07946a79
5 changed files with 207 additions and 0 deletions
@@ -0,0 +1,63 @@
using PepLib.Codes;
using PepLib.Enums;
using PepLib.Geometry;
using PepLib.IO;
using PepLib.Models;
using Xunit;
namespace PepLib.Core.Tests.IO;
public class DrawingDxfExporterTests
{
[Fact]
public void Export_RectangularLoop_ProducesFourLines()
{
// Arrange: a square 10x10 in incremental mode
var drawing = new Drawing();
drawing.Info = new DrawingInfo { Name = "TEST" };
var loop = new Loop { Name = "TEST.loop-000" };
// Mode defaults to Incremental in Loop constructor
loop.Add(new RapidMove(new Vector(0, 0))); // position at (0,0)
loop.Add(new LinearMove(new Vector(10, 0))); // → (10,0)
loop.Add(new LinearMove(new Vector(0, 10))); // → (10,10)
loop.Add(new LinearMove(new Vector(-10, 0))); // → (0,10)
loop.Add(new LinearMove(new Vector(0, -10))); // → (0,0)
drawing.Loops.Add(loop);
// Act
using var stream = DrawingDxfExporter.Export(drawing);
// Assert: stream is non-empty and contains 4 line entities
Assert.True(stream.Length > 0);
stream.Position = 0;
using var reader = new ACadSharp.IO.DxfReader(stream);
var doc = reader.Read();
var lines = doc.Entities.OfType<ACadSharp.Entities.Line>().ToList();
var arcs = doc.Entities.OfType<ACadSharp.Entities.Arc>().ToList();
Assert.Equal(4, lines.Count);
Assert.Empty(arcs);
}
[Fact]
public void Export_LoopWithArc_ProducesLineAndArc()
{
var drawing = new Drawing();
drawing.Info = new DrawingInfo { Name = "TEST" };
var loop = new Loop { Name = "TEST.loop-000" };
loop.Add(new RapidMove(new Vector(5, 0))); // start at (5,0)
loop.Add(new LinearMove(new Vector(5, 0))); // → (10,0)
loop.Add(new CircularMove(new Vector(-5, 5), new Vector(-5, 0), RotationType.CCW)); // arc: end=(5,5), center=(5,0)
drawing.Loops.Add(loop);
using var stream = DrawingDxfExporter.Export(drawing);
stream.Position = 0;
using var reader = new ACadSharp.IO.DxfReader(stream);
var doc = reader.Read();
var lines = doc.Entities.OfType<ACadSharp.Entities.Line>().ToList();
var arcs = doc.Entities.OfType<ACadSharp.Entities.Arc>().ToList();
Assert.Single(lines);
Assert.Single(arcs);
}
}