BOSL2 Library

repository·master·Indexed 25 days ago

https://github.com/belfryscad/bosl2

A comprehensive OpenSCAD library providing advanced geometric primitives, transformation shorthands, attachment systems, and mathematical utilities. Key features include the align() and attach() modules for positioning components, advanced rounding and filleting, support for Beziers and NURBS, and a functional parts library containing gears, threading, and joints. It includes a testing framework using .scadtest files and the openscad-test Python framework.

Tokens
33.1K
Snippets
100
Records
164
Agent score
81%

What's inside BOSL2

  1. Overview of BOSL2 capabilities

    master

    BOSL2 is an extensive OpenSCAD library designed to simplify complex modeling. Key features include:

    • Attachments: Position components relative to others (e.g., placing an object on the TOP of another, aligned RIGHT) without manual coordinate tracking.
    • Rounding and Filleting: Advanced modules like cuboid() for rounded cubes, offset_sweep() for rounded extrusions, and prism_connector() for filleted connections.
    • Shorthands: Readable alternatives to standard OpenSCAD commands (e.g., up(z) instead of translate([0,0,z])).
    • Complex Objects: Support for path_sweep(), skin(), Beziers, NURBS, and metaballs() for organic surfaces.
    • Building Blocks: Extended primitives like prisms, tubes, and shapes with inner radius support for guaranteed hole sizes.
    • Texturing: Ability to apply repeating patterns or images (embossing) to surfaces.
    • Parts Library: Functional components like gears, threading (NPT, bottle caps, screws), clips, hinges, and dovetail joints.
    • Geometric Operations: Operations on 2D paths (regions) and 3D VNFs (Vertices 'N' Faces).
    • Programming Aids: Math utilities (linear algebra, root finding), geometric intersections, coordinate transformations, and string/list processing.
  2. What is a VNF and how to use vnf_polyhedron()

    master

    A VNF (Vertices 'N' Faces) is a two-item list used to represent a polyhedron. The first item is a list of vertex coordinates, and the second item is a list of faces (where each face is a list of indices into the vertex list). Using a VNF is more efficient than passing vertices and faces separately to functions.

    To render a VNF, use the vnf_polyhedron() function.

    include <BOSL2/std.scad>
    
    // Define a VNF as a two-item list: [vertices, faces]
    vnf = [
        [
            [-1,-1,-1], [1,-1,-1], [1,1,-1], [-1,1,-1],
            [-1,-1, 1], [1,-1, 1], [1,1, 1], [-1,1, 1],
        ],
        [
            [0,1,2], [0,2,3],  //BOTTOM
            [0,4,5], [0,5,1],  //FRONT
            [1,5,6], [1,6,2],  //RIGHT
            [2,6,7], [2,7,3],  //BACK
            [3,7,4], [3,4,0],  //LEFT
            [6,4,7], [6,5,4]   //TOP
        ]
    ];
    
    // Render the VNF
    vnf_polyhedron(vnf);
  3. Rotate 2D shapes with the spin argument

    master

    You can rotate square() and circle() in place using the spin= argument. Pass a number of degrees to rotate the shape clockwise.

    Note: Anchoring or centering is performed before the spin. This allows you to spin a shape around its anchor point.

    include <BOSL2/std.scad>
    // Spin a square 30 degrees around its center
    square([60,40], anchor=CENTER, spin=30);
    
    // Spin a circle around its left anchor
    circle(d=50, $fn=6, anchor=LEFT, spin=15);
  4. Choose a cube-like primitive for rounding

    master

    BOSL2 provides four primary 3D shape primitives for creating cube-like objects with different rounding capabilities:

    • cuboid(): The standard choice for creating a cube with built-in support for chamfering and roundovers on various edges.
    • cube(): An extended version of the standard OpenSCAD cube() that includes anchors for attaching children.
    • prismoid(): Creates a rectangular prismoid (tapered shape) with built-in support for rounding/chamfering vertical edges.
    • rounded_prism(): Connects two polygons with the same vertex count and supports continuous curvature rounding.
  5. Anchor 2D shapes using vectors and constants

    master

    BOSL2 enhances OpenSCAD's square() and circle() by replacing the center= argument with a more flexible anchor= argument. The anchor= argument takes a vector that points towards the part of the shape you want to align to the origin.

    To use standard directions, you can use vector constants. Note that while these are 3D vectors, they work for 2D anchoring (except UP/DOWN).

    Standard Vector Constants:

    • LEFT: [-1, 0, 0]
    • RIGHT: [1, 0, 0]
    • FRONT, FORWARD, or FWD: [0, -1, 0]
    • BACK: [0, 1, 0]
    • CENTER or CTR: [0, 0, 0]

    You can combine vectors to target corners, such as anchor=FRONT+RIGHT.

    To visualize available anchor points on a shape, use the show_anchors() module as a child of that shape.

    include <BOSL2/std.scad>
    // Align the back edge center to the origin
    square([60,40], anchor=BACK);
    
    // Align the front right corner to the origin
    square([60,40], anchor=FRONT+RIGHT);
    
    // Center the shape
    square([60,40], anchor=CENTER);
    
    // Visualize anchors
    circle(d=50)
        show_anchors();
  6. Identify faces using direction vectors

    master

    In BOSL2, faces on cube-like shapes are identified by unit direction vectors. You can use these vectors directly or use the provided constant names to select faces for operations like masking or rounding.

    NameVectorDescription
    LEFT[-1,0,0]Left face (-X)
    RIGHT[1,0,0]Right face (+X)
    FRONT / FWD[0,-1,0]Front face (-Y)
    BACK[0,1,0]Back face (+Y)
    BOTTOM / BOT / DOWN[0,0,-1]Bottom face (-Z)
    TOP / UP[0,0,1]Top face (+Z)
  7. Apply 3D edge and corner masks

    master

    3D edge masks (like rounding_edge_mask()) can be attached using edge_mask(). Unlike 2D masks, 3D masks can vary the rounding radius along the length of the edge.

    Key features:

    • Variable Radius: rounding_edge_mask() supports r1 and r2 to vary radius along the edge.
    • Parent Size: edge_mask() sets a special variable $parent_size which can be used to ensure the mask covers the full length of the parent object (e.g., l = $parent_size.x + 0.1).
    • Corner Smoothing: Using only edge masks can leave corners unrounded. Use corner_mask() combined with rounding_corner_mask() or teardrop_corner_mask() to smooth the intersections of edges.
    • Teardrop Masks: teardrop_edge_mask() and teardrop_corner_mask() can be used to limit overhang angles for better FDM printing.
    include <BOSL2/std.scad>
    diff()
     cuboid([60,80,40]) {
      edge_mask(TOP+FWD)
       rounding_edge_mask(r = 10, l = $parent_size.x + 0.1);
      edge_mask(TOP+RIGHT)
       rounding_edge_mask(r = 10, l = $parent_size.y + 0.1);
      edge_mask(RIGHT+FWD)
       rounding_edge_mask(r = 10, l = $parent_size.z + 0.1);
      corner_mask(TOP+RIGHT+FWD)
                rounding_corner_mask(r = 10);
     }
  8. Calculate complex anchor positions using transformation matrices

    master

    If an anchor point is reached through a sequence of transformations, you can calculate its position by multiplying the transformation matrices and applying them to a point using the apply() function. This is more efficient than manually calculating coordinates for complex geometries.

    sphere_pt = apply(
        scale([1.1, 1.2, 1.3]) * xrot(15) * zrot(25) * right(20),
        [0,0,0]
    );
  9. How relative positioning works in BOSL2

    master

    BOSL2 allows you to make an object a child of another object, meaning the child's position is calculated relative to its parent rather than using absolute coordinates. By default, a child's anchor point coincides with the center of the parent. This approach simplifies modeling by removing the need to track absolute positions and orientations, making models easier to maintain and more intuitive.

    There are three primary modules for controlling relative positioning:

    1. position(): Places the child's anchor point at a specific chosen anchor point on the parent.
    2. align(): Places the child on a parent's face without changing its orientation, aligning it with specific edges or corners of that face.
    3. attach(): Places the child on a face (similar to stacking blocks) by mating a designated face of the child with a chosen face on the parent. It also supports alignment to the edges and corners of the face.
  10. Position attachable objects using anchor, spin, and orient

    master

    BOSL2 provides three optional named parameters for controlling how attachable objects are positioned relative to the origin and coordinate axes:

    1. anchor: Aligns a specific point or part of the object with the origin.
    2. spin: Rotates the object around the Z axis (applied after anchoring).
    3. orient: Tilts the top (Z-axis) of the object towards a specified direction.

    Note: For 2D shapes, you can use anchor and spin, but orient is not supported as 2D shapes lack a Z vector.

    include <BOSL2/std.scad>
    // Example of combining all three
    cube([20,20,50], anchor=CENTER, spin=45, orient=UP+FWD);
  11. Attach shapes using position(), orient(), and attach()

    master

    BOSL2 allows you to attach child shapes to parent shapes at specific anchor points.

    1. position(anchor): Positions the child at the specified anchor point of the parent. By default, children are centered on the parent.
    2. orient(anchor): Rotates the child to match the orientation of the parent's anchor point (which usually points outward from the shape's center). It does not move the child.
    3. attach(anchor, [child_anchor]): The simplest method. It combines position() and orient() into one module. You can optionally provide a second argument to specify which side of the child should be attached to the parent's anchor.

    Example of attach() usage:

    • attach(LEFT+BACK): Positions and orients the child at the back-left corner.
    • attach(BACK, LEFT): Attaches the LEFT side of the child to the BACK anchor of the parent.
    include <BOSL2/std.scad>
    // Using attach to position and orient a child at the parent's LEFT+BACK corner
    square(50, center=true)
        attach(LEFT+BACK)
            #square([10,40], anchor=FWD);
    
    // Using attach with a specific child side
    square([10,50], center=true)
        attach(BACK, LEFT)
            #square([10,40], center=true);
  12. Align children with the align() module

    master

    The align() module simplifies positioning children on the faces, edges, or corners of a parent object. Unlike position(), which requires you to manually specify the correct anchor on the child to make it flush with the parent, align() automatically determines and applies the correct child anchor.

    Key behaviors:

    • Automatic Anchoring: align() overrides any anchor parameter specified on the child.
    • Orientation: align() does not change the orientation of the child object. If you need the child to face a certain direction, use orient or spin separately.
    • Multiple Placements: You can pass a list of anchors to align() to create multiple copies of a child, each positioned flush at a different location (e.g., one on the left and one on the right).
    • Inset: Use the inset parameter to position children near an edge without being perfectly flush.
    include<BOSL2/std.scad>
    cuboid([50,40,15])
        align(TOP,RIGHT+FRONT)
            color("lightblue")prismoid([10,5],[7,4],height=4);