"""Serialize a `DrillFile` as a Gerber image.
Spec 6.6 recommends Gerber over any NC format for drill and rout data: the
copper is already Gerber, mixing formats risks registration errors, and a
Gerber drill file carries attributes natively instead of smuggling them
through comments the way XNC has to.
A drill file is an ordinary Gerber image under the restrictions 6.6 states.
Only circular apertures may appear, and only as a flash for a hole or a
non-closed chain of draws and arcs for a slot. The image is the material to
remove and the diameter is the finished size after plating, which is exactly
what a `Tool` already means, so nothing is converted here beyond the object
types. Drill files must be positive.
This writer therefore builds a `GerberFile` and hands it to the ordinary
emitter rather than reimplementing coordinates, apertures or the attribute
state machine. Every property the Gerber emitter already guarantees - D-code
assignment, the 1 nm grid, arc splitting over 180 degrees, attribute diffing -
holds here for free and cannot drift away from the copper files.
"""
from __future__ import annotations
from .apertures import circle_aperture
from .emit import emit_gerber
from .model import (
Aperture,
ArcObj,
Draw,
DrillFile,
FileMeta,
FilePolarity,
Flash,
GerberFile,
Seg,
Slot,
Tool,
)
[docs]
def to_gerber(file: DrillFile) -> GerberFile:
"""The Gerber image of a drill file, hits first and then rout paths.
Object order follows `emit_xnc` so the two formats produce the same
image in the same sequence and a golden test can compare them.
"""
out = GerberFile(file.function, FilePolarity.POSITIVE)
for hit in file.hits:
out.add(Flash(hit.x, hit.y, tool_aperture(hit.tool), hit.attrs))
for slot in file.slots:
_slot(out, slot)
return out
def _slot(out: GerberFile, slot: Slot) -> None:
"""One rout path as a chain of draws and arcs of the tool's aperture."""
aperture = tool_aperture(slot.tool)
here = slot.start
for segment in slot.segments:
if isinstance(segment, Seg):
out.add(Draw(here[0], here[1], segment.x, segment.y, aperture, slot.attrs))
else:
out.add(
ArcObj(
here[0],
here[1],
segment.x,
segment.y,
segment.cx,
segment.cy,
segment.ccw,
aperture,
slot.attrs,
)
)
here = (segment.x, segment.y)
[docs]
def emit_drill_gerber(file: DrillFile, meta: FileMeta) -> str:
"""The complete text of one drill file in Gerber format, `M02*` included."""
return emit_gerber(to_gerber(file), meta)