Source code for jitxlib.jlcpcb.export.cpl
"""The pick-and-place file (CPL) JLCPCB's placement machines read."""
from __future__ import annotations
from itertools import combinations
from logging import getLogger
from pathlib import Path
from . import _csv
from .components import Part
logger = getLogger(__name__)
HEADER = ("Designator", "Mid X", "Mid Y", "Rotation", "Layer")
MIN_SPACING = 0.2
"""JLCPCB requires at least this much between two different designators, in
millimetres, and reports closer pairs as a repeated designator."""
[docs]
def write(path: Path, parts: list[Part]) -> None:
"""Write the CPL: millimetres, counter-clockwise degrees, both sides.
Coordinates are the design origin unchanged, which is what the gerber
image uses; JLCPCB aligns the two against each other, so what matters is
that they agree rather than where the origin is.
"""
rows = [
(
part.designator,
f"{part.x:.4f}",
f"{part.y:.4f}",
f"{part.rotation:.2f}",
part.layer,
)
for part in parts
]
_csv.write(path, HEADER, rows)
check_spacing(parts)
[docs]
def check_spacing(parts: list[Part]) -> None:
"""Warn about designators closer together than JLCPCB allows.
A warning rather than an error: their rule does not say whether it applies
across sides, and two parts at the same position on opposite sides is a
normal design (a decoupling capacitor mirrored under a package).
"""
for first, second in combinations(parts, 2):
if first.side != second.side:
continue
if abs(first.x - second.x) >= MIN_SPACING:
continue
if abs(first.y - second.y) >= MIN_SPACING:
continue
distance = ((first.x - second.x) ** 2 + (first.y - second.y) ** 2) ** 0.5
if distance < MIN_SPACING:
logger.warning(
"%s and %s are %.3f mm apart, closer than JLCPCB's %.1f mm "
"minimum between designators",
first.designator,
second.designator,
distance,
MIN_SPACING,
)