annotate graster/graster/bin/gtile @ 11:f952052e37b7

trying a fix.
author Robert McIntyre <rlm@mit.edu>
date Tue, 24 Aug 2010 19:06:45 -0400
parents
children
rev   line source
rlm@11 1 #!/usr/bin/env ruby -rubygems
rlm@11 2 # Tokyo cache cow command line interface script.
rlm@11 3 # Run <tt>tokyo_cache_cow -h</tt> to get more usage.
rlm@11 4 require File.dirname(__FILE__) + '/../lib/graster'
rlm@11 5
rlm@11 6 unless ARGV.size == 5
rlm@11 7 puts "usage: ruby tile.rb <input-gcode-file> <tile-width> <tile-height> <horiz-count> <vert-count>"
rlm@11 8 exit(1)
rlm@11 9 end
rlm@11 10
rlm@11 11 def parse_line line
rlm@11 12 nc = {}
rlm@11 13 line.gsub(/\([^)]*\)/,'').upcase.scan(/([A-Z])\s*([0-9\.]+)?/).each {|code| nc[code[0].intern] = (code[1] && code[1].to_f) }
rlm@11 14 nc
rlm@11 15 end
rlm@11 16
rlm@11 17 def gcode ncs
rlm@11 18 ncs = [ncs] unless ncs.is_a? Array
rlm@11 19 ncs.reduce('') {|a,nc| a << (nc.map {|k,v| "#{k}#{v}" }.join(' ') + "\n") }
rlm@11 20 end
rlm@11 21
rlm@11 22 tile_width = ARGV[1].to_f
rlm@11 23 tile_height = ARGV[2].to_f
rlm@11 24 horiz_count = ARGV[3].to_i
rlm@11 25 vert_count = ARGV[4].to_i
rlm@11 26
rlm@11 27 header = []
rlm@11 28 body = []
rlm@11 29 footer = []
rlm@11 30 state = :header
rlm@11 31
rlm@11 32
rlm@11 33 File.open ARGV[0] do |io|
rlm@11 34 io.each_line do |line|
rlm@11 35 if (nc = parse_line(line)) != {}
rlm@11 36 case state
rlm@11 37 when :header
rlm@11 38 if nc[:G] == 0 || nc[:G] == 1
rlm@11 39 state = :body
rlm@11 40 body << nc
rlm@11 41 else
rlm@11 42 header << nc
rlm@11 43 end
rlm@11 44
rlm@11 45 when :body
rlm@11 46 if nc[:G] == 0 || nc[:G] == 1
rlm@11 47 body << nc
rlm@11 48 else
rlm@11 49 state = :footer
rlm@11 50 footer << nc
rlm@11 51 end
rlm@11 52
rlm@11 53 when :footer
rlm@11 54 footer << nc
rlm@11 55 end
rlm@11 56 end # case
rlm@11 57
rlm@11 58 end # io.each_line
rlm@11 59 end # File.open
rlm@11 60
rlm@11 61 print gcode(header)
rlm@11 62
rlm@11 63 vert_count.times do |yc|
rlm@11 64 horiz_count.times do |xc|
rlm@11 65 body.each do |nc|
rlm@11 66 nc = nc.dup
rlm@11 67 nc[:X] += xc*tile_width if nc[:X]
rlm@11 68 nc[:Y] += yc*tile_height if nc[:Y]
rlm@11 69 print gcode(nc)
rlm@11 70 end
rlm@11 71 end
rlm@11 72 end
rlm@11 73
rlm@11 74 print gcode(footer)