Foundation (Python)ExamplesChess set library

Chess set library

Hello and welcome to this simple guide to making your first chess pieces! We will start with simple pieces, like the pawn, and then we will try some more complicated pieces.

The Pawn

The pawn is indeed the simplest piece, so we’ll start by creating the component using:

pawn = Part()

Once that’s done, we’ll create a sketch and initialize it with a plane from our component. In this example, we’ll choose the xz plane:

sketch = Sketch(pawn.xz())

Now that everything is set up, we can start drawing the shape of the pawn by specifying dimensions using arcs or lines with the sketch.pencil.(line/arc) methods. Remember, we can add _to or not to define whether point1 will be the initial point (default 0,0) or the last point that was traced via sketch.pencil.

### Base
sketch.pencil.line_to(15, 0)
sketch.pencil.line(0, 3)
sketch.pencil.line(-2, 2)
sketch.pencil.arc(-3, 8.97, -6)
sketch.pencil.line(0, 1)
sketch.pencil.arc(-2, 2, -2)
 
### Body
sketch.pencil.arc(-3, 24.06, 50)
sketch.pencil.arc(1, 3.26, -2)
sketch.pencil.arc_to(0, 60, -9)

Now that we’ve drawn our shape, let’s check that it looks good before moving on. To do this, we need to finish our drawing and retrieve the shape that we’ll use later to turn it into 3D with:

shape = sketch.pencil.get_closed_shape()

We can now display the shape to see the first rendering.

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

pawn = Part()
sketch = Sketch(pawn.xz())

### Base

sketch.pencil.line_to(15, 0)
sketch.pencil.line(0, 3)
sketch.pencil.line(-2, 2)
sketch.pencil.arc(-3, 8.97, -6)
sketch.pencil.line(0, 1)
sketch.pencil.arc(-2, 2, -2)

### Body

sketch.pencil.arc(-3, 24.06, 50)
sketch.pencil.arc(1, 3.26, -2)
sketch.pencil.arc_to(0, 60, -9)

shape = sketch.pencil.get_closed_shape()

show(sketch)

Now that we can see our well-drawn sketch, let’s turn it into 3D. To do this, we’ll apply the Lathe method to our sketch. First, we need to define the axis for this operation. Since we want our sketch to rotate around itself, we’ll create a line on our sketch and define it as the axis.

line = Line(Point(sketch, 0, 0), Point(sketch, 0, 10))
axis = Axis(line)

Now that we have our axis, we can perform the Lathe operation using our shape and axis. Once it’s defined, we need to add this operation to our component.

lathe = Lathe(shape, axis)
pawn.add_operation(lathe)

Now let’s do some code formatting and take a look at our final render:

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


class Pawn(Part):
    def __init__(self):
        axis, shape = self.get_sketch()
        # do the lathe.
        self.add_operation(Lathe(shape, axis))

    def get_sketch(self, debug=False):
        sketch = Sketch(self.xz())

        # Base
        sketch.pencil.line_to(15, 0)
        sketch.pencil.line(0, 3)
        sketch.pencil.line(-2, 2)
        sketch.pencil.arc(-3, 8.97, -6)
        sketch.pencil.line(0, 1)
        sketch.pencil.arc(-2, 2, -2)

        # Body
        sketch.pencil.arc(-3, 24.06, 50)
        sketch.pencil.arc(1, 3.26, -2)
        sketch.pencil.arc_to(0, 60, -9)

        shape = sketch.pencil.get_closed_shape()
        axis = Axis(Line(Point(sketch, 0, 0), Point(sketch, 0, 10)))

        if debug:
            return sketch

        return axis, shape


show(Pawn())

# %%

The Bishop

Now that we know how to make a pawn, the bishop won’t be very different. The only thing that will change for the bishop is the sketch drawing. The rest will be very similar.

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


class Bishop(Part):
    def __init__(self):
        axis, shape = self.get_sketch()
        # do the lathe.
        self.add_operation(Lathe(shape, axis))

    def get_sketch(self, debug=False):
        sketch = Sketch(self.xz())

        # Base
        sketch.pencil.line_to(15, 0)
        sketch.pencil.line(0, 3)
        sketch.pencil.line(-2, 2)
        sketch.pencil.arc(-3, 8.97, -6)
        sketch.pencil.line(0, 1)
        sketch.pencil.arc(-2, 2, -2)

        # Head
        sketch.pencil.arc(-0.5, 31.02, 45)
        # normally its -2 but error
        sketch.pencil.arc(1, 3.94, -2.05)
        sketch.pencil.arc(-4.5, 19.06, -20)
        # random radius
        sketch.pencil.arc_to(0, 75, -3.1)

        shape = sketch.pencil.get_closed_shape()
        axis = Axis(Line(Point(sketch, 0, 0), Point(sketch, 0, 10)))

        if debug:
            return sketch

        return axis, shape


