#!/usr/bin/python from __future__ import absolute_import from __future__ import print_function import sys import os import getopt import xml.etree.ElementTree as ET import shapes as shapes_pkg from shapes import point_generator from config import * debug = True 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(): print("Usage:") print(os.path.basename(__file__) + " -i -o ") print("Example:") print(os.path.basename(__file__) + " " + example_usage) sys.exit(1) def parse_arguments(argv): try: opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) except getopt.GetoptError: print_help() for opt, arg in opts: if opt in ("-i", "--ifile"): inputfile = arg elif opt in ("-o", "--ofile"): outputfile = arg return (inputfile, outputfile) def read_input_file(svg_file_path): try: tree = ET.parse(svg_file_path) return tree.getroot() except Exception as e: print("unable to read svg file: \n" + str(e)) sys.exit(1) def generate_gcode(svg_file_root_tree, gcode_file): width = svg_file_root_tree.get('width') height = svg_file_root_tree.get('height') if width == None or height == None: viewbox = svg_file_root_tree.get('viewBox') if viewbox: if (debug): print("Using viewbox size") _, _, width, height = viewbox.split() if width == None or height == None: print("Unable to get width and height for the svg") close_on_failure() width = float(width.split("mm")[0]) height = float(height.split("mm")[0]) print("[SVG loaded] With:" + str(width) + "mm Height:" + str(height) + "mm") #generate start cmds's gcode_write(preamble) num_points = 0 for elem in svg_file_root_tree.iter(): try: _, tag_suffix = elem.tag.split('}') except ValueError: continue if tag_suffix in svg_shapes: shape_class = getattr(shapes_pkg, tag_suffix) shape_obj = shape_class(elem) d = shape_obj.d_path() m = shape_obj.transformation_matrix() if d: #gcode_file.write(shape_preamble) p = point_generator(d, m, smoothness) for x,y in p: #print(";X: " + str(x) + " Y: " + str(y)) if x > 0 and x < bed_max_x and y > 0 and y < bed_max_y: gcode_file.write("G1 X%0.01f Y%0.01f\n" % (x, y)) num_points += 1 if num_points == 1: gcode_file.write("M3 I S150 ;start laser\n") else: print("\n; Coordinates out of range:", "G1 X%0.01f Y%0.01f" % (x, y)) print("; Raw:", str(x), str(y), "\nScaled:", str(x), str(y), "\n") close_on_failure() gcode_file.write("M5 ;stop laser\n") num_points = 0 #gcode_file.write(shape_postamble) gcode_file.write(postamble) print("\nGenerated", num_points, "points") if __name__ == "__main__": sys.setrecursionlimit(20000) #needed for svg's with more indepented paths if(len(sys.argv) != (len(example_usage.split())+1)): print_help() svg_file_path, gcode_file_path = parse_arguments(sys.argv[1:]) try: with open(gcode_file_path, 'w') as gcode_file: generate_gcode(read_input_file(svg_file_path),gcode_file) except Exception as e: print("unable to create gcode file: \n" + str(e)) close_on_failure()