OpenSCAD libraries

What is it?

OpenSCAD allows you to create files that contain a series of modules. You can then use one of two keywords use or include to incorporate those modules into another file.

Here is an example. member Tony Buser created a scad file called pins.scad. This file contains several modules:
  • pinhole(h=10, r=4, lh=3, lt=1, t=0.3, tight=true)
    Where:
    h = shaft height
    r = shaft radius
    lh = lip height
    lt = lip thickness
    t = tolerance
    tight = set to false if you want a joint that spins easily

  • pin(h=10, r=4, lh=3, lt=1, side=false)
    Where:
    h = shaft height
    r = shaft radius
    lh = lip height
    lt = lip thickness
    side = set to true if you want it printed horizontally

  • pintack(h=10, r=4, lh=3, lt=1, bh=3, br=8.75)
    Where:
    h = shaft height
    r = shaft radius
    lh = lip height
    lt = lip thickness
    bh = base_height
    br = base_radius

  • pinpeg(h=20, r=4, lh=3, lt=1)
    Where:
    h = shaft height
    r = shaft radius
    lh = lip height
    lt = lip thickness

  • pin_vertical(h=10, r=4, lh=3, lt=1)
    Where:
    h = shaft height
    r = shaft radius
    lh = lip height
    lt = lip thickness
  • pin_horizontal(h=10, r=4, lh=3, lt=1)
    Where:
    h = shaft height
    r = shaft radius
    lh = lip height
    lt = lip thickness

  • pin_solid(h=10, r=4, lh=3, lt=1)
    Where:
    h = shaft height
    r = shaft radius
    lh = lip height
    lt = lip thickness

  • test()
Here is the complete file:
// Pin Connectors V2
// Tony Buser <tbuser@gmail.com>


module test() {
  tolerance = 0.3;
  
  translate([-12, 12, 0]) pinpeg(h=20);
  translate([12, 12, 0]) pintack(h=10);
  
  difference() {
    union() {
      translate([0, -12, 2.5]) cube(size = [59, 20, 5], center = true);
      translate([24, -12, 7.5]) cube(size = [12, 20, 15], center = true);
    }
    translate([-24, -12, 0]) pinhole(h=5, t=tolerance);
    translate([-12, -12, 0]) pinhole(h=5, t=tolerance, tight=false);
    translate([0, -12, 0]) pinhole(h=10, t=tolerance);
    translate([12, -12, 0]) pinhole(h=10, t=tolerance, tight=false);
    translate([24, -12, 15]) rotate([0, 180, 0]) pinhole(h=10, t=tolerance);
  }
}

module pinhole(h=10, r=4, lh=3, lt=1, t=0.3, tight=true) {
  // h = shaft height
  // r = shaft radius
  // lh = lip height
  // lt = lip thickness
  // t = tolerance
  // tight = set to false if you want a joint that spins easily
  
  union() {
    pin_solid(h, r+(t/2), lh, lt);
    cylinder(h=h+0.2, r=r);
    // widen the cylinder slightly
    // cylinder(h=h+0.2, r=r+(t-0.2/2));
    if (tight == false) {
      cylinder(h=h+0.2, r=r+(t/2)+0.25);
    }
    // widen the entrance hole to make insertion easier
    translate([0, 0, -0.1]) cylinder(h=lh/3, r2=r, r1=r+(t/2)+(lt/2));
  }
}

module pin(h=10, r=4, lh=3, lt=1, side=false) {
  // h = shaft height
  // r = shaft radius
  // lh = lip height
  // lt = lip thickness
  // side = set to true if you want it printed horizontally

  if (side) {
    pin_horizontal(h, r, lh, lt);
  } else {
    pin_vertical(h, r, lh, lt);
  }
}

module pintack(h=10, r=4, lh=3, lt=1, bh=3, br=8.75) {
  // bh = base_height
  // br = base_radius
  
