"""Draw the 66 x 53 mm outline and put every part of the breakout board in its place.

The lesson's placement script with the whole table: all 24 parts of atmega328p_breakout.py, each where
the first lesson's board has it, so the result is that board. Checked in KiCad 10.0.8.
Give it a board that already holds the footprints (PCB Editor: File > Import > Netlist..., then save).
Run:  python place_board.py atmega328p_breakout.kicad_pcb
      (flatpak: flatpak run --command=python3 org.kicad.KiCad place_board.py atmega328p_breakout.kicad_pcb)
"""
import sys
import pcbnew

src = sys.argv[1] if len(sys.argv) > 1 else "atmega328p_breakout.kicad_pcb"
board = pcbnew.LoadBoard(src)

outline = pcbnew.PCB_SHAPE(board)
outline.SetShape(pcbnew.SHAPE_T_RECTANGLE)
outline.SetStart(pcbnew.VECTOR2I_MM(0, 0))
outline.SetEnd(pcbnew.VECTOR2I_MM(66, 53))
outline.SetLayer(pcbnew.Edge_Cuts)
outline.SetWidth(pcbnew.FromMM(0.1))
board.Add(outline)

# (x mm, y mm, rotation in degrees); y grows downward; a footprint's origin is its pin 1.
# Every 2.54 mm pin lands on a 0.254 mm grid, so tracks between pins stay on the grid too.
placement = {
    # connectors along the edges
    'J4': (17.78, 2.54, 90),     # 14-way socket, top edge (analog pins, AREF, power, reset)
    'J3': (22.86, 50.546, 90),   # 14-way socket, bottom edge (digital pins)
    'J1': (14.732, 14.478, 0),   # barrel jack, plug from the left edge
    'J6': (5.842, 25.4, -90),    # screw terminal, left edge
    'J2': (62.484, 17.018, 180), # ISP header, top right
    'J5': (63.754, 37.592, 180), # serial (FTDI) header, right edge, pin 6 (DTR) at the top
    # the chip and what sits close to its pins
    'U1': (21.59, 29.21, 90),    # ATmega328P in its socket, pins 1 to 14 face J3
    'C3': (36.83, 33.274, 0),    # VCC decoupling, under pins 7 and 8
    'C4': (40.64, 18.288, 90),   # AVCC decoupling, over pin 20
    'C7': (37.338, 18.288, 90),  # AREF capacitor, over pin 21
    'Y1': (41.91, 37.846, 0),    # crystal, under pins 9 and 10
    'C1': (41.91, 42.418, 270),  # crystal load capacitors
    'C2': (46.99, 42.418, 270),
    # supply: jack or terminal, diode, regulator, its capacitors
    'D3': (19.05, 50.292, 180),  # reverse-polarity diode, cathode at U2
    'U2': (19.304, 45.72, 90),   # LM7805 standing up
    'C5': (11.176, 45.72, 0),    # regulator input capacitor
    'C6': (19.304, 35.306, 90),  # regulator output capacitor
    # reset, LEDs, auto-reset capacitor
    'SW1': (7.62, 36.322, 0),    # reset button, lower left
    'R1': (41.91, 6.35, 0),      # reset pull-up, under J4 pin 9 (RESET)
    'C8': (59.436, 21.082, 0),   # DTR to RESET capacitor
    'R3': (43.942, 9.906, 0),    # user LED resistor
    'D2': (56.642, 9.906, 180),  # user LED (red)
    'R2': (44.196, 15.24, 0),    # power LED resistor
    'D1': (56.642, 15.24, 180),  # power LED (green)
}
missing = [ref for ref in placement if board.FindFootprintByReference(ref) is None]
if missing:
    sys.exit(f"not on the board: {missing}. Import the netlist first.")
for ref, (x, y, angle) in placement.items():
    fp = board.FindFootprintByReference(ref)
    fp.SetPosition(pcbnew.VECTOR2I_MM(x, y))
    fp.SetOrientationDegrees(angle)
left = [fp.GetReference() for fp in board.GetFootprints() if fp.GetReference() not in placement]
if left:
    print("not in the table, still where the import put them:", left)

out = src.replace(".kicad_pcb", "_placed.kicad_pcb")
board.Save(out)
print(f"{len(placement)} parts placed, saved {out}. Route the tracks in the PCB Editor.")
