// Sheet Metal Bracket
// A component typically made from flat sheet metal through various manufacturing processes such as bending, punching, cutting, and forming. These brackets are used to support, attach, or mount other hardware components, often providing a structural or functional base for assembly.

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

// Input bolt pattern dimensions to mount the bracket
mountingBoltDiameter = 1 / 4
mountingBoltPatternX = 7
mountingBoltPatternY = 4

// Input bolt pattern dimensions to mount a component to the bracket
componentBoltDiameter = 3 / 16
componentBoltPatternX = 2
componentBoltPatternY = 3

// Define bracket parameters such as sheet metal thickness, bend radius, flange length, etc.
hatHeight = 2.5
bendAngle = 75
thickness = 0.125
interiorBendRadius = 0.125

// Calculate remaining parameters
exteriorBendRadius = interiorBendRadius + thickness
overhang = 3 * mountingBoltDiameter
flangeLength = 6 * mountingBoltDiameter
flangeExtrusion = if mountingBoltPatternY > componentBoltPatternY {
  mountingBoltPatternY + overhang * 2
} else {
  componentBoltPatternY + overhang * 2
}

// Draw the extrusion profile of the sheet metal bracket
bracketProfile = startSketchOn(XZ)
  |> startProfile(at = [
       -mountingBoltPatternX / 2 - overhang,
       0
     ])
  |> xLine(length = flangeLength)
  |> tangentialArc(radius = exteriorBendRadius, angle = bendAngle)
  |> angledLine(angle = bendAngle, endAbsoluteY = hatHeight - thickness, tag = $seg01)
  |> tangentialArc(radius = interiorBendRadius, angle = -bendAngle)
  |> xLine(endAbsolute = 0, tag = $seg02)
  |> xLine(length = segLen(seg02))
  |> tangentialArc(radius = interiorBendRadius, angle = -bendAngle)
  |> angledLine(angle = -bendAngle, length = segLen(seg01))
  |> tangentialArc(radius = exteriorBendRadius, angle = bendAngle)
  |> xLine(length = flangeLength)
  |> yLine(length = thickness, tag = $seg03)
  |> xLine(length = -flangeLength, tag = $seg04)
  |> tangentialArc(radius = interiorBendRadius, angle = -bendAngle)
  |> angledLine(angle = 180 - bendAngle, length = segLen(seg01))
  |> tangentialArc(radius = exteriorBendRadius, angle = bendAngle)
  |> xLine(endAbsolute = 0, tag = $seg05)
  |> xLine(length = -segLen(seg05))
  |> tangentialArc(radius = exteriorBendRadius, angle = bendAngle)
  |> angledLine(angle = bendAngle - 180, length = segLen(seg01))
  |> tangentialArc(radius = interiorBendRadius, angle = -bendAngle)
  |> xLine(length = -flangeLength, tag = $seg06)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)], tag = $seg07)
  |> close()
  |> extrude(length = flangeExtrusion)
  |> fillet(
       radius = flangeLength / 3,
       tags = [
         seg03,
         getOppositeEdge(seg03),
         seg07,
         getOppositeEdge(seg07)
       ],
     )

// Cut the bolt pattern in the left base flange
leftFlangeBoltPattern = startSketchOn(bracketProfile, face = seg04)
  |> circle(center = [-mountingBoltPatternX / 2, overhang], radius = mountingBoltDiameter / 2)
  |> patternLinear2d(
       %,
       instances = 2,
       distance = mountingBoltPatternY,
       axis = [0, 1],
     )
  |> extrude(length = -thickness)

// Cut the bolt pattern in the right base flange
rightFlangeBoltPattern = startSketchOn(bracketProfile, face = seg06)
  |> circle(center = [mountingBoltPatternX / 2, overhang], radius = mountingBoltDiameter / 2)
  |> patternLinear2d(
       %,
       instances = 2,
       distance = mountingBoltPatternY,
       axis = [0, 1],
     )
  |> extrude(length = -thickness)

// Provision the top flange with holes to mount an object
topFlangeBoltPattern = startSketchOn(bracketProfile, face = seg05)
  |> circle(
       center = [
         -componentBoltPatternX / 2,
         -componentBoltPatternY / 2 + flangeExtrusion / 2
       ],
       radius = componentBoltDiameter / 2,
     )
  |> patternLinear2d(
       %,
       instances = 2,
       distance = componentBoltPatternX,
       axis = [1, 0],
     )
  |> patternLinear2d(
       %,
       instances = 2,
       distance = componentBoltPatternY,
       axis = [0, 1],
     )
  |> extrude(length = -thickness)

// Place a hole at the center of the component bolt pattern to reduce mass
centeredHole = startSketchOn(bracketProfile, face = seg05)
  |> circle(center = [0, flangeExtrusion / 2], radius = 0.75)
  |> extrude(length = -thickness)
