Created
January 9, 2011 20:12
-
-
Save martymcguire/771967 to your computer and use it in GitHub Desktop.
Generate Post-It banners for plotting with your MakerBot Unicorn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# Usage: python sticky_notes.py 'Your Message Here!' > out.gcode | |
import cairo | |
import sys | |
import re | |
### BEGIN CONFIGURATION ### | |
feedrate = 3500 | |
start_delay = 120 | |
stop_delay = 120 | |
pen_up_angle = 50 | |
pen_down_angle = 30 | |
tool_up_height = 15 | |
size = 20 | |
font_face = "Georgia" | |
### END CONFIGURATION ### | |
preamble = [ | |
"G21 (metric ftw)", | |
"G90 (absolute mode)" | |
] | |
sheet_header = [ | |
"G92 X0 Y0 Z0 (zero all axes", | |
"M300 S%d (pen down)" % (pen_down_angle), | |
"G4 P%d (wait %dms" % (start_delay, start_delay), | |
"M300 S%d (pen up)" % (pen_up_angle), | |
"G4 P%d (wait %dms" % (stop_delay, stop_delay), | |
"M18 (disengage drives)", | |
"M01 (Was registration test successful?)", | |
"M17 (engage drives if YES, and continue)", | |
"(end of sheet header)" | |
] | |
sheet_footer = [ | |
"(Start of sheet footer.)", | |
"M300 S%d (pen up)" % (pen_up_angle), | |
"G4 P%d (wait %dms" % (stop_delay, stop_delay), | |
"G0 X0 Y0 Z%d F%d (retrieving platform)" % (tool_up_height, feedrate), | |
"G4 P300 (wait 300ms)", | |
"M01 (Have you retrieved the print?)", | |
"(machine halts until 'okay')", | |
"G4 P%d (wait %dms" % (start_delay, start_delay), | |
"G0 Z0 F%d (return to start position of current sheet)" % (feedrate), | |
"G4 P300 (wait 300ms)", | |
"M18 (disengage drives)", | |
"(End of sheet footer)", | |
] | |
postscript = ["(End of print job)", | |
"M300 S%d (pen up)" % (pen_up_angle), | |
"M18 (disengage drives)" | |
] | |
def draw_character(char, cr): | |
x_bearing, y_bearing, width, height = cr.text_extents(char)[:4] | |
xoff = width/2.0 | |
yoff = height/2.0 | |
cr.text_path(char) | |
path = cr.copy_path_flat() | |
print "M300 S%d (pen up)" % (pen_up_angle) | |
print "G4 P%d (wait %dms)" % (stop_delay, stop_delay) | |
pen_down = 0 | |
shape_start = [0,0] # first point | |
next_shape = 1 | |
for line in path: | |
if(line[0] == cairo.PATH_MOVE_TO): | |
if(next_shape == 1): | |
shape_start = line[1] | |
next_shape = 0 | |
if(pen_down == 1): | |
print "M300 S%d (pen up)" % (pen_up_angle) | |
print "G4 P%d (wait %dms)" % (stop_delay, stop_delay) | |
pen_down = 0 | |
print "G1 X%f Y%f F%f" % (line[1][0]-xoff, line[1][1]*-1.0-yoff, feedrate) | |
if(line[0] == cairo.PATH_LINE_TO): | |
if(next_shape == 1): | |
shape_start = line[1] | |
next_shape = 0 | |
if(pen_down == 0): | |
print "M300 S%d (pen down)" % (pen_down_angle) | |
print "G4 P%d (wait %dms)" % (start_delay, start_delay) | |
pen_down = 1 | |
print "G1 X%f Y%f F%f" % (line[1][0]-xoff, line[1][1]*-1.0-yoff, feedrate) | |
if(line[0] == cairo.PATH_CLOSE_PATH): | |
print "G1 X%f Y%f F%f" % (shape_start[0]-xoff, shape_start[1]*-1.0-yoff, feedrate) | |
next_shape = 1 | |
if(pen_down == 1): | |
print "M300 S%d (pen up)" % (pen_up_angle) | |
print "G4 P%d (wait %dms)" % (stop_delay, stop_delay) | |
pen_down = 0 | |
def create_context(): | |
surface = cairo.SVGSurface("foo.svg", size, size) | |
cr = cairo.Context(surface) | |
cr.scale(size, size) | |
cr.set_line_width(0.01) | |
cr.select_font_face(font_face, | |
cairo.FONT_SLANT_NORMAL, | |
cairo.FONT_WEIGHT_BOLD) | |
cr.set_font_size(64) | |
return cr | |
def sanitize_message(message): | |
return re.sub(r'[ ]','', message) | |
if __name__ == '__main__': | |
if(len(sys.argv) != 2): | |
message = "Hello" | |
else: | |
message = sys.argv[1] | |
message = sanitize_message(message) | |
print "(Sticky Notes banner for '%s')" % (message) | |
print "\n".join(preamble) # print preamble codes | |
for char in str(message): | |
print "\n".join(sheet_header) # print sheet header | |
cr = create_context() | |
draw_character(char, cr) | |
print "\n".join(sheet_footer) # print sheet header | |
print "\n".join(postscript) # print postscript codes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment