Skip to content

Instantly share code, notes, and snippets.

@nus
Created November 6, 2010 03:21
Show Gist options
  • Save nus/665163 to your computer and use it in GitHub Desktop.
Save nus/665163 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: sjis -*-
import Tkinter as tk
import time
import csv
class Tegaki(tk.Frame):
'''make a stroke order'''
def __init__(self, file_name, master = None):
'''constructor method'''
self._width = 100 # width of letter
self._height = 148 # height of letter
self._coordinates = [] # all coodinates
self._log_file = open(file_name, 'w+')
tk.Frame.__init__(self, master)
self.pack()
# create white canvas
self._canvas = tk.Canvas(self, width = self._width, height = self._height,
bg = '#ffffff')
self._canvas.bind('<B1-Motion>', self._on_dragged)
self._canvas.bind('<ButtonPress-1>', self._on_click_down)
self._canvas.bind('<ButtonRelease-1>', self._on_click_up)
self._canvas.pack()
def __del__(self):
'''destructor method'''
self._log_file.close()
def _on_click_down(self, event):
'''callback function when click down'''
self._pre_x = event.x
self._pre_y = event.y
def _on_click_up(self, event):
'''callback function when click up'''
self._write_log()
# reset
self._coordinates = []
def _on_dragged(self, event):
'''callback function when mouse was dragged'''
self._canvas.create_line(self._pre_x, self._pre_y, event.x, event.y,
fill = 'black', width = 1)
self._coordinates.append(self._pre_x)
self._coordinates.append(self._pre_y)
self._pre_x = event.x
self._pre_y = event.y
def _write_log(self):
'''write a log file to file_name'''
self._log_file.write(str(self._coordinates)[1:-1] + '\n')
# log file of mouse points
LOG_FILE = 'points.log'
# robot arm up height
UP_HEIGHT = 100.0
# robot arm down heihgt
DOWN_HEIGHT = 0.0
header = '''
Program arm_shodo
TakeArm ' ロボット制御権の取得
'''
footer = '''
GIVEARM
END
'''
def inc_move(x, y ,z):
'''incremental move (x, y, z)
return move operation string'''
return ' MOVE L, @0 P8+(%.4f,%.4f,%.4f)\n' % (float(x), float(y), float(z))
def make_pac():
'''make pac source file from locus of mouse dragging'''
tegaki = csv.reader(open(LOG_FILE, 'r'))
pac_src = open('nengajo.pac', 'w+')
# make a pac source
pac_src.write(header)
for path in tegaki:
for index_x in range(0, len(path), 2):
x = path[index_x]
y = path[index_x + 1]
pac_src.write(inc_move(x, y, DOWN_HEIGHT))
pac_src.write(inc_move(x, y, UP_HEIGHT))
pac_src.write(footer)
def main():
'''main function'''
tgk = Tegaki(LOG_FILE)
tgk.pack()
tgk.mainloop()
tgk.__del__() # call destructor method for release _log_file object
make_pac()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment