"""The board profile, and the holes that are not pads.
A JITX `Cutout` outside a pad is a physical hole in the board. A circular one
becomes a non-plated mechanical drill hit, which is what a fabricator expects
for a mounting hole; any other shape becomes an inner contour of the profile.
The profile file is stroked, not filled: it images the board edge rather than
the board. `Profile,NP` throughout, since JITX has no edge-plating concept.
"""
from __future__ import annotations
from collections.abc import Sequence
from dataclasses import dataclass
from logging import getLogger
from jitx import Cutout, Via
from jitx.landpattern import Pad
from jitx.run.runtime import RuntimeDesign
from jitx.shapes.primitive import Circle
from jitx.transform import IDENTITY
from ..format.apertures import circle_aperture
from ..format.model import ArcObj, Draw, FilePolarity, GerberFile, Profile, attr
from . import geometry
from .padstacks import NpthHole
logger = getLogger(__name__)
PROFILE_WIDTH = 0.05
"""The stroke width the reference export uses for an edge cut, `%ADD10C,
0.050000*%`. A zero-size aperture is only ever the unused current aperture
for a region, never a stroke."""
PROFILE = attr(".AperFunction", "Profile")
[docs]
@dataclass
class Outline:
"""The profile file and the board holes that are drilled instead."""
file: GerberFile
npth: list[NpthHole]
bounds: tuple[float, float, float, float]
[docs]
def build(design: RuntimeDesign) -> Outline:
"""The profile file, the non-plated board holes, and the board extent."""
board = design.root.board.shape
islands = geometry.to_islands(board)
if len(islands) != 1:
logger.warning(
"Board outline has %d islands; a board profile expects exactly one",
len(islands),
)
file = GerberFile(Profile(), FilePolarity.POSITIVE)
aperture = circle_aperture(PROFILE_WIDTH, attrs=(PROFILE,))
strokes: list[Draw | ArcObj] = []
for island in islands:
strokes += geometry.stroke_contour(island.outer, aperture)
for hole in island.holes:
strokes += geometry.stroke_contour(hole, aperture)
npth: list[NpthHole] = []
for trace, cutout in design.query(Cutout, opaque=Via | Pad):
prim = cutout.shape.to_primitive()
place = geometry.Placer((trace.transform or IDENTITY) * prim.transform)
if isinstance(prim.geometry, Circle):
x, y = place.point(0.0, 0.0)
npth.append(NpthHole(x, y, place.length(2 * prim.geometry.radius)))
continue
for island in geometry.to_islands(cutout.shape, trace.transform):
# the cutout's own outer ring is a hole in the board, so it is
# wound as one; anything nested inside it is board material the
# profile has no way to describe
strokes += geometry.stroke_contour(geometry.reverse(island.outer), aperture)
if island.holes:
logger.warning(
"Ignoring %d nested holes inside a board cutout", len(island.holes)
)
for stroke in strokes:
file.add(stroke)
return Outline(file, npth, geometry.to_shapely(board).bounds)
[docs]
def add_cutouts(outline: Outline, islands: Sequence[geometry.Island]) -> None:
"""Stroke general-shaped pad cutouts into the profile.
They arrive after the profile is built because they are found by the pad
walk, which needs the layer model and the component index the profile
does not. Same treatment as a board cutout of a general shape: the
cutout's outer ring is a hole in the board, so it is wound as one.
"""
aperture = circle_aperture(PROFILE_WIDTH, attrs=(PROFILE,))
for island in islands:
for stroke in geometry.stroke_contour(geometry.reverse(island.outer), aperture):
outline.file.add(stroke)
if island.holes:
logger.warning(
"Ignoring %d nested holes inside a pad cutout", len(island.holes)
)