Files
cadhub/docs/docs/definitive-beginners/the-basics.mdx
2021-07-17 17:47:29 +10:00

119 lines
6.0 KiB
Plaintext

---
title: The Basics
---
import Image from '@theme/IdealImage';
import profile from '../../static/img/openscad-tutorial/profile.png';
import circleCube from '../../static/img/openscad-tutorial/circle-cube.png';
import translate from '../../static/img/openscad-tutorial/translate.png';
import unroundedProfile from '../../static/img/openscad-tutorial/unrounded-profile.png';
import bigRadius from '../../static/img/openscad-tutorial/big-radius.png';
## 2D Primitives
Designing parts in OpenSCAD is a matter of combining shapes and iterating until you get what you want, so with that in mind lets start from this perspective focusing on one half of the hinge, let's look at the blue half.
<Image img={profile} className="mb-8 bg-contain rounded-md overflow-hidden" />
We can see at the very least some circles and rectangles will be of use, so lets start there.
```cpp
circle(5);
square([15,3]);
```
**[Live Demo](https://cadhub.xyz/dev-ide/openscad#encoded_script_v2=eJxLzixKzknVMNW05iouLE0sStWINjTVMY7VtAYAcmwH2A==)**
`circle(5);` gives us a circle with a radius of `5` and `square` is the 2d version of `cube`.
Here we've given it dimensions of 15 and 3 for x and y respectively (or width and height if you prefer).
<Image img={circleCube} className="mb-8 bg-contain rounded-md overflow-hidden" />
## Translate
It's a start but we need to shift things around a little (I've colored these so we can tell them apart). The cross-hairs represent the origin of the environment and we can see that lines up with the circle's center and the corner of the square. Since `5` is the radius of the circle, if we want the circle to sit on top of the origin we need to shift it up by the same amount. We do that with `translate` like so:
```cpp
// highlight-next-line
translate([0,5,0]){
circle(5);
}
square([15,3]);
```
**[Live Demo](https://cadhub.xyz/dev-ide/openscad#encoded_script_v2=eJwrKUrMK85JLEnViDbQMdUxiNWs5lJQSM4sSs5J1TDVtOaq5SouLE0sAsobmuoYx2paAwCRsA7o)**
<Image img={translate} className="mb-8 bg-contain rounded-md overflow-hidden" />
`translate` is a little different to the modules we've seen so far in that instead of ending with a semicolon `;` it ends with `{ children }`. In the case above it has one child, the `circle`. `translate` shifts its children around, we're using it to shift it up by `5`. We put `5` in the second position in the square braces because we want to shift it along the Y axis. If you're ever not sure which axis you need to shift something, just try each until you find the one you're after.
We also don't want to have the undercut where the circle meets the square, we can fix that with another square so that there's a nice 90 degree transition. Also for reasons we'll get to later, we don't actually want any overlap between these two squares so it will need a shift with translate as well. Here's what we're left with:
```cpp
translate([0,5,0]){
circle(5);
}
// highlight-next-line
square([5,5]);
// highlight-next-line
translate([5,0,0]){
square([11,2]);
}
```
**[Live Demo](https://cadhub.xyz/dev-ide/openscad#encoded_script_v2=eJwrKUrMK85JLEnViDbQMdUxiNWs5lJQSM4sSs5J1TDVtOaq5SouLE0sAsqb6pjGAgVKEDqA6mE6YIoMDXWMQKpqAc2BGmg=)**
<Image img={unroundedProfile} className="mb-8 bg-contain rounded-md overflow-hidden" />
I'm sure you can see the profile of the hinge coming together, that's great, but before we move forward we should quickly reflect on what we've done so far.
## Variables
We've written 7 lines and there are 5 module calls and . . . . a bunch of numbers, we have to ask ourselves If we came back to this code in a month would we be able to tell what it's supposed to do?
We definitely could, but all of the numbers makes it difficult because it's not easy to tell why each value is that value.
Have you ever walked into a room only to forget why?
You know you came here for a reason but it completely eludes you!? Well reading old code, even only a couple weeks later is a bit like that, you know you put these numbers here for a reason . . .right!
In programming expressing intent in one of the most important principles for making code understandable and to remember what we were doing.
Isolated values like the ones in our code so far are commonly called "magic numbers" since we can't tell what they do, they might as well be magical.
Luckily this can be solved by variables. Variables are a way of giving a number a name, so that we can reference that same number over and over again.
This what it looks like in action.
```cpp
pivotRadius=5;
translate([0,pivotRadius,0]){
circle(pivotRadius);
}
square([pivotRadius,pivotRadius]);
translate([pivotRadius,0,0]){
square([11,2]);
}
```
**[Live Demo](https://cadhub.xyz/dev-ide/openscad#encoded_script_v2=eJwryCzLLwlKTMksLbY1teYqKUrMK85JLEnViDbQKUDI6RjEalZzKSgkZxYl56RqIMloWnPVchUXliYWAfUg60Bix2qimIxiLsxkmBGGhjpGIPW1AMDeNbI=)**
The first line `pivotRadius=5;` is where the variable is assigned, that is the value of `5` is assigned to the name `pivotRadius`, Then we can use it everywhere we need that value.
The beauty of this is two fold,
1) We have now given lots of context about what value of `5` actually means, that it relates to the pivot radius and
2) if we ever what to change this value it's easy to just change the first line and the rest will update, for example we can more than double the value and the model stays cohesive, here it is with `pivotRadius=11;`
<Image img={bigRadius} style={{backgroundSize: 'contain', marginBottom: '1rem'}} />
Let's clean up the rest of the magic numbers.
```cpp
pivotRadius=5;
baseThickness=3;
baseWidth=15;
translate([0,pivotRadius,0]){
circle(pivotRadius);
}
square([pivotRadius,pivotRadius]);
translate([pivotRadius,0,0]){
square([baseWidth,baseThickness]);
}
```
**[Live Demo](https://cadhub.xyz/dev-ide/openscad#encoded_script_v2=eJwryCzLLwlKTMksLbY1teZKSixODcnITM7OSy0utjWGCIRnppRk2BoCpUuKEvOKcxJLUjWiDXQKEFp1DGI1q7kUFJIzi5JzUjWQZDStuWq5igtLE4uAepB1ILFjNVFMRjEXZjLMCLh7dFCcCjKiFgD+/Uh/)**
There are some exceptions to the "magic number" rule, for example above `0` isn't a "magic number" since this conveys "no value for this axis" quite well.