Source code for jitxlib.jlcpcb.export.gerber

"""The relay into `jitxlib-gerber`.

The only module that imports `jitxlib.gerber`, and it does so inside the call
rather than at module level: the plugin module is imported whenever anything
enumerates jitx plugins, and a top-level chain from there would make importing
a stackup expensive.

The registry (`jitx.plugin.Export.get("gerber")`) is deliberately not used.
That is introspection machinery for the CLI, which derives its flags from the
signature of `export()`, so everything reachable that way is a flat scalar. A
direct import takes a configured naming strategy, a drill format and the
compatibility adjustments, and gets the written file set back.
"""

from __future__ import annotations

from dataclasses import dataclass, field
from pathlib import Path
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
    from jitx.run import RuntimeDesign


[docs] @dataclass class Board: """What the relay produced, as the packet and order sheet need it.""" archive: Path | None """The gerber zip, at the packet root.""" files: list[tuple[str, str]] = field(default_factory=list) """(file function, filename) for every gerber file written. For an archive these are the names inside it; nothing is on disk but the zip."""
[docs] def export( design: "RuntimeDesign", packet: Path, stem: str, *, overwrite: bool = False, creation_date: str | None = None, ) -> Board: """Write the gerber set as the single archive JLCPCB's form takes. Passing an output path ending in `.zip` writes exactly that archive with the files at its root and nothing beside it, which is the layout the upload form expects and what the packet wants sitting next to the CSVs. """ from jitxlib.gerber import export_gerber from jitxlib.gerber.format.compat import Compat from jitxlib.gerber.format.model import DrillFormat archive = packet / f"{stem}-gerber.zip" result = export_gerber( design, output=archive, naming="jlcpcb", overwrite=overwrite, # JLCPCB's intake wants an NC drill file, which they name .XLN and # which the jlcpcb naming scheme is written for. The gerber default # is Gerber-format drills, following the spec's own recommendation. drill_format=DrillFormat.XNC, creation_date=creation_date, # JLCPCB's parser mishandles the aperture macro rotation argument: a # capsule pad flashed through a rotated macro was imaged a quarter # turn off and shorted two planes. Baking the rotation into the macro # coordinates leaves it nothing to misread. See jitxlib-gerber # plans/capsule-two-circles-repro.md. compat=Compat(bake_macro_rotation=True), ) files = [(_render(function), Path(path).name) for function, path in result.files] return Board(archive=result.archive, files=files)
def _render(function: Any) -> str: """The file function as its `TF.FileFunction` string, for the order sheet.""" try: return function.render() except TypeError: # The job file has no TF.FileFunction; it renders as its kind. return str(getattr(function, "kind", "job"))