print gcode in file

This commit is contained in:
Hendrik Schutter 2022-06-13 15:19:01 +02:00
parent 4b3a944635
commit 834341584d
3 changed files with 56 additions and 69 deletions

View File

@ -1,45 +1,23 @@
"""Bed width in mm"""
bed_max_x = 376
"""Z position when tool is touching surface in mm""" """Bed height in mm"""
z_touching = 0.0 bed_max_y = 315
"""Z position where tool should travel in mm"""
z_travel = 0.0
"""Distance between pen and Y=0.0 in mm"""
y_offset = 0.0
"""Distance between pen and X=0.0 in mm"""
x_offset = 0.0
"""Print bed width in mm"""
bed_max_x = 300
"""Print bed height in mm"""
bed_max_y = 300
"""X/Y speed in mm/minute""" """X/Y speed in mm/minute"""
xy_speed = 700 xy_speed = 50
"""Wait time between phases in milliseconds"""
wait_time = 1000
"""G-code emitted at the start of processing the SVG file""" """G-code emitted at the start of processing the SVG file"""
preamble = "G90 ;Absolute programming\nG21 ;Programming in millimeters (mm)\nM5 ;Disable laser" preamble = "G90 ;Absolute programming\nG21 ;Programming in millimeters (mm)\nM5 ;Disable laser\n"
"""G-code emitted at the end of processing the SVG file""" """G-code emitted at the end of processing the SVG file"""
postamble = "G1 X0.0 Y0.0; Display printbed\nM02 ;End of program" postamble = "G1 X0.0 Y0.0; Display printbed\nM02 ;End of program\n"
"""G-code emitted before processing a SVG shape"""
shape_preamble = "; ------\n; Draw shapes"
"""G-code emitted after processing a SVG shape"""
shape_postamble = "; ------\n; Shape completed"
""" """
Used to control the smoothness/sharpness of the curves. Used to control the smoothness/sharpness of the curves.
Smaller the value greater the sharpness. Make sure the Smaller the value greater the sharpness. Make sure the
value is greater than 0.1 value is greater than 0.1
""" """
smoothness = 0.5 smoothness = 0.2

View File

