Tetrahedron

Method used

In this example we will use the “Cut Cube” method for making a tetrahedron using extrusions only. The idea is to start from a cube. On that cube we can look at the opposite corners (ie a set of corners that are not connected by an edge). If we define a plane going through 3 of these corners, we get 4 faces of a tetrahedron.

Helper assembly

The helper assembly displays the cube and the opposite corners.

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


class Sphere(Part):
    def __init__(self, radius=2):
        self.radius = radius
        self.create_lathe()
        self.paint("red")

    def create_lathe(self):
        plane = self.xy()
        sketch = Sketch(plane)
        sketch.pencil.move_to(0, -self.radius)
        sketch.pencil.arc(0, 2 * self.radius, self.radius)
        shape = sketch.pencil.get_closed_shape()
        axis = Axis(Line(Point(sketch, 0, 0), Point(sketch, 0, 1)))
        lathe = Lathe(shape, axis)
        self.add_operation(lathe)


class Tetrahedron(Part):
    """Using the cut cube method
    1. Create a cube
    2. Find the opposite points of the cube
    3. Cut the cube using the 4 faces of the tetrahedron
    """

    def __init__(self, side=10, color="red", build=True):
        self.side = side
        # the sides of the tetrahedron are the diagonals of faces of the cube
        self.cube_side = side * np.sqrt(2)
        self.add_cube()
        if build:
            self.build()

    def build(self):
        points = self.get_cube_points()
        tetra_indices = [0, 3, 5, 6]  # opposite points of the cube
        o_pts = [points[i] for i in tetra_indices]
        # we have 4 faces of the tetrahedron using 3 of the 4 opposite points
        # python itertools.combinations() method to get all possible combinations of a list
        # point_sets = list(itertools.combinations(o_pts, 3))
        point_sets = [
            [o_pts[0], o_pts[1], o_pts[2]],
            # need to switch the order for the direction of cut
            [o_pts[0], o_pts[3], o_pts[1]],
            [o_pts[0], o_pts[2], o_pts[3]],
            # need to switch the order for the direction of cut
            [o_pts[1], o_pts[3], o_pts[2]],
        ]
        for p in point_sets[:4]:
            self.cut_plane_from_3_points(p)

    def add_cube(self):
        sketch = Sketch(self.xy())
        square = Square.from_center_and_side(sketch.origin, self.cube_side)
        extrusion = Extrusion(square, -self.cube_side / 2, self.cube_side / 2)
        self.add_operation(extrusion)

    def get_cube_points(self):
        points = []
        for i in range(2):
            for j in range(2):
                for k in range(2):
                    points.append(
                        Point3D(
                            i * self.cube_side - self.cube_side / 2,
                            j * self.cube_side - self.cube_side / 2,
                            k * self.cube_side - self.cube_side / 2,
                        )
                    )
        return points

    def cut_plane_from_3_points(self, points, cut=True):
        p1, p2, p3 = points
        plane = self.pf.get_plane_from_3_points(self.head._frame, [p1, p2, p3])
        sketch = Sketch(plane)
        # cut almost everything one one side of the plane
        square = Square.from_center_and_side(sketch.origin, self.side * 4)
        extrusion = Extrusion(square, -self.side, cut=True)
        self.add_operation(extrusion)


class HelperAssy(Assembly):
    def __init__(self):

        self.tetrahedron = Tetrahedron(build=False)
        self.add_component(self.tetrahedron)

        points = self.tetrahedron.get_cube_points()
        tetra_indices = [0, 3, 5, 6]
        o_pts = [points[i] for i in tetra_indices]
        # add a red spehere in the opposite points
        for p in o_pts:
            sphere = Sphere(1)
            sphere.translate(p.to_array())
            self.add_component(sphere)



show(HelperAssy())

Full Code

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


class Sphere(Part):
    def __init__(self, radius=2):
        self.radius = radius
        self.create_lathe()
        self.paint("red")

    def create_lathe(self):
        plane = self.xy()
        sketch = Sketch(plane)
        sketch.pencil.move_to(0, -self.radius)
        sketch.pencil.arc(0, 2 * self.radius, self.radius)
        shape = sketch.pencil.get_closed_shape()
        axis = Axis(Line(Point(sketch, 0, 0), Point(sketch, 0, 1)))
        lathe = Lathe(shape, axis)
        self.add_operation(lathe)


