// J3 Robot Arm

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

import plane002, axisJ2, axisJ3C, axisJ4, axisJ2ArmLength, axisJ3CArmLength, axisJ3CArmWidth, axisJ3CArmThickness from "globals.kcl"

// Create Body of J3 Robot Arm
sketch017 = startSketchOn(plane002)
  |> startProfile(at = [
       1.75 + axisJ2ArmLength * cos(axisJ2) - (axisJ3CArmWidth / 2 * sin(axisJ3C)),
       8 + axisJ2ArmLength * sin(axisJ2) + axisJ3CArmWidth / 2 * cos(axisJ3C)
     ])
  |> arc(angleStart = 90 + axisJ3C, angleEnd = 270 + axisJ3C, radius = axisJ3CArmWidth / 2)
  |> angledLine(angle = axisJ3C, length = axisJ3CArmLength)
  |> arc(angleStart = 270 + axisJ3C, angleEnd = 90 + axisJ3C, radius = axisJ3CArmWidth / 2)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)], tag = $seg01)
  |> close()
extrude017 = extrude(sketch017, length = axisJ3CArmThickness)

sketch018 = startSketchOn(extrude017, face = END)
  |> circle(
       center = [
         1.75 + axisJ2ArmLength * cos(axisJ2),
         8 + axisJ2ArmLength * sin(axisJ2)
       ],
       radius = 3.7 / 2,
       tag = $referenceEdge6,
     )

extrude018 = extrude(sketch018, length = 0.15)
  |> fillet(radius = 0.1, tags = [getOppositeEdge(referenceEdge6)])

// Draw Bolt Pattern on J3 Robot Arm
sketch019 = startSketchOn(extrude018, face = END)
  |> circle(
       center = [
         1.75 + (axisJ2ArmLength - 1) * cos(axisJ2),
         8 + (axisJ2ArmLength - 1.5) * sin(axisJ2)
       ],
       radius = 0.2,
     )
  |> patternCircular2d(
       center = [
         1.75 + axisJ2ArmLength * cos(axisJ2),
         8 + axisJ2ArmLength * sin(axisJ2)
       ],
       instances = 8,
       arcDegrees = 360,
       rotateDuplicates = true,
     )

extrude019 = extrude(sketch019, length = 0.15)

// On the J3 Robot Arm Body, Create Mounting Clevis for Grabber Claw
sketch020 = startSketchOn(extrude017, face = START)
  |> circle(
       center = [
         -1.75 - (axisJ2ArmLength * cos(axisJ2)) - (axisJ3CArmLength * cos(axisJ3C)),
         8 + axisJ2ArmLength * sin(axisJ2) + axisJ3CArmLength * sin(axisJ3C)
       ],
       radius = axisJ3CArmWidth / 2,
     )
extrude020 = extrude(sketch020, length = -0.5)

sketch021 = startSketchOn(extrude017, face = END)
  |> circle(
       center = [
         1.75 + axisJ2ArmLength * cos(axisJ2) + axisJ3CArmLength * cos(axisJ3C),
         8 + axisJ2ArmLength * sin(axisJ2) + axisJ3CArmLength * sin(axisJ3C)
       ],
       radius = axisJ3CArmWidth / 2.01,
     )

extrude021 = extrude(sketch021, length = -0.5)

// Define Grabber Claw Constants
grabberLength = 7

sketch022 = startSketchOn(extrude021, face = START)
  |> circle(center = [0, 0], radius = 0.10)

extrude022 = extrude(sketch022, length = -0.01)

// Build Upper Claw Finger
sketch023 = startSketchOn(extrude022, face = START)
  |> startProfile(at = [
       1.75 + axisJ2ArmLength * cos(axisJ2) + axisJ3CArmLength * cos(axisJ3C),
       8 + axisJ2ArmLength * sin(axisJ2) + axisJ3CArmLength * sin(axisJ3C)
     ])
  |> angledLine(angle = axisJ3C + axisJ4 / 2, length = grabberLength / 4)
  |> arc(angleStart = 150 + axisJ3C + axisJ4 / 2, angleEnd = 30 + axisJ3C + axisJ4 / 2, radius = grabberLength / 3)
  |> angledLine(angle = axisJ3C + axisJ4 / 2, length = grabberLength / 6)
  |> angledLine(angle = axisJ3C + axisJ4 / 2 + 132, length = grabberLength / 3.5)
  |> angledLine(angle = axisJ3C + axisJ4 / 2 + 160, length = grabberLength / 3.5)
  |> angledLine(angle = axisJ3C + axisJ4 / 2 + 200, length = grabberLength / 3)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()

extrude023 = extrude(sketch023, length = -1.5)

// Build Lower Claw Finger
sketch024 = startSketchOn(extrude022, face = START)
  |> startProfile(at = [
       1.75 + axisJ2ArmLength * cos(axisJ2) + axisJ3CArmLength * cos(axisJ3C),
       8 + axisJ2ArmLength * sin(axisJ2) + axisJ3CArmLength * sin(axisJ3C)
     ])
  |> angledLine(angle = axisJ3C - (axisJ4 / 2), length = grabberLength / 4)
  |> arc(angleStart = 210 + axisJ3C - (axisJ4 / 2), angleEnd = 330 + axisJ3C - (axisJ4 / 2), radius = grabberLength / 3)
  |> angledLine(angle = axisJ3C - (axisJ4 / 2), length = grabberLength / 6)
  |> angledLine(angle = axisJ3C - (axisJ4 / 2) - 132, length = grabberLength / 3.5)
  |> angledLine(angle = axisJ3C - (axisJ4 / 2) - 160, length = grabberLength / 3.5)
  |> angledLine(angle = axisJ3C - (axisJ4 / 2) - 200, length = grabberLength / 3)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()

extrude(sketch024, length = -1.5)
  |> appearance(color = "#6572b3", metalness = 90, roughness = 90)
