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