mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-15 23:55:37 -05:00
feat(shapes): keep diameter and radius leaders aimed at the circle center
Dragging a Shape Library leader on a bolt circle, hole or corner arc now moves its arrow round the curve so the line stays radial. The flange's bolt circle is drawn dashed in teal instead of dotted orange, which read as a solid dimension line. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
8e79a416ed
commit
536f7f7fd7
6 files changed
+157
-23
No files matched your search
@@ -35,7 +35,7 @@ namespace OpenNest.Shapes
|
||||
|
||||
public override IReadOnlyList<ShapeDimension> GetDimensions()
|
||||
{
|
||||
// The dotted bolt circle through the hole centers is called out by a leader from
|
||||
// The dashed bolt circle through the hole centers is called out by a leader from
|
||||
// outside, down and to the left, landing between holes rather than on one.
|
||||
var boltAngle = 1.25 * System.Math.PI;
|
||||
if (HoleCount > 0)
|
||||
|
||||
@@ -27,9 +27,8 @@ namespace OpenNest.Shapes
|
||||
ShapeDimension.Linear(nameof(Width), new Vector(Length, 0), new Vector(Length, Width), ShapeDimension.Right),
|
||||
// On the top-left corner arc, pointing out along its 45-degree radius, clear of
|
||||
// the width dimension on the right.
|
||||
ShapeDimension.Leader(nameof(Radius),
|
||||
new Vector(Radius * (1 - System.Math.Sqrt(0.5)), Width - Radius * (1 - System.Math.Sqrt(0.5))),
|
||||
new Vector(-1, 1), "R"),
|
||||
ShapeDimension.ArcLeader(nameof(Radius), new Vector(Radius, Width - Radius), Radius,
|
||||
Angle.ToRadians(90), Angle.ToRadians(180), "R"),
|
||||
};
|
||||
|
||||
public override Drawing GetDrawing()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using OpenNest.Geometry;
|
||||
using OpenNest.Math;
|
||||
|
||||
namespace OpenNest.Shapes
|
||||
{
|
||||
@@ -58,7 +59,7 @@ namespace OpenNest.Shapes
|
||||
|
||||
/// <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.
|
||||
/// The leader's line crosses the hole through it, wherever the label is dragged.
|
||||
/// </summary>
|
||||
public Vector? HoleCenter { get; private init; }
|
||||
|
||||
@@ -67,16 +68,32 @@ namespace OpenNest.Shapes
|
||||
|
||||
/// <summary>
|
||||
/// The center of a reference circle (such as a flange's bolt circle) that the preview
|
||||
/// draws dotted; null when there is none.
|
||||
/// draws dashed; null when there is none. A leader on it points at this center
|
||||
/// wherever the label is dragged.
|
||||
/// </summary>
|
||||
public Vector? CircleCenter { get; private init; }
|
||||
|
||||
/// <summary>The radius of the reference circle at <see cref="CircleCenter"/>.</summary>
|
||||
public double CircleRadius { get; private init; }
|
||||
|
||||
/// <summary>True when the preview draws a dotted reference circle for this dimension.</summary>
|
||||
/// <summary>True when the preview draws a dashed reference circle for this dimension.</summary>
|
||||
public bool DrawsCircle => CircleCenter != null;
|
||||
|
||||
/// <summary>
|
||||
/// The center of the arc a radius leader calls out; null for other dimensions. The
|
||||
/// leader points at it, its arrow sliding along the arc as the label is dragged.
|
||||
/// </summary>
|
||||
public Vector? ArcCenter { get; private init; }
|
||||
|
||||
/// <summary>The radius of the arc at <see cref="ArcCenter"/>.</summary>
|
||||
public double ArcRadius { get; private init; }
|
||||
|
||||
/// <summary>The angle the arc at <see cref="ArcCenter"/> starts at, in radians counterclockwise.</summary>
|
||||
public double ArcStartAngle { get; private init; }
|
||||
|
||||
/// <summary>The angle the arc at <see cref="ArcCenter"/> ends at; never less than <see cref="ArcStartAngle"/>.</summary>
|
||||
public double ArcEndAngle { 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)
|
||||
@@ -87,6 +104,10 @@ namespace OpenNest.Shapes
|
||||
HoleRadius = HoleRadius,
|
||||
CircleCenter = CircleCenter,
|
||||
CircleRadius = CircleRadius,
|
||||
ArcCenter = ArcCenter,
|
||||
ArcRadius = ArcRadius,
|
||||
ArcStartAngle = ArcStartAngle,
|
||||
ArcEndAngle = ArcEndAngle,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
@@ -123,9 +144,10 @@ namespace OpenNest.Shapes
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A diameter leader on a dotted reference circle (such as a flange's bolt circle) at
|
||||
/// A diameter leader on a dashed reference circle (such as a flange's bolt circle) at
|
||||
/// <paramref name="center"/>: it starts on the circle facing <paramref name="direction"/>
|
||||
/// and runs out from there, its arrow pointing in onto the circle; no line crosses it.
|
||||
/// Dragged, the arrow moves around the circle so the leader keeps pointing at the center.
|
||||
/// </summary>
|
||||
public static ShapeDimension CircleLeader(string property, Vector center, double radius, Vector direction, string prefix = "")
|
||||
{
|
||||
@@ -139,6 +161,25 @@ namespace OpenNest.Shapes
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A radius leader on the arc at <paramref name="center"/> from <paramref name="startAngle"/>
|
||||
/// counterclockwise to <paramref name="endAngle"/> (radians): it starts at the arc's middle
|
||||
/// and runs out along that radius, in line with the center.
|
||||
/// </summary>
|
||||
public static ShapeDimension ArcLeader(string property, Vector center, double radius, double startAngle, double endAngle, string prefix = "")
|
||||
{
|
||||
var middle = (startAngle + endAngle) / 2;
|
||||
var unit = new Vector(PortableMath.Cos(middle), PortableMath.Sin(middle));
|
||||
var edge = new Vector(center.X + unit.X * radius, center.Y + unit.Y * radius);
|
||||
return new(property, ShapeDimensionKind.Leader, edge, edge, unit, prefix)
|
||||
{
|
||||
ArcCenter = center,
|
||||
ArcRadius = radius,
|
||||
ArcStartAngle = startAngle,
|
||||
ArcEndAngle = endAngle,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>The distance the dimension shows between its points (zero for a leader).</summary>
|
||||
public double Measured =>
|
||||
Kind switch
|
||||
|
||||
@@ -67,7 +67,7 @@ public class ShapeDimensionTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FlangeHolePatternMeasuresADottedBoltCircle()
|
||||
public void FlangeHolePatternMeasuresADashedBoltCircle()
|
||||
{
|
||||
var flange = new PipeFlangeShape();
|
||||
flange.SetPreviewDefaults();
|
||||
@@ -90,6 +90,11 @@ public class ShapeDimensionTests
|
||||
var radius = shape.GetDimensions().Single(d => d.Property == nameof(RoundedRectangleShape.Radius));
|
||||
|
||||
Assert.Equal(2, radius.Start.DistanceTo(new OpenNest.Geometry.Vector(2, 4)), 6);
|
||||
// It calls out the top-left corner arc and starts out in line with that arc's center.
|
||||
Assert.Equal(new OpenNest.Geometry.Vector(2, 4), radius.ArcCenter);
|
||||
Assert.Equal(2, radius.ArcRadius, 6);
|
||||
Assert.Equal(-System.Math.Sqrt(0.5), radius.Side.X, 6);
|
||||
Assert.Equal(System.Math.Sqrt(0.5), radius.Side.Y, 6);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
||||
@@ -116,6 +116,32 @@ public class ShapeLibraryFormTests
|
||||
Assert.Equal(15, preview.Plate.Parts.Single().BoundingBox.Length, 6);
|
||||
});
|
||||
|
||||
[Theory]
|
||||
[InlineData(typeof(PipeFlangeShape), "HolePatternDiameter")]
|
||||
[InlineData(typeof(PipeFlangeShape), "HoleDiameter")]
|
||||
[InlineData(typeof(RoundedRectangleShape), "Radius")]
|
||||
public void ADraggedDiameterOrRadiusLeaderStillPointsAtTheCenter(Type shapeType, string property) => ArchUnitsTests.RunSta(() =>
|
||||
{
|
||||
using var form = new ShapeLibraryForm();
|
||||
SelectShape(form, shapeType);
|
||||
var preview = Find<ShapePreviewControl>(form, "previewBox");
|
||||
|
||||
// Off the default radius, but still within a corner arc's quarter turn.
|
||||
foreach (var offset in new[] { PointF.Empty, new PointF(-25, -60), new PointF(-70, 10) })
|
||||
{
|
||||
preview.SetDragOffset(property, offset);
|
||||
var (tip, end, center) = preview.RadialLeader(property)!.Value;
|
||||
|
||||
var toTip = new PointF(tip.X - center.X, tip.Y - center.Y);
|
||||
var toEnd = new PointF(end.X - center.X, end.Y - center.Y);
|
||||
var cross = toTip.X * toEnd.Y - toTip.Y * toEnd.X;
|
||||
var lengths = System.Math.Sqrt(toTip.X * toTip.X + toTip.Y * toTip.Y) * System.Math.Sqrt(toEnd.X * toEnd.X + toEnd.Y * toEnd.Y);
|
||||
Assert.True(lengths > 0);
|
||||
Assert.True(System.Math.Abs(cross) / lengths < 0.01, $"{property} at {offset}: leader misses the center");
|
||||
Assert.True(toTip.X * toEnd.X + toTip.Y * toEnd.Y > 0);
|
||||
}
|
||||
});
|
||||
|
||||
[Fact]
|
||||
public void FlangeHoleCountSitsBesideTheHoleDiameter() => ArchUnitsTests.RunSta(() =>
|
||||
{
|
||||
|
||||
@@ -41,6 +41,9 @@ namespace OpenNest.Controls
|
||||
private static readonly Color DimensionColor = Color.FromArgb(235, 230, 120, 20);
|
||||
private static readonly Color HoverColor = Color.FromArgb(255, 140, 0);
|
||||
private static readonly Color ActiveColor = Color.FromArgb(220, 30, 110, 200);
|
||||
// Reference geometry, such as a bolt circle: not a cut, and not a dimension line.
|
||||
private static readonly Color ReferenceColor = Color.FromArgb(0, 120, 140);
|
||||
private static readonly float[] ReferenceDashes = { 7f, 5f };
|
||||
|
||||
private string[] infoLines;
|
||||
private Vector shapeOffset;
|
||||
@@ -402,6 +405,13 @@ namespace OpenNest.Controls
|
||||
ViewChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// For a leader on a circle or arc: its arrow tip, its label end and the curve's center
|
||||
/// on the control; null for other dimensions.
|
||||
/// </summary>
|
||||
internal (PointF Tip, PointF End, PointF Center)? RadialLeader(string property) =>
|
||||
CurrentLayout().Radials.TryGetValue(property, out var radial) ? radial : null;
|
||||
|
||||
private Label HitTest(Point location) =>
|
||||
CurrentLayout().Labels.FirstOrDefault(l => l.Enabled && l.Bounds.Contains(location));
|
||||
|
||||
@@ -480,8 +490,11 @@ 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>
|
||||
/// <summary>Dashed reference circles, such as a flange's bolt circle.</summary>
|
||||
public readonly List<(PointF Center, float Radius)> Circles = new();
|
||||
|
||||
/// <summary>Each leader on a circle or arc: its arrow tip, label end and the curve's center.</summary>
|
||||
public readonly Dictionary<string, (PointF Tip, PointF End, PointF Center)> Radials = new();
|
||||
public readonly List<Label> Labels = new();
|
||||
|
||||
/// <summary>Everything the dimensions draw (not the notes), in control pixels.</summary>
|
||||
@@ -681,21 +694,72 @@ namespace OpenNest.Controls
|
||||
var shoulder = new PointF(end.X + (right ? Shoulder : -Shoulder), end.Y);
|
||||
|
||||
AddReferenceCircle(result, d);
|
||||
if (!AddHoleDiameter(result, d, end))
|
||||
if (!AddHoleDiameter(result, d, end, dir))
|
||||
{
|
||||
result.Lines.Add((p, end));
|
||||
result.Arrows.Add((p, end));
|
||||
// On a circle or arc the arrow moves round the curve, so the leader stays radial.
|
||||
var tip = RadialTip(result, d, end, p);
|
||||
result.Lines.Add((tip, end));
|
||||
result.Arrows.Add((tip, end));
|
||||
}
|
||||
result.Lines.Add((end, shoulder));
|
||||
AddLabels(result, annotation, new PointF(shoulder.X + (right ? 2 : -2), shoulder.Y), right ? 0 : 1, drag);
|
||||
}
|
||||
|
||||
/// <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.
|
||||
/// Where a leader's arrow touches its curve with the label end at <paramref name="end"/>:
|
||||
/// on the reference circle or arc where the line from the center to the label crosses
|
||||
/// it, so the leader points at the center. Past an arc's ends the arrow stops at the
|
||||
/// nearer end. Other leaders keep <paramref name="start"/>.
|
||||
/// </summary>
|
||||
private bool AddHoleDiameter(AnnotationLayout result, ShapeDimension d, PointF end)
|
||||
private PointF RadialTip(AnnotationLayout result, ShapeDimension d, PointF end, PointF start)
|
||||
{
|
||||
Vector center;
|
||||
double radius;
|
||||
var isArc = false;
|
||||
if (d.CircleCenter is { } circleCenter && d.CircleRadius > 0)
|
||||
(center, radius) = (circleCenter, d.CircleRadius);
|
||||
else if (d.ArcCenter is { } arcCenter && d.ArcRadius > 0)
|
||||
(center, radius, isArc) = (arcCenter, d.ArcRadius, true);
|
||||
else
|
||||
return start;
|
||||
|
||||
var c = ToScreen(center);
|
||||
var tip = start;
|
||||
var toEnd = new PointF(end.X - c.X, end.Y - c.Y);
|
||||
if (Dot(toEnd, toEnd) >= 1)
|
||||
{
|
||||
// Screen Y runs down; shape angles are counterclockwise with Y up.
|
||||
var angle = System.Math.Atan2(-toEnd.Y, toEnd.X);
|
||||
if (isArc)
|
||||
angle = ClampToArc(angle, d.ArcStartAngle, d.ArcEndAngle);
|
||||
tip = ToScreen(new Vector(center.X + radius * System.Math.Cos(angle), center.Y + radius * System.Math.Sin(angle)));
|
||||
}
|
||||
result.Radials[d.Property] = (tip, end, c);
|
||||
return tip;
|
||||
}
|
||||
|
||||
/// <summary>The angle on the arc from <paramref name="start"/> to <paramref name="end"/> nearest <paramref name="angle"/>.</summary>
|
||||
private static double ClampToArc(double angle, double start, double end)
|
||||
{
|
||||
const double turn = 2 * System.Math.PI;
|
||||
var sweep = end - start;
|
||||
if (sweep >= turn)
|
||||
return angle;
|
||||
var offset = (angle - start) % turn;
|
||||
if (offset < 0)
|
||||
offset += turn;
|
||||
if (offset <= sweep)
|
||||
return start + offset;
|
||||
return offset - sweep < turn - offset ? end : start;
|
||||
}
|
||||
|
||||
/// <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; with the label
|
||||
/// end inside the hole, the line spans the hole through that end. False (nothing added)
|
||||
/// for other leaders.
|
||||
/// </summary>
|
||||
private bool AddHoleDiameter(AnnotationLayout result, ShapeDimension d, PointF end, PointF fallback)
|
||||
{
|
||||
if (d.HoleCenter is not { } holeCenter || d.HoleRadius <= 0)
|
||||
return false;
|
||||
@@ -704,16 +768,15 @@ namespace OpenNest.Controls
|
||||
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 u = distance >= 1 ? new PointF(toEnd.X / distance, toEnd.Y / distance) : fallback;
|
||||
var near = Along(center, u, radius);
|
||||
var far = Along(center, u, -radius);
|
||||
result.Lines.Add((far, end));
|
||||
result.Lines.Add((far, distance > radius ? end : near));
|
||||
// Both arrows point out from the center onto the hole's edges.
|
||||
result.Arrows.Add((near, center));
|
||||
result.Arrows.Add((far, center));
|
||||
result.Radials[d.Property] = (near, end, center);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -803,9 +866,9 @@ 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 };
|
||||
using var dashed = new Pen(ReferenceColor, 1f) { DashPattern = ReferenceDashes };
|
||||
foreach (var (center, radius) in drawn.Circles)
|
||||
g.DrawEllipse(dotted, center.X - radius, center.Y - radius, 2 * radius, 2 * radius);
|
||||
g.DrawEllipse(dashed, center.X - radius, center.Y - radius, 2 * radius, 2 * radius);
|
||||
|
||||
using var arrowBrush = new SolidBrush(DimensionColor);
|
||||
foreach (var (tip, from) in drawn.Arrows)
|
||||
|
||||
Reference in new issue
Block a user