Jan-03-2021, 01:37 AM
Hi there!
I'm new to python, haven't done any coding in 20 years. I used to do Basic code for the Basic stamp controller.
Anyways, I have a script that a nice person made for me that will strip the comment lines out of my Gcode. The software I use to create the Gcode is LightBurn.
LightBurn software adds a ; to the beginning of every comment line. My CNC controller does not understand ; and goes nuts!!! But I would rather keep the comment lines instead of stripping it out. To do this I would need to add a( at the start of the comment line and ) at the end. So basically I'd like Python to find any line that starts with ; and then add ( ) to that whole line. I googled to try to learn this myself but couldn't come up with info geared to what I'm after.
Here is the script and some sample code. Any help would be great!
import sys
import os
if len(sys.argv) != 2:
print('Usage: python nocomment.py <file.gcode>')
sys.exit(1)
in_file = os.path.abspath(sys.argv[1])
if not os.path.isfile(in_file):
print(in_file + ' does not exist!')
sys.exit(1)
with open(in_file, 'r+') as f:
out_lines = []
for line in f.readlines():
if not line.lstrip().startswith(';'):
out_lines.append(line)
if(len(out_lines) > 0):
f.seek(0)
f.truncate()
f.writelines(out_lines)
print('comment removal complete for ' + in_file)
And the sample Gcode:
; LightBurn 0.9.20
; GRBL-M3 (1.1e or earlier) device profile, user origin
; Bounds: X103.74 Y238.22 to X190.39 Y319.63
;USER START SCRIPT
;USER START SCRIPT
G00 G17 G40 G21 G54
G90
G0 X0 Y0
G91
; Scan+Cut @ 1200 mm/min, 100% power
M9
M5
G0X16.15Y6.26
; Layer C01
M3
G1X0.39F1200S255
G1X72.62Y0.2F2800S0
G1X-0.69F1200S255
G1X-3.3F2800S0
G1X-2.8F1200S255
G1X-0.7S0
G1X-3.5S255
Thanks
Glen
I'm new to python, haven't done any coding in 20 years. I used to do Basic code for the Basic stamp controller.
Anyways, I have a script that a nice person made for me that will strip the comment lines out of my Gcode. The software I use to create the Gcode is LightBurn.
LightBurn software adds a ; to the beginning of every comment line. My CNC controller does not understand ; and goes nuts!!! But I would rather keep the comment lines instead of stripping it out. To do this I would need to add a( at the start of the comment line and ) at the end. So basically I'd like Python to find any line that starts with ; and then add ( ) to that whole line. I googled to try to learn this myself but couldn't come up with info geared to what I'm after.
Here is the script and some sample code. Any help would be great!
import sys
import os
if len(sys.argv) != 2:
print('Usage: python nocomment.py <file.gcode>')
sys.exit(1)
in_file = os.path.abspath(sys.argv[1])
if not os.path.isfile(in_file):
print(in_file + ' does not exist!')
sys.exit(1)
with open(in_file, 'r+') as f:
out_lines = []
for line in f.readlines():
if not line.lstrip().startswith(';'):
out_lines.append(line)
if(len(out_lines) > 0):
f.seek(0)
f.truncate()
f.writelines(out_lines)
print('comment removal complete for ' + in_file)
And the sample Gcode:
; LightBurn 0.9.20
; GRBL-M3 (1.1e or earlier) device profile, user origin
; Bounds: X103.74 Y238.22 to X190.39 Y319.63
;USER START SCRIPT
;USER START SCRIPT
G00 G17 G40 G21 G54
G90
G0 X0 Y0
G91
; Scan+Cut @ 1200 mm/min, 100% power
M9
M5
G0X16.15Y6.26
; Layer C01
M3
G1X0.39F1200S255
G1X72.62Y0.2F2800S0
G1X-0.69F1200S255
G1X-3.3F2800S0
G1X-2.8F1200S255
G1X-0.7S0
G1X-3.5S255
Thanks
Glen