From 8a293bcc9dabce5a0f86877a21b5a19bbc07a22d Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Tue, 31 Mar 2026 21:38:36 -0400 Subject: [PATCH] feat: implement G-code editor with Apply parsing Wire up the Apply button to parse the G-code text back into a Program, rebuild contours via ConvertProgram/ShapeBuilder/ContourInfo, and fire ProgramChanged so callers receive the updated program. Co-Authored-By: Claude Sonnet 4.6 --- OpenNest/Controls/ProgramEditorControl.cs | 48 +++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/OpenNest/Controls/ProgramEditorControl.cs b/OpenNest/Controls/ProgramEditorControl.cs index 1ac9fe0..48ffa74 100644 --- a/OpenNest/Controls/ProgramEditorControl.cs +++ b/OpenNest/Controls/ProgramEditorControl.cs @@ -1,6 +1,7 @@ using OpenNest.CNC; using OpenNest.Converters; using OpenNest.Geometry; +using OpenNest.IO; using System; using System.Collections.Generic; using System.Drawing; @@ -24,6 +25,7 @@ namespace OpenNest.Controls contourList.SelectedIndexChanged += OnContourSelectionChanged; reverseButton.Click += OnReverseClicked; menuReverse.Click += OnReverseClicked; + applyButton.Click += OnApplyClicked; } public Program Program { get; private set; } @@ -315,5 +317,51 @@ namespace OpenNest.Controls preview.Entities.Add(new Line(left, tip) { Color = arrowColor }); preview.Entities.Add(new Line(right, tip) { Color = arrowColor }); } + + private void OnApplyClicked(object sender, EventArgs e) + { + var text = gcodeEditor.Text; + if (string.IsNullOrWhiteSpace(text)) + { + MessageBox.Show("G-code is empty.", "Apply", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + + try + { + using var stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(text)); + var reader = new ProgramReader(stream); + var parsed = reader.Read(); + + if (parsed == null || parsed.Length == 0) + { + MessageBox.Show("No valid G-code found.", "Apply", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + + // Rebuild shapes from the parsed program + var entities = ConvertProgram.ToGeometry(parsed); + var shapes = ShapeBuilder.GetShapes(entities); + + if (shapes.Count == 0) + { + MessageBox.Show("No contours found in parsed G-code.", "Apply", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + + contours = ContourInfo.Classify(shapes); + Program = parsed; + isDirty = true; + + PopulateContourList(); + RefreshPreview(); + ProgramChanged?.Invoke(this, EventArgs.Empty); + } + catch (Exception ex) + { + MessageBox.Show($"Error parsing G-code: {ex.Message}", "Apply", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } } }