---
title: Adding Clearances
---

import Image from '@theme/IdealImage';

import pivotGap from '../../static/img/openscad-tutorial/pivot-gap.png';
import clearancePivot from '../../static/img/openscad-tutorial/clearance-pivot.png';
import rotate from '../../static/img/openscad-tutorial/rotate.png';
import readScad from '../../static/img/openscad-tutorial/read-scad.png';

Since we've started thinking about how the hinge will be assembled with the pin taper, we also need to think about tolerances and clearance gaps. Because we want this hinge to be "print in place" which mean it prints pre-assembled, we need to add clearance gaps so that our part doesn't print solid!

Let start by adding a `clearance` variable and first thing we need to do is extrude our `hingeBodyHalf` slightly less than `hingeLength/2` since there needs to be some play between the two halves.

```cpp
// highlight-next-line
clearance=0.2;
module hingeBodyHalf() {
  // highlight-next-line
  linear_extrude(hingeLength/2-clearance/2){
	// ... rest of module definition
}
```

Oh no! there's now a gap between our `pin` and `hingeBodyHalf`

<Image img={pivotGap} className="mb-8 bg-contain rounded-md overflow-hidden" />

Here's the fix for that:

```cpp
// highlight-next-line
hingeHalfExtrudeLength=hingeLength/2-clearance/2;

module hingeBodyHalf() {
  linear_extrude(hingeHalfExtrudeLength){
  // ... rest of module definiton
}

module pin() {
  // highlight-next-line
  translate([0,pivotRadius,hingeHalfExtrudeLength]){
    // highlight-next-line
    cylinder(h=hingeLength/2+clearance/2, r1=pinRadius, r2=pinRadius+pinTaper);
  }
}
```
**[Live Demo](https://cadhub.xyz/dev-ide/openScad#encoded_script_v2=eJx9Uk1rhDAQvfsrcughsm7XD3qSXAqFHnooZaGHIiXVcQ21cZto6VL87x2/stGVFSSZzHuT9yZzk0sWhbFzFD9V/cIz0Wh2FzsfXMO+EOmnBK1ZNBy8iqwuWIDpQsgDPIE8YBz5HVuO3L6U3PMjKObfhohNS+CKyxS6eKQ+8jJ/+K1Vk01VrIq7cGs4O2Q4X1XWlEB6yD3qeFZVLkqgLvlzCKkRqEteA32zTHi+5ydulydEfzdcYdp48Gb2EjdGWOu0i5uq7NTpHK8phURN7zCopus2xgurPNdQ08AdN9vQNUcDYibb92bCE4MhJBUKe0GtfC+2+9pxndzZNax9YgiX/RsyrTP8aw6v2wrc9Zp2L3EaLh9q7ni9l1Mb0hMKy0DRxZBsrCHxiAqYGUKMwnO0mebxLG3xvv3I4vIPjiMA/w==)**

We've done something new here, we've defined a variable with other variables and some arithmetic with `hingeHalfExtrudeLength=hingeLength/2-clearance/2;`.
This variable is then used both in `hingeBodyHalf` and `pin`.
The benefit of doing so is if we need to update this length again we only need to update it in one place since both of the modules need the same calculated length.
Also notice that've we increased the length of the `cylinder` too so that it reaches the full `hingeLength`.

Much better

<Image img={clearancePivot} style={{backgroundSize: 'contain'}} className="pb-8" />

We still have more work to do on the pin though.
We want to re-use the pin to "subtract" its shape from the other half of the hinge, basically we can use our current pint to make a hole.
Lets make a temporary new module called `pin2`. In this new module and we're going to introduce a new function `rotate` to re-orientate the pin

```cpp
module pin2() {
  translate([0,pivotRadius,hingeHalfExtrudeLength]){
    // highlight-next-line
    rotate([0,45,0]){
      cylinder(h=hingeLength/2+clearance/2, r1=pinRadius, r2=pinRadius+pinTaper);
    }
  }
}

hingeBodyHalf();
pin2();
```
**[Live Demo](https://cadhub.xyz/dev-ide/openScad#encoded_script_v2=eJytU8FOhDAQvfcrevAAWVaguCfCxcTEgwdjNvFgiKlQlkYsa1uMG8O/O0DBwhIvSkLa6cybeW86vShEEpEYHflHrR9ozhuV7GL0QhXblzx7FUypJBoOHnmuyyQEd8nFgd0xcQA7Cjq0MNg+ldjTI5NJcEkgNqsYlVRkrLMN9JZWxc2nlk0+ZrEy+mQ7YXxAoLc6byqG+5Br4HEv64JXzHHxF8JYQ6CqqGbOkyXCC7wgdTs/xuq9oRLckwZvJi91YwhrUbuoVOenjqcpU3EBnJ7ZwNpZl2EK1kWhmHZC12y2xJ2OhogZ7cCbEU+nGIwzLqEXjuXvyXZfa9ZRnZ3D2qcT4Lx/g6dFw7+m8HdZobue0+4lTMP5Rc0Vr/dybEN2AmI5k85iSDbWkHhYhsk0hGCRH2szzuMaNfI3brLWBnO1sy/ufzmPVwTMF5MZI98fOtw/O9Kt3+efP8E=)**

<Image img={rotate} className="mb-8 bg-contain rounded-md overflow-hidden" />

This is not where we want to leave our pin, but it's a good way to introduce `rotate` as well as using multiple modifiers, i.e. we're using both `translate` and `rotate` together here.
`rotate` is similar to `translate` in that it takes an argument `[x, y, z]` but instead of moving, it rotates about each of those axes. In the above example of `[0, 45, 0]` it's as if were were to put a pin into the object along the `y` axis and then rotate 45 degrees around that pin.

## How To Read Chained Operations

Notice the order that we applied the `rotate` and `transform` we applied the `rotate` first and then the`transform`.
This might seem counter intuitive because `translate` is on top, but nesting operations should be read from the most nest outward in openscad.
Here's the correct way to read the above code:

<Image img={readScad} className="mb-8 bg-contain rounded-md overflow-hidden" />

The same thing applies to `hingeBodyHalf` that should read as follows:

```cpp
// read as
// 1) Add circle, square and hingeBaseProfile
// 2) Apply an offset of 1 (far right offset)
// 3) then on offset of -2 (middle) then an offset of 1 (far left)
// 4) Extrude the 2d shape of hingeHalfExtrudeLength
module hingeBodyHalf() {
  linear_extrude(hingeHalfExtrudeLength){
    offset(1)offset(-2)offset(1){
      translate([0,pivotRadius,0]){
        circle(pivotRadius);
      }
      square([pivotRadius,pivotRadius]);
      hingeBaseProfile();
    }
  }
  linear_extrude(hingeLength){
    offset(1)offset(-1)hingeBaseProfile();
  }
}
```

Back to our pin and `rotate`. The reason we `rotate` first is because openscad shapes always rotates around the origin point `[0,0,0]` and translating first can make things very confusing, in general you should ALWAYS `rotate` first unless you have a good reason not to.
