Thingiverse

OpenSCAD - faster coding with shortcuts library and new primitives by Parkinbot
by Thingiverse
Last crawled date: 4 years, 1 month ago
shortcuts.scad
[work in progress. Be sure to always have downloaded the latest version!]
[last update 2020.03.02]
OpenSCAD is a wonderful means to forget about time. I love programming with it and am almost adicted to do my designs in a more mathematical way.
But, also being well acquainted to some more professional IDEs like Eclipse and Visual Studio, I must admit that one day I really got tired about having to type always the same stuff like
difference()
{
rotate([0, 45, 0]) translate([0, 0, 4])
cylinder(r = 100, h = 10, center = true);
rotate([45, 0, 0]) translate([0, 0, 4])
cube([50, 10, 30], center = true);
}
Wouldn't it be nicer and more readable like this?
D()
{
Ry(45) Tz(4) Cy(r=100, h=10);
Rx(45) Tz(4) Cu(50, 10, 30);
}
My solution was to write a private shortcut library which speeded up my coding significantly, as it saves you about 70% of typing and allows for neatly arranged code.
Besides that, it is so much fun to write libraries, because one gets into contact with the more advanced language features, like module operators.
In this little project I am presenting you my 'best of' library. A collection of my daily used shortcuts and extended primitives for MUCH faster programming. The most important shortcuts are the transformations and logical operations:
To enhance
translate([x, y, z])
I defined
T(x = 0, y=0, z=0)
Tx(x=0)
Ty(y=0)
Tz(z =0)
Similar abbreviations exist for rotate() and scale().
Further I introduced skew operations
SkX(1) Cu(10);
SkY(1) Cu(10);
SkZ(1) Cu(10);
The Boolean operators
difference()
union()
intersection()
are aliased by
D()
U()
I()
The aliases for primitives are
cylinder() -> Cy()
cube() -> Cu()
circle() -> Ci()
square() -> Sq()
sphere() -> Sp()
with usable defaults, like centered = true. Using primitives not being centered by default is so time consuming.
Additionally I put some extended primitives into this library that are for daily use, like
ring() <-> Ri()
ring_half() <-> RiH()
ring_segment() <-> RiS()
cylinder_half() <-> CyH()
cylinder_segment() <-> CyS() <-> Pie()
circle_half() <-> CiH()
circle_segment() -> CiS()
sphere_half() <-> SpH()
torus() -> To()
and there are macros like
forN() *{operands}*
forX() *{operands}*
forXY() *{operands}*
...
These operator modules let you arrange multiple instances of the operands easily in a circle, along (and centered to) an axis or a matrix.
Want to know what's in the lib, which parameters and defaults are defined? Output a 'cheat sheet' with prototypes and parameters in the console
help() -> help_shortcuts()
To make the lib available for your code, copy shortcut.scad into your OpenSCAD libaries folder or your current project folder and reference it with
use
in your code.
I use this library for fast, sloppy and sketchy programming and it saves me lots and lots of time. Of course when it gets more and more into reusable programming, these shortcuts might appear to be too cryptic - not if you use them daily.
For publishing code the lib might be a drawback, because you can't expect other people to read stenografic code. But why not? At least, it forces you to rethink the code and make it even better, when you transcode it back to the commonly readable format.
Anyway, have fun with enlarging the lib.
[work in progress. Be sure to always have downloaded the latest version!]
[last update 2020.03.02]
OpenSCAD is a wonderful means to forget about time. I love programming with it and am almost adicted to do my designs in a more mathematical way.
But, also being well acquainted to some more professional IDEs like Eclipse and Visual Studio, I must admit that one day I really got tired about having to type always the same stuff like
difference()
{
rotate([0, 45, 0]) translate([0, 0, 4])
cylinder(r = 100, h = 10, center = true);
rotate([45, 0, 0]) translate([0, 0, 4])
cube([50, 10, 30], center = true);
}
Wouldn't it be nicer and more readable like this?
D()
{
Ry(45) Tz(4) Cy(r=100, h=10);
Rx(45) Tz(4) Cu(50, 10, 30);
}
My solution was to write a private shortcut library which speeded up my coding significantly, as it saves you about 70% of typing and allows for neatly arranged code.
Besides that, it is so much fun to write libraries, because one gets into contact with the more advanced language features, like module operators.
In this little project I am presenting you my 'best of' library. A collection of my daily used shortcuts and extended primitives for MUCH faster programming. The most important shortcuts are the transformations and logical operations:
To enhance
translate([x, y, z])
I defined
T(x = 0, y=0, z=0)
Tx(x=0)
Ty(y=0)
Tz(z =0)
Similar abbreviations exist for rotate() and scale().
Further I introduced skew operations
SkX(1) Cu(10);
SkY(1) Cu(10);
SkZ(1) Cu(10);
The Boolean operators
difference()
union()
intersection()
are aliased by
D()
U()
I()
The aliases for primitives are
cylinder() -> Cy()
cube() -> Cu()
circle() -> Ci()
square() -> Sq()
sphere() -> Sp()
with usable defaults, like centered = true. Using primitives not being centered by default is so time consuming.
Additionally I put some extended primitives into this library that are for daily use, like
ring() <-> Ri()
ring_half() <-> RiH()
ring_segment() <-> RiS()
cylinder_half() <-> CyH()
cylinder_segment() <-> CyS() <-> Pie()
circle_half() <-> CiH()
circle_segment() -> CiS()
sphere_half() <-> SpH()
torus() -> To()
and there are macros like
forN() *{operands}*
forX() *{operands}*
forXY() *{operands}*
...
These operator modules let you arrange multiple instances of the operands easily in a circle, along (and centered to) an axis or a matrix.
Want to know what's in the lib, which parameters and defaults are defined? Output a 'cheat sheet' with prototypes and parameters in the console
help() -> help_shortcuts()
To make the lib available for your code, copy shortcut.scad into your OpenSCAD libaries folder or your current project folder and reference it with
use
in your code.
I use this library for fast, sloppy and sketchy programming and it saves me lots and lots of time. Of course when it gets more and more into reusable programming, these shortcuts might appear to be too cryptic - not if you use them daily.
For publishing code the lib might be a drawback, because you can't expect other people to read stenografic code. But why not? At least, it forces you to rethink the code and make it even better, when you transcode it back to the commonly readable format.
Anyway, have fun with enlarging the lib.