diff --git a/OpenNest.Core/Shapes/PipeFlangeShape.cs b/OpenNest.Core/Shapes/PipeFlangeShape.cs index c2ead366..6593a6e3 100644 --- a/OpenNest.Core/Shapes/PipeFlangeShape.cs +++ b/OpenNest.Core/Shapes/PipeFlangeShape.cs @@ -35,7 +35,7 @@ namespace OpenNest.Shapes public override IReadOnlyList 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) diff --git a/OpenNest.Core/Shapes/RoundedRectangleShape.cs b/OpenNest.Core/Shapes/RoundedRectangleShape.cs index 94e207f9..b0ece55f 100644 --- a/OpenNest.Core/Shapes/RoundedRectangleShape.cs +++ b/OpenNest.Core/Shapes/RoundedRectangleShape.cs @@ -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() diff --git a/OpenNest.Core/Shapes/ShapeDimension.cs b/OpenNest.Core/Shapes/ShapeDimension.cs index bc6a0465..ed9533d8 100644 --- a/OpenNest.Core/Shapes/ShapeDimension.cs +++ b/OpenNest.Core/Shapes/ShapeDimension.cs @@ -1,4 +1,5 @@ using OpenNest.Geometry; +using OpenNest.Math; namespace OpenNest.Shapes { @@ -58,7 +59,7 @@ namespace OpenNest.Shapes /// /// 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. /// public Vector? HoleCenter { get; private init; } @@ -67,16 +68,32 @@ namespace OpenNest.Shapes /// /// 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. /// public Vector? CircleCenter { get; private init; } /// The radius of the reference circle at . public double CircleRadius { get; private init; } - /// True when the preview draws a dotted reference circle for this dimension. + /// True when the preview draws a dashed reference circle for this dimension. public bool DrawsCircle => CircleCenter != null; + /// + /// 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. + /// + public Vector? ArcCenter { get; private init; } + + /// The radius of the arc at . + public double ArcRadius { get; private init; } + + /// The angle the arc at starts at, in radians counterclockwise. + public double ArcStartAngle { get; private init; } + + /// The angle the arc at ends at; never less than . + public double ArcEndAngle { get; private init; } + /// This dimension with "x {count} {noun}" after its value. 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, }; /// @@ -123,9 +144,10 @@ namespace OpenNest.Shapes } /// - /// 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 /// : it starts on the circle facing /// 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. /// public static ShapeDimension CircleLeader(string property, Vector center, double radius, Vector direction, string prefix = "") { @@ -139,6 +161,25 @@ namespace OpenNest.Shapes }; } + /// + /// A radius leader on the arc at from + /// counterclockwise to (radians): it starts at the arc's middle + /// and runs out along that radius, in line with the center. + /// + 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, + }; + } + /// The distance the dimension shows between its points (zero for a leader). public double Measured => Kind switch diff --git a/OpenNest.Tests/Shapes/ShapeDimensionTests.cs b/OpenNest.Tests/Shapes/ShapeDimensionTests.cs index dc6b5fa9..7a79bcba 100644 --- a/OpenNest.Tests/Shapes/ShapeDimensionTests.cs +++ b/OpenNest.Tests/Shapes/ShapeDimensionTests.cs @@ -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] diff --git a/OpenNest.WinForms.Tests/Forms/ShapeLibraryFormTests.cs b/OpenNest.WinForms.Tests/Forms/ShapeLibraryFormTests.cs index 976efd1f..44a3a720 100644 --- a/OpenNest.WinForms.Tests/Forms/ShapeLibraryFormTests.cs +++ b/OpenNest.WinForms.Tests/Forms/ShapeLibraryFormTests.cs @@ -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(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(() => { diff --git a/OpenNest/Controls/ShapePreviewControl.cs b/OpenNest/Controls/ShapePreviewControl.cs index 93d15916..5dbdafa9 100644 --- a/OpenNest/Controls/ShapePreviewControl.cs +++ b/OpenNest/Controls/ShapePreviewControl.cs @@ -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); } + /// + /// 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. + /// + 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(); - /// Dotted reference circles, such as a flange's bolt circle. + /// Dashed reference circles, such as a flange's bolt circle. public readonly List<(PointF Center, float Radius)> Circles = new(); + + /// Each leader on a circle or arc: its arrow tip, label end and the curve's center. + public readonly Dictionary Radials = new(); public readonly List