// A mounting bracket for the Focusrite Scarlett Solo audio interface
// This is a bracket that holds an audio device underneath a desk or shelf. The audio device has dimensions of 144mm wide, 80mm length and 45mm depth with fillets of 6mm. This mounting bracket is designed to be 3D printed with PLA material


// define constants in mm
const radius = 6.0
const width = 144.0
const length = 80.0
const depth = 45.0
const thk = 4
const holeDiam = 5
const tabLength = 25
const tabWidth = 12
const tabThk = 4

// define a rectangular shape func
fn rectShape = (pos, w, l) => {
  const rr = startSketchOn('xy')
    |> startProfileAt([pos[0] - (w / 2), pos[1] - (l / 2)], %)
    |> lineTo([pos[0] + w / 2, pos[1] - (l / 2)], %, $edge01)
    |> lineTo([pos[0] + w / 2, pos[1] + l / 2], %, $edge02)
    |> lineTo([pos[0] - (w / 2), pos[1] + l / 2], %, $edge03)
    |> close(%, $edge04)
  return rr
}

// define the bracket plane
const bracketPlane = {
  plane: {
    origin: { x: 0, y: length / 2 + thk, z: 0 },
    x_axis: { x: 1, y: 0, z: 0 },
    y_axis: { x: 0, y: 0, z: 1 },
    z_axis: { x: 0, y: -1, z: 0 }
  }
}

// build the bracket sketch around the body
fn bracketSketch = (w, d, t) => {
  const s = startSketchOn(bracketPlane)
    |> startProfileAt([-w / 2 - t, d + t], %)
    |> lineTo([-w / 2 - t, -t], %, $edge1)
    |> lineTo([w / 2 + t, -t], %, $edge2)
    |> lineTo([w / 2 + t, d + t], %, $edge3)
    |> lineTo([w / 2, d + t], %, $edge4)
    |> lineTo([w / 2, 0], %, $edge5)
    |> lineTo([-w / 2, 0], %, $edge6)
    |> lineTo([-w / 2, d + t], %, $edge7)
    |> close(%, $edge8)
    |> extrude(length + 2 * thk, %)
  return s
}

// build the body of the bracket
const bs = bracketSketch(width, depth, thk)
const bracketBody = bs
  |> fillet({
       radius: radius,
       tags: [
         getNextAdjacentEdge(bs.sketchGroup.tags.edge7),
         getNextAdjacentEdge(bs.sketchGroup.tags.edge2),
         getNextAdjacentEdge(bs.sketchGroup.tags.edge3),
         getNextAdjacentEdge(bs.sketchGroup.tags.edge6)
       ]
     }, %)


// sketch on the face
const retBack = startSketchOn(bs, bs.sketchGroup.tags.edge4)
  |> startProfileAt([length / 2 + thk, 0], %)
  |> line([0, thk], %)
  |> line([-thk, 0], %)
  |> line([0, -thk], %)
  |> close(%)
  |> extrude(width - 40, %)
