-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqrcode2scad.rb
55 lines (44 loc) · 1.22 KB
/
qrcode2scad.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
require 'rubygems'
require 'rqrcode'
def generateQrcode(string)
return RQRCode::QRCode.new(string)
end
def extrusionArray(qr, type)
extrusion = []
33.times do |column|
33.times do |row|
if qr.dark?(column, row)
extrusion << case type
when 'jscad' then " translate([6, #{column*2+2}, #{row*2+2}], cube({size: [4, 2.01, 2.01]}))"
when 'scad' then " translate([6, #{column*2+2}, #{row*2+2}]) cube(size = [4, 2.01, 2.01]);"
end
end
end
end
return extrusion
end
def generateScadFile(extrusion)
File.open("qrcode.scad", "w") do |f|
f.puts "difference() {"
f.puts " cube(size = [8, 74, 74]);"
f.puts extrusion.join("\n")
f.puts "};"
end
end
def generateJscadFile(extrusion)
File.open("qrcode.jscad", "w") do |f|
f.puts "function main() {"
f.puts " return difference("
f.puts " cube({size: [8, 74, 74]}),"
f.puts extrusion.join(",\n")
f.puts " );"
f.puts "}"
end
end
if ARGV.length == 1
qr = generateQrcode(ARGV[0])
generateJscadFile(extrusionArray(qr, 'jscad'))
generateScadFile(extrusionArray(qr, 'scad'))
else
puts 'Please give a string to build qrcode scad. Example : ruby qrcode2scad.rb "Hello World!"'
end