"""JITX export plugin: write a JLCPCB manufacturing and assembly packet.
No `from __future__ import annotations` in this module, deliberately. The CLI
builds its command by rewriting `inspect.signature(plugin.export)`, and typer
cannot resolve string annotations; postponed annotations here fail the whole
design export command group.
"""
from logging import getLogger
from pathlib import Path
from typing import TYPE_CHECKING, override
import jitx.log
import jitx.plugin
if TYPE_CHECKING:
from jitx.run import RuntimeDesign
logger = getLogger(__name__)
def _packet(output: Path | None, design_name: str) -> Path:
"""The packet directory: `--output`, or the scrubbed design name in cwd."""
from jitxlib.gerber.format.naming import legal_stem
if output is not None:
return Path(output).absolute()
return Path.cwd() / legal_stem(design_name)
[docs]
@jitx.plugin.Export.register("jlcpcb")
class JLCPCBExport(jitx.plugin.Export):
"""Export a JLCPCB manufacturing and assembly packet."""
[docs]
@override
def preflight(
self,
*,
output: Path | None = None,
overwrite: bool = False,
assembly: bool = True,
):
# Our own arguments only. The gerber exporter validates its own, and
# the relay is a direct call rather than a plugin handoff.
if output is not None and not overwrite:
packet = Path(output).absolute()
if packet.exists() and any(packet.iterdir()):
raise ValueError(
f"Output path {packet} already exists and is not empty, "
"and overwrite was not specified"
)
[docs]
@override
def export(
self,
design: "RuntimeDesign",
*,
output: Path | None = None,
overwrite: bool = False,
assembly: bool = True,
):
from jitxlib.gerber.format.naming import legal_stem
from . import bom, cpl, gerber, lock, order, orientation
from .components import build
# Every file in the packet takes its stem from the design, including
# the ones gerber names, so a packet does not carry two different
# names for one board. Taking it from the output directory instead
# would rename the CSVs with `--output` while leaving the files inside
# the archive on the design name.
packet = _packet(output, design.name)
stem = legal_stem(design.name)
packet.mkdir(parents=True, exist_ok=True)
board = gerber.export(design, packet, stem, overwrite=overwrite)
# Resolve orientations before the walk that builds the fab files, so
# that both files and the order sheet see one settled set of values.
# The lock file is read first and written last: measuring is skipped
# entirely for parts it already knows, which is every part on every
# run after the first.
held = lock.read(lock.path())
if assembly:
measured = orientation.resolve(design, held.orientations())
if measured:
held.record(measured)
lock.write(held)
jitx.log.notice(
logger,
"Recorded %d new orientation(s) in %s",
len(measured),
held.path,
)
inventory = build(design, held.orientations())
lines: list[bom.Line] = []
if assembly:
lines = bom.write(packet / f"{stem}-bom.csv", inventory.parts)
cpl.write(packet / f"{stem}-cpl.csv", inventory.parts)
order.write(
packet / f"{stem}-order.md",
order.Sheet(
design=design.name,
inventory=inventory,
lines=lines,
board=board,
substrate=getattr(design.root, "substrate", None),
shape=getattr(getattr(design.root, "board", None), "shape", None),
),
)
jitx.log.notice(
logger,
"Wrote JLCPCB packet %s (%d placements, %d BOM lines)",
packet,
len(inventory.parts) if assembly else 0,
len(lines),
)