show(Bishop())

# %%

The Queen

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


class Queen(Part):
    def __init__(self):
        shape, axis = self.get_sketch()

        self.add_operation(Lathe(shape, axis))
        return self

    def get_sketch(self, debug=False):
        sketch = Sketch(self.xz())

        # Base
        sketch.pencil.line_to(15, 0)
        sketch.pencil.line(0, 3)
        sketch.pencil.line(-2, 2)
        sketch.pencil.arc(-3, 8.97, -6)
        sketch.pencil.line(0, 1)
        sketch.pencil.arc(-2, 2, -2)

        # Head
        sketch.pencil.arc(-2, 42.43, 90)
        sketch.pencil.arc(1.5, 3.32, -2)
        sketch.pencil.line(0, 1)
        sketch.pencil.line(5, 15)
        sketch.pencil.line(0, 1)
        sketch.pencil.arc(-10.5, 3.27, -25)

        sketch.pencil.arc(-2, 2, -2)

        shape = sketch.pencil.get_closed_shape()
        axis = Axis(Line(Point(sketch, 0, 0), Point(sketch, 0, 10)))

        if debug:
            return sketch

        return shape, axis


show(Queen())

# %%

The Rook

Now let’s move on to the rook. The biggest difference here is that we also need to take care of the top, as it’s not flat like the others. To do this, we’ll first draw the rook without the top, like this:

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

class Rook(Part):

    def __init__(self):
        # build the rook
        shape, axis = self.get_sketch()
        self.add_operation(Lathe(shape, axis))

    def get_sketch(self, debug=False):
        sketch = Sketch(self.xz())

        # Base
        sketch.pencil.line_to(15, 0)
        sketch.pencil.line(0, 3)
        sketch.pencil.line(-2, 2)
        sketch.pencil.arc(-3, 8.97, -6)
        sketch.pencil.line(0, 1)
        sketch.pencil.arc(-2, 2, -2)

        # Head
        sketch.pencil.arc(0.5, 34.2, 50)
        sketch.pencil.arc(2, 3.32, -2)
        sketch.pencil.line(2.5, 2.5)
        sketch.pencil.line(0, 18)

        sketch.pencil.line_to(8.5, 75)
        sketch.pencil.line(0, -8)
        sketch.pencil.line(-8.5, 0)

        shape = sketch.pencil.get_closed_shape()
        axis = Axis(Line(Point(sketch, 0, 0), Point(sketch, 0, 10)))

        if debug:
            return sketch

        return shape, axis

show(Rook())

Now we’ll cut off the top. To do this, we’ll add a new operation. By extruding the top, we’ll get the desired result.

First, let’s change the plane and position ourselves below the area we want to cut. Since we want to cut from 70 to 75, we’ll position ourselves at 70. We’ll use the PlaneFactory class for this, which has a get_parallel_plane method:

pf = PlaneFactory()
plane = pf.get_parallel_plane(self.part.xy(), 70)
sketch = Sketch(plane)

Now that our sketch is in the right place, we want to draw a cross shape to cut from all four sides. To do this, we’ll draw on the sketch again:

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

class Rook:

    def __init__(self):
        self.rook = Part()

    def cut_sketch(self, debug=False):
        pf = PlaneFactory()
        plane = pf.get_parallel_plane(self.rook.xy(), 70)
        sketch = Sketch(plane)

        # make a cross to cut the top
        sketch.pencil.move_to(3.5, 3.5)

        # top
        sketch.pencil.line(0, 12)
        sketch.pencil.line(-7, 0)
        sketch.pencil.line(0, -12)

        # left
        sketch.pencil.line(-12, 0)
        sketch.pencil.line(0, -7)
        sketch.pencil.line(12, 0)

        # bottom
        sketch.pencil.line(0, -12)
        sketch.pencil.line(7, 0)
        sketch.pencil.line(0, 12)

        # right
        sketch.pencil.line(12, 0)
        sketch.pencil.line(0, 7)
        sketch.pencil.line(-12, 0)

        shape = sketch.pencil.get_closed_shape()

        if debug:
            return sketch

        return shape

