// Router template for a cross bar
// A guide for routing a notch into a cross bar.

// Set units
@settings(defaultLengthUnit = mm, kclVersion = 1.0)

// Define parameters
routerDiameter = 12.7
templateDiameter = (11 / 16): number(in)
slateWidthHalf = 41.5 / 2
minClampingDistance = 50 + 30
templateThickness = 10
radius = 10
depth = 30

// Calculated parameters
templateGap = (templateDiameter - routerDiameter) / 2 - 0.5
distanceToInsideEdge = slateWidthHalf + templateThickness + templateGap

// Create the first sketch
sketch001 = startSketchOn(XZ)
  |> startProfile(at = [0, depth + templateGap])
  |> xLine(length = slateWidthHalf - radius, tag = $seg01)
  |> arc(
       angleStart = 90,
       angleEnd = 0,
       radius = 10 + templateGap,
       tag = $seg09,
     )
  |> yLine(endAbsolute = -templateThickness, tag = $seg03)
  |> xLine(length = templateThickness, tag = $seg07)
  |> yLine(endAbsolute = (segEndY(seg01) + templateThickness) / 2_ - templateThickness, tag = $seg02)
  |> xLine(endAbsolute = segEndX(seg03) + minClampingDistance, tag = $seg06)
  |> yLine(length = templateThickness * 2, tag = $seg08)
  |> xLine(endAbsolute = segEndX(seg02) + 0, tag = $seg05)
  |> yLine(endAbsolute = segEndY(seg01) + templateThickness, tag = $seg10)
  |> xLine(endAbsolute = 0, tag = $seg04)
  |> xLine(length = -segLen(seg04))
  |> yLine(length = -segLen(seg10))
  |> xLine(length = -segLen(seg05))
  |> yLine(length = -segLen(seg08))
  |> xLine(length = segLen(seg06))
  |> yLine(length = -segLen(seg02))
  |> xLine(length = segLen(seg07))
  |> yLine(length = segLen(seg03))
  |> arc(angleStart = 180, angleEnd = 90, radius = 10 + templateGap)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()

// Extrude the first sketch
extrude001 = extrude(sketch001, length = 5)

// Create the second sketch
sketch002 = startSketchOn(extrude001, face = START)
  |> startProfile(at = [distanceToInsideEdge, 0])
  |> angledLine(angle = 180, length = templateThickness, tag = $rectangleSegmentA002)
  |> angledLine(angle = segAng(rectangleSegmentA002) + 90, length = templateThickness, tag = $rectangleSegmentB002)
  |> angledLine(angle = segAng(rectangleSegmentA002), length = -segLen(rectangleSegmentA002), tag = $rectangleSegmentC002)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()

// Extrude the second sketch
extrude002 = extrude(sketch002, length = 13)

// Create the third sketch
sketch003 = startSketchOn(extrude001, face = START)
  |> startProfile(at = [-distanceToInsideEdge, 0])
  |> angledLine(angle = 0, length = templateThickness, tag = $rectangleSegmentA001)
  |> angledLine(angle = segAng(rectangleSegmentA001) - 90, length = templateThickness, tag = $rectangleSegmentB001)
  |> angledLine(angle = segAng(rectangleSegmentA001), length = -segLen(rectangleSegmentA001), tag = $rectangleSegmentC001)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()

// Extrude the third sketch
extrude003 = extrude(sketch003, length = 13)

// Create the fourth sketch
sketch004 = startSketchOn(extrude002, face = END)
  |> startProfile(at = [-distanceToInsideEdge, 0])
  |> angledLine(angle = 0, length = distanceToInsideEdge * 2, tag = $rectangleSegmentA003)
  |> angledLine(angle = segAng(rectangleSegmentA003) - 90, length = templateThickness, tag = $rectangleSegmentB003)
  |> angledLine(angle = segAng(rectangleSegmentA003), length = -segLen(rectangleSegmentA003), tag = $rectangleSegmentC003)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()

// Extrude the fourth sketch
extrude004 = extrude(sketch004, length = 4)
