Please list the Primitives available in OpenScad
Most of the tutorials show two primitives : cube and sphere. Are there any other Primitives?
Old Man Emu
What do the round and curly brackets mean?
Here is a random bit of code I found in a tutorial:
difference() {
union() {
// teardrop shape
sphere(r = 20);
translate([0, 0, 20 * sin(30)])
cylinder(h = 30, r1 = 20 * cos(30), r2 = 0);
// dollop
translate([0, 0, 30 + 20 * sin(30)])
sphere(r = 6);
}
//cut out slot
rotate([45, 0, 0])
translate([-20, 0, 0])
cube([40, 5, 40]);
}
What is the meaning or function of:
1. the round brackets - Difference () Union ()
2. Wiggly brackets {lines of code}
3. stuff inside square brackets [ ]
I know that // indicates a coder's comment that does not get read when the code is run.
OME
Understanding OpenScad Punctuation
I have been studying the punctuation in OpenScad and think I've got it under control. I've written up my findings and present them here for your information and critique.
Understanding OpenScad Punctuation
The OpenScad manual goes on for pages and pages giving examples of code which explains the many functions the program can do, however, when someone without a background in programming first looks at some OpenScad coding for an object, or tries to create some, they find that there is no explanation of the punctuation used to get things done. If the punctuation is not correct, nothing works.
Here are the punctuation symbols used by OpenScad, and how their function.
PUNCTUATION SYMBOL |
FUNCTION |
//
/* and */ |
// is used to insert Coder’s comments into the coding. Anything written on the same line is invisible to the program. If the comment goes into two or more lines, each line must begin with // to continue hiding its content. If the comment goes on for more than three lines, mark the first comment line with /* and end the comment with */ |
{ and } |
Braces hold things in a group. They must be used in pairs with each opening brace { matched with a closing brace } |
( and ) |
Parentheses are used to hold the parameters of an object or operator. The parameter could be the radius of a circle or sphere, or it could be the co-ordinates of a point in space. When used with Boolean operations, the parentheses are left empty. |
[ and ] |
Square Brackets are used to hold the parameters that describe width and height of squares and also the depth of cubes. When used with Transformations, they hold the x,y and z coordinates associated with the particular operator (Translate, Scale, Mirror etc.) within parentheses. |
; |
Semi-colons are the symbol that says “Do it!” A semi-colon is placed at the end of the coding that defines the object to be created. If the semi-colon is omitted, nothing is created. The automatic debugging facility in OpenScad will place a marker in the code to indicate where the bug is hiding. |
Here is a bit of code that will make the top of a chess bishop. It was copied from here: http://blog.cubehero.com/2013/11/19/...us-in-openscad
Union()
{// teardrop shape
sphere (r=20) ;
translate ([0,0,20*sin(30)])
cylinder (h = 30, r1 = 20 * cos(30), r2 = 0) ;
//dollop
translate ([0,0,30 + 20* sin (30)])
sphere (r = 6) ;
}
Here is an explanation of what each line means and what OpenScad does with it:
The Code |
The Meaning |
|
|
union() |
I am going to join some things together |
{ all the code down to the colon after (r=6)} |
This is the group of things I am going to join |
// teardrop shape |
This is the name I gave to one of the parts of the object I am going to make. |
From sphere(r = 20); to r2 = 0 |
These steps are how I made the teardrop shape |
sphere(r = 20) |
This made a sphere with a radius of 20 units centred on the Origin (0,0,0) |
; |
This ended the making of the sphere and let Openscad proceed to the next bit of the shape. |
translate([0, 0, 20 * sin(30)]) |
This told OpenScap to move the centre of the next shape I defined to a point (20 x sin (30) up the Z-axis above the Origin (0,0,0) |
cylinder(h = 30, r1 = 20 * cos(30), r2 = 0) |
This made a cone 30 units high with its base radius (20 * cos 30) and the top radius a point |
; |
This ended the making and positioning of the cylinder and let Openscad proceed to the next bit of the shape |
// dollop |
This is the name of the next part of the thing I am going to make. |
From translate to the colon after (r = 6) |
These steps are how I made the dollop |
translate([0, 0, 30 + 20 * sin(30)]) |
This told OpenScap to move the centre of the next shape I defined to a point (20 x sin (30) up the Z-axis above the point (0,0,30) |
sphere(r = 6) |
This made a sphere with a radius of 6 units, centred on the point defined in the Translate command just before it. |
; |
This ended the making of the dollop and let Openscad proceed to the next bit of the shape. |
} |
This told OpenScap that I had reached the end of the job of positioning and joining the shapes I made. |
5 Attachment(s)
Few simple tips that became quick tutorial on the HULL command :-)
Great idea emu :-)
In the tips side of the equation.
1) use diameter NOT radius. ie: cylinder(d=10,h=5);
several reasons. 1) it's much easier to visualise if you don't have to keep doubling the radius in your head.
2) makes calculations much easier when you don't have to double everything.
Example: a sphere with a radius of 6mm is actually 12mm across. So why not just use d=12 and never have to worry about how big it is.
2) the hull command.
This is a brilliant command and answers a lot of questions. Like: how do i make a wedge ? Can i make non equalatrial triangles. And 'how on earth do I make an irregular shape without having to try and under stand the polygon command' ?
Okay lets use a real world example of a model rocket.
The cylinder is easy and the pointy top is just another cylinder with different starting and finishing diameters.
$fn=100;
cylinder (d=20,h=40);
translate([0,0,40]) cylinder(d1=20, d2=1, h=20);
This gives you the basic rocket trunk - I like round things to be round and we're not going to get complicated here, so facets = 100: is set as a global variable.
Attachment 3696
Now to make fins you could make a thin cube, then make another cube the same size and rotate and translate it in a difference command to remove half the cube and leave a triangle. Or you could set a cylinder to have 3 facets and then stretch it. Both of which are unnecessarily complicated
OR
You can do this:
//making rocket fin
hull() {
translate([-1,0,0]) cube([2,25,0.1]);
translate([-1,0,40]) cube([1.5,0.1,0.1]);
} //end of hull command
So we're drawing a long thin cube that sticks out from the bottom of the rocket. Then we're drawing a small cube further up the rocket.
Pretty useless so far. But then we tell opesncad to form a shape or HULL between the two cubes and viola, we have a fin.
Attachment 3697
We can then duplicate and rotate this to make more fins:
//second fin
rotate([0,0,120]) {
hull() {
translate([-1,0,0]) cube([2,25,0.1]);
translate([-1,0,40]) cube([1.5,0.1,0.1]);
}
} //end of rotate
//third fin
rotate([0,0,240]) {
hull() {
translate([-1,0,0]) cube([2,25,0.1]);
translate([-1,0,40]) cube([1.5,0.1,0.1]);
}
} //end of rotate
we now have: a solid rocket with 3 fins:
Attachment 3698
All that's left is to remove a hole for the rocket motor.
Now because I don't do elegant, I can't just remove a cylinder from the rocket centre. Because that would leave the fins behind in the hole.
So what I'm going to do is stitch everything together with a union command (openscad's equivalent of duct tape)
And then I can remove a hole for the motor from the entire rocket:
Which gives the finished rocket:
Quote:
$fn=100;
difference() { //removes cylinder from entire ducttaped model
union() {
cylinder (d=20,h=40);
translate([0,0,40]) cylinder(d1=20, d2=1, h=20);
hull() {
translate([-1,0,0]) cube([2,25,0.1]);
translate([-1,0,40]) cube([1.5,0.1,0.1]);
}
//second fin
rotate([0,0,120]) {
hull() {
translate([-1,0,0]) cube([2,25,0.1]);
translate([-1,0,40]) cube([1.5,0.1,0.1]);
}
} //end of rotate
//third fin
rotate([0,0,240]) {
hull() {
translate([-1,0,0]) cube([2,25,0.1]);
translate([-1,0,40]) cube([1.5,0.1,0.1]);
}
} //end of rotate
} //end of union to stitch the whole model together
cylinder (d=16,h=38); //removes hole for rocket motor
} //end of difference to remove hole for motor
Attachment 3699
Now this is all well and good and gives a conventional rocket with straight fins.
But say you want to give a little slant to your fins to spin the rocket.
You could piss about with rotate and rotate the fins on another axis. Or you could simply move the top cube:
hull() {
translate([-1,0,0]) cube([2,25,0.1]);
translate([-5,0,40]) cube([1.5,0.1,0.1]);
}
Change the fin code so that the top cube is moved an extra 4mm to the right and you end up with this:
Attachment 3700
1 Attachment(s)
rocket and HULL tips part the second
Attachment 3701
Finished code:
Quote:
$fn=100;
difference() { //removes cylinder from entire ducttaped model
union() {
cylinder (d=20,h=40);
translate([0,0,40]) cylinder(d1=20, d2=1, h=20);
hull() {
translate([-1,0,0]) cube([2,25,0.1]);
translate([-5,0,40]) cube([1.5,0.1,0.1]);
}
//second fin
rotate([0,0,120]) {
hull() {
translate([-1,0,0]) cube([2,25,0.1]);
translate([-5,0,40]) cube([1.5,0.1,0.1]);
}
} //end of rotate
//third fin
rotate([0,0,240]) {
hull() {
translate([-1,0,0]) cube([2,25,0.1]);
translate([-5,0,40]) cube([1.5,0.1,0.1]);
}
} //end of rotate
} //end of union to stitch the whole model together
cylinder (d=16,h=38); //removes hole for rocket motor
} //end of difference to remove hole for motor
Now to make this more elegant we could do a number of things. Make the rocket fin a module. Add variables for rocket height, diameter, notor chamber size and fin offset.
But that's not necessary if we just want one rocket once :-)
You'll notice I add comments to the } brackets so that I have some idea what the bloody things are for.
Good habit to get into.
The hull command can be used to make pretty much any shape you like. Have a play :-)