Create keyboard-layout-editor.rb

This commit is contained in:
Bob 2022-11-30 23:33:56 -05:00
parent 19f0d2faad
commit 8b0a28bbc9
2 changed files with 70 additions and 7 deletions

View File

@ -9047,7 +9047,6 @@ module resin() {
module spacebar() {
$inverted_dish = $dish_type != "disable";
$dish_type = $dish_type != "disable" ? "sideways cylindrical" : "disable";
// $dish_type = "cylindrical";
6_25u() stabilized(mm=50) children();
}
@ -16098,7 +16097,6 @@ module sideways_cylindrical_dish(width, height, depth, inverted){
translate([0,0, chord_length * direction]){
// cylinder is rendered facing up, so we rotate it on the y axis first
rotate([0,90,0]) cylinder(h = width + 20,r=rad, center=true); // +20 for fudge factor
// %rotate([0,90,0]) cylinder(h = width + 20,r=rad, center=true); // +20 for fudge factor
}
}
module spherical_dish(width, height, depth, inverted){
@ -18161,10 +18159,8 @@ module envelope(depth_difference=0, extra_floor_depth=0) {
hull(){
translate([0,0,extra_floor_depth]) cube([key_width_at_progress(extra_floor_depth / $total_depth) * size, key_height_at_progress(extra_floor_depth / $total_depth) * size, 0.01], center = true);
%translate([0,0,extra_floor_depth]) cube([key_width_at_progress(extra_floor_depth / $total_depth) * size, key_height_at_progress(extra_floor_depth / $total_depth) * size, 0.01], center = true);
top_placement(SMALLEST_POSSIBLE + depth_difference){
cube([top_total_key_width() * size, top_total_key_height() * size, 0.01], center = true);
%cube([top_total_key_width() * size, top_total_key_height() * size, 0.01], center = true);
}
}
}
@ -18179,7 +18175,6 @@ module dished(depth_difference = 0, inverted = false) {
union() {
// envelope is needed to "fill in" the rest of the keycap. intersections with small objects are much faster than differences with large objects
envelope(depth_difference, $stem_inset);
// %envelope(depth_difference, $stem_inset);
if (inverted) top_placement(depth_difference) color($secondary_color) _dish(inverted);
}
if (!inverted) top_placement(depth_difference) color($secondary_color) _dish(inverted);
@ -18190,8 +18185,10 @@ module dished(depth_difference = 0, inverted = false) {
// just to DRY up the code
// TODO is putting special vars in function signatures legal
module _dish(inverted) {
translate([$dish_offset_x,0,0]) color($secondary_color) dish(top_total_key_width() + $dish_overdraw_width, top_total_key_height() + $dish_overdraw_height, $dish_depth, inverted);
module _dish(inverted=$inverted_dish) {
translate([$dish_offset_x,0,0]) color($secondary_color)
dish(top_total_key_width() + $dish_overdraw_width, top_total_key_height() + $dish_overdraw_height, $dish_depth, inverted);
// %dish(top_total_key_width() + $dish_overdraw_width, top_total_key_height() + $dish_overdraw_height, $dish_depth, inverted);
}
// puts its children at each keystem position provided

66
keyboard-layout-editor.rb Normal file
View File

@ -0,0 +1,66 @@
require 'json'
json = JSON.load(File.read('./keyboard-layout-editor.json'))
file = File.open("./keyboard-layout-editor.scad", "w")
file.write("include <./includes.scad>\n")
file.write("$stem_support_type=\"disable\";\n")
# puts json
# TODO add theses
name = "unnamed"
author = "unknown"
def sanitize_legend(legend)
return legend.gsub("\\", "\\\\\\").gsub('"', '\"')
end
y = 0
num_rows = json.reduce(0) { |coll, row| coll + (row.is_a?(Hash) ? 0 : 1) }
json.each_with_index do |row, index|
x = 0
w = 1
next if row.is_a? Hash
row_sculpt = [(index + 5 - num_rows), 4].min;
row.each do |potential_keycap|
if potential_keycap.is_a? Hash
y += (potential_keycap['y'] || 0)
x += (potential_keycap['x'] || 0)
w = (potential_keycap['w'] || w)
else
first_legend = sanitize_legend(potential_keycap.split("\n").first || "")
first_legend_x_position = (2 - first_legend.length**0.25).round(2)
first_legend_font_size = ((4 + w) - 1.25 * first_legend.length**0.5).round(2)
second_legend = sanitize_legend(potential_keycap.split("\n")[1] || "")
second_legend_x_position = (2 - second_legend.length**0.25).round(2)
second_legend_font_size = ((4 + w) - 1.25 * second_legend.length**0.5).round(2)
# puts([legend, x, y, w, (w - 1)/2].inspect)
key_x_position = (x + ((w - 1) / 2.0)).round(2)
stabilized_or_spacebar =
if w >= 6
"spacebar()"
elsif w >= 1.75
"stabilized()"
else
""
end
file.write("translate_u(#{key_x_position}, -#{y}) u(#{w}) dcs_row(#{row_sculpt}) legend(\"#{first_legend}\", [-#{first_legend_x_position},-1], #{first_legend_font_size}) #{stabilized_or_spacebar} legend(\"#{second_legend}\", [-#{second_legend_x_position},1], #{second_legend_font_size}) key();\n")
x += w
w = 1
end
end
y += 1
end