From 157b4a1017bc828587a15a0e86ce0178809b34f2 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Sun, 29 Nov 2026 12:24:18 -0500 Subject: [PATCH] refactor(core): share the mode-conversion walk in ConvertMode Co-Authored-By: Claude Opus 5.5 --- OpenNest.Core/Converters/ConvertMode.cs | 47 ++++++++++--------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/OpenNest.Core/Converters/ConvertMode.cs b/OpenNest.Core/Converters/ConvertMode.cs index 8e8b9937..4a7dc031 100644 --- a/OpenNest.Core/Converters/ConvertMode.cs +++ b/OpenNest.Core/Converters/ConvertMode.cs @@ -9,35 +9,15 @@ namespace OpenNest.Converters /// Converts the program to absolute coordinates. /// Does NOT check program mode before converting. /// - 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); /// /// Converts the program to incremental coordinates. /// Does NOT check program mode before converting. /// - 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; + } } } }