mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-04 06:19:24 -05:00
refactor(core): share the mode-conversion walk in ConvertMode
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7b78dc3c08
commit
157b4a1017
1 file changed
+18
-29
@@ -9,35 +9,15 @@ namespace OpenNest.Converters
|
||||
/// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void ToAbsolute(Program pgm) => Convert(pgm, toAbsolute: true);
|
||||
|
||||
/// <summary>
|
||||
/// Converts the program to incremental coordinates.
|
||||
/// Does NOT check program mode before converting.
|
||||
/// </summary>
|
||||
public static void ToIncremental(Program pgm)
|
||||
public static void ToIncremental(Program pgm) => Convert(pgm, toAbsolute: false);
|
||||
|
||||
private static void Convert(Program pgm, bool toAbsolute)
|
||||
{
|
||||
var pos = new Vector(0, 0);
|
||||
|
||||
@@ -47,17 +27,26 @@ namespace OpenNest.Converters
|
||||
|
||||
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.
|
||||
// Sub-program is placed at Offset in this program's frame, regardless of
|
||||
// where the tool was before the call. 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)
|
||||
{
|
||||
var pos2 = motion.EndPoint;
|
||||
motion.Offset(-pos.X, -pos.Y);
|
||||
pos = pos2;
|
||||
if (toAbsolute)
|
||||
{
|
||||
motion.Offset(pos.X, pos.Y);
|
||||
pos = motion.EndPoint;
|
||||
}
|
||||
else
|
||||
{
|
||||
var end = motion.EndPoint;
|
||||
motion.Offset(-pos.X, -pos.Y);
|
||||
pos = end;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user