Fix ExcellonFile.to_gerber and add a unit test

This commit is contained in:
jaseg 2026-03-08 15:12:58 +01:00
parent 8de776616c
commit 575046a60c
2 changed files with 30 additions and 4 deletions

View file

@ -30,9 +30,10 @@ from pathlib import Path
from .cam import CamFile, FileSettings
from .graphic_objects import Flash, Line, Arc
from .apertures import ExcellonTool
from .apertures import ExcellonTool, CircleAperture
from .utils import Inch, MM, to_unit, InterpMode, RegexMatcher
class ExcellonContext:
""" Internal helper class used for tracking graphics state when writing Excellon. """
@ -268,17 +269,19 @@ class ExcellonFile(CamFile):
""" Counterpart to :py:meth:`~.rs274x.GerberFile.to_excellon`. Does nothing and returns :py:obj:`self`. """
return self
def to_gerber(self, errros='raise'):
def to_gerber(self, errors='raise'):
""" Convert this excellon file into a :py:class:`~.rs274x.GerberFile`. """
from .rs274x import GerberFile
out = GerberFile()
out.comments = self.comments
apertures = {}
for obj in self.objects:
if not (ap := apertures[obj.tool]):
ap = apertures[obj.tool] = CircleAperture(obj.tool.diameter)
if not (ap := apertures.get(obj.tool)):
ap = apertures[obj.tool] = CircleAperture(obj.tool.diameter, unit=obj.aperture.unit)
out.objects.append(dataclasses.replace(obj, aperture=ap))
return out
@property
def generator(self):

View file

@ -100,6 +100,28 @@ def test_first_level_idempotence_svg(reference, tmpfile, img_support):
assert hist[9] == 0
assert hist[3:].sum() < 5e-5*hist.size
@filter_syntax_warnings
@pytest.mark.parametrize('reference', list(REFERENCE_FILES.items()), indirect=True)
def test_gerber_conversion(reference, tmpfile, img_support):
reference, (unit_spec, _) = reference
tmp = tmpfile('Output gerber', '.gbr')
ref_svg = tmpfile('Reference SVG render', '.svg')
out_svg = tmpfile('Output SVG render', '.svg')
a = ExcellonFile.open(reference)
a.to_gerber().save(tmp)
b = GerberFile.open(tmp)
ref_svg.write_text(str(a.to_svg(fg='black', bg='white')))
out_svg.write_text(str(b.to_svg(fg='black', bg='white')))
mean, _max, hist = img_support.svg_difference(ref_svg, out_svg, diff_out=tmpfile('Difference', '.png'), background='white')
assert mean < 2e-4
assert hist[9] == 0
assert hist[3:].sum() < 2e-4*hist.size
@filter_syntax_warnings
@pytest.mark.parametrize('reference', list(REFERENCE_FILES.items()), indirect=True)
def test_idempotence(reference, tmpfile):
@ -123,6 +145,7 @@ def test_idempotence(reference, tmpfile):
assert tmp_1.read_text() == tmp_2.read_text()
@filter_syntax_warnings
@pytest.mark.parametrize('reference', list(REFERENCE_FILES.items()), indirect=True)
def test_gerber_alignment(reference, tmpfile, print_on_error):