From dc9b2a5aa64bbce2be14da88acf826aa96e933a2 Mon Sep 17 00:00:00 2001 From: Robert Sheldon Date: Wed, 4 Jul 2018 21:12:07 -0400 Subject: [PATCH] remove models directory and switch to generating them via script --- .gitignore | 1 + customizer.scad | 8 ++------ expand.rb | 28 ++-------------------------- keys.scad | 7 ++++++- model_base.scad | 21 +++++++++++++++++++++ models.rb | 34 ++++++++++++++++++++++++++++++++++ openscad.rb | 30 ++++++++++++++++++++++++++++++ src/key_transformations.scad | 8 ++------ 8 files changed, 98 insertions(+), 39 deletions(-) create mode 100644 model_base.scad create mode 100644 models.rb create mode 100644 openscad.rb diff --git a/.gitignore b/.gitignore index 09b61a1..93b4f6b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ *.scad.stl +models/ diff --git a/customizer.scad b/customizer.scad index bf59969..19d18ec 100644 --- a/customizer.scad +++ b/customizer.scad @@ -617,14 +617,10 @@ module bump(depth=undef) { children(); } -module one_single_key(profile, row, unsculpted) { - key_profile(profile, unsculpted ? 3 : row) key(); -} - -module one_row_profile(profile, unsculpted = false) { +module row_profile(profile, unsculpted = false) { rows = [5, 1, 2, 3, 4]; for(row = [0:len(rows)-1]) { - translate_u(0, -row) one_single_key(profile, rows[row], unsculpted); + translate_u(0, -row) key_profile(profile, unsculpted ? 3 : rows[row]); } } diff --git a/expand.rb b/expand.rb index 007bcb9..16973e6 100644 --- a/expand.rb +++ b/expand.rb @@ -1,27 +1,3 @@ +require './openscad.rb' -def expand(filename) - lines = File.readlines(filename) - old_dir = Dir.getwd - - Dir.chdir File.dirname(filename) - lines = lines.flat_map do |line| - if line =~ /(include|use)\s*<(.*)>/ - # File.readlines("./#{$2}") - expand("./#{$2}") - # in lieu of actually implementing `use`, we can just cull this final line from key.scad - elsif line =~ /example\_key\(\);/ - "" - else - line - end - end - - Dir.chdir old_dir - - lines -end - -lines = expand(ARGV[1] || 'customizer_base.scad') - -f = File.open('customizer.scad', 'w') -f.write lines.join +OpenSCAD::expand_openSCAD_file(ARGV[1] || 'customizer_base.scad', 'customizer.scad') diff --git a/keys.scad b/keys.scad index ba356c4..005e204 100644 --- a/keys.scad +++ b/keys.scad @@ -16,4 +16,9 @@ include //$has_brim=true; - key(); +$inverted_dish = true; +$key_length = 6; + dcs_row(3) { + $dish_type = "sideways cylindrical"; + key(); + } diff --git a/model_base.scad b/model_base.scad new file mode 100644 index 0000000..d64089a --- /dev/null +++ b/model_base.scad @@ -0,0 +1,21 @@ +// entry point for customizer script. This probably isn't useful to most people, +// as it's just a wrapper that helps generate customizer.scad for thingiverse. + +/* [Basic-Settings] */ + +// what preset profile do you wish to use? disable if you are going to set paramters below +key_profile = "dcs"; // [dcs, oem, dsa, sa, g20, disable] +// what key profile row is this keycap on? 0 for disable +row = 1; // [5,1,2,3,4,0] + +// What does the top of your key say? +legend = ""; + +include + +include +include +include +include + +use diff --git a/models.rb b/models.rb new file mode 100644 index 0000000..4e4ecfb --- /dev/null +++ b/models.rb @@ -0,0 +1,34 @@ +require './openscad.rb' +require 'tempfile' + +ROWS = [5,1,2,3,4].freeze +SIZES = [1].freeze # , 1.25, 1.5, 1.75, 2, 2.25, 2.75, 6.25 +PROFILES = ['dcs', 'dsa', 'g20', 'oem', 'sa'].freeze + +# can't get include statements to work dynamically so we'll just use a file +def make_file(command) + lines = OpenSCAD::expand('model_base.scad') + lines << command + f = Tempfile.new('models.scad') + f.write lines.join + f.close + f.path +end + +# length is not used in the command here to get the automatic stabilizers from the script +def make_command(row, length, profile) + "key_profile(\"#{profile}\", #{row}) { union() { #{length == 6.25 ? '$dish_type = "sideways cylindrical"; $inverted_dish=true;' : ''} key(); } }" +end + +ROWS.each do |row| + SIZES.each do |length| + PROFILES.each do |profile| + command = make_command(row, length, profile) + filename = "#{profile}_row-#{row}_length-#{length.to_s.sub('.', '_')}.stl" + puts "rendering #{filename}:" + puts "\tcommand: #{command} \n\n" + path = make_file(command) + `openscad -o ./models/#{filename} -D "\\\$key_length = #{length};" #{path}` + end + end +end diff --git a/openscad.rb b/openscad.rb new file mode 100644 index 0000000..ca2e529 --- /dev/null +++ b/openscad.rb @@ -0,0 +1,30 @@ +module OpenSCAD + def self.expand(filename) + lines = File.readlines(filename) + old_dir = Dir.getwd + + Dir.chdir File.dirname(filename) + lines = lines.flat_map do |line| + if line =~ /(include|use)\s*<(.*)>/ + # File.readlines("./#{$2}") + expand("./#{$2}") + # in lieu of actually implementing `use`, we can just cull this final line from key.scad + elsif line =~ /example\_key\(\);/ + "" + else + line + end + end + + Dir.chdir old_dir + + lines + end + + def self.expand_openSCAD_file(source, destination) + lines = OpenSCAD::expand(source) + + f = File.open(destination, 'w') + f.write lines.join + end +end diff --git a/src/key_transformations.scad b/src/key_transformations.scad index 3c9495d..13b1240 100644 --- a/src/key_transformations.scad +++ b/src/key_transformations.scad @@ -125,13 +125,9 @@ module bump(depth=undef) { children(); } -module one_single_key(profile, row, unsculpted) { - key_profile(profile, unsculpted ? 3 : row) key(); -} - -module one_row_profile(profile, unsculpted = false) { +module row_profile(profile, unsculpted = false) { rows = [5, 1, 2, 3, 4]; for(row = [0:len(rows)-1]) { - translate_u(0, -row) one_single_key(profile, rows[row], unsculpted); + translate_u(0, -row) key_profile(profile, unsculpted ? 3 : rows[row]) children(); } }