show(Rook().cut_sketch(debug=True))

Tip: We can see that this gives us quite repetitive code. We’ll see how to simplify this in the king section.

Now that we have this cross, all we have to do is use the extrusion operation, making sure to pass cut=True as an argument:

shape = self.cut_sketch()
self.rook.add_operation(Extrusion(shape, 20, cut=True))

Here’s our final render:

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


class Rook(Part):
    def __init__(self):
        # build the rook
        shape, axis = self.get_sketch()
        self.add_operation(Lathe(shape, axis))

        # cut the top
        shape = self.cut_sketch()
        self.add_operation(Extrusion(shape, 20, cut=True))

    def get_sketch(self, debug=False):
        sketch = Sketch(self.xz())

        # Base
        sketch.pencil.line_to(15, 0)
        sketch.pencil.line(0, 3)
        sketch.pencil.line(-2, 2)
        sketch.pencil.arc(-3, 8.97, -6)
        sketch.pencil.line(0, 1)
        sketch.pencil.arc(-2, 2, -2)

        # Head
        sketch.pencil.arc(0.5, 34.2, 50)
        sketch.pencil.arc(2, 3.32, -2)
        sketch.pencil.line(2.5, 2.5)
        sketch.pencil.line(0, 18)

        sketch.pencil.line_to(8.5, 75)
        sketch.pencil.line(0, -8)
        sketch.pencil.line(-8.5, 0)

        shape = sketch.pencil.get_closed_shape()
        axis = Axis(Line(Point(sketch, 0, 0), Point(sketch, 0, 10)))

        if debug:
            return sketch

        return shape, axis

    def cut_sketch(self, debug=False):
        pf = PlaneFactory()
        plane = pf.get_parallel_plane(self.xy(), 70)
        sketch = Sketch(plane)

        # make a cross to cut the top
        sketch.pencil.move_to(3.5, 3.5)

        # top
        sketch.pencil.line(0, 12)
        sketch.pencil.line(-7, 0)
        sketch.pencil.line(0, -12)

        # left
        sketch.pencil.line(-12, 0)
        sketch.pencil.line(0, -7)
        sketch.pencil.line(12, 0)

        # bottom
        sketch.pencil.line(0, -12)
        sketch.pencil.line(7, 0)
        sketch.pencil.line(0, 12)

        # right
        sketch.pencil.line(12, 0)
        sketch.pencil.line(0, 7)
        sketch.pencil.line(-12, 0)

        shape = sketch.pencil.get_closed_shape()

        if debug:
            return sketch

        return shape


show(Rook())

# %%

The King

Now let’s move on to the king. The king will be quite similar to the rook, but the top cut is more complex. We can see that it needs to be cut at an angle. In this case, we’ll use CircularPattern. It’s pretty simple. We’ll start by drawing the first part of the top cross, but only the first part. Once that’s done, we’ll ask for the pattern to be repeated 6 times with respect to a point placed exactly at the bottom and in the middle of our drawing. We won’t forget to add the pattern we already have at the return. Like this:

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

king = Part()
sketch = Sketch(king.xz())

sketch.pencil.move_to(1, 1)

### Top

sketch.pencil.line(0, 15)
sketch.pencil.line(-2, 0)
sketch.pencil.line(0, -15)

shape = sketch.pencil.get_closed_shape()

cp = CircularPattern(Point(sketch, 0, 0), 6).run(shape)
cp.insert(0, shape)

show(sketch)

Once this is done, just like with the rook, we’ll extrude, and we’re done:

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


