From 7cf41c6a72e52a63b4f4d4497732a72d6623eec8 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sat, 22 Jan 2022 15:00:05 +0100 Subject: Fix a few more tests --- gerbonara/gerber/apertures.py | 17 ++++++----- gerbonara/gerber/excellon.py | 12 +++++++- gerbonara/gerber/layers.py | 8 +++--- gerbonara/gerber/tests/image_support.py | 50 ++++++++++++++++++++++----------- gerbonara/gerber/tests/test_excellon.py | 12 ++++++-- gerbonara/gerber/tests/utils.py | 2 +- gerbonara/gerber/utils.py | 3 ++ 7 files changed, 73 insertions(+), 31 deletions(-) (limited to 'gerbonara/gerber') diff --git a/gerbonara/gerber/apertures.py b/gerbonara/gerber/apertures.py index 0f723d4..b3e462c 100644 --- a/gerbonara/gerber/apertures.py +++ b/gerbonara/gerber/apertures.py @@ -96,12 +96,15 @@ class ExcellonTool(Aperture): plated : bool = None depth_offset : Length(float) = 0 + def __post_init__(self): + print('created', self) + def primitives(self, x, y, unit=None): return [ gp.Circle(x, y, self.unit.convert_to(unit, self.diameter/2)) ] def to_xnc(self, settings): - z_off = 'Z' + settings.write_excellon_value(self.depth_offset) if self.depth_offset is not None else '' - return 'C' + settings.write_excellon_value(self.diameter) + z_off + z_off = 'Z' + settings.write_excellon_value(self.depth_offset, self.unit) if self.depth_offset is not None else '' + return 'C' + settings.write_excellon_value(self.diameter, self.unit) + z_off def __eq__(self, other): if not isinstance(other, ExcellonTool): @@ -118,7 +121,7 @@ class ExcellonTool(Aperture): def __str__(self): plated = '' if self.plated is None else (' plated' if self.plated else ' non-plated') z_off = '' if self.depth_offset is None else f' z_offset={self.depth_offset}' - return f'' + return f'' def equivalent_width(self, unit=MM): return unit(self.diameter, self.unit) @@ -150,7 +153,7 @@ class CircleAperture(Aperture): return [ gp.Circle(x, y, self.unit.convert_to(unit, self.diameter/2)) ] def __str__(self): - return f'' + return f'' flash = _flash_hole @@ -191,7 +194,7 @@ class RectangleAperture(Aperture): return [ gp.Rectangle(x, y, self.unit.convert_to(unit, self.w), self.unit.convert_to(unit, self.h), rotation=self.rotation) ] def __str__(self): - return f'' + return f'' flash = _flash_hole @@ -240,7 +243,7 @@ class ObroundAperture(Aperture): return [ gp.Obround(x, y, self.unit.convert_to(unit, self.w), self.unit.convert_to(unit, self.h), rotation=self.rotation) ] def __str__(self): - return f'' + return f'' flash = _flash_hole @@ -289,7 +292,7 @@ class PolygonAperture(Aperture): return [ gp.RegularPolygon(x, y, self.unit.convert_to(unit, self.diameter)/2, self.n_vertices, rotation=self.rotation) ] def __str__(self): - return f'<{self.n_vertices}-gon aperture d={self.diameter:.3}' + return f'<{self.n_vertices}-gon aperture d={self.diameter:.3} [{self.unit}]>' def dilated(self, offset, unit=MM): offset = self.unit(offset, unit) diff --git a/gerbonara/gerber/excellon.py b/gerbonara/gerber/excellon.py index a42a667..4887245 100755 --- a/gerbonara/gerber/excellon.py +++ b/gerbonara/gerber/excellon.py @@ -194,12 +194,20 @@ class ExcellonFile(CamFile): tool_map = { id(obj.tool): obj.tool for obj in self.objects } tools = sorted(tool_map.items(), key=lambda id_tool: (id_tool[1].plated, id_tool[1].diameter, id_tool[1].depth_offset)) tools = { tool_id: index for index, (tool_id, _tool) in enumerate(tools, start=1) } + # FIXME dedup tools + + mixed_plating = (len({ tool.plated for tool in tool_map.values() }) > 1) + if mixed_plating: + warnings.warn('Multiple plating values in same file. Will use non-standard Altium comment syntax to indicate hole plating.') if tools and max(tools.values()) >= 100: warnings.warn('More than 99 tools defined. Some programs may not like three-digit tool indices.', SyntaxWarning) for tool_id, index in tools.items(): - yield f'T{index:02d}' + tool_map[tool_id].to_xnc(settings) + tool = tool_map[tool_id] + if mixed_plating: + yield ';TYPE=PLATED' if tool.plated else ';TYPE=NON_PLATED' + yield f'T{index:02d}' + tool.to_xnc(settings) yield '%' @@ -325,6 +333,8 @@ class ExcellonParser(object): # from the excellon file, the caller must pass in an already filled-out FileSettings object. if settings is None: self.settings = FileSettings(number_format=(None, None)) + else: + self.settings = settings self.program_state = None self.interpolation_mode = InterpMode.LINEAR self.tools = {} diff --git a/gerbonara/gerber/layers.py b/gerbonara/gerber/layers.py index d428722..7c9ae8c 100644 --- a/gerbonara/gerber/layers.py +++ b/gerbonara/gerber/layers.py @@ -114,7 +114,7 @@ def layername_autoguesser(fn): elif re.match('(solder)?mask', fn): use = 'mask' - elif (m := re.match('(la?y?e?r?|in(ner)?)\W*(?P[0-9]+)', fn)): + elif (m := re.match(f'(la?y?e?r?|in(ner)?)\W*(?P[0-9]+)', fn)): use = 'copper' side = f'inner_{m["num"]:02d}' @@ -129,7 +129,7 @@ def layername_autoguesser(fn): use = 'drill' side = 'unknown' - if re.match('np(th)?|(non|un)\W*plated|(non|un)\Wgalv', fn): + if re.match(r'np(th)?|(non|un)\W*plated|(non|un)\Wgalv', fn): side = 'nonplated' elif re.match('pth|plated|galv', fn): @@ -216,8 +216,8 @@ class LayerStack: 'tracker and if possible please provide these input files for reference.') board_name = common_prefix([f.name for f in filemap.values()]) - board_name = re.subs('^\W+', '', board_name) - board_name = re.subs('\W+$', '', board_name) + board_name = re.subs(r'^\W+', '', board_name) + board_name = re.subs(r'\W+$', '', board_name) return kls(layers, drill_layers, board_name=board_name) def __init__(self, graphic_layers, drill_layers, board_name=None): diff --git a/gerbonara/gerber/tests/image_support.py b/gerbonara/gerber/tests/image_support.py index 36343d9..662dbed 100644 --- a/gerbonara/gerber/tests/image_support.py +++ b/gerbonara/gerber/tests/image_support.py @@ -1,6 +1,7 @@ import subprocess from pathlib import Path import tempfile +import textwrap import os from functools import total_ordering import shutil @@ -65,16 +66,33 @@ def svg_to_png(in_svg, out_png, dpi=100, bg='black'): to_gerbv_svg_units = lambda val, unit='mm': val*72 if unit == 'inch' else val/25.4*72 -def gerbv_export(in_gbr, out_svg, format='svg', origin=(0, 0), size=(6, 6), fg='#ffffff', bg='#000000'): - x, y = origin - w, h = size - cmd = ['gerbv', '-x', format, - '--border=0', - f'--origin={x:.6f}x{y:.6f}', f'--window_inch={w:.6f}x{h:.6f}', - f'--foreground={fg}', - f'--background={bg}', - '-o', str(out_svg), str(in_gbr)] - subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) +def gerbv_export(in_gbr, out_svg, export_format='svg', origin=(0, 0), size=(6, 6), fg='#ffffff', bg='#000000', override_unit_spec=None): + with tempfile.NamedTemporaryFile('w') as f: + if override_unit_spec: + units, zeros, digits = override_unit_spec + units = 0 if units == 'inch' else 1 + zeros = {None: 0, 'leading': 1, 'trailing': 2}[zeros] + unit_spec = textwrap.dedent(f'''(cons 'attribs (list + (list 'autodetect 'Boolean 0) + (list 'zero_suppression 'Enum {zeros}) + (list 'units 'Enum {units}) + (list 'digits 'Integer {digits}) + ))''') + else: + unit_spec = '' + + f.write(f'''(gerbv-file-version! "2.0A")(define-layer! 0 (cons 'filename "{in_gbr}"){unit_spec})''') + f.flush() + + x, y = origin + w, h = size + cmd = ['gerbv', '-x', export_format, + '--border=0', + f'--origin={x:.6f}x{y:.6f}', f'--window_inch={w:.6f}x{h:.6f}', + f'--foreground={fg}', + f'--background={bg}', + '-o', str(out_svg), '-p', f.name] + subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) @contextmanager def svg_soup(filename): @@ -101,12 +119,12 @@ def cleanup_gerbv_svg(filename): with svg_soup(filename) as soup: cleanup_clips(soup) -def gerber_difference(reference, actual, diff_out=None, svg_transform=None, size=(10,10)): +def gerber_difference(reference, actual, diff_out=None, svg_transform=None, size=(10,10), ref_unit_spec=None): with tempfile.NamedTemporaryFile(suffix='.svg') as act_svg,\ tempfile.NamedTemporaryFile(suffix='.svg') as ref_svg: - gerbv_export(reference, ref_svg.name, size=size, format='svg') - gerbv_export(actual, act_svg.name, size=size, format='svg') + gerbv_export(reference, ref_svg.name, size=size, export_format='svg', override_unit_spec=ref_unit_spec) + gerbv_export(actual, act_svg.name, size=size, export_format='svg') with svg_soup(ref_svg.name) as soup: if svg_transform is not None: @@ -123,9 +141,9 @@ def gerber_difference_merge(ref1, ref2, actual, diff_out=None, composite_out=Non tempfile.NamedTemporaryFile(suffix='.svg') as ref1_svg,\ tempfile.NamedTemporaryFile(suffix='.svg') as ref2_svg: - gerbv_export(ref1, ref1_svg.name, size=size, format='svg') - gerbv_export(ref2, ref2_svg.name, size=size, format='svg') - gerbv_export(actual, act_svg.name, size=size, format='svg') + gerbv_export(ref1, ref1_svg.name, size=size, export_format='svg') + gerbv_export(ref2, ref2_svg.name, size=size, export_format='svg') + gerbv_export(actual, act_svg.name, size=size, export_format='svg') with svg_soup(ref1_svg.name) as soup1: if svg_transform1 is not None: diff --git a/gerbonara/gerber/tests/test_excellon.py b/gerbonara/gerber/tests/test_excellon.py index f8d54ed..9aa5232 100644 --- a/gerbonara/gerber/tests/test_excellon.py +++ b/gerbonara/gerber/tests/test_excellon.py @@ -10,6 +10,7 @@ from ..cam import FileSettings from .image_support import * from .utils import * +from ..utils import Inch, MM REFERENCE_FILES = [ 'easyeda/Gerber_Drill_NPTH.DRL', @@ -34,10 +35,17 @@ REFERENCE_FILES = [ @pytest.mark.parametrize('reference', REFERENCE_FILES, indirect=True) def test_round_trip(reference, tmpfile): tmp = tmpfile('Output excellon', '.drl') + # Altium uses an excellon format specification format that gerbv doesn't understand, so we have to fix that. + unit_spec = ('mm', 'leading', 4) if 'altium-composite-drill' in str(reference) else None + # pcb-rnd does not include any unit specification at all + if 'pcb-rnd' in str(reference): + settings = FileSettings(unit=Inch, zeros='leading', number_format=(2,4)) + else: + settings = None - ExcellonFile.open(reference).save(tmp) + ExcellonFile.open(reference, settings=settings).save(tmp) - mean, _max, hist = gerber_difference(reference, tmp, diff_out=tmpfile('Difference', '.png')) + mean, _max, hist = gerber_difference(reference, tmp, diff_out=tmpfile('Difference', '.png'), ref_unit_spec=unit_spec) assert mean < 5e-5 assert hist[9] == 0 assert hist[3:].sum() < 5e-5*hist.size diff --git a/gerbonara/gerber/tests/utils.py b/gerbonara/gerber/tests/utils.py index ee8fc68..f63f3c6 100644 --- a/gerbonara/gerber/tests/utils.py +++ b/gerbonara/gerber/tests/utils.py @@ -11,7 +11,7 @@ from PIL import Image import pytest fail_dir = Path('gerbonara_test_failures') -reference_path = lambda reference: Path(__file__).parent / 'resources' / reference +reference_path = lambda reference: Path(__file__).parent / 'resources' / str(reference) to_gerbv_svg_units = lambda val, unit='mm': val*72 if unit == 'inch' else val/25.4*72 def path_test_name(request): diff --git a/gerbonara/gerber/utils.py b/gerbonara/gerber/utils.py index 30b03c9..fded04a 100644 --- a/gerbonara/gerber/utils.py +++ b/gerbonara/gerber/utils.py @@ -86,6 +86,9 @@ class LengthUnit: def __deepcopy__(self, memo): return self + def __str__(self): + return self.shorthand + MILLIMETERS_PER_INCH = 25.4 Inch = LengthUnit('inch', 'in', MILLIMETERS_PER_INCH) -- cgit