Files
OpenNest/OpenNest.IO/Bending/BendNote.cs
T
ajandClaude Opus 5.5 45e7d6f1c8 feat(cad-converter): export DXF, DWG or SVG with bend lines that import reads back
The CAD Converter's Export button only wrote DXF (or SVG) with every entity on
layer 0 and no bends, so a re-imported export lost its bends and turned the
bend etch marks into cut geometry. Exporting now goes through a new
OpenNest.IO CadExporter:

- DXF R14-2018, DWG 2004-2018 or SVG. ACadSharp cannot write DWG 2007, and
  its DWG 2000/R14 output loses entities and layers, so those are not offered.
- Entities keep their layers; etch marks land on ETCH, which import drops and
  regenerates from the bends. A line picked as a bend is written only as it.
- DXF/DWG bends are BEND-layer CENTER lines with an "UP 90° R 0.06" MText note,
  the form SolidWorksBendDetector reads. Pre-2007 files escape non-ASCII note
  text as \U+XXXX, since ACadSharp reads raw non-ASCII back mangled.
- SVG bends are lines in a BEND group with data-bend-* attributes; the SVG
  reader now returns them as bends (scaled with the drawing units) instead of
  dropping the layer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 08:23:13 -05:00

72 lines
2.6 KiB
C#

using System.Globalization;
using OpenNest.Bending;
using OpenNest.Geometry;
using OpenNest.Math;
namespace OpenNest.IO.Bending
{
/// <summary>
/// The note an export writes beside a bend line, in the form
/// <see cref="SolidWorksBendDetector"/> reads back ("UP 90° R 0.06").
/// </summary>
internal static class BendNote
{
/// <summary>
/// The bend's own note when it has one, otherwise one built from its direction, angle
/// and radius; null when the direction is unknown, since the detector pairs a note with
/// its line by the word UP or DOWN.
/// </summary>
public static string Text(Bend bend)
{
if (!string.IsNullOrWhiteSpace(bend.NoteText))
return bend.NoteText;
var direction = bend.Direction switch
{
BendDirection.Up => "UP",
BendDirection.Down => "DOWN",
_ => null,
};
if (direction == null)
return null;
var text = direction;
if (bend.Angle.HasValue)
text += $" {Number(bend.Angle.Value)}°";
if (bend.Radius.HasValue)
text += $" R {Number(bend.Radius.Value)}";
return text;
}
/// <summary>A readable text height; the detector accepts a note within two heights of its line.</summary>
public static double Height(Units units) => units == Units.Millimeters ? 3.0 : 0.125;
/// <summary>
/// Where the note sits: just off the middle of the line, turned along it so it reads
/// left to right. The angle is in radians.
/// </summary>
public static (Vector Position, double Angle) Placement(Bend bend, double height)
{
var angle = bend.StartPoint.AngleTo(bend.EndPoint);
if (angle > Angle.HalfPI + Tolerance.Epsilon)
angle -= System.Math.PI;
else if (angle <= -Angle.HalfPI + Tolerance.Epsilon)
angle += System.Math.PI;
var gap = height * 0.25;
var middle = new Vector(
(bend.StartPoint.X + bend.EndPoint.X) / 2,
(bend.StartPoint.Y + bend.EndPoint.Y) / 2
);
var position = new Vector(
middle.X - PortableMath.Sin(angle) * gap,
middle.Y + PortableMath.Cos(angle) * gap
);
return (position, angle);
}
private static string Number(double value) =>
value.ToString("0.######", CultureInfo.InvariantCulture);
}
}