diff --git a/svg2gcode.py b/svg2gcode.py index fb58966..2ce6367 100755 --- a/svg2gcode.py +++ b/svg2gcode.py @@ -1,23 +1,51 @@ -#!/usr/bin/env python +#!/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 * -def generate_gcode(): +example_usage = "-i test_data/Test_H.svg -o test_data/test.gcode" + +def print_help(): + print("Usage:") + print(os.path.basename(__file__) + " -i -o ") + 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): + + svg_shapes = set(['rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path']) - tree = ET.parse(sys.stdin) - root = tree.getroot() - - width = root.get('width') - height = root.get('height') + width = svg_file_root_tree.get('width') + height = svg_file_root_tree.get('height') if width == None or height == None: - viewbox = root.get('viewBox') + viewbox = svg_file_root_tree.get('viewBox') if viewbox: _, _, width, height = viewbox.split() @@ -38,7 +66,12 @@ def generate_gcode(): # Iterator to lower printhead at first point num_points = 0 - for elem in root.iter(): + gcode_file.write("Line01") + gcode_file.write("Line02") + + sys.exit() + + for elem in svg_file_root_tree.iter(): try: _, tag_suffix = elem.tag.split('}') @@ -74,8 +107,17 @@ def generate_gcode(): print("; Generated", num_points, "points") if __name__ == "__main__": - print("; " + str(sys.setrecursionlimit(20000))) - generate_gcode() + 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:]) + print("SVG: " + svg_file_path + " Gcode: " + gcode_file_path) + 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)) + sys.exit(1) diff --git a/test_data/test.gcode b/test_data/test.gcode index a99c393..95ca621 100644 --- a/test_data/test.gcode +++ b/test_data/test.gcode @@ -1,26 +1 @@ -; None -;SVG: With:10.0 Height:10.0 -G90 ;Absolute programming -G21 ;Programming in millimeters (mm) -M5 ;Disable laser -; ------ -; Draw shapes -;X: 0.053103756 Y: 0.053103756 -G1 X0.1 Y0.1 -M3 I S150 ;start laser -;X: 0.053103756 Y: 0.053103756 -G1 X0.1 Y0.1 -;X: 9.946895956 Y: 0.053103756 -G1 X9.9 Y0.1 -;X: 9.946895956 Y: 9.946895956 -G1 X9.9 Y9.9 -;X: 0.0531037560000005 Y: 9.946895956 -G1 X0.1 Y9.9 -;X: 0.053103756 Y: 0.053103756 -G1 X0.1 Y0.1 -M5 ;stop laser -; ------ -; Shape completed -G1 X0.0 Y0.0; Display printbed -M02 ;End of program -; Generated 0 points +Line01Line02 \ No newline at end of file