// 80/20 Rail
// An 80/20 extruded aluminum linear rail. T-slot profile adjustable by profile height, rail length, and origin position

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

// Create a function to make the 80-20 rail
fn rail8020(originStart, railHeight, railLength) {
  // Sketch side 1 of profile
  sketch001 = startSketchOn(-XZ)
    |> startProfile(at = [
         originStart[0],
         0.1 * railHeight + originStart[1]
       ])
    |> arc(angleStart = 180, angleEnd = 270, radius = 0.1 * railHeight)
    |> arc(angleStart = 180, angleEnd = 0, radius = 0.072 / 4 * railHeight)
    |> xLine(length = 0.1 * railHeight)
    |> arc(angleStart = 180, angleEnd = 0, radius = 0.072 / 4 * railHeight)
    |> xLine(length = 0.06 * railHeight, tag = $edge1)
    |> yLine(length = 0.087 * railHeight, tag = $edge2)
    |> xLine(length = -0.183 * railHeight, tag = $edge3)
    |> angledLine(angle = 45, endAbsoluteY = (1 - 0.356) / 2 * railHeight + originStart[1], tag = $edge4)
    |> xLine(length = 0.232 * railHeight, tag = $edge5)
    |> angledLine(angle = -45, endAbsoluteY = 0.087 * railHeight + originStart[1], tag = $edge6)
    |> xLine(length = -0.183 * railHeight, tag = $edge7)
    |> yLine(length = -0.087 * railHeight, tag = $edge8)
    |> xLine(length = 0.06 * railHeight)
    |> arc(angleStart = 180, angleEnd = 0, radius = 0.072 / 4 * railHeight)
    |> xLine(length = 0.1 * railHeight)
    |> arc(angleStart = 180, angleEnd = 0, radius = 0.072 / 4 * railHeight)
    |> arc(angleStart = -90, angleEnd = 0, radius = 0.1 * railHeight)

    // Sketch side 2 of profile
    |> arc(angleStart = 270, angleEnd = 90, radius = 0.072 / 4 * railHeight)
    |> yLine(length = 0.1 * railHeight)
    |> arc(angleStart = 270, angleEnd = 90, radius = 0.072 / 4 * railHeight)
    |> yLine(length = 0.06 * railHeight, tag = $edge9)
    |> xLine(length = -0.087 * railHeight, tag = $edge10)
    |> yLine(length = -0.183 * railHeight, tag = $edge11) // edge11
    |> angledLine(angle = 135, endAbsoluteX = ((1 - 0.356) / 2 + 0.356) * railHeight + originStart[0], tag = $edge12) // edge12
    |> yLine(length = 0.232 * railHeight, tag = $edge13) // 13
    |> angledLine(angle = 45, endAbsoluteX = (1 - 0.087) * railHeight + originStart[0], tag = $edge14) // 14
    |> yLine(length = -0.183 * railHeight, tag = $edge15) // 15
    |> xLine(length = 0.087 * railHeight, tag = $edge16)
    |> yLine(length = 0.06 * railHeight)
    |> arc(angleStart = 270, angleEnd = 90, radius = 0.072 / 4 * railHeight)
    |> yLine(length = 0.1 * railHeight)
    |> arc(angleStart = 270, angleEnd = 90, radius = 0.072 / 4 * railHeight)

    // Sketch side 3 of profile
    |> arc(angleStart = 0, angleEnd = 90, radius = 0.1 * railHeight)
    |> arc(angleStart = 0, angleEnd = -180, radius = 0.072 / 4 * railHeight)
    |> xLine(length = -0.1 * railHeight)
    |> arc(angleStart = 0, angleEnd = -180, radius = 0.072 / 4 * railHeight)
    |> xLine(length = -0.06 * railHeight, tag = $edge17)
    |> yLine(length = -0.087 * railHeight, tag = $edge18)
    |> xLine(length = 0.183 * railHeight, tag = $edge19)
    |> angledLine(angle = 45, endAbsoluteY = ((1 - 0.356) / 2 + 0.356) * railHeight + originStart[1], tag = $edge20)
    |> xLine(length = -0.232 * railHeight, tag = $edge21)
    |> angledLine(angle = 135, endAbsoluteY = (1 - 0.087) * railHeight + originStart[1], tag = $edge22)
    |> xLine(length = 0.183 * railHeight, tag = $edge23)
    |> yLine(length = 0.087 * railHeight, tag = $edge24)
    |> xLine(length = -0.06 * railHeight)
    |> arc(angleStart = 0, angleEnd = -180, radius = 0.072 / 4 * railHeight)
    |> xLine(length = -0.1 * railHeight)
    |> arc(angleStart = 0, angleEnd = -180, radius = 0.072 / 4 * railHeight)
    |> arc(angleStart = 90, angleEnd = 180, radius = 0.1 * railHeight)

    // Sketch side 4 of profile
    |> arc(angleStart = 90, angleEnd = -90, radius = 0.072 / 4 * railHeight)
    |> yLine(length = -0.1 * railHeight)
    |> arc(angleStart = 90, angleEnd = -90, radius = 0.072 / 4 * railHeight)
    |> yLine(length = -0.06 * railHeight, tag = $edge25)
    |> xLine(length = 0.087 * railHeight, tag = $edge26)
    |> yLine(length = 0.183 * railHeight, tag = $edge27)
    |> angledLine(angle = 135, endAbsoluteX = (1 - 0.356) / 2 * railHeight + originStart[0], tag = $edge28)
    |> yLine(length = -0.232 * railHeight, tag = $edge29)
    |> angledLine(angle = 45, endAbsoluteX = 0.087 * railHeight + originStart[0], tag = $edge30)
    |> yLine(length = 0.183 * railHeight, tag = $edge31)
    |> xLine(length = -0.087 * railHeight, tag = $edge32)
    |> yLine(length = -0.06 * railHeight)
    |> arc(angleStart = 90, angleEnd = -90, radius = 0.072 / 4 * railHeight)
    |> yLine(length = -0.1 * railHeight)
    |> arc(angleStart = 90, angleEnd = -90, radius = 0.072 / 4 * railHeight)
    |> close()

    // Sketch center hole of profile
    |> subtract2d(tool = circle(
         center = [
           .5 * railHeight + originStart[0],
           .5 * railHeight + originStart[1]
         ],
         radius = .205 * railHeight / 2,
       ))
    |> extrude(length = railLength)
    |> fillet(
         radius = 0.06,
         tags = [
           getNextAdjacentEdge(edge3),
           getNextAdjacentEdge(edge4),
           getNextAdjacentEdge(edge5),
           getNextAdjacentEdge(edge6),
           getNextAdjacentEdge(edge11),
           getNextAdjacentEdge(edge12),
           getNextAdjacentEdge(edge13),
           getNextAdjacentEdge(edge14),
           getNextAdjacentEdge(edge19),
           getNextAdjacentEdge(edge20),
           getNextAdjacentEdge(edge21),
           getNextAdjacentEdge(edge22),
           getNextAdjacentEdge(edge27),
           getNextAdjacentEdge(edge28),
           getNextAdjacentEdge(edge29),
           getNextAdjacentEdge(edge30)
         ],
       )
    |> fillet(
         radius = 0.03,
         tags = [
           getNextAdjacentEdge(edge1),
           getNextAdjacentEdge(edge2),
           getNextAdjacentEdge(edge7),
           getNextAdjacentEdge(edge8),
           getNextAdjacentEdge(edge9),
           getNextAdjacentEdge(edge10),
           getNextAdjacentEdge(edge15),
           getNextAdjacentEdge(edge16),
           getNextAdjacentEdge(edge17),
           getNextAdjacentEdge(edge18),
           getNextAdjacentEdge(edge23),
           getNextAdjacentEdge(edge24),
           getNextAdjacentEdge(edge25),
           getNextAdjacentEdge(edge26),
           getNextAdjacentEdge(edge31),
           getNextAdjacentEdge(edge32)
         ],
       )
  return sketch001
}

// Generate one adjustable rail of 80/20
rail8020(originStart = [0, 0], railHeight = 1.5, railLength = 48)
