-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathveneer.rb
73 lines (60 loc) · 1.75 KB
/
veneer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env ruby -wKU
require 'rubygems'
require 'rmagick'
# Usage: ruby veneer.rb filename.gcode filename.png > new.gcode
# Only works with skeinforge gcode comment markers
# This simple proof of concept expects a 100x100 pixel png and a cylinder with 100 sides
gcode_filename = ARGV.shift || "wood_texture_cup.gcode"
img_filename = ARGV.shift || "checker_board.png"
pixels = []
img = Magick::Image::read(img_filename)[0]
img = img.quantize(256, Magick::GRAYColorspace)
img.flip!
# puts "This image is #{img.columns}x#{img.rows} pixels"
# puts img.pixel_color(0,0).intensity/257
# img.rows.times do |row|
# line = ""
# img.columns.times do |col|
# line << (img.pixel_color(col, row).intensity/257 == 255 ? 1 : 0).to_s
# end
# puts line
# end
in_perimeter = false
current_row = 0
current_column = 0
File.open(gcode_filename, "r") do |file|
file.each_line do |line|
if line =~ /\(\<edge\> outer \)/
in_perimeter = true
current_row += 1
current_column = 0
end
if line =~ /\(\<\/edge\>\)/
in_perimeter = false
current_column = 0
end
if in_perimeter
if line =~ /^G1.*X.*Y.*Z.*F.*E.*$/
current_column += 1
end
if line =~ /^G1 X(.*) Y(.*) Z(.*) F(.*) E(.*)$/
x = $1
y = $2
z = $3
f = $4
e = $5
# TODO: add shades of gray, for now it's just black and white
darkness = img.pixel_color(current_column-1, current_row-1).intensity/257 == 255 ? 0 : 1
# slow speed down on black parts
if darkness == 1
f = f.to_f/4.0
# slow down extrusion?
# e = e.to_f/4.0
end
puts "G1 X#{x} Y#{y} Z#{z} F#{f} E#{e}"
end
else
puts line
end
end
end