Skip to content

Instantly share code, notes, and snippets.

@dreilly369
Created June 26, 2020 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreilly369/1347d4743c58585ccfdc67e11d00512e to your computer and use it in GitHub Desktop.
Save dreilly369/1347d4743c58585ccfdc67e11d00512e to your computer and use it in GitHub Desktop.
post processor for FlatCAM NC files to make them work better for laser etching circuit boards.
# -*- coding: utf-8 -*-
from optparse import OptionParser
from os import path
"""
Quick and dirty script to edit FlatCAM NC files to make them work better for
laser etching circuit boards.
"""
new_lines = []
move_up = "G00 Z"
move_down = "G01 Z-"
parser = OptionParser()
parser.add_option("-i", "--in-file", dest="inf", default=None,
help="Input GCode File", metavar="FILE")
parser.add_option("-o", "--out-prefix", dest="outf",
help="Output File prefix", default="post_")
parser.add_option("-v", "--verts", dest="verts",
help="Leave vert travel commands", action="store_true",
default=False)
parser.add_option("-c", "--cut-power", dest="pwr", default=255, type="int",
help="Power to turn the laser back on after moves")
parser.add_option("-s", "--travel-speed", dest="speed", default=200.0, type="float",
help="speed for X,Y movement")
(opts, args) = parser.parse_args()
if opts.inf is None:
print("need input Gcode file (-i)")
exit()
with open(opts.inf) as f:
raw = f.read().strip()
lines = raw.split("\n")
for l in lines :
if l.startswith("M03 S"):
new_lines.append("M03 S1000") # max spindle speed
elif l.startswith("F"): # Bad speed setting command, patch
new_lines.append("G01 F%.1f" % opts.speed)
# Check for move down first (denoted by - z value)
elif l.startswith(move_down):
if opts.verts:
new_lines.append(l)
new_lines.append("M4 S %d" % opts.pwr)
elif l.startswith(move_up):
new_lines.append("M5")
if opts.verts:
new_lines.append(l)
else:
new_lines.append(l)
# End of program, shut laser and return to home
new_lines.append("M5")
new_lines.append("G00 X0 Y0")
out = "\n".join(new_lines)
new_name = "%s%s" % (opts.outf, path.basename(opts.inf))
with open(new_name, "w") as of:
of.write(out)
print("%d lines written" % len(new_lines))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment