From ae3bbff8b0849e0b49dc139396d7f8c57334a7b8 Mon Sep 17 00:00:00 2001 From: Hamilton Kibbe Date: Fri, 10 Oct 2014 23:07:51 -0400 Subject: Added excellon format detection --- gerber/cam.py | 124 ++++++++++++++++++++++++++ gerber/cnc.py | 119 ------------------------- gerber/excellon.py | 155 +++++++++++++++++++++++++++++++-- gerber/gerber.py | 4 +- gerber/gerber_statements.py | 7 +- gerber/tests/test_cam.py | 50 +++++++++++ gerber/tests/test_cnc.py | 50 ----------- gerber/tests/test_gerber_statements.py | 76 ++++++++++++---- 8 files changed, 392 insertions(+), 193 deletions(-) create mode 100644 gerber/cam.py delete mode 100644 gerber/cnc.py create mode 100644 gerber/tests/test_cam.py delete mode 100644 gerber/tests/test_cnc.py diff --git a/gerber/cam.py b/gerber/cam.py new file mode 100644 index 0000000..e7a49d1 --- /dev/null +++ b/gerber/cam.py @@ -0,0 +1,124 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +# copyright 2014 Hamilton Kibbe +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +CAM File +============ +**AM file classes** + +This module provides common base classes for Excellon/Gerber CNC files +""" + + +class FileSettings(object): + """ CAM File Settings + + Provides a common representation of gerber/excellon file settings + """ + def __init__(self, notation='absolute', units='inch', + zero_suppression='trailing', format=(2, 5)): + if notation not in ['absolute', 'incremental']: + raise ValueError('Notation must be either absolute or incremental') + self.notation = notation + + if units not in ['inch', 'metric']: + raise ValueError('Units must be either inch or metric') + self.units = units + + if zero_suppression not in ['leading', 'trailing']: + raise ValueError('Zero suppression must be either leading or \ + trailling') + self.zero_suppression = zero_suppression + + if len(format) != 2: + raise ValueError('Format must be a tuple(n=2) of integers') + self.format = format + + def __getitem__(self, key): + if key == 'notation': + return self.notation + elif key == 'units': + return self.units + elif key == 'zero_suppression': + return self.zero_suppression + elif key == 'format': + return self.format + else: + raise KeyError() + + +class CamFile(object): + """ Base class for Gerber/Excellon files. + + Provides a common set of settings parameters. + + Parameters + ---------- + settings : FileSettings + The current file configuration. + + filename : string + Name of the file that this CamFile represents. + + layer_name : string + Name of the PCB layer that the file represents + + Attributes + ---------- + settings : FileSettings + File settings as a FileSettings object + + notation : string + File notation setting. May be either 'absolute' or 'incremental' + + units : string + File units setting. May be 'inch' or 'metric' + + zero_suppression : string + File zero-suppression setting. May be either 'leading' or 'trailling' + + format : tuple (, ) + File decimal representation format as a tuple of (integer digits, + decimal digits) + """ + + def __init__(self, statements=None, settings=None, filename=None, + layer_name=None): + if settings is not None: + self.notation = settings['notation'] + self.units = settings['units'] + self.zero_suppression = settings['zero_suppression'] + self.format = settings['format'] + else: + self.notation = 'absolute' + self.units = 'inch' + self.zero_suppression = 'trailing' + self.format = (2, 5) + self.statements = statements if statements is not None else [] + self.filename = filename + self.layer_name = layer_name + + @property + def settings(self): + """ File settings + + Returns + ------- + settings : FileSettings (dict-like) + A FileSettings object with the specified configuration. + """ + return FileSettings(self.notation, self.units, self.zero_suppression, + self.format) diff --git a/gerber/cnc.py b/gerber/cnc.py deleted file mode 100644 index d17517a..0000000 --- a/gerber/cnc.py +++ /dev/null @@ -1,119 +0,0 @@ -#! /usr/bin/env python -# -*- coding: utf-8 -*- - -# copyright 2014 Hamilton Kibbe -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at - -# http://www.apache.org/licenses/LICENSE-2.0 - -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -""" -gerber.cnc -============ -**CNC file classes** - -This module provides common base classes for Excellon/Gerber CNC files -""" - - -class FileSettings(object): - """ CNC File Settings - - Provides a common representation of gerber/excellon file settings - """ - def __init__(self, notation='absolute', units='inch', - zero_suppression='trailing', format=(2, 5)): - if notation not in ['absolute', 'incremental']: - raise ValueError('Notation must be either absolute or incremental') - self.notation = notation - - if units not in ['inch', 'metric']: - raise ValueError('Units must be either inch or metric') - self.units = units - - if zero_suppression not in ['leading', 'trailing']: - raise ValueError('Zero suppression must be either leading or \ - trailling') - self.zero_suppression = zero_suppression - - if len(format) != 2: - raise ValueError('Format must be a tuple(n=2) of integers') - self.format = format - - def __getitem__(self, key): - if key == 'notation': - return self.notation - elif key == 'units': - return self.units - elif key == 'zero_suppression': - return self.zero_suppression - elif key == 'format': - return self.format - else: - raise KeyError() - - -class CncFile(object): - """ Base class for Gerber/Excellon files. - - Provides a common set of settings parameters. - - Parameters - ---------- - settings : FileSettings - The current file configuration. - - filename : string - Name of the file that this CncFile represents. - - Attributes - ---------- - settings : FileSettings - File settings as a FileSettings object - - notation : string - File notation setting. May be either 'absolute' or 'incremental' - - units : string - File units setting. May be 'inch' or 'metric' - - zero_suppression : string - File zero-suppression setting. May be either 'leading' or 'trailling' - - format : tuple (, ) - File decimal representation format as a tuple of (integer digits, - decimal digits) - """ - - def __init__(self, statements=None, settings=None, filename=None): - if settings is not None: - self.notation = settings['notation'] - self.units = settings['units'] - self.zero_suppression = settings['zero_suppression'] - self.format = settings['format'] - else: - self.notation = 'absolute' - self.units = 'inch' - self.zero_suppression = 'trailing' - self.format = (2, 5) - self.statements = statements if statements is not None else [] - self.filename = filename - - @property - def settings(self): - """ File settings - - Returns - ------- - settings : FileSettings (dict-like) - A FileSettings object with the specified configuration. - """ - return FileSettings(self.notation, self.units, self.zero_suppression, - self.format) diff --git a/gerber/excellon.py b/gerber/excellon.py index 4166de6..1a498dc 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -24,16 +24,22 @@ This module provides Excellon file classes and parsing utilities from .excellon_statements import * -from .cnc import CncFile, FileSettings +from .cam import CamFile, FileSettings +import math def read(filename): """ Read data from filename and return an ExcellonFile """ - return ExcellonParser().parse(filename) + detected_settings = detect_excellon_format(filename) + settings = FileSettings(**detected_settings) + zeros = '' + print('Detected %d:%d format with %s zero suppression' % + (settings.format[0], settings.format[1], settings.zero_suppression)) + return ExcellonParser(settings).parse(filename) -class ExcellonFile(CncFile): +class ExcellonFile(CamFile): """ A class representing a single excellon file The ExcellonFile class represents a single excellon file. @@ -83,8 +89,13 @@ class ExcellonFile(CncFile): class ExcellonParser(object): """ Excellon File Parser + + Parameters + ---------- + settings : FileSettings or dict-like + Excellon file settings to use when interpreting the excellon file. """ - def __init__(self): + def __init__(self, settings=None): self.notation = 'absolute' self.units = 'inch' self.zero_suppression = 'trailing' @@ -95,7 +106,38 @@ class ExcellonParser(object): self.hits = [] self.active_tool = None self.pos = [0., 0.] - + if settings is not None: + self.units = settings['units'] + self.zero_suppression = settings['zero_suppression'] + self.notation = settings['notation'] + self.format = settings['format'] + + + @property + def coordinates(self): + return [(stmt.x, stmt.y) for stmt in self.statements if isinstance(stmt, CoordinateStmt)] + + @property + def bounds(self): + xmin = ymin = 100000000000 + xmax = ymax = -100000000000 + for x, y in self.coordinates: + if x is not None: + xmin = x if x < xmin else xmin + xmax = x if x > xmax else xmax + if y is not None: + ymin = y if y < ymin else ymin + ymax = y if y > ymax else ymax + return ((xmin, xmax), (ymin, ymax)) + + @property + def hole_sizes(self): + return [stmt.diameter for stmt in self.statements if isinstance(stmt, ExcellonTool)] + + @property + def hole_count(self): + return len(self.hits) + def parse(self, filename): with open(filename, 'r') as f: for line in f: @@ -194,3 +236,106 @@ class ExcellonParser(object): return FileSettings(units=self.units, format=self.format, zero_suppression=self.zero_suppression, notation=self.notation) + + +def detect_excellon_format(filename): + """ Detect excellon file decimal format and zero-suppression settings. + + Parameters + ---------- + filename : string + Name of the file to parse. This does not check if the file is actually + an Excellon file, so do that before calling this. + + Returns + ------- + settings : dict + Detected excellon file settings. Keys are + - `format`: decimal format as tuple (, ) + - `zero_suppression`: zero suppression, 'leading' or 'trailing' + """ + results = {} + detected_zeros = None + detected_format = None + zs_options = ('leading', 'trailing', ) + format_options = ((2, 4), (2, 5), (3, 3),) + + # Check for obvious clues: + p = ExcellonParser() + p.parse(filename) + + # Get zero_suppression from a unit statement + zero_statements = [stmt.zero_suppression for stmt in p.statements + if isinstance(stmt, UnitStmt)] + + # get format from altium comment + format_comment = [stmt.comment for stmt in p.statements + if isinstance(stmt, CommentStmt) + and 'FILE_FORMAT' in stmt.comment] + + detected_format = (tuple([int(val) for val in + format_comment[0].split('=')[1].split(':')]) + if len(format_comment) == 1 else None) + detected_zeros = zero_statements[0] if len(zero_statements) == 1 else None + + # Bail out here if possible + if detected_format is not None and detected_zeros is not None: + return {'format': detected_format, 'zero_suppression': detected_zeros} + + # Only look at remaining options + if detected_format is not None: + format_options = (detected_format,) + if detected_zeros is not None: + zs_options = (detected_zeros,) + + # Brute force all remaining options, and pick the best looking one... + for zs in zs_options: + for fmt in format_options: + key = (fmt, zs) + settings = FileSettings(zero_suppression=zs, format=fmt) + try: + p = ExcellonParser(settings) + p.parse(filename) + size = tuple([t[1] - t[0] for t in p.bounds]) + hole_area = 0.0 + for hit in p.hits: + tool = hit[0] + hole_area += math.pow(math.pi * tool.diameter, 2) + results[key] = (size, p.hole_count, hole_area) + except: + pass + + # See if any of the dimensions are left with only a single option + formats = set(key[0] for key in results.iterkeys()) + zeros = set(key[1] for key in results.iterkeys()) + if len(formats) == 1: + detected_format = formats.pop() + if len(zeros) == 1: + detected_zeros = zeros.pop() + + # Bail out here if we got everything.... + if detected_format is not None and detected_zeros is not None: + return {'format': detected_format, 'zero_suppression': detected_zeros} + + # Otherwise score each option and pick the best candidate + else: + scores = {} + for key in results.keys(): + size, count, diameter = results[key] + scores[key] = _layer_size_score(size, count, diameter) + minscore = min(scores.values()) + for key in scores.iterkeys(): + if scores[key] == minscore: + return {'format': key[0], 'zero_suppression': key[1]} + + +def _layer_size_score(size, hole_count, hole_area): + """ Heuristic used for determining the correct file number interpretation. + Lower is better. + """ + board_area = size[0] * size[1] + hole_percentage = hole_area / board_area + hole_score = (hole_percentage - 0.25) ** 2 + size_score = (board_area - 8) **2 + return hole_score * size_score + \ No newline at end of file diff --git a/gerber/gerber.py b/gerber/gerber.py index 4ce261d..215b970 100644 --- a/gerber/gerber.py +++ b/gerber/gerber.py @@ -27,7 +27,7 @@ This module provides an RS-274-X class and parser import re import json from .gerber_statements import * -from .cnc import CncFile, FileSettings +from .cam import CamFile, FileSettings @@ -38,7 +38,7 @@ def read(filename): return GerberParser().parse(filename) -class GerberFile(CncFile): +class GerberFile(CamFile): """ A class representing a single gerber file The GerberFile class represents a single gerber file. diff --git a/gerber/gerber_statements.py b/gerber/gerber_statements.py index a22eae2..218074f 100644 --- a/gerber/gerber_statements.py +++ b/gerber/gerber_statements.py @@ -133,7 +133,12 @@ class MOParamStmt(ParamStmt): @classmethod def from_dict(cls, stmt_dict): param = stmt_dict.get('param') - mo = 'inch' if stmt_dict.get('mo') == 'IN' else 'metric' + if stmt_dict.get('mo').lower() == 'in': + mo = 'inch' + elif stmt_dict.get('mo').lower() == 'mm': + mo = 'metric' + else: + mo = None return cls(param, mo) def __init__(self, param, mo): diff --git a/gerber/tests/test_cam.py b/gerber/tests/test_cam.py new file mode 100644 index 0000000..4af1984 --- /dev/null +++ b/gerber/tests/test_cam.py @@ -0,0 +1,50 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +# Author: Hamilton Kibbe + +from ..cam import CamFile, FileSettings +from tests import * + + +def test_smoke_filesettings(): + """ Smoke test FileSettings class + """ + fs = FileSettings() + + +def test_filesettings_defaults(): + """ Test FileSettings default values + """ + fs = FileSettings() + assert_equal(fs.format, (2, 5)) + assert_equal(fs.notation, 'absolute') + assert_equal(fs.zero_suppression, 'trailing') + assert_equal(fs.units, 'inch') + + +def test_filesettings_dict(): + """ Test FileSettings Dict + """ + fs = FileSettings() + assert_equal(fs['format'], (2, 5)) + assert_equal(fs['notation'], 'absolute') + assert_equal(fs['zero_suppression'], 'trailing') + assert_equal(fs['units'], 'inch') + + +def test_filesettings_assign(): + """ Test FileSettings attribute assignment + """ + fs = FileSettings() + fs.units = 'test' + fs.notation = 'test' + fs.zero_suppression = 'test' + fs.format = 'test' + assert_equal(fs.units, 'test') + assert_equal(fs.notation, 'test') + assert_equal(fs.zero_suppression, 'test') + assert_equal(fs.format, 'test') + +def test_smoke_camfile(): + cf = CamFile diff --git a/gerber/tests/test_cnc.py b/gerber/tests/test_cnc.py deleted file mode 100644 index ace047e..0000000 --- a/gerber/tests/test_cnc.py +++ /dev/null @@ -1,50 +0,0 @@ -#! /usr/bin/env python -# -*- coding: utf-8 -*- - -# Author: Hamilton Kibbe - -from ..cnc import CncFile, FileSettings -from tests import * - - -def test_smoke_filesettings(): - """ Smoke test FileSettings class - """ - fs = FileSettings() - - -def test_filesettings_defaults(): - """ Test FileSettings default values - """ - fs = FileSettings() - assert_equal(fs.format, (2, 5)) - assert_equal(fs.notation, 'absolute') - assert_equal(fs.zero_suppression, 'trailing') - assert_equal(fs.units, 'inch') - - -def test_filesettings_dict(): - """ Test FileSettings Dict - """ - fs = FileSettings() - assert_equal(fs['format'], (2, 5)) - assert_equal(fs['notation'], 'absolute') - assert_equal(fs['zero_suppression'], 'trailing') - assert_equal(fs['units'], 'inch') - - -def test_filesettings_assign(): - """ Test FileSettings attribute assignment - """ - fs = FileSettings() - fs.units = 'test' - fs.notation = 'test' - fs.zero_suppression = 'test' - fs.format = 'test' - assert_equal(fs.units, 'test') - assert_equal(fs.notation, 'test') - assert_equal(fs.zero_suppression, 'test') - assert_equal(fs.format, 'test') - - def test_smoke_cncfile(): - pass diff --git a/gerber/tests/test_gerber_statements.py b/gerber/tests/test_gerber_statements.py index 9e73fd4..a463c9d 100644 --- a/gerber/tests/test_gerber_statements.py +++ b/gerber/tests/test_gerber_statements.py @@ -8,7 +8,7 @@ from ..gerber_statements import * def test_FSParamStmt_factory(): - """ Test FSParamStruct factory correctly handles parameters + """ Test FSParamStruct factory """ stmt = {'param': 'FS', 'zero': 'L', 'notation': 'A', 'x': '27'} fs = FSParamStmt.from_dict(stmt) @@ -24,6 +24,18 @@ def test_FSParamStmt_factory(): assert_equal(fs.notation, 'incremental') assert_equal(fs.format, (2, 7)) +def test_FSParamStmt(): + """ Test FSParamStmt initialization + """ + param = 'FS' + zeros = 'trailing' + notation = 'absolute' + fmt = (2, 5) + stmt = FSParamStmt(param, zeros, notation, fmt) + assert_equal(stmt.param, param) + assert_equal(stmt.zero_suppression, zeros) + assert_equal(stmt.notation, notation) + assert_equal(stmt.format, fmt) def test_FSParamStmt_dump(): """ Test FSParamStmt to_gerber() @@ -38,17 +50,31 @@ def test_FSParamStmt_dump(): def test_MOParamStmt_factory(): - """ Test MOParamStruct factory correctly handles parameters + """ Test MOParamStruct factory """ - stmt = {'param': 'MO', 'mo': 'IN'} - mo = MOParamStmt.from_dict(stmt) - assert_equal(mo.param, 'MO') - assert_equal(mo.mode, 'inch') + stmts = [{'param': 'MO', 'mo': 'IN'}, {'param': 'MO', 'mo': 'in'}, ] + for stmt in stmts: + mo = MOParamStmt.from_dict(stmt) + assert_equal(mo.param, 'MO') + assert_equal(mo.mode, 'inch') + + stmts = [{'param': 'MO', 'mo': 'MM'}, {'param': 'MO', 'mo': 'mm'}, ] + for stmt in stmts: + mo = MOParamStmt.from_dict(stmt) + assert_equal(mo.param, 'MO') + assert_equal(mo.mode, 'metric') + +def test_MOParamStmt(): + """ Test MOParamStmt initialization + """ + param = 'MO' + mode = 'inch' + stmt = MOParamStmt(param, mode) + assert_equal(stmt.param, param) - stmt = {'param': 'MO', 'mo': 'MM'} - mo = MOParamStmt.from_dict(stmt) - assert_equal(mo.param, 'MO') - assert_equal(mo.mode, 'metric') + for mode in ['inch', 'metric']: + stmt = MOParamStmt(param, mode) + assert_equal(stmt.mode, mode) def test_MOParamStmt_dump(): @@ -64,7 +90,7 @@ def test_MOParamStmt_dump(): def test_IPParamStmt_factory(): - """ Test IPParamStruct factory correctly handles parameters + """ Test IPParamStruct factory """ stmt = {'param': 'IP', 'ip': 'POS'} ip = IPParamStmt.from_dict(stmt) @@ -74,6 +100,15 @@ def test_IPParamStmt_factory(): ip = IPParamStmt.from_dict(stmt) assert_equal(ip.ip, 'negative') +def test_IPParamStmt(): + """ Test IPParamStmt initialization + """ + param = 'IP' + for ip in ['positive', 'negative']: + stmt = IPParamStmt(param, ip) + assert_equal(stmt.param, param) + assert_equal(stmt.ip, ip) + def test_IPParamStmt_dump(): """ Test IPParamStmt to_gerber() @@ -88,14 +123,23 @@ def test_IPParamStmt_dump(): def test_OFParamStmt_factory(): - """ Test OFParamStmt factory correctly handles parameters + """ Test OFParamStmt factory """ stmt = {'param': 'OF', 'a': '0.1234567', 'b': '0.1234567'} of = OFParamStmt.from_dict(stmt) assert_equal(of.a, 0.1234567) assert_equal(of.b, 0.1234567) - +def test_OFParamStmt(): + """ Test IPParamStmt initialization + """ + param = 'OF' + for val in [0.0, -3.4567]: + stmt = OFParamStmt(param, val, val) + assert_equal(stmt.param, param) + assert_equal(stmt.a, val) + assert_equal(stmt.b, val) + def test_OFParamStmt_dump(): """ Test OFParamStmt to_gerber() """ @@ -105,7 +149,7 @@ def test_OFParamStmt_dump(): def test_LPParamStmt_factory(): - """ Test LPParamStmt factory correctly handles parameters + """ Test LPParamStmt factory """ stmt = {'param': 'LP', 'lp': 'C'} lp = LPParamStmt.from_dict(stmt) @@ -128,7 +172,7 @@ def test_LPParamStmt_dump(): def test_INParamStmt_factory(): - """ Test INParamStmt factory correctly handles parameters + """ Test INParamStmt factory """ stmt = {'param': 'IN', 'name': 'test'} inp = INParamStmt.from_dict(stmt) @@ -143,7 +187,7 @@ def test_INParamStmt_dump(): def test_LNParamStmt_factory(): - """ Test LNParamStmt factory correctly handles parameters + """ Test LNParamStmt factory """ stmt = {'param': 'LN', 'name': 'test'} lnp = LNParamStmt.from_dict(stmt) -- cgit