class King(Part):
    def __init__(self):
        # build the King
        shape, axis = self.get_sketch()
        self.add_operation(Lathe(shape, axis))

        # cut the top
        cp = self.cut_sketch()
        for shape in cp:
            self.add_operation(Extrusion(shape, 5, cut=True))

    def get_sketch(self, debug=False):
        sketch = Sketch(self.xz())

        # Base
        sketch.pencil.line_to(15, 0)
        sketch.pencil.line(0, 3)
        sketch.pencil.line(-2, 2)
        sketch.pencil.arc(-3, 8.97, -6)
        sketch.pencil.line(0, 1)
        sketch.pencil.arc(-2, 2, -2)

        # Head
        sketch.pencil.arc(0, 44.02, 85)
        sketch.pencil.arc(1.625, 3.16, -2)
        sketch.pencil.arc(-0.625, 10.84, 10.16)
        sketch.pencil.line(4, 8)
        sketch.pencil.line(0, 2)

        sketch.pencil.line(-5, 0)
        sketch.pencil.line(0, -10)
        sketch.pencil.line(-8, 0)

        shape = sketch.pencil.get_closed_shape()
        axis = Axis(Line(Point(sketch, 0, 0), Point(sketch, 0, 10)))

        if debug:
            return sketch

        return shape, axis

    def cut_sketch(self, debug=False):
        pf = PlaneFactory()
        plane = pf.get_parallel_plane(self.xy(), 80)
        sketch = Sketch(plane)

        sketch.pencil.move_to(1, 1)

        # top
        sketch.pencil.line(0, 15)
        sketch.pencil.line(-2, 0)
        sketch.pencil.line(0, -15)

        shape = sketch.pencil.get_closed_shape()

        cp = CircularPattern(Point(sketch, 0, 0), 6).run(shape)
        cp.insert(0, shape)

        if debug:
            return sketch

        return cp


show(King())

# %%

The Knight

Now let’s move on to the knight. The challenge here is to be able to perform a different operation on the same plane. Indeed, the base will be a Lathe as with the others. But the body of the knight will be made using an Extrusion.

Here’s how to draw the knight:

To create the base, we’ll use the Lathe operation as before:

shape, axis = self.get_base_sketch()
self.knight.add_operation(Lathe(shape, axis))

For the body, we’ll use the Extrusion operation:

shape = self.get_knight_sketch()
self.knight.add_operation(Extrusion(shape, -5, 5))

Here’s our final render:

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


class Knight(Part):
    def __init__(self):
        # build the base of the knight with a Lathe operation
        shape, axis = self.get_base_sketch()
        self.add_operation(Lathe(shape, axis))

        # build the top of the knight with a Extrusion operation
        shape = self.get_knight_sketch()
        self.add_operation(Extrusion(shape, -5, 5))

        return self

    def get_base_sketch(self, debug=False):
        sketch = Sketch(self.xz())

        # Base
        sketch.pencil.line_to(15, 0)
        sketch.pencil.line(0, 3)
        sketch.pencil.line(-2, 2)
        sketch.pencil.arc(-3, 8.97, -6)
        sketch.pencil.line(0, 1)
        sketch.pencil.arc(-2, 2, -2)
        sketch.pencil.line_to(0, 16.97)

        shape = sketch.pencil.get_closed_shape()
        axis = Axis(Line(Point(sketch, 0, 0), Point(sketch, 0, 10)))

        if debug:
            return sketch

        return shape, axis

    def get_knight_sketch(self, debug=False):
        sketch = Sketch(self.xz())

        # move to the top of the base
        sketch.pencil.move_to(13.05 / 2, 16.97)

        # draw the knight
        # right side
        sketch.pencil.arc(8.67 - 13.05 / 2, 7.06, 16.08)
        sketch.pencil.arc(-(8.67 - 7.12), 21.71 - 7.06, -12.68)
        sketch.pencil.line(-7.12, 29.37 - 21.71)
        sketch.pencil.line(-1, 33.34 - 29.37)
        sketch.pencil.line(1.78, 35.83 - 33.34)
        sketch.pencil.line(12.3 - 0.78, 34.89 - 35.83)
        sketch.pencil.line(14.14 - 12.3, 38.64 - 34.89)
        sketch.pencil.line(-(14.14 - 3.08), 45.91 - 38.64)
        sketch.pencil.line(4.18 - 3.08, 47.58 - 45.91)
        sketch.pencil.line(-4.18, 50.31 - 47.58)
        sketch.pencil.line(-1.33, (75 - 16.97) - 50.31)

        # left side
        sketch.pencil.arc(-(8.67 - 1.33), 7.06 - (75 - 16.97), -42.07)
        sketch.pencil.arc(8.67 - 13.05 / 2, -7.06, 16.08)

        shape = sketch.pencil.get_closed_shape()

        if debug:
            return sketch

        return shape


show(Knight())

# %%