use input and output file instead stdin and stdout
This commit is contained in:
parent
eff60aa3ee
commit
4b3a944635
64
svg2gcode.py
64
svg2gcode.py
|
@ -1,23 +1,51 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/python
|
||||||
|
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import sys
|
import sys
|
||||||
|
import os
|
||||||
|
import getopt
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
import shapes as shapes_pkg
|
import shapes as shapes_pkg
|
||||||
from shapes import point_generator
|
from shapes import point_generator
|
||||||
from config import *
|
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 <svg file> -o <gcode file>")
|
||||||
|
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'])
|
svg_shapes = set(['rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path'])
|
||||||
|
|
||||||
tree = ET.parse(sys.stdin)
|
width = svg_file_root_tree.get('width')
|
||||||
root = tree.getroot()
|
height = svg_file_root_tree.get('height')
|
||||||
|
|
||||||
width = root.get('width')
|
|
||||||
height = root.get('height')
|
|
||||||
if width == None or height == None:
|
if width == None or height == None:
|
||||||
viewbox = root.get('viewBox')
|
viewbox = svg_file_root_tree.get('viewBox')
|
||||||
if viewbox:
|
if viewbox:
|
||||||
_, _, width, height = viewbox.split()
|
_, _, width, height = viewbox.split()
|
||||||
|
|
||||||
|
@ -38,7 +66,12 @@ def generate_gcode():
|
||||||
# Iterator to lower printhead at first point
|
# Iterator to lower printhead at first point
|
||||||
num_points = 0
|
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:
|
try:
|
||||||
_, tag_suffix = elem.tag.split('}')
|
_, tag_suffix = elem.tag.split('}')
|
||||||
|
@ -74,8 +107,17 @@ def generate_gcode():
|
||||||
print("; Generated", num_points, "points")
|
print("; Generated", num_points, "points")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("; " + str(sys.setrecursionlimit(20000)))
|
sys.setrecursionlimit(20000) #needed for svg's with more indepented paths
|
||||||
generate_gcode()
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,26 +1 @@
|
||||||
; None
|
Line01Line02
|
||||||
;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
|
|
Loading…
Reference in New Issue