Foundation (Python)TutorialsYour first assembly

Your first assembly

An assembly is a collection of components that are connected together. Such a component can be a part, or a subassembly.

Renders the 3D model in your browser. The first run downloads the Python runtime (≈30 MB).
# %%
from cadbuildr.foundation import (
    Sketch,
    Part,
    Extrusion,
    Square,
    Assembly,
    show,
    Lathe,
    Axis,
    Line,
    Point,
)
import numpy as np


class Star(Part):
    def __init__(self, inside_radius=5, outside_radius=10, num_points=5, height=4):
        self.inside_radius = inside_radius
        self.outside_radius = outside_radius
        self.num_points = num_points
        self.height = height
        # Create a Sketch from XY Plane
        s = Sketch(self.xy())
        # draw a star
        angle = np.pi / 2  # start angle
        # Use the Pencil class instead of points !
        s.pencil.move(
            self.outside_radius * np.cos(angle), self.outside_radius * np.sin(angle)
        )

        for i in range(self.num_points):
            angle += np.pi / self.num_points
            s.pencil.line_to(
                self.inside_radius * np.cos(angle), self.inside_radius * np.sin(angle)
            )
            angle += np.pi / self.num_points
            s.pencil.line_to(
                self.outside_radius * np.cos(angle), self.outside_radius * np.sin(angle)
            )
        e = Extrusion(s.pencil.get_closed_polygon(), -self.height / 2, self.height / 2)
        self.add_operation(e)
        self.paint("yellow")


class Tree(Part):
    def __init__(self, n_branches=7, branch_dx=10, branch_height=20):
        self.n_branches = n_branches
        self.branch_dx = branch_dx
        self.branch_height = branch_height
        # Create a Sketch from XY Plane
        s = Sketch(self.xz())

        # s.pencil.move(0, -100)
        s.pencil.line((self.n_branches + 1) * self.branch_dx, 0)
        for i in range(self.n_branches):
            s.pencil.line(-2 * self.branch_dx, self.branch_height)
            if i != self.n_branches - 1:
                s.pencil.line(self.branch_dx, 0)

        axis_line = Line(Point(s, 0, 0), Point(s, 0, 100))
        l = Lathe(s.pencil.get_closed_polygon(), Axis(axis_line))
        self.add_operation(l)
        self.paint("dark_green")


class TreeStand(Part):
    def __init__(self, size=20):
        self.size = size

        # Create a Sketch from XY Plane
        s = Sketch(self.xy())
        square = Square.from_center_and_side(s.origin, self.size)
        e = Extrusion(square, self.size)
        self.add_operation(e)
        self.paint("brown")


class TreeAssembly(Assembly):
    def __init__(self, n_branches=7, branch_height=20):
        self.n_branches = n_branches
        self.branch_height = branch_height

        # add the tree
        tree = Tree()
        self.add_component(tree)
        # add the star
        star = Star()
        tree_height = self.n_branches * self.branch_height
        # rotate the star to be vertical
        # translate the star by the height of the tree
        star.translate_z(tree_height)
        star.rotate([1, 0, 0], np.pi / 2)

        self.add_component(star)
        # add the cube
        stand = TreeStand()
        # cube.rotate([1, 0, 0], np.pi / 2)
        stand.translate([0, 0, -stand.size])
        self.add_component(stand)


if __name__ == "__main__":
    show(TreeAssembly())

# %%

In this example, we are creating combining three parts into an assembly. The TreePart, TreeStandPart and the StarPart. These parts can individually be made using Etsrusions and Lathe operations. The assembly is created by combining these parts together.

To add a part to an assembly you can use the add_component method. Before doing so you might want to translate or rotate the part. In this example, we are translating the TreeStandPart and translating then rotating the StarPart to be at the top of the tree.

For components that connect to each other, prefer anchors and joints over raw transforms: they express the relationship (RigidJoint, RevoluteJoint, …) instead of coordinates, and domain libraries build one-liners like connect_with_screw(...) on top of them.

Renders the 3D model in your browser. The first run downloads the Python runtime (≈30 MB).
from cadbuildr.foundation import *
import random


class Cube(Part):
    def __init__(self, size=20):
        self.size = size
        # Create a Sketch from XY Plane
        s = Sketch(self.xy())
        square = Square.from_center_and_side(s.origin, self.size)
        e = Extrusion(square, self.size)
        self.add_operation(e)

        # random color in red, green, blue, yellow, purple, cyan
        color_choices = ["red", "green", "blue", "yellow", "purple", "cyan"]
        self.paint(color_choices[random.randint(0, len(color_choices) - 1)])


class Pyramid(Assembly):
    """Recursive Assembly"""

    def __init__(self, depth=2, cube_size=20, total_depth=None):
        self.depth = depth
        self.cube_size = cube_size
        if total_depth is None:
            total_depth = self.depth

        top_cube = Cube(self.cube_size)
        if self.depth > 1:
            p_left = Pyramid(self.depth - 1, cube_size, total_depth)
            p_right = Pyramid(self.depth - 1, cube_size, total_depth)

            # move the pyramides by cube_size * 0.6 * (self.depth - 1) sideways and cube_size down
            p_left.translate(
                [self.cube_size * 0.6 * (self.depth - 1), 0, -self.cube_size]
            )

            p_right.translate(
                [-self.cube_size * 0.6 * (self.depth - 1), 0, -self.cube_size]
            )

            self.add_component(top_cube)
            self.add_component(p_left)
            self.add_component(p_right)
        else:
            self.add_component(top_cube)


show(Pyramid(depth=2))

In this other example, we define two classes: Cube and Pyramid. The Cube class creates cubes with random colors using similar code as in previous examples. The Pyramid class creates pyramids by recursively building each level of the pyramid using the Cube class. In this case, we display a pyramid with 2 layers and display it.