Your first sketch

2D Sketches are often the first step in making a 3D operation. Extrusions, and Lathe all start with a 2D sketch.

To create a sketch, you can use the Sketch class. You will need to specify the plane that the sketch will be on. As a starting Point you can use the origin planes that can access using part.xy(), part.yz(), and part.xz().

As you saw in the first example, on a Sketch you can create Points and Lines.

p1 = Point(s, 0, 0)
p2 = Point(s, 40, 0)
p3 = Point(s, 40, 40)
p4 = Point(s, 0, 40)

l1 = Line(p1, p2) l2 = Line(p2, p3) l3 = Line(p3, p4) l4 = Line(p4, p1)

You can also use the sketch.pencil to make it easier to draw lines.

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


def get_star(inside_radius=20, outside_radius=40, num_points=5, height=10):
    # initialize the component
    star = Part()
    # Create a Sketch from XY Plane
    s = Sketch(star.xy())
    # Start point
    start_x, start_y = outside_radius, 0
    s.pencil.move(start_x, start_y)

    # draw a star
    angle = 0
    for i in range(num_points):
        angle += np.pi / num_points
        s.pencil.line_to(inside_radius * np.cos(angle), inside_radius * np.sin(angle))
        angle += np.pi / num_points
        if i != num_points - 1:
            s.pencil.line_to(
                outside_radius * np.cos(angle), outside_radius * np.sin(angle)
            )

    e = Extrusion(s.pencil.get_closed_polygon(), height)
    star.add_operation(e)
    star.paint("yellow")
    return star


show(get_star())

We can also use the pencil attribute of the Sketch. The pencil object has methods for moving in absolute coordinates (move_to) as well as relatively (move) and drawing lines (line_to), which are used in a loop to draw the star shape. Once the shape is drawn, you can extract the Polygon using the get_closed_polygon.

As you can see we are also using import numpy as np to perform maths operations.

You can also use many tools to create more complex Sketches. Extrusion require a closed shape but you can make use of many of the already available shapes:

  • Rectangle
  • Square
  • Circle
  • Ellipse
  • Hexagon
  • Polygon
Renders the 3D model in your browser. The first run downloads the Python runtime (≈30 MB).
from cadbuildr.foundation import *


def closed_shapes():
    # initialize the component
    closed_shapes = Part()
    # Create a Sketch from XY Plane
    s = Sketch(closed_shapes.xy())
    # Use the Square class instead of points !
    square = Square.from_center_and_side(s.origin, 100)
    closed_shapes.add_operation(Extrusion(square, 5))

    shapes = []
    shapes.append(Rectangle.from_center_and_sides(Point(s, 20, 20), 10, 15))
    shapes.append(Circle(Point(s, -20, 20), 15))
    shapes.append(Ellipse(Point(s, 20, -20), 15, 10))
    shapes.append(Hexagon(Point(s, -20, -20), 15))
    for shape in shapes:
        closed_shapes.add_operation(Extrusion(shape, 15))
    closed_shapes.paint("orange")
    return closed_shapes


show(closed_shapes())