Skip to content

Examples

Jon Gilbert edited this page Apr 24, 2024 · 9 revisions

A short collection of examples.

Expanding parts with partno()

partno() exists to label discrete parts within a model built hierarchically, providing textual label context to individual elements within the model, like a flyout saying "hey, this thing here is part number one, and this thing over here is part number two. Yeehaw!". Here's about what that looks like: a baseplate, part-number 1, has two donut-looking shapes, parts 1-0 and 1-1, sitting within shallow pockets.

include <openscad_annotations/annotate.scad>
diff()
    partno(1)
        cuboid([60, 20, 3], anchor=TOP, rounding=0.1) {
            up(0.51)
                xcopies(spacing=30)
                    tag("remove")
                        cyl(d=18.05, h=2, anchor=CENTER, rounding2=-0.5);
            up(2)
                xcopies(spacing=30)
                    partno($idx)
                        cyl(d=18, h=4, rounding=2)
                            attach(CENTER)
                                tag("remove")
                                    cyl(d=5, h=4.1, rounding2=-1);
        }

partno-0 partno-1

As a by-product, partno() can expand your model into a cloud of pieces, letting you examine parts that might be obscured normally, and making them easier to print. Expansion produces a model with distinct parts separated from each other, for either clarity or part printing or whatever, without significant changes to the OpenSCAD source itself. Adding a single boolean variable, EXPAND_PARTS, anywhere within the source or as a -D command-line argument will take effect within partno(), and the parts will be placed at positions elsewhere to their placement in the original model.

EXPAND_PARTS = true;
// remainder of the above script unchanged

partno-2

This gives the viewer the opportunity to visually examine all sides of the shapes within scene; for example, it's far easier to look at the underside of the donut-looking shapes to notice that they have no internal rounding in the cutaway, something that is difficult to see in their original position situated onto the baseplate.

partno-3

Expanding parts with partno_attach()

We start with a model with four simple but distinct parts: a collar or slot (made of the first tube() call); a rod that has a fastener mounted to its top; and two rods that fit into that fastener.

include <openscad_annotations/annotate.scad>

tube(id=4, od=8, h=5)
    partno_attach(CENTER, undef, 0, 2)
        cyl(d=4, h=10)
            attach(TOP, RIGHT, overlap=2)
                tube(id=4, od=8, h=8)
                    partno_attach([TOP, BOTTOM], BOTTOM, overlap=4, partno="idx")
                        cyl(d=4, h=10, anchor=BOTTOM);

On its own, the entire in-scene model looks like this:

partno_attach_1

When an EXPAND_PARTNO variable is inserted and set to true, the individual parts will be expanded outwards from each other:

// Insert this anywhere within the above partno_attach() script:
EXPAND_PARTS = true; 

partno_attach_2

Parts remain oriented as they are originally modeled and attached, but for clarity they are displaced from each other some distance. Each distinct part in the model has a leader line indicating how it fits within the rest of the model.

The leader lines are only shown when previewing the model. The primary goal of partno() expansion is to produce a model with distinct parts in a quick hierarchy, with an easy way to separate distinct parts in that model, for either clarity or part printing or whatever. The leader lines make it really easy to see how parts fit together, but they're illustrative and not a part of the model per se. So, when fully rendering the model (as with F6), the leaders aren't produced.

Animating models with partno_attach()

partno_attach() already honors OpenSCAD's $t time variable, so animating the expansion of a collection of parts needs no extra OpenSCAD code: pull up the Animation tab in OpenSCAD and set the FPS and Steps like normal, and OpenSCAD will produce frames of the distinct parts collapsing in on themselves.

partno_attach_animate_1

If you want to animate your model without part expansion (say, you've already got some animation in play showing two gears meshing with each other or some such), just set EXPAND_PARTS back to false (or, simply remove the variable declaration).