Compare commits
6 Commits
4cecaba83a
...
2bae5340f0
| Author | SHA1 | Date | |
|---|---|---|---|
| 2bae5340f0 | |||
| 0b322817d7 | |||
| e41f335c63 | |||
| 0ab33af5d3 | |||
| e04c9381f3 | |||
| ceb9cc0b44 |
@@ -82,7 +82,7 @@ namespace OpenNest.Converters
|
||||
var startpt = arc.StartPoint();
|
||||
var endpt = arc.EndPoint();
|
||||
|
||||
if (startpt != lastpt)
|
||||
if (startpt.DistanceTo(lastpt) > Tolerance.ChainTolerance)
|
||||
pgm.MoveTo(startpt);
|
||||
|
||||
lastpt = endpt;
|
||||
@@ -104,7 +104,7 @@ namespace OpenNest.Converters
|
||||
{
|
||||
var startpt = new Vector(circle.Center.X + circle.Radius, circle.Center.Y);
|
||||
|
||||
if (startpt != lastpt)
|
||||
if (startpt.DistanceTo(lastpt) > Tolerance.ChainTolerance)
|
||||
pgm.MoveTo(startpt);
|
||||
|
||||
pgm.ArcTo(startpt, circle.Center, circle.Rotation);
|
||||
@@ -115,7 +115,7 @@ namespace OpenNest.Converters
|
||||
|
||||
private static Vector AddLine(Program pgm, Vector lastpt, Line line)
|
||||
{
|
||||
if (line.StartPoint != lastpt)
|
||||
if (line.StartPoint.DistanceTo(lastpt) > Tolerance.ChainTolerance)
|
||||
pgm.MoveTo(line.StartPoint);
|
||||
|
||||
var move = new LinearMove(line.EndPoint);
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
using OpenNest.Math;
|
||||
using System;
|
||||
using OpenNest.Math;
|
||||
|
||||
namespace OpenNest.Geometry
|
||||
{
|
||||
public class Box
|
||||
public class Box : IComparable<Box>
|
||||
{
|
||||
public static readonly Box Empty = new Box();
|
||||
|
||||
@@ -214,5 +215,19 @@ namespace OpenNest.Geometry
|
||||
{
|
||||
return string.Format("[Box: X={0}, Y={1}, Width={2}, Length={3}]", X, Y, Width, Length);
|
||||
}
|
||||
|
||||
public int CompareTo(Box other)
|
||||
{
|
||||
var cmp = Width.CompareTo(other.Width);
|
||||
return cmp != 0 ? cmp : Length.CompareTo(other.Length);
|
||||
}
|
||||
|
||||
public static bool operator >(Box a, Box b) => a.CompareTo(b) > 0;
|
||||
|
||||
public static bool operator <(Box a, Box b) => a.CompareTo(b) < 0;
|
||||
|
||||
public static bool operator >=(Box a, Box b) => a.CompareTo(b) >= 0;
|
||||
|
||||
public static bool operator <=(Box a, Box b) => a.CompareTo(b) <= 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
using OpenNest.Math;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenNest.Geometry
|
||||
{
|
||||
public static class ShapeBuilder
|
||||
{
|
||||
public static List<Shape> GetShapes(IEnumerable<Entity> entities)
|
||||
public static List<Shape> GetShapes(IEnumerable<Entity> entities, double? weldTolerance = null)
|
||||
{
|
||||
var lines = new List<Line>();
|
||||
var arcs = new List<Arc>();
|
||||
@@ -57,6 +58,9 @@ namespace OpenNest.Geometry
|
||||
entityList.AddRange(lines);
|
||||
entityList.AddRange(arcs);
|
||||
|
||||
if (weldTolerance.HasValue)
|
||||
WeldEndpoints(entityList, weldTolerance.Value);
|
||||
|
||||
while (entityList.Count > 0)
|
||||
{
|
||||
var next = entityList[0];
|
||||
@@ -107,6 +111,93 @@ namespace OpenNest.Geometry
|
||||
return shapes;
|
||||
}
|
||||
|
||||
public static void WeldEndpoints(List<Entity> entities, double tolerance)
|
||||
{
|
||||
var endpointGroups = new List<List<(Entity entity, bool isStart, Vector point)>>();
|
||||
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
var (start, end) = GetEndpoints(entity);
|
||||
if (!start.IsValid() || !end.IsValid())
|
||||
continue;
|
||||
|
||||
AddToGroup(endpointGroups, entity, true, start, tolerance);
|
||||
AddToGroup(endpointGroups, entity, false, end, tolerance);
|
||||
}
|
||||
|
||||
foreach (var group in endpointGroups)
|
||||
{
|
||||
if (group.Count <= 1)
|
||||
continue;
|
||||
|
||||
var avgX = group.Average(g => g.point.X);
|
||||
var avgY = group.Average(g => g.point.Y);
|
||||
var weldedPoint = new Vector(avgX, avgY);
|
||||
|
||||
foreach (var (entity, isStart, _) in group)
|
||||
ApplyWeld(entity, isStart, weldedPoint);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddToGroup(
|
||||
List<List<(Entity entity, bool isStart, Vector point)>> groups,
|
||||
Entity entity, bool isStart, Vector point, double tolerance)
|
||||
{
|
||||
foreach (var group in groups)
|
||||
{
|
||||
if (group[0].point.DistanceTo(point) <= tolerance)
|
||||
{
|
||||
group.Add((entity, isStart, point));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
groups.Add(new List<(Entity, bool, Vector)> { (entity, isStart, point) });
|
||||
}
|
||||
|
||||
private static (Vector start, Vector end) GetEndpoints(Entity entity)
|
||||
{
|
||||
switch (entity.Type)
|
||||
{
|
||||
case EntityType.Arc:
|
||||
var arc = (Arc)entity;
|
||||
return (arc.StartPoint(), arc.EndPoint());
|
||||
|
||||
case EntityType.Line:
|
||||
var line = (Line)entity;
|
||||
return (line.StartPoint, line.EndPoint);
|
||||
|
||||
default:
|
||||
return (Vector.Invalid, Vector.Invalid);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ApplyWeld(Entity entity, bool isStart, Vector weldedPoint)
|
||||
{
|
||||
switch (entity.Type)
|
||||
{
|
||||
case EntityType.Line:
|
||||
var line = (Line)entity;
|
||||
if (isStart)
|
||||
line.StartPoint = weldedPoint;
|
||||
else
|
||||
line.EndPoint = weldedPoint;
|
||||
break;
|
||||
|
||||
case EntityType.Arc:
|
||||
var arc = (Arc)entity;
|
||||
var deltaX = weldedPoint.X - arc.Center.X;
|
||||
var deltaY = weldedPoint.Y - arc.Center.Y;
|
||||
var angle = System.Math.Atan2(deltaY, deltaX);
|
||||
|
||||
if (isStart)
|
||||
arc.StartAngle = angle;
|
||||
else
|
||||
arc.EndAngle = angle;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
internal static Entity GetConnected(Vector pt, IEnumerable<Entity> geometry)
|
||||
{
|
||||
var tol = Tolerance.ChainTolerance;
|
||||
|
||||
@@ -3,7 +3,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace OpenNest.IO.Bom
|
||||
namespace OpenNest.Math
|
||||
{
|
||||
public static class Fraction
|
||||
{
|
||||
@@ -5,6 +5,7 @@ using OpenNest.Bending;
|
||||
using OpenNest.Converters;
|
||||
using OpenNest.Geometry;
|
||||
using OpenNest.IO.Bending;
|
||||
using OpenNest.Math;
|
||||
|
||||
namespace OpenNest.IO
|
||||
{
|
||||
@@ -25,6 +26,8 @@ namespace OpenNest.IO
|
||||
|
||||
var dxf = Dxf.Import(path);
|
||||
|
||||
RemoveDuplicateArcs(dxf.Entities);
|
||||
|
||||
var bends = new List<Bend>();
|
||||
if (options.DetectBends && dxf.Document != null)
|
||||
{
|
||||
@@ -136,5 +139,33 @@ namespace OpenNest.IO
|
||||
|
||||
return drawing;
|
||||
}
|
||||
|
||||
internal static void RemoveDuplicateArcs(List<Entity> entities)
|
||||
{
|
||||
var circles = entities.OfType<Circle>().ToList();
|
||||
var arcs = entities.OfType<Arc>().ToList();
|
||||
var arcsToRemove = new List<Arc>();
|
||||
|
||||
foreach (var arc in arcs)
|
||||
{
|
||||
foreach (var circle in circles)
|
||||
{
|
||||
if (arc.Layer?.Name != circle.Layer?.Name)
|
||||
continue;
|
||||
|
||||
if (!arc.Center.DistanceTo(circle.Center).IsEqualTo(0))
|
||||
continue;
|
||||
|
||||
if (!arc.Radius.IsEqualTo(circle.Radius))
|
||||
continue;
|
||||
|
||||
arcsToRemove.Add(arc);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var arc in arcsToRemove)
|
||||
entities.Remove(arc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
using OpenNest.CNC;
|
||||
using OpenNest.Engine;
|
||||
using OpenNest.Engine.BestFit;
|
||||
using OpenNest.Geometry;
|
||||
using OpenNest.Math;
|
||||
using System.Threading;
|
||||
|
||||
namespace OpenNest.Tests.Engine;
|
||||
|
||||
public class NestInvarianceTests
|
||||
{
|
||||
private static OpenNest.CNC.Program MakeLShapedProgram()
|
||||
{
|
||||
// L-shape: 100x50 outer rect with a 50x30 notch removed from top-right.
|
||||
var pgm = new OpenNest.CNC.Program();
|
||||
pgm.Codes.Add(new RapidMove(new Vector(0, 0)));
|
||||
pgm.Codes.Add(new LinearMove(new Vector(100, 0)));
|
||||
pgm.Codes.Add(new LinearMove(new Vector(100, 20)));
|
||||
pgm.Codes.Add(new LinearMove(new Vector(50, 20)));
|
||||
pgm.Codes.Add(new LinearMove(new Vector(50, 50)));
|
||||
pgm.Codes.Add(new LinearMove(new Vector(0, 50)));
|
||||
pgm.Codes.Add(new LinearMove(new Vector(0, 0)));
|
||||
return pgm;
|
||||
}
|
||||
|
||||
private static Drawing MakeImportedAt(double rotation)
|
||||
{
|
||||
var pgm = MakeLShapedProgram();
|
||||
if (!Tolerance.IsEqualTo(rotation, 0))
|
||||
pgm.Rotate(rotation, pgm.BoundingBox().Center);
|
||||
return new Drawing("L", pgm);
|
||||
}
|
||||
|
||||
private static Plate MakePlate() => new Plate(new Size(500, 500))
|
||||
{
|
||||
Quadrant = 1,
|
||||
PartSpacing = 2,
|
||||
};
|
||||
|
||||
private static int RunFillCount(Drawing drawing, Plate plate)
|
||||
{
|
||||
BestFitCache.Clear();
|
||||
var engine = new DefaultNestEngine(plate);
|
||||
var item = new NestItem { Drawing = drawing };
|
||||
var parts = engine.Fill(item, plate.WorkArea(), progress: null, token: CancellationToken.None);
|
||||
return parts?.Count ?? 0;
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0.0)]
|
||||
[InlineData(0.3)]
|
||||
[InlineData(0.8)]
|
||||
[InlineData(1.2)]
|
||||
public void Fill_SameCount_AcrossImportOrientations(double theta)
|
||||
{
|
||||
var baseline = RunFillCount(MakeImportedAt(0.0), MakePlate());
|
||||
var rotated = RunFillCount(MakeImportedAt(theta), MakePlate());
|
||||
|
||||
// Allow +/-1 tolerance for sweep quantization edge effects near plate boundaries.
|
||||
Assert.InRange(rotated, baseline - 1, baseline + 1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fill_PlacedPartsStayWithinWorkArea_AcrossImportOrientations()
|
||||
{
|
||||
var plate = MakePlate();
|
||||
var workArea = plate.WorkArea();
|
||||
|
||||
foreach (var theta in new[] { 0.0, 0.3, 0.8, 1.2 })
|
||||
{
|
||||
BestFitCache.Clear();
|
||||
var engine = new DefaultNestEngine(plate);
|
||||
var item = new NestItem { Drawing = MakeImportedAt(theta) };
|
||||
var parts = engine.Fill(item, workArea, progress: null, token: CancellationToken.None);
|
||||
|
||||
Assert.NotNull(parts);
|
||||
foreach (var p in parts)
|
||||
{
|
||||
Assert.InRange(p.BoundingBox.Left, workArea.Left - 0.5, workArea.Right + 0.5);
|
||||
Assert.InRange(p.BoundingBox.Bottom, workArea.Bottom - 0.5, workArea.Top + 0.5);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using OpenNest.Geometry;
|
||||
using Xunit;
|
||||
|
||||
namespace OpenNest.Tests.Geometry;
|
||||
|
||||
public class BoxComparisonTests
|
||||
{
|
||||
[Fact]
|
||||
public void GreaterThan_TallerBox_ReturnsTrue()
|
||||
{
|
||||
var tall = new Box(0, 0, 10, 20);
|
||||
var short_ = new Box(0, 0, 10, 10);
|
||||
|
||||
Assert.True(tall > short_);
|
||||
Assert.False(short_ > tall);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GreaterThan_SameWidthLongerBox_ReturnsTrue()
|
||||
{
|
||||
var longer = new Box(0, 0, 20, 10);
|
||||
var shorter = new Box(0, 0, 10, 10);
|
||||
|
||||
Assert.True(longer > shorter);
|
||||
Assert.False(shorter > longer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LessThan_ShorterBox_ReturnsTrue()
|
||||
{
|
||||
var tall = new Box(0, 0, 10, 20);
|
||||
var short_ = new Box(0, 0, 10, 10);
|
||||
|
||||
Assert.True(short_ < tall);
|
||||
Assert.False(tall < short_);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GreaterThanOrEqual_EqualBoxes_ReturnsTrue()
|
||||
{
|
||||
var a = new Box(0, 0, 10, 20);
|
||||
var b = new Box(0, 0, 10, 20);
|
||||
|
||||
Assert.True(a >= b);
|
||||
Assert.True(b >= a);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LessThanOrEqual_EqualBoxes_ReturnsTrue()
|
||||
{
|
||||
var a = new Box(0, 0, 10, 20);
|
||||
var b = new Box(0, 0, 10, 20);
|
||||
|
||||
Assert.True(a <= b);
|
||||
Assert.True(b <= a);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CompareTo_TallerBox_ReturnsPositive()
|
||||
{
|
||||
var tall = new Box(0, 0, 10, 20);
|
||||
var short_ = new Box(0, 0, 10, 10);
|
||||
|
||||
Assert.True(tall.CompareTo(short_) > 0);
|
||||
Assert.True(short_.CompareTo(tall) < 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CompareTo_EqualBoxes_ReturnsZero()
|
||||
{
|
||||
var a = new Box(0, 0, 10, 20);
|
||||
var b = new Box(0, 0, 10, 20);
|
||||
|
||||
Assert.Equal(0, a.CompareTo(b));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Sort_OrdersByWidthThenLength()
|
||||
{
|
||||
var boxes = new List<Box>
|
||||
{
|
||||
new Box(0, 0, 20, 10),
|
||||
new Box(0, 0, 5, 30),
|
||||
new Box(0, 0, 10, 10),
|
||||
};
|
||||
|
||||
boxes.Sort();
|
||||
|
||||
Assert.Equal(10, boxes[0].Width);
|
||||
Assert.Equal(10, boxes[0].Length);
|
||||
Assert.Equal(10, boxes[1].Width);
|
||||
Assert.Equal(20, boxes[1].Length);
|
||||
Assert.Equal(30, boxes[2].Width);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using System.Collections.Generic;
|
||||
using OpenNest.Geometry;
|
||||
using OpenNest.Math;
|
||||
using Xunit;
|
||||
|
||||
namespace OpenNest.Tests.Geometry;
|
||||
|
||||
public class WeldEndpointsTests
|
||||
{
|
||||
[Fact]
|
||||
public void WeldEndpoints_SnapsNearbyLineEndpoints()
|
||||
{
|
||||
var line1 = new Line(0, 0, 10, 0);
|
||||
var line2 = new Line(10.0000005, 0, 20, 0);
|
||||
var entities = new List<Entity> { line1, line2 };
|
||||
|
||||
ShapeBuilder.WeldEndpoints(entities, 0.000001);
|
||||
|
||||
Assert.True(line1.EndPoint.DistanceTo(line2.StartPoint) <= Tolerance.Epsilon);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WeldEndpoints_SnapsArcEndpointByAdjustingAngle()
|
||||
{
|
||||
var line = new Line(0, 0, 10, 0);
|
||||
var arc = new Arc(15, 0, 5, Angle.ToRadians(180.001), Angle.ToRadians(90));
|
||||
var entities = new List<Entity> { line, arc };
|
||||
|
||||
ShapeBuilder.WeldEndpoints(entities, 0.01);
|
||||
|
||||
var arcStart = arc.StartPoint();
|
||||
Assert.True(line.EndPoint.DistanceTo(arcStart) <= 0.01);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WeldEndpoints_DoesNotWeldDistantEndpoints()
|
||||
{
|
||||
var line1 = new Line(0, 0, 10, 0);
|
||||
var line2 = new Line(10.1, 0, 20, 0);
|
||||
var entities = new List<Entity> { line1, line2 };
|
||||
|
||||
ShapeBuilder.WeldEndpoints(entities, 0.000001);
|
||||
|
||||
Assert.True(line1.EndPoint.DistanceTo(line2.StartPoint) > 0.01);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetShapes_WithWeldTolerance_WeldsBeforeChaining()
|
||||
{
|
||||
var line1 = new Line(0, 0, 10, 0);
|
||||
var line2 = new Line(10.0000005, 0, 10.0000005, 10);
|
||||
var entities = new List<Entity> { line1, line2 };
|
||||
|
||||
var shapes = ShapeBuilder.GetShapes(entities, weldTolerance: 0.000001);
|
||||
|
||||
Assert.Single(shapes);
|
||||
Assert.Equal(2, shapes[0].Entities.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetShapes_WithoutWeldTolerance_DefaultBehavior()
|
||||
{
|
||||
var line1 = new Line(0, 0, 10, 0);
|
||||
var line2 = new Line(10, 0, 10, 10);
|
||||
var entities = new List<Entity> { line1, line2 };
|
||||
|
||||
var shapes = ShapeBuilder.GetShapes(entities);
|
||||
|
||||
Assert.Single(shapes);
|
||||
Assert.Equal(2, shapes[0].Entities.Count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenNest.Geometry;
|
||||
using OpenNest.IO;
|
||||
using OpenNest.Math;
|
||||
using Xunit;
|
||||
|
||||
namespace OpenNest.Tests.IO;
|
||||
|
||||
public class RemoveDuplicateArcsTests
|
||||
{
|
||||
[Fact]
|
||||
public void RemoveDuplicateArcs_RemovesArcMatchingCircle_SameLayer()
|
||||
{
|
||||
var layer = new Layer("0");
|
||||
var circle = new Circle(10, 10, 5) { Layer = layer };
|
||||
var arc = new Arc(10, 10, 5, 0, Angle.ToRadians(90)) { Layer = layer };
|
||||
var line = new Line(0, 0, 10, 0) { Layer = layer };
|
||||
var entities = new List<Entity> { circle, arc, line };
|
||||
|
||||
CadImporter.RemoveDuplicateArcs(entities);
|
||||
|
||||
Assert.Equal(2, entities.Count);
|
||||
Assert.Contains(circle, entities);
|
||||
Assert.Contains(line, entities);
|
||||
Assert.DoesNotContain(arc, entities);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveDuplicateArcs_KeepsArcOnDifferentLayer()
|
||||
{
|
||||
var layer1 = new Layer("cut");
|
||||
var layer2 = new Layer("etch");
|
||||
var circle = new Circle(10, 10, 5) { Layer = layer1 };
|
||||
var arc = new Arc(10, 10, 5, 0, Angle.ToRadians(90)) { Layer = layer2 };
|
||||
var entities = new List<Entity> { circle, arc };
|
||||
|
||||
CadImporter.RemoveDuplicateArcs(entities);
|
||||
|
||||
Assert.Equal(2, entities.Count);
|
||||
Assert.Contains(arc, entities);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveDuplicateArcs_KeepsArcWithDifferentRadius()
|
||||
{
|
||||
var layer = new Layer("0");
|
||||
var circle = new Circle(10, 10, 5) { Layer = layer };
|
||||
var arc = new Arc(10, 10, 3, 0, Angle.ToRadians(90)) { Layer = layer };
|
||||
var entities = new List<Entity> { circle, arc };
|
||||
|
||||
CadImporter.RemoveDuplicateArcs(entities);
|
||||
|
||||
Assert.Equal(2, entities.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveDuplicateArcs_KeepsArcWithDifferentCenter()
|
||||
{
|
||||
var layer = new Layer("0");
|
||||
var circle = new Circle(10, 10, 5) { Layer = layer };
|
||||
var arc = new Arc(20, 20, 5, 0, Angle.ToRadians(90)) { Layer = layer };
|
||||
var entities = new List<Entity> { circle, arc };
|
||||
|
||||
CadImporter.RemoveDuplicateArcs(entities);
|
||||
|
||||
Assert.Equal(2, entities.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveDuplicateArcs_NoCircles_NoChange()
|
||||
{
|
||||
var arc = new Arc(10, 10, 5, 0, Angle.ToRadians(90));
|
||||
var line = new Line(0, 0, 10, 0);
|
||||
var entities = new List<Entity> { arc, line };
|
||||
|
||||
CadImporter.RemoveDuplicateArcs(entities);
|
||||
|
||||
Assert.Equal(2, entities.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveDuplicateArcs_MultipleArcsMatchOneCircle_RemovesAll()
|
||||
{
|
||||
var layer = new Layer("0");
|
||||
var circle = new Circle(10, 10, 5) { Layer = layer };
|
||||
var arc1 = new Arc(10, 10, 5, 0, Angle.ToRadians(90)) { Layer = layer };
|
||||
var arc2 = new Arc(10, 10, 5, Angle.ToRadians(90), Angle.ToRadians(180)) { Layer = layer };
|
||||
var entities = new List<Entity> { circle, arc1, arc2 };
|
||||
|
||||
CadImporter.RemoveDuplicateArcs(entities);
|
||||
|
||||
Assert.Single(entities);
|
||||
Assert.Contains(circle, entities);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using OpenNest.Math;
|
||||
using Xunit;
|
||||
|
||||
namespace OpenNest.Tests.Math;
|
||||
|
||||
public class FractionTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("3/8", 0.375)]
|
||||
[InlineData("1 3/4", 1.75)]
|
||||
[InlineData("1-3/4", 1.75)]
|
||||
[InlineData("1/2", 0.5)]
|
||||
public void Parse_ValidFraction_ReturnsDouble(string input, double expected)
|
||||
{
|
||||
var result = Fraction.Parse(input);
|
||||
|
||||
Assert.Equal(expected, result, 8);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("3/8", true)]
|
||||
[InlineData("abc", false)]
|
||||
[InlineData("1 3/4", true)]
|
||||
public void IsValid_ReturnsExpected(string input, bool expected)
|
||||
{
|
||||
Assert.Equal(expected, Fraction.IsValid(input));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryParse_InvalidInput_ReturnsFalse()
|
||||
{
|
||||
var result = Fraction.TryParse("abc", out var value);
|
||||
|
||||
Assert.False(result);
|
||||
Assert.Equal(0, value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReplaceFractionsWithDecimals_ReplacesFractionInString()
|
||||
{
|
||||
var result = Fraction.ReplaceFractionsWithDecimals("length is 1 3/4 inches");
|
||||
|
||||
Assert.Contains("1.75", result);
|
||||
Assert.DoesNotContain("3/4", result);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using OpenNest.IO.Bom;
|
||||
using OpenNest.Math;
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Text;
|
||||
|
||||
@@ -138,9 +138,20 @@ namespace OpenNest
|
||||
break;
|
||||
|
||||
case CodeType.RapidMove:
|
||||
cutPath.StartFigure();
|
||||
leadPath.StartFigure();
|
||||
AddLine(cutPath, (RapidMove)code, mode, ref curpos);
|
||||
{
|
||||
var rapid = (RapidMove)code;
|
||||
var endpt = rapid.EndPoint;
|
||||
if (mode == Mode.Incremental)
|
||||
endpt += curpos;
|
||||
var dx = endpt.X - curpos.X;
|
||||
var dy = endpt.Y - curpos.Y;
|
||||
if (dx * dx + dy * dy > 0.001 * 0.001)
|
||||
{
|
||||
cutPath.StartFigure();
|
||||
leadPath.StartFigure();
|
||||
}
|
||||
curpos = endpt;
|
||||
}
|
||||
break;
|
||||
|
||||
case CodeType.SubProgramCall:
|
||||
@@ -300,8 +311,17 @@ namespace OpenNest
|
||||
break;
|
||||
|
||||
case CodeType.RapidMove:
|
||||
Flush();
|
||||
AddLine(path, (RapidMove)code, mode, ref curpos);
|
||||
{
|
||||
var rapid = (RapidMove)code;
|
||||
var endpt = rapid.EndPoint;
|
||||
if (mode == Mode.Incremental)
|
||||
endpt += curpos;
|
||||
var dx = endpt.X - curpos.X;
|
||||
var dy = endpt.Y - curpos.Y;
|
||||
if (dx * dx + dy * dy > 0.001 * 0.001)
|
||||
Flush();
|
||||
curpos = endpt;
|
||||
}
|
||||
break;
|
||||
|
||||
case CodeType.SubProgramCall:
|
||||
|
||||
Reference in New Issue
Block a user