mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-17 16:37:41 -05:00
feat(ui): aim the flange hole callout through its center and dot the bolt circle
The Shape Library's hole diameter leader now crosses its hole through the center to the far edge, arrows pointing out onto both edges, wherever the label is dragged. The hole pattern dimension measures a dotted bolt circle through the hole centers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ea540253bb
commit
d7f9f25265
4 files changed
+126
-7
No files matched your search
@@ -37,19 +37,20 @@ namespace OpenNest.Shapes
|
||||
var dimensions = new List<ShapeDimension>
|
||||
{
|
||||
ShapeDimension.Linear(nameof(OD), new Vector(-OD / 2, 0), new Vector(OD / 2, 0), ShapeDimension.Up, "Ø"),
|
||||
// Measures the dotted bolt circle through the hole centers.
|
||||
ShapeDimension.Linear(nameof(HolePatternDiameter), new Vector(-HolePatternDiameter / 2, 0),
|
||||
new Vector(HolePatternDiameter / 2, 0), ShapeDimension.Down, "Ø"),
|
||||
new Vector(HolePatternDiameter / 2, 0), ShapeDimension.Down, "Ø").WithCircle(),
|
||||
};
|
||||
|
||||
// The leader starts on the hole nearest 45 degrees, at its edge facing out that way.
|
||||
// The leader crosses the hole nearest 45 degrees through its center, out that way.
|
||||
if (HoleCount > 0)
|
||||
{
|
||||
var step = 2.0 * System.Math.PI / HoleCount;
|
||||
var angle = System.Math.Round(System.Math.PI / 4 / step) * step;
|
||||
var out45 = new Vector(System.Math.Sqrt(0.5), System.Math.Sqrt(0.5));
|
||||
var center = new Vector(HolePatternDiameter / 2 * System.Math.Cos(angle), HolePatternDiameter / 2 * System.Math.Sin(angle));
|
||||
var edge = new Vector(center.X + out45.X * HoleDiameter / 2, center.Y + out45.Y * HoleDiameter / 2);
|
||||
dimensions.Add(ShapeDimension.Leader(nameof(HoleDiameter), edge, out45, "Ø").WithCount(nameof(HoleCount), "holes"));
|
||||
dimensions.Add(ShapeDimension.HoleLeader(nameof(HoleDiameter), center, HoleDiameter / 2, out45, "Ø")
|
||||
.WithCount(nameof(HoleCount), "holes"));
|
||||
}
|
||||
|
||||
return dimensions;
|
||||
|
||||
@@ -56,9 +56,42 @@ namespace OpenNest.Shapes
|
||||
/// <summary>The plural noun after the count, such as "holes".</summary>
|
||||
public string CountNoun { get; private init; }
|
||||
|
||||
/// <summary>
|
||||
/// The center of the hole a diameter leader calls out; null for other dimensions.
|
||||
/// The leader's line crosses the hole through it, whichever way the label is dragged.
|
||||
/// </summary>
|
||||
public Vector? HoleCenter { get; private init; }
|
||||
|
||||
/// <summary>The radius of the hole at <see cref="HoleCenter"/>.</summary>
|
||||
public double HoleRadius { get; private init; }
|
||||
|
||||
/// <summary>
|
||||
/// True when <see cref="Start"/> and <see cref="End"/> span the diameter of a reference
|
||||
/// circle (such as a flange's bolt circle) that the preview draws dotted.
|
||||
/// </summary>
|
||||
public bool DrawsCircle { get; private init; }
|
||||
|
||||
/// <summary>This dimension with "x {count} {noun}" after its value.</summary>
|
||||
public ShapeDimension WithCount(string countProperty, string noun) =>
|
||||
new(Property, Kind, Start, End, Side, Prefix) { CountProperty = countProperty, CountNoun = noun };
|
||||
new(Property, Kind, Start, End, Side, Prefix)
|
||||
{
|
||||
CountProperty = countProperty,
|
||||
CountNoun = noun,
|
||||
HoleCenter = HoleCenter,
|
||||
HoleRadius = HoleRadius,
|
||||
DrawsCircle = DrawsCircle,
|
||||
};
|
||||
|
||||
/// <summary>This dimension measuring a dotted circle whose diameter runs from <see cref="Start"/> to <see cref="End"/>.</summary>
|
||||
public ShapeDimension WithCircle() =>
|
||||
new(Property, Kind, Start, End, Side, Prefix)
|
||||
{
|
||||
CountProperty = CountProperty,
|
||||
CountNoun = CountNoun,
|
||||
HoleCenter = HoleCenter,
|
||||
HoleRadius = HoleRadius,
|
||||
DrawsCircle = true,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Measures from <paramref name="start"/> to <paramref name="end"/> across
|
||||
@@ -77,6 +110,22 @@ namespace OpenNest.Shapes
|
||||
new Vector(direction.X / length, direction.Y / length), prefix);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A diameter leader on the hole at <paramref name="center"/>: it starts on the hole's
|
||||
/// edge facing <paramref name="direction"/>, and its line runs across the hole through the center.
|
||||
/// </summary>
|
||||
public static ShapeDimension HoleLeader(string property, Vector center, double radius, Vector direction, string prefix = "")
|
||||
{
|
||||
var length = System.Math.Sqrt(direction.X * direction.X + direction.Y * direction.Y);
|
||||
var unit = new Vector(direction.X / length, direction.Y / length);
|
||||
var edge = new Vector(center.X + unit.X * radius, center.Y + unit.Y * radius);
|
||||
return new(property, ShapeDimensionKind.Leader, edge, edge, unit, prefix)
|
||||
{
|
||||
HoleCenter = center,
|
||||
HoleRadius = radius,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>The distance the dimension shows between its points (zero for a leader).</summary>
|
||||
public double Measured =>
|
||||
Kind switch
|
||||
|
||||
@@ -49,6 +49,35 @@ public class ShapeDimensionTests
|
||||
Assert.Equal("holes", holes.CountNoun);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(4)]
|
||||
[InlineData(8)]
|
||||
public void FlangeHoleLeaderCrossesAHoleThroughItsCenter(int holeCount)
|
||||
{
|
||||
var flange = new PipeFlangeShape { OD = 7.5, HoleDiameter = 0.875, HolePatternDiameter = 5.5, HoleCount = holeCount };
|
||||
|
||||
var holes = flange.GetDimensions().Single(d => d.Property == nameof(PipeFlangeShape.HoleDiameter));
|
||||
|
||||
Assert.NotNull(holes.HoleCenter);
|
||||
var center = holes.HoleCenter!.Value;
|
||||
Assert.Equal(5.5 / 2, center.DistanceTo(new OpenNest.Geometry.Vector(0, 0)), 6);
|
||||
Assert.Equal(0.875 / 2, holes.HoleRadius, 6);
|
||||
Assert.Equal(holes.HoleRadius, holes.Start.DistanceTo(center), 6);
|
||||
Assert.Equal(nameof(PipeFlangeShape.HoleCount), holes.CountProperty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FlangeHolePatternMeasuresADottedBoltCircle()
|
||||
{
|
||||
var flange = new PipeFlangeShape();
|
||||
flange.SetPreviewDefaults();
|
||||
|
||||
var pattern = flange.GetDimensions().Single(d => d.Property == nameof(PipeFlangeShape.HolePatternDiameter));
|
||||
|
||||
Assert.True(pattern.DrawsCircle);
|
||||
Assert.Equal(flange.HolePatternDiameter, pattern.Start.DistanceTo(pattern.End), 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RoundedRectangleRadiusLeaderStartsOnTheCornerArc()
|
||||
{
|
||||
|
||||
@@ -421,6 +421,9 @@ namespace OpenNest.Controls
|
||||
{
|
||||
public readonly List<(PointF A, PointF B)> Lines = new();
|
||||
public readonly List<(PointF Tip, PointF From)> Arrows = new();
|
||||
|
||||
/// <summary>Dotted reference circles, such as a flange's bolt circle.</summary>
|
||||
public readonly List<(PointF Center, float Radius)> Circles = new();
|
||||
public readonly List<Label> Labels = new();
|
||||
|
||||
/// <summary>Everything the dimensions draw (not the notes), in control pixels.</summary>
|
||||
@@ -511,6 +514,9 @@ namespace OpenNest.Controls
|
||||
|
||||
var lineA = Along(a, side, extreme - Dot(a, side) + gap);
|
||||
var lineB = Along(b, side, extreme - Dot(b, side) + gap);
|
||||
if (d.DrawsCircle)
|
||||
result.Circles.Add((new PointF((a.X + b.X) / 2, (a.Y + b.Y) / 2),
|
||||
(float)System.Math.Sqrt(Dot(new PointF(b.X - a.X, b.Y - a.Y), new PointF(b.X - a.X, b.Y - a.Y))) / 2));
|
||||
result.Lines.Add((Along(a, side, 3), Along(lineA, side, ExtensionOverrun)));
|
||||
result.Lines.Add((Along(b, side, 3), Along(lineB, side, ExtensionOverrun)));
|
||||
AddDimensionLine(result, lineA, lineB);
|
||||
@@ -564,12 +570,42 @@ namespace OpenNest.Controls
|
||||
var right = end.X >= p.X;
|
||||
var shoulder = new PointF(end.X + (right ? Shoulder : -Shoulder), end.Y);
|
||||
|
||||
result.Lines.Add((p, end));
|
||||
if (!AddHoleDiameter(result, d, end))
|
||||
{
|
||||
result.Lines.Add((p, end));
|
||||
result.Arrows.Add((p, end));
|
||||
}
|
||||
result.Lines.Add((end, shoulder));
|
||||
result.Arrows.Add((p, end));
|
||||
AddLabels(result, annotation, new PointF(shoulder.X + (right ? 2 : -2), shoulder.Y), right ? 0 : 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// For a hole's diameter leader, the line from <paramref name="end"/> straight through the
|
||||
/// hole's center to its far edge, with arrows pointing out onto both edges; false (nothing added) for
|
||||
/// other leaders, or when the label end sits inside the hole.
|
||||
/// </summary>
|
||||
private bool AddHoleDiameter(AnnotationLayout result, ShapeDimension d, PointF end)
|
||||
{
|
||||
if (d.HoleCenter is not { } holeCenter || d.HoleRadius <= 0)
|
||||
return false;
|
||||
|
||||
var center = ToScreen(holeCenter);
|
||||
var radius = LengthWorldToGui(d.HoleRadius);
|
||||
var toEnd = new PointF(end.X - center.X, end.Y - center.Y);
|
||||
var distance = (float)System.Math.Sqrt(Dot(toEnd, toEnd));
|
||||
if (distance <= radius)
|
||||
return false;
|
||||
|
||||
var u = new PointF(toEnd.X / distance, toEnd.Y / distance);
|
||||
var near = Along(center, u, radius);
|
||||
var far = Along(center, u, -radius);
|
||||
result.Lines.Add((far, end));
|
||||
// Both arrows point out from the center onto the hole's edges.
|
||||
result.Arrows.Add((near, center));
|
||||
result.Arrows.Add((far, center));
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void AddDimensionLine(AnnotationLayout result, PointF a, PointF b)
|
||||
{
|
||||
result.Lines.Add((a, b));
|
||||
@@ -654,6 +690,10 @@ namespace OpenNest.Controls
|
||||
foreach (var (a, b) in drawn.Lines)
|
||||
g.DrawLine(line, a, b);
|
||||
|
||||
using var dotted = new Pen(DimensionColor, 1f) { DashStyle = System.Drawing.Drawing2D.DashStyle.Dot };
|
||||
foreach (var (center, radius) in drawn.Circles)
|
||||
g.DrawEllipse(dotted, center.X - radius, center.Y - radius, 2 * radius, 2 * radius);
|
||||
|
||||
using var arrowBrush = new SolidBrush(DimensionColor);
|
||||
foreach (var (tip, from) in drawn.Arrows)
|
||||
FillArrow(g, arrowBrush, tip, from);
|
||||
|
||||
Reference in new issue
Block a user