debugged conversion to python3

now accepts file through python not stdin
now returns a list of commands instead of printing them
still not modified for grbl
This commit is contained in:
arcadeperfect committed 2019-12-31 15:07:42 -05:00
1 parent ac65d7cd44
commit 3d669a68ec
1 file changed
+71 -14
+71 -14
View File
@@ -6,56 +6,113 @@ import shapes as shapes_pkg
from shapes import point_generator
from config import *
def generate_gcode():
path = "./svg/example.svg"
path = "./svg/big_example.svg"
tree = ET.parse(path)
debug = False
def generate_gcode(tree):
svg_shapes = set(['rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path'])
tree = ET.parse(sys.stdin)
commands = []
# tree = ET.parse(sys.stdin)
root = tree.getroot()
# print("\n printing root.iter")
# for i in root.iter():
# print(i.tag)
#
# print("\n printing root with tag and attrib")
# for i in root:
# print(i.tag, i.attrib)
width = root.get('width')
height = root.get('height')
if width == None or height == None:
viewbox = root.get('viewBox')
if viewbox:
_, _, width, height = viewbox.split()
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)
print("\n width / height")
print(width, height)
width = float(width)
height = float(height)
scale_x = bed_max_x / max(width, height)
scale_y = bed_max_y / max(width, height)
print preamble
print("\npreamble")
print(preamble)
commands.append(preamble)
print("\n begin main loop")
for elem in root.iter():
if debug:
print("\nelem")
print(elem)
try:
_, tag_suffix = elem.tag.split('}')
except ValueError:
continue
if tag_suffix in svg_shapes:
if debug:
print("\ntag_suffix")
print(tag_suffix)
shape_class = getattr(shapes_pkg, tag_suffix)
shape_obj = shape_class(elem)
d = shape_obj.d_path()
m = shape_obj.transformation_matrix()
if d:
print shape_preamble
if debug:
print("\nshape preamble")
print(shape_preamble)
commands.append(shape_preamble)
p = point_generator(d, m, smoothness)
# print("\npoint_generator\n", p)
# for i in p:
# print(i)
for x,y in p:
if x > 0 and x < bed_max_x and y > 0 and y < bed_max_y:
print "G1 X%0.1f Y%0.1f" % (scale_x*x, scale_y*y)
print shape_postamble
#if x > 0 and x < bed_max_x and y > 0 and y < bed_max_y:
if debug:
print("G1 X%0.1f Y%0.1f" % (scale_x*x, scale_y*y))
commands.append("G1 X%0.1f Y%0.1f" % (scale_x*x, scale_y*y))
print postamble
# else:
# print("out of bed")
if debug:
print(shape_postamble)
commands.append(shape_postamble)
print(postamble)
commands.append(postamble)
return commands
if __name__ == "__main__":
generate_gcode()
c = generate_gcode(tree)
for i in c:
print(i)