@ -10,11 +10,27 @@ import shapes as shapes_pkg
from shapes import point_generator from shapes import point_generator
from config import * from config import *
debug = True
example_usage = "-i test_data/Test_H.svg -o test_data/test.gcode" example_usage = "-i test_data/Test_H.svg -o test_data/test.gcode"
svg_shapes = set(['rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path'])
gcode_file_path = " "
def gcode_write(gcode):
if (debug):
for cmd in gcode.split("\n"):
if len(cmd):
print("[GCODE] " + str(cmd))
gcode_file.write(gcode)
def close_on_failure():
os.remove(gcode_file_path)
sys.exit(1)
def print_help(): def print_help():
print("Usage:") print("Usage:")
print(os.path.basename(__file__) + " -i <svg file> -o <gcode file>") print(os.path.basename(__file__) + " -i <svg file> -o <gcode file>")
print("Example:")
print(os.path.basename(__file__) + " " + example_usage)
sys.exit(1) sys.exit(1)
def parse_arguments(argv): def parse_arguments(argv):
@ -37,47 +53,30 @@ def read_input_file(svg_file_path):
print("unable to read svg file: \n" + str(e)) print("unable to read svg file: \n" + str(e))
sys.exit(1) sys.exit(1)
def generate_gcode(svg_file_root_tree, gcode_file): def generate_gcode(svg_file_root_tree, gcode_file):
svg_shapes = set(['rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path'])
width = svg_file_root_tree.get('width') width = svg_file_root_tree.get('width')
height = svg_file_root_tree.get('height') height = svg_file_root_tree.get('height')
if width == None or height == None: if width == None or height == None:
viewbox = svg_file_root_tree.get('viewBox') viewbox = svg_file_root_tree.get('viewBox')
if viewbox: if viewbox:
if (debug): print("Using viewbox size")
_, _, width, height = viewbox.split() _, _, width, height = viewbox.split()
if width == None or height == None: if width == None or height == None:
print("Unable to get width and height for the svg") print("Unable to get width and height for the svg")
sys.exit(1) close_on_failure()
width = float(width.split("mm")[0]) width = float(width.split("mm")[0])
height = float(height.split("mm")[0]) height = float(height.split("mm")[0])
print(";SVG: With:" + str(width) + " Height:" + str(height)) print("[SVG loaded] With:" + str(width) + "mm Height:" + str(height) + "mm")
# Must keep the ratio of the svg, so offset will be largest between x/y
offset = max(x_offset, y_offset) #generate start cmds's
corrected_bed_max_x = bed_max_x - offset gcode_write(preamble)
corrected_bed_max_y = bed_max_y - offset
scale_x = 1 #corrected_bed_max_x / max(width, height)
scale_y = 1 #corrected_bed_max_y / max(width, height)
print(preamble)
# Iterator to lower printhead at first point
num_points = 0 num_points = 0
gcode_file.write("Line01")
gcode_file.write("Line02")
sys.exit()
for elem in svg_file_root_tree.iter(): for elem in svg_file_root_tree.iter():
try: try:
_, tag_suffix = elem.tag.split('}') _, tag_suffix = elem.tag.split('}')
except ValueError: except ValueError:
continue continue
if tag_suffix in svg_shapes: if tag_suffix in svg_shapes:
shape_class = getattr(shapes_pkg, tag_suffix) shape_class = getattr(shapes_pkg, tag_suffix)
shape_obj = shape_class(elem) shape_obj = shape_class(elem)
@ -85,39 +84,37 @@ def generate_gcode(svg_file_root_tree, gcode_file):
m = shape_obj.transformation_matrix() m = shape_obj.transformation_matrix()
if d: if d:
print(shape_preamble) #gcode_file.write(shape_preamble)
p = point_generator(d, m, smoothness) p = point_generator(d, m, smoothness)
for x,y in p: for x,y in p:
print(";X: " + str(x) + " Y: " + str(y)) #print(";X: " + str(x) + " Y: " + str(y))
if x > 0 and x < bed_max_x and y > 0 and y < bed_max_y: if x > 0 and x < bed_max_x and y > 0 and y < bed_max_y:
print("G1 X%0.01f Y%0.01f" % ((scale_x*x)+x_offset, (scale_y*y)+y_offset)) gcode_file.write("G1 X%0.01f Y%0.01f\n" % (x, y))
num_points += 1 num_points += 1
if num_points == 1: if num_points == 1:
print("M3 I S150 ;start laser") gcode_file.write("M3 I S150 ;start laser\n")
else: else:
print("\n; Coordinates out of range:", "G1 X%0.01f Y%0.01f" % ((scale_x*x)+x_offset, (scale_y*y)+y_offset)) print("\n; Coordinates out of range:", "G1 X%0.01f Y%0.01f" % (x, y))
print("; Raw:", str(x), str(y), "\nScaled:", str(scale_x*x), str(scale_y*y), "\nScale Factors:", scale_x, scale_y, "\n") print("; Raw:", str(x), str(y), "\nScaled:", str(x), str(y), "\n")
print("exit") close_on_failure()
sys.exit(-1) gcode_file.write("M5 ;stop laser\n")
print("M5 ;stop laser")
num_points = 0 num_points = 0
print(shape_postamble) #gcode_file.write(shape_postamble)
print(postamble) gcode_file.write(postamble)
print("; Generated", num_points, "points") print("\nGenerated", num_points, "points")
if __name__ == "__main__": if __name__ == "__main__":
sys.setrecursionlimit(20000) #needed for svg's with more indepented paths sys.setrecursionlimit(20000) #needed for svg's with more indepented paths
if(len(sys.argv) != (len(example_usage.split())+1)): if(len(sys.argv) != (len(example_usage.split())+1)):
print_help() print_help()
svg_file_path, gcode_file_path = parse_arguments(sys.argv[1:]) svg_file_path, gcode_file_path = parse_arguments(sys.argv[1:])
print("SVG: " + svg_file_path + " Gcode: " + gcode_file_path)
try: try:
with open(gcode_file_path, 'w') as gcode_file: with open(gcode_file_path, 'w') as gcode_file:
generate_gcode(read_input_file(svg_file_path),gcode_file) generate_gcode(read_input_file(svg_file_path),gcode_file)
except Exception as e: except Exception as e:
print("unable to create gcode file: \n" + str(e)) print("unable to create gcode file: \n" + str(e))
sys.exit(1) close_on_failure()

View File

@ -1 +1,13 @@
Line01Line02 G90 ;Absolute programming
G21 ;Programming in millimeters (mm)
M5 ;Disable laser
G1 X0.1 Y0.1
M3 I S150 ;start laser
G1 X0.1 Y0.1
G1 X9.9 Y0.1
G1 X9.9 Y9.9
G1 X0.1 Y9.9
G1 X0.1 Y0.1
M5 ;stop laser
G1 X0.0 Y0.0; Display printbed
M02 ;End of program