@settings(defaultLengthUnit = cm)

// Define parameters
nTeeth = 21
module = 0.5
pitchDiameter = module * nTeeth
pressureAngle = 20deg
addendum = module
deddendum = 1.25 * module
baseDiameter = pitchDiameter * cos(pressureAngle)
tipDiameter = pitchDiameter + 2 * module
gearHeight = .3

angle001 = 6
sketch001 = startSketchOn(XY)
  |> startProfile(at = [baseDiameter / 2, 0])
  |> involuteCircular(
       startRadius = baseDiameter / 2,
       endRadius = tipDiameter / 2,
       angle = 0,
       tag = $seg01,
     )
  |> line(endAbsolute = [lastSegX(%), lastSegY(%)])
  |> arc(
       angleStart = asin(segEndY(seg01) / segEndX(seg01)),
       angleEnd = 9,
       radius = tipDiameter / 2,
       tag = $seg02,
     )
  |> involuteCircular(
       startRadius = baseDiameter / 2,
       endRadius = tipDiameter / 2,
       angle = -12,
       reverse = true,
     )
  |> line(endAbsolute = [lastSegX(%), lastSegY(%)])
  |> line(endAbsolute = polar(angle = 360 / nTeeth, length = baseDiameter / 2))
  |> patternCircular2d(
       %,
       instances = nTeeth,
       center = [0, 0],
       arcDegrees = 360,
       rotateDuplicates = true,
     )
  |> close()
  |> subtract2d(tool = circle(center = [0, 0], radius = 1))
  |> extrude(length = gearHeight)
