Python Forum
Help moding python script to edit Gcode
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help moding python script to edit Gcode
#1
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
Reply


Messages In This Thread
Help moding python script to edit Gcode - by AntaresSky - Jan-03-2021, 01:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,009 Jun-29-2023, 11:57 AM
Last Post: gologica
  Edit Open and Active Excel sheet in Python JoeDainton123 1 2,059 Jul-29-2020, 12:52 AM
Last Post: Larz60+
  python doc edit and run online? luckrill 0 1,552 Jul-09-2020, 01:28 PM
Last Post: luckrill
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,791 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,248 May-28-2020, 05:27 PM
Last Post: micseydel
  How do I edit saved Python 3 script? Mocap 1 2,004 Jul-17-2019, 08:41 AM
Last Post: perfringo
  Package python script which has different libraries as a single executable or script tej7gandhi 1 2,583 May-11-2019, 08:12 PM
Last Post: keames
  Need someone to help me edit this script edlentz 1 1,882 Apr-04-2019, 09:13 PM
Last Post: Larz60+
  Python code for gcode reader and representation ralmeida 1 6,190 Jul-31-2018, 09:20 AM
Last Post: DeaD_EyE
  How to run python script which has dependent python script in another folder? PrateekG 1 3,105 May-23-2018, 04:50 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020