Foundation (Python)ReferencePencil (sketch tool)

Pencil

The most practical method for drawing a complex shape is undoubtedly using sketch.pencil. This tool allows us to design our drawing point by point. To use it, you simply need to initialize a sketch.

It’s important to note that for each method we’ll discuss, we can add _to to use relative coordinates to the previous point.

Movement

move(x, y): With the move method, we can define where to start our shape on the sketch.

Drawing

There are several methods available for drawing all the shapes for your design:

  • line(x, y): Draws a line to the coordinates x, y.

  • arc(x, y, radius): Draws an arc to x, y with a specified radius.

  • tangent_arc(x, y): Draws an arc tangentially to the last primitive (arc or line)

  • rounded_corner_then_line(x, y, radius): Draws a rounded corner followed by a line.

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


class PencilExample:
    def __init__(self):
        self.part = Part()
        self.sketch = Sketch(self.part.xz())

    def line_example(self):
        self.sketch.pencil.move_to(30, 30)
        self.sketch.pencil.line(10, 10)
        self.sketch.pencil.line(10, -10)

        # useless to draw the last line bc get_closed_shape will close the shape
        shape = self.sketch.pencil.get_closed_shape()

    def arc_example(self):
        self.sketch.pencil.move_to(30, -30)
        self.sketch.pencil.arc(20, 0, 10)

        shape = self.sketch.pencil.get_closed_shape()

    def tangent_arc_example(self):
        self.sketch.pencil.move_to(-30, -30)
        self.sketch.pencil.line(10, 10)
        self.sketch.pencil.tangent_arc(10, -10)

        shape = self.sketch.pencil.get_closed_shape()

    def rounded_corner_then_line_example(self):
        self.sketch.pencil.move_to(-30, 30)
        self.sketch.pencil.line(10, 10)
        self.sketch.pencil.rounded_corner_then_line(10, -10, 1)

        shape = self.sketch.pencil.get_closed_shape()

    def get_sketch(self):
        self.line_example()
        self.sketch.pencil.reset()

        self.arc_example()
        self.sketch.pencil.reset()

        self.tangent_arc_example()
        self.sketch.pencil.reset()

        self.rounded_corner_then_line_example()

        return self.sketch


show(PencilExample().get_sketch())

# %%

Getting the Shape

Once you have completed your design, there are two ways to get your shape to be used in extrusion or other operations:

  • get_closed_shape: This will draw a line to your first point to close the shape if it isn’t already closed.

  • close_with_mirror: This will mirror the pattern using the first-to-last point line as the mirror axis to close the shape.

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


class PencilExample:
    def __init__(self):
        self.part = Part()
        self.sketch = Sketch(self.part.xz())

    def create_shape(self):
        self.sketch.pencil.line(15, 0)
        self.sketch.pencil.line(0, 3)
        self.sketch.pencil.line(-2, 2)
        self.sketch.pencil.arc(-3, 8.97, -6)
        self.sketch.pencil.line(0, 1)
        self.sketch.pencil.arc(-2, 2, -2)

        # Body
        self.sketch.pencil.arc(-3, 24.06, 50)
        self.sketch.pencil.arc(1, 3.26, -2)
        self.sketch.pencil.arc(-6, 15.71, -9)

    def get_sketch(self):
        self.sketch.pencil.move_to(30, 0)
        self.create_shape()
        shape = self.sketch.pencil.get_closed_shape()
        self.sketch.pencil.reset()

        self.sketch.pencil.move_to(-30, 0)
        self.create_shape()
        shape = self.sketch.pencil.close_with_mirror()

        return self.sketch


show(PencilExample().get_sketch())

# %%