## OpenSCAD

OpenSCAD is a rather simple to use, open-source, CAD-software which is more than enough to create the custom PCB shapes we may need. It is available for **Windows**, **OSX** and **Linux**: www.openscad.org. This introduction to OpenSCAD will focus on creating 2D-shapes for use as custom PCB shapes.

### OpenSCAD syntax

Since we are creating shapes for the outline of the PCB, we only care about the 2-dimensional objects available in OpenSCAD which are projected onto the xy-plane when rendered. It is, however, possible to model 3-dimensional objects using OpenSCAD.

The syntax itself is very simple and we’ll go through some basic functions that may be of use when creating the shape of your PCB. *Note that OpenSCAD is unitless and it is ultimately the “receiving” application (or manufacturer) that may determine which unit is used for measurements.*

#### The `$fn`-variable

The `$fn`-variable is a special variable, which can be used together with shape-functions to create high-resolution shapes. This variable determines the amount of points which create the shape. See the image below for some references.

Now this could be valuable not only for creating high-resolution circles, but also if we want to create a hexagon, octagon and so on.

#### 2D-Shapes

##### square

This function allows us to create a 2-dimensional **square** or **rectangle**. It has two arguments:

Argument | Type | Description |
---|---|---|

size | decimal or [decimal, decimal] |
Sides of square/rectangle. The name of this argument may be omitted. |

center | boolean |
If square is to be centered at (0,0) or set lower “left” corner at (0,0). Default is FALSE. |

1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Square with side 10 square(10); // Square with side 10, centered at (0,0) square(10, center = true); // Rectangle with sides 10 and 5 (x and y) square([10, 5]); // Rectangle with sides 10 and 5 (x and y), centered at (0,0) square([10, 5], center = true); |

##### circle

This function allows us to create a 2-dimensional **circle**. To create an oval shape, we can use the circle in combination with the `resize` function.

Argument | Type | Description |
---|---|---|

r | decimal |
Radius of circle. The name of this argument may be omitted. |

d | decimal |
Diameter of the circle. Used instead of r. |

1 2 3 4 5 6 7 |
// Circle with radius 10 circle(10); // circle with diameter 10 circle(d = 10); |

##### polygon

This function allows us to create a 2-dimensional **polygon** of complex shape, which may come in handy if the PCB is to be a complex shape.

Argument | Type | Description |
---|---|---|

points | [[x1,y1],…,[xn,yn]] |
The list of points in the polygon, a vector of 2-value vectors. |

path | [integer] or [[integer], [integer]] |
Either a single vector enumerating the points in traverse order or a vector of vectors containing enumerated points to define multiple paths of the polygon. This is optional and if omitted, the points will be used in order as path. |

convexity | integer |
Number of expected path crossings (see the OpenSCAD user manual). |

1 2 3 4 5 6 7 |
// A triangle polygon([[0,0],[1,1],[2,0]]); // A triangle with cut-out center polygon(points = [[0,0],[100,100],[200,0],[30,10],[100,80],[170,10]], paths = [[0,1,2],[3,4,5]]); |

#### Combining- and transforming shapes

##### translate

This function allows us to move an object wherever we want in x-y-z space.

Argument | Type | Description |
---|---|---|

v | [x,y,z] |
Vector of decimals for x,y and z-position to translate (move) to. |

1 2 3 4 5 6 7 |
// A square placed at position (10,10) // translate([x,y,z]){} translate([10,10,0]){ square(10); } |

##### rotate

This function allows us to rotate an object. It can be used in fairly complex ways, but we’ll only cover its simplest use, rotation in 2d-space.

Argument | Type | Description |
---|---|---|

a | integer or [x,y,z] |
Degrees to rotate. Either single value to rotate around z-axis OR as a vector with degrees per axis. |

v | [x,y,z] |
An arbitrary vector around which the object may be rotated. Ignored if a is set as vector. |

1 2 3 4 5 6 7 8 9 10 11 |
// A square rotated 45 degrees around origo. rotate(45){ square(100); } // A square rotated 45 degrees around the y-axis. rotate(a = 45, v = [0,1,0]){ square(100); } |

##### resize

This function allows us to resize an object. It can be used to, for example, create a oval shape from a circle.

Argument | Type | Description |
---|---|---|

newsize | [x,y,z] |
New size in x, y and z-axis. |

1 2 3 4 5 6 |
// An oval shape of size (200, 100) resize(newsize = [200,100,0]){ circle(d=100); } |

##### mirror

Mirrors the child node on a plane through origo. Mirror takes one unnamed argument, which is the normal to the reflection-plane.

Argument | Type | Description |
---|---|---|

N/A | [x,y,z] |
Normal of the plane in which the child node is to be mirrored. |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// Set high resolution $fn=50; // Module to simplify printing mickey-mouse object module mickey() { translate ([12,0,0]) { rotate(-40) { union(){ circle(r=10); translate([-10,10,0]) { circle(r=7); } translate([10,10,0]) { circle(r=7); } } } } } // Print non-mirrored mickey mickey(); // Mirror mickey in y/z-plane mirror( [1, 0, 0] ) { mickey(); } |

##### minkowski

The minkowski sum allows us to, in an easy way, to add (for example) rounded corners to a cube. The function takes no arguments. Note that if minkowski is used as in the example, the overall width of the object is the width of the square **plus** the length of the radius of the circle times two (square_width + 2 * circle_radius);

1 2 3 4 5 6 7 |
// A square with rounded corners minkowski(){ square(50); circle(r=5, $fn=50); } |

##### hull

Displays the convex hull of the objects (child nodes)

1 2 3 4 5 6 |
hull() { translate([15,10,0]) circle(10); circle(10); } |

##### union

Combines the child nodes into a single object.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Mickey mouse shape $fn=50; union(){ circle(r=10); translate([-10,10,0]) { circle(r=7); } translate([10,10,0]) { circle(r=7); } } |

##### difference

Cuts away intersecting parts of the child nodes from the first child node.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Mickey mouse cut out of a square $fn=50; difference(){ square(50, center = true); circle(r=10); translate([-10,10,0]) { circle(r=7); } translate([10,10,0]) { circle(r=7); } } |

##### intersection

Intersection keeps the overlapping parts of the child nodes.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Intersection (overlapping) of three circles. $fn=50; intersection(){ translate([-5,-2.5,0]) { circle(r=10); } translate([5,-2.5,0]) { circle(r=10); } translate([0,5,0]) { circle(r=10); } } |

Now to render the PCB-shape in OpenSCAD. This can be done by clicking * Design > Render* in the application menu,

*in the icon-toolbar below the render-window or simply by pressing*

**Render**`F6`.

### Further reading

For further reading on OpenSCAD, see the OpenSCAD User Manual.

## From OpenSCAD to other programs

The first step is to make sure the shape is rendered correctly, otherwise the export to a more widely adopted format may not be correct. OpenSCAD allows for exporting the shape to multiple different formats which then can be imported by other programs. These formats are `STL`, `OFF`, `AMF`, `DXF`, `SVG`, `CSG`. Which one is right for you depends on the situation, but if you are in need of a format that is easy to parse — the DXF-format (Design eXchange Format) is a good choice.