Skip to content

Instantly share code, notes, and snippets.

@semick-dev
Last active February 11, 2025 08:10
Show Gist options
  • Save semick-dev/1dc0b0636bab18d676d3ba931c18a674 to your computer and use it in GitHub Desktop.
Save semick-dev/1dc0b0636bab18d676d3ba931c18a674 to your computer and use it in GitHub Desktop.
Using regex to clean up gcode output
# To run this script, no installation necessary, but python must be present on system.
# install python, then simply run `python remove-nline-prefix.py` to see it work.
# Replace sample_input as necessary.
# this is reflected in regex 101: https://regex101.com/r/Z7z2sE/1
# and example regex usage in this repo at /scrapyard/notepad++_regex_replace.gif
import re
REPLACEMENT_REGEX = r"^N\d+\ "
SAMPLE_INPUT = """
!
! WARNING
! For demonstration purposes only
%
O0001 (CATIA P.O. COMMENTS)
(COMPANY : ICAM Technologies Corporation)
(MACHINE : VMC - Fanuc 30i)
(CL FILE : _gcode_test_in_I.aptsource)
(DATE & TIME : 1-Jul-2023 - 08:28:35)
(PROGRAMMER : Your Name )
(===== TOOL LIST FOR PROGRAM =====)
(TOOL # TOOL NAME )
(======================================)
(OPERATION NAME : Tool Change.1)
N1 G94 G01 X-50. Y-1300. Z-70. F5010.9
N2 T1 M6
(OPERATION NAME : Facing.1)
N3 G01 G43 X-12.7 Y15.87 Z19.52 F5013.6 S70 H1 M3
N4 Z9.53 F300.
N5 X12.7 F1000.
N6 G17 G02 X15.86 Y12.99 J-3.17
N7 G01 X-15.86
N8 X-15.87 Y10.1
N9 X15.87
N10 Y7.22
N11 X-15.87
N12 Y4.33
N13 X15.87
N14 Y1.44
N15 X-15.87
N16 Y-1.44
N17 X15.87
N18 Y-4.33
N19 X-15.87
N20 Y-7.22
N21 X15.87
N22 Y-10.1
N23 X-15.87
N24 X-15.86 Y-12.99
N25 X15.86
N26 G02 X12.7 Y-15.87 I-3.16 J.29
N27 G01 X-12.7
N28 Z19.52 F300.
N29 G5 P0
N30 M5
N31 G91 G28 Z0.
N32 G28 X0. Y0.
(Total Machining Time : 0:00:60)
N33 G90 M30
%
"""
nless_output = re.sub(REPLACEMENT_REGEX, "", SAMPLE_INPUT, flags=re.MULTILINE)
print(nless_output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment