Skip to content

Commit 7f04405

Browse files
Add hole/pocket calculators, live previews, and persistent snippets
1 parent aa0bd74 commit 7f04405

35 files changed

Lines changed: 7735 additions & 3846 deletions

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.6.3 - 2026-09-23
4+
5+
- Fixed milling 3D camera jumps during automatic refresh after pasting or dropping G-code. Ordinary updates preserve the camera center and zoom; a newly inserted path is fitted only when it grows or moves substantially and would otherwise extend beyond the current view. Turning camera behavior is unchanged.
6+
- Added Hole Calculator, Pocket Calculator and Snippets dialogs ported from CNCEditor, including circular/grid hole coordinates, circular/rectangular pocket G-code, helical-entry and compensation options, persistent editable snippets, insertion at the editor caret, and the original CNCEditor toolbar icons. Calculator windows are now compact/resizable with live painter-based XY previews, use a simple Insert action, place pocket-option checkboxes in the first column, and are disabled in Lathe mode; rectangular Spiral generates a real interior clearing path plus the final contour. Snippets uses a SQLite library, imports the former text-file store once without deleting it, protects unsaved edits when selection/closing changes and persists manual Up/Down ordering.
7+
38
## 1.6.2 - 2026-09-23
49

510
- Added a machine-neutral cycle execution contract while preserving machine-specific expansion models. Milling canned cycles now return geometry, signals, modal updates and position updates as one atomic outcome built against temporary state; the existing turning expansion pipeline is connected through an adapter without rewriting G70-G76 semantics. Cycle implementations now live under canonical `turning/cycles` and `milling/cycles` packages, with historical import paths retained as aliases.

‎FAQ.md‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This document is the detailed user and developer reference for Easy G-Code Plot.
66

