Skip to content

Instantly share code, notes, and snippets.

@andrew-wilkes
Created April 13, 2019 07:55
Show Gist options
  • Save andrew-wilkes/4dff3185af52920c32da42fc55e33e0f to your computer and use it in GitHub Desktop.
Save andrew-wilkes/4dff3185af52920c32da42fc55e33e0f to your computer and use it in GitHub Desktop.
Straight and curved railway rail track OpenScad code
/* Track */
module track(num_sleepers = 10)
{
SPACING = 8;
RAIL_PITCH = 16.5;
RAIL_LENGTH = num_sleepers * SPACING;
RAIL_SIZE = [RAIL_LENGTH, 0.5, 0.8];
SLEEPER_SIZE = [2, RAIL_PITCH * 1.2, 0.4];
CURVE_ANGLE = 160;
straight(RAIL_LENGTH, RAIL_SIZE, SLEEPER_SIZE, num_sleepers, RAIL_PITCH, SPACING);
curved(CURVE_ANGLE, RAIL_SIZE, SLEEPER_SIZE, num_sleepers, RAIL_PITCH, SPACING);
}
module sleeper(size)
{
color("brown")
cube(size, center=true);
}
module rail(size)
{
color("silver")
cube(size, center=true);
}
module straight(length, rail_size, sleeper_size, num_sleepers, pitch, spacing)
{
for(i = [0:num_sleepers-1]){
translate([spacing / 2 + i * spacing, 0, 0])
sleeper(sleeper_size);
}
translate([length / 2, pitch / 2, (sleeper_size.z + rail_size.z) / 2])
{
rail(rail_size);
translate([0, -pitch, 0])
rail(rail_size);
}
}
module curved(radius, rail_size, sleeper_size, num_sleepers, pitch, spacing)
{
$fa = spacing / radius * 180 / PI;
angle = $fa * num_sleepers;
translate([0, -radius, 0.01])
{
translate([0, 0, (sleeper_size.z + rail_size.z) / 2])
{
curve(radius + pitch / 2, rail_size, angle);
curve(radius - pitch / 2, rail_size, angle);
}
for(i = [0:num_sleepers-1]){
rotate([0, 0, -$fa/2 - $fa * i])
translate([0, radius, 0])
sleeper(sleeper_size);
}
}
}
module curve(radius, size, angle)
{
color("silver")
difference()
{
cylinder(r = radius + size.y / 2, h = size.z, center=true);
cylinder(r = radius - size.y / 2, h = size.z + 1, center=true);
translate([(radius + size.y) / -2, 0, 0])
cube([radius + size.y, 2 * (radius + size.y), size.z + 2], center=true);
rotate([0, 0, 90 - angle])
translate([0, (radius + size.y) / -2, 0])
cube([2 * (radius + size.y), radius + size.y, size.z + 2], center=true);
}
}
track();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment