Results 1 to 10 of 72
Thread: OpenScad Help Centre
Hybrid View
-
12-09-2014, 02:23 AM #1
Modules - OpenScad's answer to toolbar icons?
I was reading this, http://blog.cubehero.com/2013/12/18/...d-code-part-i/ and came across this example of a module,
module ring(height, radius, radial_width) {
difference() {
cylinder(h = height, r = radius + radial_width / 2);
translate([0, 0, -1])
cylinder(h = height + 2, r = radius - radial_width / 2);
}}
After studying it for a while, it dawned on me that the module was like an icon on the object creation toolbar of a GUI based progam like Rhino. With Rhino, if I want to create a 3D object, or perform some action on one, I click on an icon on a toolbar, or in a drop-down menu. Then the program prompts me to input the parameters necessary to achieve the particular result at the time. In OpenScad, there is no icon, but a block of text called a "module". These modules are user created and result in the creation of a specific object.
Having created a general set of steps that OpenScad must take to create an object, it only requires the user to provide the parameters specific to the object being made at the time. This is done by writing this:
ring (height, radius, radial width) in the body of the code. To keep with convention, this would actually be coded as ring(h,r,rw)
This would be how it would appear in practice:
// ring implemented with rotational extrusion
module ring(height, radius, radial_width) {
rotate_extrude()
translate([radius, 0, 0])
square([radial_width, height]);
}
// There might be lines and lines of code before the need to enter specific values //to create the ring
ring(4,10,5);
QUESTIONS:
1. Say I've just created a module to make a whatsit, and I'm going to be making whatsits of various sizes in the future. If I click on <Save As> in Openscad do I save my .scad file here
C:\Program Files\OpenSCAD\Libraries?
2. If I've saved my module as whatsit.scad, can i create a whatsit using this bit of code:
use <whatsit.scad>
whatsit (parameters);
3. Which is better practice, putting this in the code:
module ring(height, radius, radial_width)
{ difference()
{ cylinder(h = height, r = radius + radial_width / 2);
translate([0, 0, -1])
cylinder(h = height + 2, r = radius - radial_width / 2);
}}
or this:
use <ring.scad>
ring(h,r,rw);
OMELast edited by old man emu; 12-10-2014 at 06:18 AM.
-
12-09-2014, 08:02 AM #2
- Join Date
- Jun 2014
- Location
- Burnley, UK
- Posts
- 1,662
If you have some modules that are in another file and you want to call those modules then you can either "include" the file or "use" the file.
"Include" will perform calculations that are within the file whereas "use" will not do any internal calculations, it will just use the functions.
For example:
With the toroid file from the other thread I can simply
use <./new_toroid.scad>
//CentretoShape,CornerRadius
toroid (25,5);
to make the toroid
-
12-09-2014, 10:00 AM #3
Here is a piece of OpenScad code I wrote to make ridged beams. I was making a Pump Jack for my brother that just graduated from University of Texas as a Petroleum Engineer. I wanted things as big as they could be because this was going to sit on his desk. But I ran into problems with the beams warping when they were long.
This is a good example of how to use modules to insulate yourself from a problem. I already had a module that made a ridged_beam() and let me set numbers to build it. But then I defined a module to make a stress_relieved_ridged_beam() that took some input numbers and generated an 'appropriate' beam from them.
And at a higher level, it only takes a few lines of code to make the base() with all these fancy holes. If I need to do further work to reduce stress on the model, I just have to do it in the one routine stress_relieved_ridged_beam() and it will automatically get replicated everywhere that function is used. If the topic is of interest to you, you can zoom in and see the miniature cuts between each big hole to eliminate stress build up on the early layers.
PumpJack_Base.jpg
Proper and judicious use of 'module' can help make your code more maintainable and easier to reuse. (Not to mention... Easier for other people to understand!!!!)
Please explain to me how to...
Yesterday, 12:15 PM in 3D Printer Parts, Filament & Materials