print number of generated points
This commit is contained in:
parent
834341584d
commit
ff49e082e8
42
svg2gcode.py
42
svg2gcode.py
|
@ -15,7 +15,7 @@ example_usage = "-i test_data/Test_H.svg -o test_data/test.gcode"
|
||||||
svg_shapes = set(['rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path'])
|
svg_shapes = set(['rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path'])
|
||||||
gcode_file_path = " "
|
gcode_file_path = " "
|
||||||
|
|
||||||
def gcode_write(gcode):
|
def gcode_write(gcode_file, gcode):
|
||||||
if (debug):
|
if (debug):
|
||||||
for cmd in gcode.split("\n"):
|
for cmd in gcode.split("\n"):
|
||||||
if len(cmd):
|
if len(cmd):
|
||||||
|
@ -53,7 +53,8 @@ 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):
|
||||||
|
generated_points_count = 0
|
||||||
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:
|
||||||
|
@ -69,9 +70,9 @@ def generate_gcode(svg_file_root_tree, gcode_file):
|
||||||
print("[SVG loaded] With:" + str(width) + "mm Height:" + str(height) + "mm")
|
print("[SVG loaded] With:" + str(width) + "mm Height:" + str(height) + "mm")
|
||||||
|
|
||||||
#generate start cmds's
|
#generate start cmds's
|
||||||
gcode_write(preamble)
|
gcode_write(gcode_file, preamble)
|
||||||
|
|
||||||
num_points = 0
|
|
||||||
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('}')
|
||||||
|
@ -80,29 +81,30 @@ def generate_gcode(svg_file_root_tree, gcode_file):
|
||||||
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)
|
||||||
d = shape_obj.d_path()
|
obj_path = shape_obj.d_path()
|
||||||
m = shape_obj.transformation_matrix()
|
obj_trans_matrix = shape_obj.transformation_matrix()
|
||||||
|
|
||||||
if d:
|
if obj_path:
|
||||||
#gcode_file.write(shape_preamble)
|
gcode_write(gcode_file, "; == Start of " + tag_suffix + " ==\n")
|
||||||
p = point_generator(d, m, smoothness)
|
points = point_generator(obj_path, obj_trans_matrix, smoothness)
|
||||||
for x,y in p:
|
num_points = 0
|
||||||
#print(";X: " + str(x) + " Y: " + str(y))
|
for x,y in points:
|
||||||
|
if (debug): print("[Point] 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:
|
||||||
gcode_file.write("G1 X%0.01f Y%0.01f\n" % (x, y))
|
gcode_write(gcode_file, "G1 X%0.001f Y%0.001f\n" % (x, y))
|
||||||
num_points += 1
|
num_points += 1
|
||||||
if num_points == 1:
|
if (num_points == 1):
|
||||||
gcode_file.write("M3 I S150 ;start laser\n")
|
gcode_write(gcode_file, "M3 I S150 ;start laser\n")
|
||||||
else:
|
elif (debug):
|
||||||
print("\n; Coordinates out of range:", "G1 X%0.01f Y%0.01f" % (x, y))
|
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")
|
print("; Raw:", str(x), str(y), "\nScaled:", str(x), str(y), "\n")
|
||||||
close_on_failure()
|
close_on_failure()
|
||||||
gcode_file.write("M5 ;stop laser\n")
|
gcode_write(gcode_file, "M5 ;stop laser\n")
|
||||||
num_points = 0
|
gcode_write(gcode_file, "; == End of " + tag_suffix + " ==\n")
|
||||||
#gcode_file.write(shape_postamble)
|
generated_points_count += num_points
|
||||||
|
|
||||||
gcode_file.write(postamble)
|
gcode_write(gcode_file, postamble)
|
||||||
print("\nGenerated", num_points, "points")
|
print("\nGenerated", generated_points_count, "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
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
G90 ;Absolute programming
|
G90 ;Absolute programming
|
||||||
G21 ;Programming in millimeters (mm)
|
G21 ;Programming in millimeters (mm)
|
||||||
M5 ;Disable laser
|
M5 ;Disable laser
|
||||||
|
; == Start of rect ==
|
||||||
G1 X0.1 Y0.1
|
G1 X0.1 Y0.1
|
||||||
M3 I S150 ;start laser
|
M3 I S150 ;start laser
|
||||||
G1 X0.1 Y0.1
|
G1 X0.1 Y0.1
|
||||||
|
@ -9,5 +10,6 @@ G1 X9.9 Y9.9
|
||||||
G1 X0.1 Y9.9
|
G1 X0.1 Y9.9
|
||||||
G1 X0.1 Y0.1
|
G1 X0.1 Y0.1
|
||||||
M5 ;stop laser
|
M5 ;stop laser
|
||||||
|
; == End of rect ==
|
||||||
G1 X0.0 Y0.0; Display printbed
|
G1 X0.0 Y0.0; Display printbed
|
||||||
M02 ;End of program
|
M02 ;End of program
|
||||||
|
|
Loading…
Reference in New Issue