mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 00:51:34 -05:00
fix(cad): run extension lines to the offset part profile
Extension lines started at the extents' corners, which float in empty space on angled or irregular parts. They now run from the dimension line toward the part until they first meet the profile offset by a fixed screen gap, so they reach the feature they measure without touching it at any angle. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
abced0a7ec
commit
05d5c56ac0
4 files changed
+123
-15
No files matched your search
@@ -20,22 +20,27 @@ namespace OpenNest.Dimensions
|
||||
/// </summary>
|
||||
public static Box Extents(IEnumerable<Entity> entities)
|
||||
{
|
||||
var measured = entities
|
||||
.Where(e => SpecialLayers.IsMaterial(e.Layer) && !ScribeLayerRepair.IsMarkLayer(e.Layer))
|
||||
.ToList();
|
||||
var measured = Measured(entities);
|
||||
return measured.Count == 0 ? null : measured.GetBoundingBox();
|
||||
}
|
||||
|
||||
private static List<Entity> Measured(IEnumerable<Entity> entities) =>
|
||||
entities
|
||||
.Where(e => SpecialLayers.IsMaterial(e.Layer) && !ScribeLayerRepair.IsMarkLayer(e.Layer))
|
||||
.ToList();
|
||||
|
||||
public static DimensionLayout Layout(IEnumerable<Entity> entities, DrawingDimension dimension)
|
||||
{
|
||||
var kind = dimension.Kind;
|
||||
if (dimension.Offset == 0 || !double.IsFinite(dimension.Offset))
|
||||
return new DimensionLayout { Kind = kind, Error = "its offset is not usable" };
|
||||
|
||||
var box = Extents(entities);
|
||||
if (box == null)
|
||||
var measured = Measured(entities);
|
||||
if (measured.Count == 0)
|
||||
return new DimensionLayout { Kind = kind, Error = "there is no cut geometry to measure" };
|
||||
|
||||
var box = measured.GetBoundingBox();
|
||||
|
||||
var offset = dimension.Offset;
|
||||
if (kind == DimensionKind.OverallHorizontal)
|
||||
{
|
||||
@@ -47,6 +52,7 @@ namespace OpenNest.Dimensions
|
||||
{
|
||||
Kind = kind,
|
||||
Extents = box,
|
||||
Profile = measured,
|
||||
Value = box.Length,
|
||||
ExtensionStart = new Vector(box.Left, edge),
|
||||
ExtensionEnd = new Vector(box.Right, edge),
|
||||
@@ -63,6 +69,7 @@ namespace OpenNest.Dimensions
|
||||
{
|
||||
Kind = kind,
|
||||
Extents = box,
|
||||
Profile = measured,
|
||||
Value = box.Width,
|
||||
ExtensionStart = new Vector(side, box.Bottom),
|
||||
ExtensionEnd = new Vector(side, box.Top),
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using OpenNest.Geometry;
|
||||
|
||||
namespace OpenNest.Dimensions
|
||||
{
|
||||
/// <summary>
|
||||
/// A dimension resolved against a part's geometry. Extension lines run from
|
||||
/// <see cref="ExtensionStart"/> to <see cref="LineStart"/> and from <see cref="ExtensionEnd"/>
|
||||
/// to <see cref="LineEnd"/>; the dimension line joins the last two.
|
||||
/// A dimension resolved against a part's geometry. Extension lines run along the extents, on
|
||||
/// the lines through <see cref="ExtensionStart"/> and <see cref="LineStart"/> and through
|
||||
/// <see cref="ExtensionEnd"/> and <see cref="LineEnd"/>, out from the part to
|
||||
/// <see cref="ExtensionTip"/>; the dimension line joins the last two.
|
||||
/// </summary>
|
||||
public sealed class DimensionLayout
|
||||
{
|
||||
private const int MaxExtensionSteps = 64;
|
||||
private const double ExtensionPrecision = 0.05;
|
||||
|
||||
public DimensionKind Kind { get; init; }
|
||||
|
||||
/// <summary>Why the dimension could not be resolved; null when it was.</summary>
|
||||
@@ -26,10 +32,50 @@ namespace OpenNest.Dimensions
|
||||
/// <summary>The part's extents the dimension was measured from.</summary>
|
||||
public Box Extents { get; init; }
|
||||
|
||||
/// <summary>The geometry behind <see cref="Extents"/>.</summary>
|
||||
public IReadOnlyList<Entity> Profile { get; init; } = Array.Empty<Entity>();
|
||||
|
||||
public string ValueText => Value.ToString("0.000", CultureInfo.InvariantCulture);
|
||||
|
||||
public Vector MidPoint => new((LineStart.X + LineEnd.X) / 2, (LineStart.Y + LineEnd.Y) / 2);
|
||||
|
||||
/// <summary>
|
||||
/// Where an extension line leaving <paramref name="foot"/> on the dimension line first
|
||||
/// meets the profile offset by <paramref name="clearance"/>, walking toward the part by the
|
||||
/// distance known to be free at each step. <paramref name="foot"/> itself when it is
|
||||
/// already that close.
|
||||
/// </summary>
|
||||
public Vector ExtensionTip(Vector foot, double clearance)
|
||||
{
|
||||
var horizontal = Kind == DimensionKind.OverallHorizontal;
|
||||
var position = horizontal ? foot.Y : foot.X;
|
||||
var center = horizontal
|
||||
? (Extents.Bottom + Extents.Top) / 2
|
||||
: (Extents.Left + Extents.Right) / 2;
|
||||
var sign = System.Math.Sign(center - position);
|
||||
var direction = horizontal ? new Vector(0, sign) : new Vector(sign, 0);
|
||||
|
||||
// The walk ends at the far side of the part at the latest: the line touches the part.
|
||||
var limit = System.Math.Abs(center - position) + (horizontal ? Extents.Width : Extents.Length) / 2;
|
||||
var travelled = 0.0;
|
||||
for (var i = 0; i < MaxExtensionSteps && travelled < limit; i++)
|
||||
{
|
||||
var free = DistanceToProfile(foot + direction * travelled) - clearance;
|
||||
if (free <= clearance * ExtensionPrecision)
|
||||
break;
|
||||
travelled += free;
|
||||
}
|
||||
return foot + direction * System.Math.Min(travelled, limit);
|
||||
}
|
||||
|
||||
private double DistanceToProfile(Vector point)
|
||||
{
|
||||
var nearest = double.MaxValue;
|
||||
foreach (var entity in Profile)
|
||||
nearest = System.Math.Min(nearest, point.DistanceTo(entity.ClosestPointTo(point)));
|
||||
return nearest;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The offset that puts the dimension line through <paramref name="point"/>, on the side of
|
||||
/// the part's center the point is on and at least <paramref name="minimum"/> clear of it.
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
using OpenNest.Dimensions;
|
||||
using OpenNest.Geometry;
|
||||
|
||||
namespace OpenNest.Tests.Dimensions;
|
||||
|
||||
public class DimensionExtensionTests
|
||||
{
|
||||
private const double Clearance = 0.1;
|
||||
|
||||
private static List<Entity> Loop(params Vector[] points)
|
||||
{
|
||||
var layer = new Layer("0") { IsVisible = true };
|
||||
var entities = new List<Entity>();
|
||||
for (var i = 0; i < points.Length; i++)
|
||||
entities.Add(new Line(points[i], points[(i + 1) % points.Length]) { Layer = layer });
|
||||
return entities;
|
||||
}
|
||||
|
||||
private static DimensionLayout Horizontal(List<Entity> entities, double offset) =>
|
||||
DimensionBuilder.Layout(entities,
|
||||
new DrawingDimension { Kind = DimensionKind.OverallHorizontal, Offset = offset });
|
||||
|
||||
[Fact]
|
||||
public void OnARectangle_TheLineStopsAtTheOffsetEdge()
|
||||
{
|
||||
var layout = Horizontal(Loop(new(0, 0), new(10, 0), new(10, 6), new(0, 6)), -1);
|
||||
|
||||
var tip = layout.ExtensionTip(layout.LineStart, Clearance);
|
||||
|
||||
Assert.Equal(0, tip.X, 6);
|
||||
Assert.Equal(-Clearance, tip.Y, 1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OnADiamond_TheLineFollowsTheExtentsUntilItMeetsTheOffsetProfile()
|
||||
{
|
||||
var part = Loop(new(5, 0), new(10, 3), new(5, 6), new(0, 3));
|
||||
var layout = Horizontal(part, -1);
|
||||
|
||||
var tip = layout.ExtensionTip(layout.LineStart, Clearance);
|
||||
|
||||
Assert.Equal(0, tip.X, 6);
|
||||
Assert.InRange(tip.Y, 0, 3);
|
||||
var distance = part.Min(e => tip.DistanceTo(e.ClosestPointTo(tip)));
|
||||
Assert.InRange(distance, Clearance, Clearance * 1.06);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AFootAlreadyWithinTheClearance_StaysPut()
|
||||
{
|
||||
var layout = Horizontal(Loop(new(0, 0), new(10, 0), new(10, 6), new(0, 6)), -0.05);
|
||||
|
||||
var tip = layout.ExtensionTip(layout.LineStart, Clearance);
|
||||
|
||||
Assert.Equal(layout.LineStart.Y, tip.Y, 6);
|
||||
}
|
||||
}
|
||||
@@ -120,8 +120,9 @@ namespace OpenNest.Controls
|
||||
return;
|
||||
var along = new PointF((float)((to.X - from.X) / length), (float)((to.Y - from.Y) / length));
|
||||
|
||||
DrawExtensionLine(g, pen, PointWorldToGraph(layout.ExtensionStart), from);
|
||||
DrawExtensionLine(g, pen, PointWorldToGraph(layout.ExtensionEnd), to);
|
||||
var clearance = LengthGuiToWorld(DimensionGapPixels);
|
||||
DrawExtensionLine(g, pen, PointWorldToGraph(layout.ExtensionTip(layout.LineStart, clearance)), from);
|
||||
DrawExtensionLine(g, pen, PointWorldToGraph(layout.ExtensionTip(layout.LineEnd, clearance)), to);
|
||||
g.DrawLine(pen, from, to);
|
||||
DrawArrow(g, brush, from, along);
|
||||
DrawArrow(g, brush, to, new PointF(-along.X, -along.Y));
|
||||
@@ -137,8 +138,7 @@ namespace OpenNest.Controls
|
||||
g.Restore(state);
|
||||
}
|
||||
|
||||
// Starts clear of the part and runs a little past the dimension line, the way drafted
|
||||
// extension lines do.
|
||||
// Runs a little past the dimension line, the way drafted extension lines do.
|
||||
private static void DrawExtensionLine(Graphics g, Pen pen, PointF from, PointF to)
|
||||
{
|
||||
var dx = to.X - from.X;
|
||||
@@ -146,11 +146,9 @@ namespace OpenNest.Controls
|
||||
var length = (float)System.Math.Sqrt(dx * dx + dy * dy);
|
||||
if (length < 1)
|
||||
return;
|
||||
var start = new PointF(from.X + dx / length * DimensionGapPixels,
|
||||
from.Y + dy / length * DimensionGapPixels);
|
||||
var end = new PointF(to.X + dx / length * DimensionOvershootPixels,
|
||||
to.Y + dy / length * DimensionOvershootPixels);
|
||||
g.DrawLine(pen, start, end);
|
||||
g.DrawLine(pen, from, end);
|
||||
}
|
||||
|
||||
private static void DrawArrow(Graphics g, Brush brush, PointF tip, PointF direction)
|
||||
|
||||
Reference in new issue
Block a user