mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-23 12:27:41 -05:00
Imported drawings are incremental programs, but PartLabels.AddTo appended absolute points, so each stroke was read as a step from the last and the label ran far off the sheet. It now writes steps from where the program stops (ConvertMode.EndPosition). On load, DrawingMarkRecovery took the etched label from applied part programs back into the drawing as a lost mark, so it was etched again by every later plan, labels off included. Drawings with a label box are now skipped. Also load a labelMode number that names no mode as if it were absent, and show such a mode as Off in the Cutting Parameters dropdown instead of throwing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
104 lines
3.5 KiB
C#
104 lines
3.5 KiB
C#
using OpenNest.CNC;
|
|
using OpenNest.Geometry;
|
|
|
|
namespace OpenNest.Converters
|
|
{
|
|
public static class ConvertMode
|
|
{
|
|
/// <summary>
|
|
/// Converts the program to absolute coordinates.
|
|
/// Does NOT check program mode before converting.
|
|
/// </summary>
|
|
public static void ToAbsolute(Program pgm)
|
|
{
|
|
var pos = new Vector(0, 0);
|
|
|
|
for (int i = 0; i < pgm.Codes.Count; ++i)
|
|
{
|
|
var code = pgm.Codes[i];
|
|
|
|
if (code is SubProgramCall subCall && subCall.Program != null)
|
|
{
|
|
// Sub-program is placed at Offset in this program's frame.
|
|
// After it runs, the tool is at Offset + (sub's end in its own frame).
|
|
pos = ComputeEndPosition(subCall.Program, subCall.Offset);
|
|
continue;
|
|
}
|
|
|
|
if (code is Motion motion)
|
|
{
|
|
motion.Offset(pos.X, pos.Y);
|
|
pos = motion.EndPoint;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts the program to incremental coordinates.
|
|
/// Does NOT check program mode before converting.
|
|
/// </summary>
|
|
public static void ToIncremental(Program pgm)
|
|
{
|
|
var pos = new Vector(0, 0);
|
|
|
|
for (int i = 0; i < pgm.Codes.Count; ++i)
|
|
{
|
|
var code = pgm.Codes[i];
|
|
|
|
if (code is SubProgramCall subCall && subCall.Program != null)
|
|
{
|
|
// Sub-program is placed at Offset in this program's frame,
|
|
// regardless of where the tool was before the call.
|
|
pos = ComputeEndPosition(subCall.Program, subCall.Offset);
|
|
continue;
|
|
}
|
|
|
|
if (code is Motion motion)
|
|
{
|
|
var pos2 = motion.EndPoint;
|
|
motion.Offset(-pos.X, -pos.Y);
|
|
pos = pos2;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Where the tool stands after running <paramref name="pgm"/> from its own origin, in
|
|
/// its own frame, whatever its mode; sub-program calls included.
|
|
/// </summary>
|
|
public static Vector EndPosition(Program pgm) => ComputeEndPosition(pgm, new Vector(0, 0));
|
|
|
|
/// <summary>
|
|
/// Computes the tool position after executing <paramref name="pgm"/>,
|
|
/// given that the program's frame origin is at <paramref name="startPos"/>
|
|
/// in the caller's frame. Walks nested sub-program calls recursively.
|
|
/// </summary>
|
|
private static Vector ComputeEndPosition(Program pgm, Vector startPos)
|
|
{
|
|
var pos = startPos;
|
|
|
|
for (int i = 0; i < pgm.Codes.Count; ++i)
|
|
{
|
|
var code = pgm.Codes[i];
|
|
|
|
if (code is SubProgramCall subCall && subCall.Program != null)
|
|
{
|
|
// Nested sub's frame origin in the caller's frame is startPos + Offset.
|
|
pos = ComputeEndPosition(subCall.Program, startPos + subCall.Offset);
|
|
continue;
|
|
}
|
|
|
|
if (code is Motion motion)
|
|
{
|
|
if (pgm.Mode == Mode.Incremental)
|
|
pos = pos + motion.EndPoint;
|
|
else
|
|
pos = startPos + motion.EndPoint;
|
|
}
|
|
}
|
|
|
|
return pos;
|
|
}
|
|
}
|
|
}
|