class Tetrahedron(Part):
    """Using the cut cube method
    1. Create a cube
    2. Find the opposite points of the cube
    3. Cut the cube using the 4 faces of the tetrahedron
    """

    def __init__(self, side=10, color="red", build=True):
        self.side = side
        # the sides of the tetrahedron are the diagonals of faces of the cube
        self.cube_side = side * np.sqrt(2)
        self.add_cube()
        if build:
            self.build()

    def build(self):
        points = self.get_cube_points()
        tetra_indices = [0, 3, 5, 6]  # opposite points of the cube
        o_pts = [points[i] for i in tetra_indices]
        # we have 4 faces of the tetrahedron using 3 of the 4 opposite points
        # python itertools.combinations() method to get all possible combinations of a list
        # point_sets = list(itertools.combinations(o_pts, 3))
        point_sets = [
            [o_pts[0], o_pts[1], o_pts[2]],
            # need to switch the order for the direction of cut
            [o_pts[0], o_pts[3], o_pts[1]],
            [o_pts[0], o_pts[2], o_pts[3]],
            # need to switch the order for the direction of cut
            [o_pts[1], o_pts[3], o_pts[2]],
        ]
        for p in point_sets[:4]:
            self.cut_plane_from_3_points(p)

    def add_cube(self):
        sketch = Sketch(self.xy())
        square = Square.from_center_and_side(sketch.origin, self.cube_side)
        extrusion = Extrusion(square, -self.cube_side / 2, self.cube_side / 2)
        self.add_operation(extrusion)

    def get_cube_points(self):
        points = []
        for i in range(2):
            for j in range(2):
                for k in range(2):
                    points.append(
                        Point3D(
                            i * self.cube_side - self.cube_side / 2,
                            j * self.cube_side - self.cube_side / 2,
                            k * self.cube_side - self.cube_side / 2,
                        )
                    )
        return points

    def cut_plane_from_3_points(self, points, cut=True):
        p1, p2, p3 = points
        plane = self.pf.get_plane_from_3_points(self.head._frame, [p1, p2, p3])
        sketch = Sketch(plane)
        # cut almost everything one one side of the plane
        square = Square.from_center_and_side(sketch.origin, self.side * 4)
        extrusion = Extrusion(square, -self.side, cut=True)
        self.add_operation(extrusion)


class HelperAssy(Assembly):
    def __init__(self):

        self.tetrahedron = Tetrahedron(build=False)
        self.add_component(self.tetrahedron)

        points = self.tetrahedron.get_cube_points()
        tetra_indices = [0, 3, 5, 6]
        o_pts = [points[i] for i in tetra_indices]
        # add a red spehere in the opposite points
        for p in o_pts:
            sphere = Sphere(1)
            sphere.translate(p.to_array())
            self.add_component(sphere)


show(HelperAssy())
# show(Tetrahedron())

# %%

Explanations

Step 1: Making a cube

def add_cube(self):
        sketch = Sketch(self.xy())
        square = Square.from_center_and_side(sketch.origin, self.cube_side)
        extrusion = Extrusion(square, -self.cube_side / 2, self.cube_side / 2)
        self.add_operation(extrusion)

After creating the component we add a cube to it using a simple extrusion.

Step 3: Get the Cube Points

We can then use the following code to obtain the 8 corners of the cube.

def get_cube_points(self):
        points = []
        for i in range(2):
            for j in range(2):
                for k in range(2):
                    points.append(
                        Point3D(
                            i * self.cube_side - self.cube_side / 2,
                            j * self.cube_side - self.cube_side / 2,
                            k * self.cube_side - self.cube_side / 2,
                        )
                    )
        return points

They come in a list of 8 points and we select the following ones :

tetra_indices = [0, 3, 5, 6]

to be the corners of the tetrahedron.

Step 4: Define Cutting Planes

def cut_plane_from_3_points(self, points, cut=True):
        p1, p2, p3 = points
        plane = self.pf.get_plane_from_3_points(self.head._frame, [p1, p2, p3])
        sketch = Sketch(plane)
        # cut almost everything one one side of the plane
        square = Square.from_center_and_side(sketch.origin, self.side * 4)
        extrusion = Extrusion(square, -self.side, cut=True)
        self.add_operation(extrusion)

Using the corners we create a list of 4 sets of 3 points. Each set of 3 points defines a plane that will cut the cube. We can use the helper function from the PlaneFactory.get_plane_from_3_points function to create planes from 3 points. We then just draw a large square to cut each of the sides of the tetrahedron.

⚠️

The order of the points in the plane definition maters as it will define the direction of plane normal and thus the side of the half-space that will be cut.