  union() {
    cylinder(h=bh, r=br);
    translate([0, 0, bh]) pin(h, r, lh, lt);
  }
}

module pinpeg(h=20, r=4, lh=3, lt=1) {
  union() {
    translate([0, -h/4+0.05, 0]) pin(h/2+0.1, r, lh, lt, side=true);
    translate([0, h/4-0.05, 0]) rotate([0, 0, 180]) pin(h/2+0.1, r, lh, lt, side=true);
  }
}

// just call pin instead, I made this module because it was easier to do the rotation option this way
// since openscad complains of recursion if I did it all in one module
module pin_vertical(h=10, r=4, lh=3, lt=1) {
  // h = shaft height
  // r = shaft radius
  // lh = lip height
  // lt = lip thickness

  difference() {
    pin_solid(h, r, lh, lt);
    
    // center cut
    translate([-r*0.5/2, -(r*2+lt*2)/2, h/4]) cube([r*0.5, r*2+lt*2, h]);
    translate([0, 0, h/4]) cylinder(h=h+lh, r=r/2.5, $fn=20);
    // center curve
    // translate([0, 0, h/4]) rotate([90, 0, 0]) cylinder(h=r*2, r=r*0.5/2, center=true, $fn=20);
  
    // side cuts
    translate([-r*2, -lt-r*1.125, -1]) cube([r*4, lt*2, h+2]);
    translate([-r*2, -lt+r*1.125, -1]) cube([r*4, lt*2, h+2]);
  }
}

// call pin with side=true instead of this
module pin_horizontal(h=10, r=4, lh=3, lt=1) {
  // h = shaft height
  // r = shaft radius
  // lh = lip height
  // lt = lip thickness
  translate([0, h/2, r*1.125-lt]) rotate([90, 0, 0]) pin_vertical(h, r, lh, lt);
}

// this is mainly to make the pinhole module easier
module pin_solid(h=10, r=4, lh=3, lt=1) {
  union() {
    // shaft
    cylinder(h=h-lh, r=r, $fn=30);
    // lip
    // translate([0, 0, h-lh]) cylinder(h=lh*0.25, r1=r, r2=r+(lt/2), $fn=30);
    // translate([0, 0, h-lh+lh*0.25]) cylinder(h=lh*0.25, r2=r, r1=r+(lt/2), $fn=30);
    // translate([0, 0, h-lh+lh*0.50]) cylinder(h=lh*0.50, r1=r, r2=r-(lt/2), $fn=30);

    // translate([0, 0, h-lh]) cylinder(h=lh*0.50, r1=r, r2=r+(lt/2), $fn=30);
    // translate([0, 0, h-lh+lh*0.50]) cylinder(h=lh*0.50, r1=r+(lt/2), r2=r-(lt/3), $fn=30);    

    translate([0, 0, h-lh]) cylinder(h=lh*0.25, r1=r, r2=r+(lt/2), $fn=30);
    translate([0, 0, h-lh+lh*0.25]) cylinder(h=lh*0.25, r=r+(lt/2), $fn=30);    
    translate([0, 0, h-lh+lh*0.50]) cylinder(h=lh*0.50, r1=r+(lt/2), r2=r-(lt/2), $fn=30);    

    // translate([0, 0, h-lh]) cylinder(h=lh, r1=r+(lt/2), r2=1, $fn=30);
    // translate([0, 0, h-lh-lt/2]) cylinder(h=lt/2, r1=r, r2=r+(lt/2), $fn=30);
  }
}

If you just compiled this program you would get the following error:



To fix this error you need to call one of the modules:
pintack(10);


To use the pins.scad file in another file
  • Save both files in the same directory.
  • The first line in your second file should use include or use
If you want to see the compiled model in the new file, use
include <pins.scad>
include will bring the entire file into the new file.


If you just want to use a module from one file in another consider using:
use <pins.scad>
use imports modules and functions, but does not execute any commands other than those definitions.