77
- [Getting started](#getting-started)
88
- [Interface and playback](#interface-and-playback)
9+
- [CNC editing assistants](#cnc-editing-assistants)
910
- [Lathe mode](#lathe-mode)
1011
- [Tool libraries](#tool-libraries)
1112
- [Turning Stock Removal](#turning-stock-removal)
@@ -78,6 +79,36 @@ The execution dialog covers tool discovery, source parsing, CNC execution, trace
7879

7980
Yes. **Help → FAQ** opens a normal resizable window with Minimize, Maximize and Close controls. Contents links navigate within the document; the License link opens the packaged `LICENSE.md` document.
8081

82+
## CNC editing assistants
83+
84+
### Where are Hole Calculator, Pocket Calculator and Snippets?
85+
86+
All three tools are available from **CNC Functions** and from their toolbar icons. Hole Calculator and Pocket Calculator are milling-only, so they are disabled in Lathe mode; Snippets remains available. Generated text is inserted at the current editor caret, replacing the current selection when one exists. Since the calculators already show a live preview, their final action is simply **Insert**. The source remains editable, and the toolpath is updated through the normal Auto Update or Refresh workflow.
87+
88+
### What does Hole Calculator generate?
89+
90+
The **Circular** tab distributes the requested number of XY coordinates evenly around a diameter, starting at the specified angle and moving clockwise or counterclockwise. The **Grid** tab creates an XY rectangular pattern and alternates the X direction on each row to produce a serpentine traversal.
91+
92+
Only coordinate blocks are inserted. Add the required drilling cycle, safe moves, feeds and controller-specific commands around them in the CNC program.
93+
94+
### What does Pocket Calculator generate?
95+
96+
Pocket Calculator inserts a milling fragment for either a circular or rectangular pocket. Tool and pocket dimensions, center, stepover, safe/reference Z, start/end depth, depth step, stock and feed are configurable. Optional controls select clockwise/counterclockwise cutting, spiral clearing, helical entry and a finish pass on the calculated tool-center contour. The finish pass removes the configured XY stock without emitting G41/G42. A live XY preview updates while parameters change; rectangular Spiral clears continuously from the interior and then runs the final outer contour. The calculator rejects unsafe Z ordering and paths above 20,000 estimated points.
97+
98+
The calculator uses a compact resizable multi-column layout rather than enforcing a tall fixed minimum height. The generated fragment is a starting point for the active program, not a complete safety-validated machine program. Verify tool data, compensation register, work offset, spindle/coolant state, clearances and controller compatibility before machining.
99+
100+
### Where are snippets stored?
101+
102+
Snippets are stored in a SQLite database beside the per-user application configuration. On Windows this is normally:
103+
104+
```text
105+
%LOCALAPPDATA%\easy-gcode-plot\snippets.db
106+
```
107+
108+
When upgrading from the earlier text-file implementation, existing UTF-8 files from the adjacent `snippets` directory are imported once. The original `.txt` files are retained as a backup and are no longer used for subsequent edits.
109+
110+
Use **Add** to create a fragment, edit its text and press **Save**. If the current text has unsaved changes, changing the selected snippet or closing the dialog asks whether to Save, Discard or Cancel instead of replacing the editor contents silently. **Rename**, **Delete**, **Up** and **Down** manage the list; manual Up/Down order is persisted across launches. **Insert** places the selected fragment into the CNC editor.
111+
81112
## Lathe mode
82113

83114
### Where are the Lathe controls?

‎README.md‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Easy G-Code Plot is a desktop editor, analyzer, simulator and trace exporter for
2828
- Turning G70–G76 cycles, G32/G33/G92 threading, tool-nose compensation and direct A/C/corner-R programming.
2929
- Milling canned cycles, helical arcs, `G15/G16` polar-coordinate programming, cutter-radius compensation and G10/G50/G51/G52/G54.1/G68/G69 coordinate operations.
3030
- G-code editor with highlighting, line numbers, search, replace and cleanup tools.
31+
- CNC editing assistants for circular/grid hole patterns, circular/rectangular pockets and reusable persistent snippets.
3132
- Interactive OpenGL toolpath, logical-motion playback and source-line synchronization.
3233
- Toolpath statistics and CIMCO-style UTF-8 Tool List export from the resolved trace and configured tools.
3334
- Cutter-aware turning Stock Removal, including thread profiles.
@@ -69,8 +70,20 @@ uv run --no-dev python main.py
6970
3. Configure WCS, machine home and tools when required.
7071
4. Refresh and inspect the resolved toolpath.
7172
5. Use playback, Tokens/Macro Variables and Statistics to inspect execution. The Macro Variables tab shows the actual Macro B state captured at the current logical playback step.
72-
6. Optionally import an STL reference model.
73-
7. Export the required program or trajectory representation.
73+
6. Use **CNC Functions → Hole Calculator**, **Pocket Calculator** or **Snippets** to generate and insert frequently used code at the editor caret.
74+
7. Optionally import an STL reference model.
75+
8. Export the required program or trajectory representation.
76+
77+
## CNC editing assistants
78+
79+
The **CNC Functions** menu and toolbar provide three editor tools:
80+
81+
- **Hole Calculator** inserts coordinates for holes distributed around a circle or over a serpentine rectangular grid and shows a live XY preview.
82+
- **Pocket Calculator** generates a milling fragment for circular or rectangular pockets. It supports clockwise/counterclockwise cutting, multiple Z depths, XY/Z stock, conventional or spiral clearing, helical entry and an optional finish pass along the calculated tool-center contour. Its compact resizable layout includes a live XY preview; rectangular Spiral generates interior clearing before the final contour.
83+
- Hole and Pocket calculators are milling-only assistants: their actions and Insert buttons are disabled in Lathe mode. Because calculation is already reflected by the live preview, the final action is simply **Insert**.
84+
- **Snippets** manages reusable G-code fragments. Snippets can be added, edited, renamed, reordered, deleted and inserted at the current editor caret. They are stored in the per-user SQLite database `snippets.db`; legacy UTF-8 files from the adjacent `snippets` directory are imported once and retained as a backup. Unsaved edits are protected by a Save/Discard/Cancel prompt when selection or dialog state would otherwise replace them, and manual ordering is persisted.
85+
86+
The calculators insert code into the editor; they do not execute or export it automatically. Refresh the toolpath after reviewing the generated block.
7487

7588
Unsupported or ambiguous controller behavior is reported explicitly instead of being converted into guessed geometry.
7689

‎app/gcode/export/mill.py‎

Lines changed: 160 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from dataclasses import replace
77

88
from ..kernel import ExecutionResult, TraceMotion
9-
from ..kernel.events import HOME_RETURN, PROGRAM_END, PROGRAM_START, SUBPROGRAM_END, SUBPROGRAM_START, event_blocks
9+
from ..kernel.events import HOME_RETURN, PROGRAM_START, SUBPROGRAM_START, event_blocks
1010
from .common import (
1111
_cancellable,
1212
_event_kinds,
@@ -62,6 +62,13 @@ def _append_mill_motion_chunk(
6262
) -> None:
6363
step_options = replace(options, incremental=not step.absolute)
6464
for motion in motions:
65+
if motion.source_kind == "g53":
66+
# G53 is non-modal and its axes are machine coordinates. Keep it on
67+
# the same block as the generated move, without WCS projection.
68+
scaled = _scale_mill_motion(motion, unit_scale=step.unit_scale)
69+
lines.append("G53 " + motion_line(scaled, step_options))
70+
continue
71+
6572
motion = _motion_in_active_wcs(motion, step, result)
6673
lines.append(
6774
motion_line(
@@ -71,6 +78,123 @@ def _append_mill_motion_chunk(
7178
)
7279

7380

81+
def _prepare_mill_block(
82+
lines: list[str],
83+
*,
84+
step,
85+
block,
86+
options: ExportOptions,
87+
program_start_blocks: set[int],
88+
subprogram_target_blocks: set[int],
89+
) -> str | None:
90+
raw = block.raw
91+
92+
for comment in _extract_comments(raw):
93+
lines.append(_format_comment(comment, options.comment_style))
94+
95+
clean = _without_sequence_number(_normalize_words_line(raw))
96+
clean = _strip_flow_event_words(clean, step)
97+
98+
if not clean or clean == "%":
99+
return None
100+
101+
if block.index in program_start_blocks or block.index in subprogram_target_blocks:
102+
clean = re.sub(r"\bO\d+\b", "", clean, count=1, flags=re.IGNORECASE).strip()
103+
if not clean:
104+
return None
105+
106+
if block.flow_node is not None:
107+
return None
108+
109+
event_kinds = _event_kinds(step)
110+
if HOME_RETURN in event_kinds and 53 not in _g_codes(clean):
111+
lines.append(clean)
112+
return None
113+
114+
return clean
115+
116+
117+
def _append_mill_non_motion_block(
118+
lines: list[str],
119+
clean: str,
120+
*,
121+
compensated_geometry: bool,
122+
emitted_motion_before_wcs_change: bool,
123+
) -> None:
124+
gcodes = _g_codes(clean)
125+
126+
if 10 in gcodes:
127+
# G10 X/Y/Z/P are WCS-setting operands, not path geometry.
128+
# ExecutionResult exposes only final WCS offsets. Earlier motions
129+
# cannot be projected faithfully after an in-program WCS change.
130+
if emitted_motion_before_wcs_change:
131+
raise ValueError("Cannot export a program that changes WCS offsets after motion")
132+
lines.append(clean)
133+
return
134+
135+
geometry_words = any(
136+
match.group(1).upper() in {"X", "Y", "Z", "I", "J", "K", "R"} for match in _WORD_RE.finditer(clean)
137+
)
138+
139+
if gcodes & {50, 51, 52, 68, 69}:
140+
# The trace already contains these coordinate transforms.
141+
controls = _geometry_block_controls(
142+
clean,
143+
suppress_compensation=compensated_geometry,
144+
)
145+
controls = re.sub(r"\bG(?:50|51|52|68|69)\b", "", controls).strip()
146+
elif gcodes & _MILL_GEOMETRY_G_CODES or geometry_words:
147+
controls = _geometry_block_controls(
148+
clean,
149+
suppress_compensation=compensated_geometry,
150+
geometry_g_codes=_MILL_GEOMETRY_G_CODES,
151+
)
152+
else:
153+
controls = clean
154+
155+
if controls:
156+
lines.append(controls)
157+
158+
159+
def _append_mill_motion_block(
160+
lines: list[str],
161+
clean: str,
162+
motions: tuple[TraceMotion, ...],
163+
*,
164+
step,
165+
block,
166+
options: ExportOptions,
167+
result: ExecutionResult,
168+
compensated_geometry: bool,
169+
) -> None:
170+
controls = _geometry_block_controls(
171+
clean,
172+
suppress_compensation=compensated_geometry,
173+
geometry_g_codes=_MILL_GEOMETRY_G_CODES,
174+
)
175+
controls = re.sub(r"\bG(?:50|51|52|68|69|53)\b", "", controls).strip()
176+
177+
if controls:
178+
lines.append(controls)
179+
180+
if any(motion.cycle_generated for motion in motions):
181+
source = clean or block.raw.strip()
182+
lines.append(
183+
_format_comment(
184+
f"EXPANDED MILL CYCLE: {source}",
185+
options.comment_style,
186+
)
187+
)
188+
189+
_append_mill_motion_chunk(
190+
lines,
191+
motions,
192+
step=step,
193+
options=options,
194+
result=result,
195+
)
196+
197+
74198
def export_full_mill_program(
75199
result: ExecutionResult,
76200
source_lines: list[str],
@@ -94,82 +218,68 @@ def export_full_mill_program(
94218
del source_lines
95219
options = _mill_program_options(options)
96220
steps = list(_execution_slices(result))
221+
97222
compensated_geometry = any(motion.compensation_applied for motion in result.motions)
98223
executed_unit_mode = any(_g_codes(_normalize_words_line(block.raw)) & {20, 21} for _, block, _ in steps)
99224

100225
safety = ["G17", "G40", "G49", "G80", "G90"]
101226
if not executed_unit_mode:
102227
safety.append("G21")
103228

104-
lines: list[str] = ["%", _program_number(result, options), " ".join(safety)]
105-
lines.append(_format_comment("EXPANDED MILL PROGRAM", options.comment_style))
229+
lines: list[str] = [
230+
"%",
231+
_program_number(result, options),
232+
" ".join(safety),
233+
]
234+
lines.append(
235+
_format_comment(
236+
"EXPANDED MILL PROGRAM",
237+
options.comment_style,
238+
)
239+
)
240+
106241
program_start_blocks = event_blocks(result.events, PROGRAM_START)
107242
subprogram_target_blocks = {
108243
event.target_block
109244
for event in result.events
110245
if event.kind == SUBPROGRAM_START and event.target_block is not None
111246
}
112247

113-
for step, block, motions in _cancellable(steps, cancelled):
114-
raw = block.raw
115-
comments = _extract_comments(raw)
116-
clean = _without_sequence_number(_normalize_words_line(raw))
117-
clean = _strip_flow_event_words(clean, step)
118-
event_kinds = _event_kinds(step)
248+
emitted_motion_before_wcs_change = False
119249

120-
for comment in comments:
121-
lines.append(_format_comment(comment, options.comment_style))
250+
for step, block, motions in _cancellable(steps, cancelled):
251+
if motions:
252+
emitted_motion_before_wcs_change = True
122253

123-
if not clean or clean == "%":
124-
continue
125-
if block.index in program_start_blocks or block.index in subprogram_target_blocks:
126-
clean = re.sub(r"\bO\d+\b", "", clean, count=1, flags=re.IGNORECASE).strip()
127-
if not clean:
128-
continue
129-
if block.flow_node is not None:
130-
continue
131-
if not clean and event_kinds & {SUBPROGRAM_START, SUBPROGRAM_END, PROGRAM_END}:
132-
continue
133-
if HOME_RETURN in event_kinds:
134-
if clean:
135-
lines.append(clean)
254+
clean = _prepare_mill_block(
255+
lines,
256+
step=step,
257+
block=block,
258+
options=options,
259+
program_start_blocks=program_start_blocks,
260+
subprogram_target_blocks=subprogram_target_blocks,
261+
)
262+
if clean is None:
136263
continue
137264

138265
if not motions:
139-
gcodes = _g_codes(clean)
140-
geometry_words = any(
141-
match.group(1).upper() in {"X", "Y", "Z", "I", "J", "K", "R"} for match in _WORD_RE.finditer(clean)
266+
_append_mill_non_motion_block(
267+
lines,
268+
clean,
269+
compensated_geometry=compensated_geometry,
270+
emitted_motion_before_wcs_change=emitted_motion_before_wcs_change,
142271
)
143-
if gcodes & _MILL_GEOMETRY_G_CODES or geometry_words:
144-
controls = _geometry_block_controls(
145-
clean,
146-
suppress_compensation=compensated_geometry,
147-
geometry_g_codes=_MILL_GEOMETRY_G_CODES,
148-
)
149-
else:
150-
controls = clean
151-
if controls:
152-
lines.append(controls)
153272
continue
154273

155-
controls = _geometry_block_controls(
156-
clean,
157-
suppress_compensation=compensated_geometry,
158-
geometry_g_codes=_MILL_GEOMETRY_G_CODES,
159-
)
160-
if controls:
161-
lines.append(controls)
162-
163-
if any(motion.cycle_generated for motion in motions):
164-
source = clean or block.raw.strip()
165-
lines.append(_format_comment(f"EXPANDED MILL CYCLE: {source}", options.comment_style))
166-
167-
_append_mill_motion_chunk(
274+
_append_mill_motion_block(
168275
lines,
276+
clean,
169277
motions,
170278
step=step,
279+
block=block,
171280
options=options,
172281
result=result,
282+
compensated_geometry=compensated_geometry,
173283
)
174284

175285
lines.extend([_program_end_event_code(result), "%"])

‎app/gcode/export/turn.py‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,9 @@ def _append_motion_chunk(
8484
if previous_end_mm is not None and (
8585
abs(previous_end_mm[0] - start_mm[0]) > 1e-6 or abs(previous_end_mm[1] - start_mm[1]) > 1e-6
8686
):
87-
reposition = TraceMotion(
88-
move=0,
89-
start_x=previous_end_mm[0],
90-
start_z=previous_end_mm[1],
91-
end_x=motion.start_x,
92-
end_z=motion.start_z,
93-
plane=18,
94-
)
95-
lines.append(
96-
motion_line(
97-
_scale_turn_motion(
98-
reposition,
99-
unit_scale=step.unit_scale,
100-
x_is_diameter=step.x_is_diameter,
101-
),
102-
options,
103-
)
87+
raise ValueError(
88+
"Cannot export a continuous turning program: the executed trace has a position gap "
89+
"and no verified repositioning move"
10490
)
10591
scaled = _scale_turn_motion(
10692
motion,

0 commit comments

Comments
 (0)