summaryrefslogtreecommitdiff
path: root/gerbonara
diff options
context:
space:
mode:
Diffstat (limited to 'gerbonara')
-rw-r--r--gerbonara/gerber/cam.py46
-rw-r--r--gerbonara/gerber/ipc356.py843
-rw-r--r--gerbonara/gerber/layers.py2
-rw-r--r--gerbonara/gerber/tests/test_ipc356.py191
-rw-r--r--gerbonara/gerber/tests/test_pcb.py7
-rw-r--r--gerbonara/gerber/tests/test_rs274x_backend.py68
-rw-r--r--gerbonara/gerber/utils.py3
7 files changed, 539 insertions, 621 deletions
diff --git a/gerbonara/gerber/cam.py b/gerbonara/gerber/cam.py
index 411bc65..83155bd 100644
--- a/gerbonara/gerber/cam.py
+++ b/gerbonara/gerber/cam.py
@@ -18,6 +18,8 @@
import math
from dataclasses import dataclass
from copy import deepcopy
+from enum import Enum
+import string
from .utils import LengthUnit, MM, Inch, Tag
from . import graphic_primitives as gp
@@ -61,6 +63,45 @@ class FileSettings:
super().__setattr__(name, value)
+ def to_radian(self, value):
+ value = float(value)
+ return math.radians(value) if self.angle_unit == 'degree' else value
+
+ def parse_ipc_length(self, value, default=None):
+ if value is None or not str(value).strip():
+ return default
+
+ if isinstance(value, str) and value[0].isalpha():
+ value = value[1:]
+
+ value = int(value)
+ value *= 0.0001 if self.is_inch else 0.001
+ return value
+
+ def format_ipc_number(self, value, digits, key='', sign=False):
+ if value is None:
+ return ' ' * (digits + 1 + len(key))
+
+ if isinstance(value, Enum):
+ value = value.value
+
+ return key + format(round(value), f'{"+" if sign else ""}0{digits+1}d')
+
+ def format_ipc_length(self, value, digits, key='', unit=None, sign=False):
+ if value is not None:
+ value = self.unit(value, unit)
+ value /= 0.0001 if self.is_inch else 0.001
+
+ return self.format_ipc_number(value, digits, key, sign=sign)
+
+ @property
+ def is_metric(self):
+ return self.unit == MM
+
+ @property
+ def is_inch(self):
+ return self.unit == Inch
+
def copy(self):
return deepcopy(self)
@@ -152,11 +193,10 @@ class FileSettings:
class CamFile:
- def __init__(self, original_path=None, layer_name=None):
+ def __init__(self, original_path=None, layer_name=None, import_settings=None):
self.original_path = original_path
self.layer_name = layer_name
- self.import_settings = None
- self.objects = []
+ self.import_settings = import_settings
def to_svg(self, tag=Tag, margin=0, arg_unit=MM, svg_unit=MM, force_bounds=None, fg='black', bg='white'):
diff --git a/gerbonara/gerber/ipc356.py b/gerbonara/gerber/ipc356.py
index 23382e3..940ed72 100644
--- a/gerbonara/gerber/ipc356.py
+++ b/gerbonara/gerber/ipc356.py
@@ -1,15 +1,15 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
-
+#
# copyright 2014 Hamilton Kibbe <ham@hamiltonkib.be>
# Modified from parser.py by Paulo Henrique Silva <ph.silva@gmail.com>
#
# 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.
@@ -19,472 +19,509 @@
from dataclasses import dataclass
import math
import re
-from .cam import CamFile, FileSettings
-
-# Net Name Variables
-_NNAME = re.compile(r'^NNAME\d+$')
-
-# Board Edge Coordinates
-_COORD = re.compile(r'X?(?P<x>[\d\s]*)?Y?(?P<y>[\d\s]*)?')
-
-_SM_FIELD = {
- '0': 'none',
- '1': 'primary side',
- '2': 'secondary side',
- '3': 'both'}
-
-
-def read(filename):
- """ Read data from filename and return an IPCNetlist
- Parameters
- ----------
- filename : string
- Filename of file to parse
-
- Returns
- -------
- file : :class:`gerber.ipc356.IPCNetlist`
- An IPCNetlist object created from the specified file.
-
- """
- # File object should use settings from source file by default.
- return IPCNetlist.from_file(filename)
-
-@dataclass
-class TestRecord:
- position : [float]
- net_name : str
- layer : str
-
-def loads(data, filename=None):
- """ Generate an IPCNetlist object from IPC-D-356 data in memory
-
- Parameters
- ----------
- data : string
- string containing netlist file contents
-
- filename : string, optional
- string containing the filename of the data source
-
- Returns
- -------
- file : :class:`gerber.ipc356.IPCNetlist`
- An IPCNetlist created from the specified file.
- """
- return IPCNetlistParser().parse_raw(data, filename)
+from enum import Enum
+import warnings
+from dataclasses import dataclass, KW_ONLY
+from pathlib import Path
+from .cam import CamFile, FileSettings
+from .utils import MM, Inch, LengthUnit
+
+
+class Netlist(CamFile):
+ def __init__(self, test_records=None, conductors=None, outlines=None, comments=None, adjacency=None,
+ params=None, import_settings=None, original_path=None):
+ super().__init__(original_path=original_path, layer_name='netlist', import_settings=import_settings)
+ self.test_records = test_records or []
+ self.conductors = conductors or []
+ self.outlines = outlines or []
+ self.comments = comments or []
+ self.adjacency = adjacency or {}
+ self.params = params or {}
+
+ def merge(self, other, our_net_prefix=None, their_net_prefix=None):
+ ''' Merge other netlist into this netlist. The respective net names are prefixed with the given prefixes
+ (default: None). Garbles other. '''
+ if not isinstance(other, Netlist):
+ raise TypeError(f'Can only merge Netlist with other Netlist, not {type(other)}')
+
+ self.prefix_nets(our_net_prefix)
+ other.prefix_nets(our_net_prefix)
+
+ self.test_records.extend(other.test_records)
+ self.conductors.extend(other.conductors)
+ self.outlines.extend(other.outlines)
+ self.comments.extend(other.comments)
+ self.adjacency.update(other.adjacency)
+ self.params.extend(other.params)
+
+ def prefix_nets(self, prefix):
+ if not prefix:
+ return
-class IPCNetlist(CamFile):
+ for record in self.test_records:
+ if record.net_name:
+ record.net_name = prefix + record.net_name
- @classmethod
- def from_file(cls, filename):
- parser = IPCNetlistParser()
- return parser.parse(filename)
-
- def __init__(self, statements, settings, primitives=None, filename=None):
- self.statements = statements
- self.units = settings.units
- self.angle_units = settings.angle_units
- self.primitives = [TestRecord((rec.x_coord, rec.y_coord), rec.net_name,
- rec.access) for rec in self.test_records]
- self.filename = filename
+ for conductor in self.conductors:
+ if conductor.net_name:
+ conductor.net_name = prefix + conductor.net_name
- @property
- def settings(self):
- return FileSettings(units=self.units, angle_units=self.angle_units)
+ new_adjacency = {}
+ for key in adjacency:
+ new_adjacency[prefix + key] = [ prefix + name for name in adjacency[key] ]
+ self.adjacency = new_adjacency
@property
- def comments(self):
- return [record for record in self.statements
- if isinstance(record, IPC356_Comment)]
+ def objects(self):
+ yield from self.test_records
+ yield from self.conductors
+ yield from self.outlines
- @property
- def parameters(self):
- return [record for record in self.statements
- if isinstance(record, IPC356_Parameter)]
+ @classmethod
+ def open(kls, filename):
+ path = Path(filename)
+ parser = NetlistParser()
+ return parser.parse(path.read_text(), path)
- @property
- def test_records(self):
- return [record for record in self.statements
- if isinstance(record, IPC356_TestRecord)]
+ @classmethod
+ def from_string(kls, data, filename=None):
+ parser = NetlistParser()
+ return parser.parse(data, Path(filename))
+
+ def save(self, filename, settings=None, drop_comments=True):
+ with open(filename, 'w', encoding='utf-8') as f:
+ f.write(self.to_ipc356(settings, drop_comments=drop_comments))
+
+ def to_ipc356(self, settings=None, drop_comments=True, job_name=None):
+ if settings is None:
+ settings = self.import_settings.copy() or FileSettings()
+ settings.zeros = None
+ settings.number_format = (5,6)
+ return '\n'.join(self._generate_lines(settings, drop_comments=drop_comments))
+
+ def _generate_lines(self, settings, drop_comments, job_name=None):
+ yield 'C IPC-D-356 generated by Gerbonara'
+ yield 'C'
+ yield f'P JOB {self.params.get("JOB", "Gerbonara netlist export")}'
+ yield 'P UNITS CUST 0' if settings.unit == Inch else 'P UNITS CUST 1'
+
+ if not drop_comments:
+ for comment in self.comments:
+ yield f'C {comment}'
+
+ for name, value in self.params.items():
+ if name == 'JOB':
+ continue
+
+ yield f'P {name} {value!s}'
+
+ net_name_map = {
+ f'NNAME{i}': name for i, name in enumerate(
+ name for name in self.net_names() if len(name) > 14
+ ) }
+
+ yield 'C'
+ yield 'C Net name mapping:'
+ yield 'C'
+ for name, value in net_name_map.items():
+ yield f'P {name} {value!s}'
+
+ yield 'C'
+ yield 'C Test records:'
+ yield 'C'
+
+ for record in self.test_records:
+ yield from record.format(settings, net_name_map)
+
+ if self.conductors:
+ yield 'C'
+ yield 'C Conductors:'
+ yield 'C'
+ for conductor in self.conductors:
+ yield from conductor.format(settings, net_name_map)
+
+ if self.outlines:
+ yield 'C'
+ yield 'C Outlines:'
+ yield 'C'
+ for outline in self.outlines:
+ yield from outline.format(settings)
+
+ if self.adjacency:
+ yield 'C'
+ yield 'C Adjacency data:'
+ yield 'C'
+ done = set()
+ for net, others in self.adjacency.items():
+ others_filtered = [ other for other in others if (net, other) not in done and (other, net) not in done ]
+
+ line = '379'
+ for net in self.nets:
+ if len(line) + 1 + len(net) > 80:
+ yield line
+ line = f'079 {net}'
+ else:
+ line += f' {net}'
+ yield line
- @property
- def nets(self):
- nets = []
- for net in list(set([rec.net_name for rec in self.test_records
- if rec.net_name is not None])):
- adjacent_nets = set()
- for record in self.adjacency_records:
- if record.net == net:
- adjacent_nets = adjacent_nets.update(record.adjacent_nets)
- elif net in record.adjacent_nets:
- adjacent_nets.add(record.net)
- nets.append(IPC356_Net(net, adjacent_nets))
+ def net_names(self):
+ nets = { record.net_name for record in self.test_records }
+ nets -= {None}
return nets
- @property
- def components(self):
- return list(set([rec.id for rec in self.test_records
- if rec.id is not None and rec.id != 'VIA']))
-
- @property
def vias(self):
- return [rec.id for rec in self.test_records if rec.id == 'VIA']
+ for record in self.test_records:
+ if record.is_via:
+ yield record
- @property
- def outlines(self):
- return [stmt for stmt in self.statements
- if isinstance(stmt, IPC356_Outline)]
+ def reference_designators(self):
+ names = { record.ref_des for record in self.test_records }
+ names -= {None}
+ return names
- @property
- def adjacency_records(self):
- return [record for record in self.statements
- if isinstance(record, IPC356_Adjacency)]
+ def records_by_reference(self, reference_designator):
+ for record in self.test_records:
+ if record.ref_des == reference_designator:
+ yield record
- def render(self, ctx, layer='both', filename=None):
- for p in self.primitives:
- if layer == 'both' and p.layer in ('top', 'bottom', 'both'):
- ctx.render(p)
- elif layer == 'top' and p.layer in ('top', 'both'):
- ctx.render(p)
- elif layer == 'bottom' and p.layer in ('bottom', 'both'):
- ctx.render(p)
- if filename is not None:
- ctx.dump(filename)
+ def records_by_net_name(self, net_name):
+ for record in self.test_records:
+ if record.net_name == net_name:
+ yield record
+ def conductors_by_net_name(self, net_name):
+ for conductor in self.conductos:
+ if conductor.net_name == net_name:
+ yield conductor
-class IPCNetlistParser(object):
- # TODO: Allow multi-line statements (e.g. Altium board edge)
+ def conductors_by_layer(self, layer : int):
+ for conductor in self.conductos:
+ if conductor.layer == layer:
+ yield conductor
- def __init__(self):
- self.units = 'inch'
- self.angle_units = 'degrees'
- self.statements = []
- self.nnames = {}
- @property
- def settings(self):
- return FileSettings(units=self.units, angle_units=self.angle_units)
-
- def parse(self, filename):
- with open(filename, 'r') as f:
- data = f.read()
- return self.parse_raw(data, filename)
-
- def parse_raw(self, data, filename=None):
- oldline = ''
- for line in data.splitlines():
- # Check for existing multiline data...
- if oldline != '':
- if len(line) and line[0] == '0':
+class NetlistParser(object):
+ # Good resources on IPC-356 syntax are:
+ # https://www.downstreamtech.com/downloads/IPCD356_Simplified.pdf
+ # https://web.pa.msu.edu/hep/atlas/l1calo/hub/hardware/components/circuit_board/ipc_356a_net_list.pdf
+
+ def __init__(self):
+ self.has_unit = False
+ self.settings = FileSettings()
+ self.net_names = {}
+ self.params = {}
+ self.comments = []
+ self.test_records = []
+ self.conductors = []
+ self.adjacency = {}
+ self.outlines = []
+ self.eof = False
+
+ def warn(self, msg, kls=SyntaxWarning):
+ warnings.warn(f'{self.filename}:{self.start_line}: {msg}', kls)
+
+ def assert_unit(self):
+ if not self.has_unit:
+ raise SyntaxError('IPC-356 netlist file does not contain unit specification before first entry')
+
+ def parse(self, data, path=None):
+ self.filename = path.name
+
+ try:
+ oldline = ''
+ for lineno, line in enumerate(data.splitlines()):
+ # Check for existing multiline data...
+ if oldline:
+ if line and line[0] == '0':
oldline = oldline.rstrip('\r\n') + line[3:].rstrip()
else:
self._parse_line(oldline)
+ self.start_line = lineno
oldline = line
else:
+ self.start_line = lineno
oldline = line
- self._parse_line(oldline)
- return IPCNetlist(self.statements, self.settings, filename=filename)
+ self._parse_line(oldline)
+ except Exception as e:
+ raise SyntaxError(f'Error parsing {self.filename}:{lineno}: {e}') from e
+
+ return Netlist(self.test_records, self.conductors, self.outlines, self.comments, self.adjacency,
+ params=self.params, import_settings=self.settings, original_path=path)
def _parse_line(self, line):
- if not len(line):
+ if not line:
return
+
+ if self.eof:
+ warnings.warn('Data following IPC-356 End Of File marker')
+
if line[0] == 'C':
- # Comment
- self.statements.append(IPC356_Comment.from_line(line))
+ line = line[2:].strip()
+
+ if line.strip().startswith('NNAME'):
+ name, *value = line.strip().split()
+ value = ' '.join(value)
+ warnings.warn('File contains non-standard Allegro-style net name alias definitions in comments.')
+ self.net_names[name] = value
+
+ else:
+ self.comments.append(line)
elif line[0] == 'P':
# Parameter
- p = IPC356_Parameter.from_line(line)
- if p.parameter == 'UNITS':
- if p.value in ('CUST', 'CUST 0'):
- self.units = 'inch'
- self.angle_units = 'degrees'
- elif p.value == 'CUST 1':
- self.units = 'metric'
- self.angle_units = 'degrees'
- elif p.value == 'CUST 2':
- self.units = 'inch'
- self.angle_units = 'radians'
- self.statements.append(p)
- if _NNAME.match(p.parameter):
- # Add to list of net name variables
- self.nnames[p.parameter] = p.value
+ name, *value = line[2:].split()
+ value = ' '.join(value)
- elif line[0] == '9':
- self.statements.append(IPC356_EndOfFile())
+ if name == 'UNITS':
+ if value in ('CUST', 'CUST 0'):
+ self.settings.units = Inch
+ self.settings.angle_unit = 'degree'
+ self.has_unit = True
- elif line[0:3] in ('317', '327', '367'):
- # Test Record
- record = IPC356_TestRecord.from_line(line, self.settings)
+ elif value == 'CUST 1':
+ self.settings.units = MM
+ self.settings.angle_unit = 'degree'
+ self.has_unit = True
- # Substitute net name variables
- net = record.net_name
- if (_NNAME.match(net) and net in self.nnames.keys()):
- record.net_name = self.nnames[record.net_name]
- self.statements.append(record)
-
- elif line[0:3] == '378':
- # Conductor
- self.statements.append(
- IPC356_Conductor.from_line(
- line, self.settings))
+ elif value == 'CUST 2':
+ self.settings.units = Inch
+ self.settings.angle_unit = 'radian'
+ self.has_unit = True
- elif line[0:3] == '379':
- # Net Adjacency
- self.statements.append(IPC356_Adjacency.from_line(line))
+ else:
+ raise SyntaxError(f'Unsupported IPC-356 netlist unit specification "{line}"')
- elif line[0:3] == '389':
- # Outline
- self.statements.append(
- IPC356_Outline.from_line(
- line, self.settings))
+ elif name.startswith('NNAME'):
+ self.net_names[name] = value
+ else:
+ self.params[name] = value
-class IPC356_Comment(object):
+ elif line[0] == '9':
+ self.eof = True
- @classmethod
- def from_line(cls, line):
- if line[0] != 'C':
- raise ValueError('Not a valid comment statment')
- comment = line[2:].strip()
- return cls(comment)
+ elif line[0:3] in ('317', '327', '367'):
+ self.assert_unit()
+ self.test_records.append(TestRecord.parse(line, self.settings, self.net_names))
- def __init__(self, comment):
- self.comment = comment
+ elif line[0:3] == '378':
+ self.assert_unit()
+ self.conductors.append(Conductor.parse(line, self.settings, self.net_names))
- def __repr__(self):
- return '<IPC-D-356 Comment: %s>' % self.comment
+ elif line[0:3] == '379':
+ net, *adjacent = line[3:].strip().split()
+ for other in adjacent:
+ self.adjacency[net] = self.adjacency.get(net, set()) | {other}
+ self.adjacency[other] = self.adjacency.get(other, set()) | {net}
-class IPC356_Parameter(object):
+ elif line[0:3] == '389':
+ self.assert_unit()
+ self.outlines.append(Outline.parse(line, self.settings))
- @classmethod
- def from_line(cls, line):
- if line[0] != 'P':
- raise ValueError('Not a valid parameter statment')
- splitline = line[2:].split()
- parameter = splitline[0].strip()
- value = ' '.join(splitline[1:]).strip()
- return cls(parameter, value)
+ else:
+ warnings.warn(f'Unknown IPC-356 record type {line[0:3]}')
- def __init__(self, parameter, value):
- self.parameter = parameter
- self.value = value
- def __repr__(self):
- return '<IPC-D-356 Parameter: %s=%s>' % (self.parameter, self.value)
+class PadType(Enum):
+ THROUGH_HOLE = 1
+ SMD_PAD = 2
+ TOOLING_FEATURE = 3
+ TOOLING_HOLE = 4
+ NONPLATED_HOLE = 6
-class IPC356_TestRecord(object):
+class SoldermaskInfo(Enum):
+ NONE = 0
+ PRIMARY = 1
+ SECONDARY = 2
+ BOTH = 3
- @classmethod
- def from_line(cls, line, settings):
- offset = 0
- units = settings.units
- angle = settings.angle_units
- feature_types = {'1': 'through-hole', '2': 'smt',
- '3': 'tooling-feature', '4': 'tooling-hole',
- '6': 'non-plated-tooling-hole'}
- access = ['both', 'top', 'layer2', 'layer3', 'layer4', 'layer5',
- 'layer6', 'layer7', 'bottom']
- record = {}
- line = line.strip()
- if line[0] != '3':
- raise ValueError('Not a valid test record statment')
- record['feature_type'] = feature_types[line[1]]
-
- end = len(line) - 1 if len(line) < 18 else 17
- record['net_name'] = line[3:end].strip()
-
- if len(line) >= 27 and line[26] != '-':
- offset = line[26:].find('-')
- offset = 0 if offset == -1 else offset
- end = len(line) - 1 if len(line) < (27 + offset) else (26 + offset)
- record['id'] = line[20:end].strip()
-
- end = len(line) - 1 if len(line) < (32 + offset) else (31 + offset)
- record['pin'] = (line[27 + offset:end].strip() if line[27 + offset:end].strip() != ''
- else None)
-
- record['location'] = 'middle' if line[31 + offset] == 'M' else 'end'
- if line[32 + offset] == 'D':
- end = len(line) - 1 if len(line) < (38 + offset) else (37 + offset)
- dia = int(line[33 + offset:end].strip())
- record['hole_diameter'] = (dia * 0.0001 if units == 'inch'
- else dia * 0.001)
- if len(line) >= (38 + offset):
- record['plated'] = (line[37 + offset] == 'P')
-
- if len(line) >= (40 + offset):
- end = len(line) - 1 if len(line) < (42 + offset) else (41 + offset)
- record['access'] = access[int(line[39 + offset:end])]
-
- if len(line) >= (43 + offset):
- end = len(line) - 1 if len(line) < (50 + offset) else (49 + offset)
- coord = int(line[42 + offset:end].strip())
- record['x_coord'] = (coord * 0.0001 if units == 'inch'
- else coord * 0.001)
-
- if len(line) >= (51 + offset):
- end = len(line) - 1 if len(line) < (58 + offset) else (57 + offset)
- coord = int(line[50 + offset:end].strip())
- record['y_coord'] = (coord * 0.0001 if units == 'inch'
- else coord * 0.001)
-
- if len(line) >= (59 + offset):
- end = len(line) - 1 if len(line) < (63 + offset) else (62 + offset)
- dim = line[58 + offset:end].strip()
- if dim != '':
- record['rect_x'] = (int(dim) * 0.0001 if units == 'inch'
- else int(dim) * 0.001)
-
- if len(line) >= (64 + offset):
- end = len(line) - 1 if len(line) < (68 + offset) else (67 + offset)
- dim = line[63 + offset:end].strip()
- if dim != '':
- record['rect_y'] = (int(dim) * 0.0001 if units == 'inch'
- else int(dim) * 0.001)
-
- if len(line) >= (69 + offset):
- end = len(line) - 1 if len(line) < (72 + offset) else (71 + offset)
- rot = line[68 + offset:end].strip()
- if rot != '':
- record['rect_rotation'] = (int(rot) if angle == 'degrees'
- else math.degrees(rot))
-
- if len(line) >= (74 + offset):
- end = 74 + offset
- sm_info = line[73 + offset:end].strip()
- record['soldermask_info'] = _SM_FIELD.get(sm_info)
-
- if len(line) >= (76 + offset):
- end = len(line) - 1 if len(line) < (80 + offset) else 79 + offset
- record['optional_info'] = line[75 + offset:end]
-
- return cls(**record)
-
- def __init__(self, **kwargs):
- for key in kwargs:
- setattr(self, key, kwargs[key])
-
- def __repr__(self):
- return '<IPC-D-356 %s Test Record: %s>' % (self.net_name,
- self.feature_type)
-
-
-class IPC356_Outline(object):
- @classmethod
- def from_line(cls, line, settings):
- type = line[3:17].strip()
- scale = 0.0001 if settings.units == 'inch' else 0.001
- points = []
- x = 0
- y = 0
- coord_strings = line.strip().split()[1:]
- for coord in coord_strings:
- coord_dict = _COORD.match(coord).groupdict()
- x = int(coord_dict['x']) if coord_dict['x'] != '' else x
- y = int(coord_dict['y']) if coord_dict['y'] != '' else y
- points.append((x * scale, y * scale))
- return cls(type, points)
-
- def __init__(self, type, points):
- self.type = type
- self.points = points
-
- def __repr__(self):
- return '<IPC-D-356 %s Outline Definition>' % self.type
-
-
-class IPC356_Conductor(object):
+@dataclass
+class TestRecord:
+ __test__ = False # tell pytest to ignore this class
+ pad_type : PadType = None
+ net_name : str = None
+ ref_des : str = None # part reference designator, e.g. "C1" or "U69"
+ is_via : bool = False
+ pin_num : int = None
+ is_middle : bool = False # is this a point in the middle or at the end of a trace/net?
+ hole_dia : float = None
+ is_plated : bool = None # None, True, or False.
+ access_layer : int = None
+ x : float = None
+ y : float = None
+ w : float = None
+ h : float = None
+ rotation : float = None
+ solder_mask : SoldermaskInfo = None
+ lefover : str = None
+ unit: KW_ONLY = None
+
+ def __str__(self):
+ x = self.unit.format(self.x)
+ y = self.unit.format(self.y)
+ return f'<IPC-356 test record @ {x},{y} {self.net_name} {self.pad_type.name} at {self.ref_des}, pin {self.pin_num}>'
@classmethod
- def from_line(cls, line, settings):
- if line[0:3] != '378':
- raise ValueError('Not a valid IPC-D-356 Conductor statement')
+ def parse(kls, line, settings, net_name_map={}):
+ obj = kls()
+ line = f'{line:<80}'
+
+ obj.unit = settings.unit
+ obj.pad_type = PadType(int(line[1]))
+ net_name = line[3:17].strip() or None
+ obj.net_name = net_name_map.get(net_name, net_name)
+ obj.ref_des = line[20:26].strip() or None
+ obj.pin = line[27:31].strip() or None
+
+ if line[31] == 'M':
+ obj.is_middle = True
+ if line[32] == 'D':
+ obj.hole_dia = settings.parse_ipc_length(line[33:37])
+ if line[37] in ('P', 'U'):
+ obj.is_plated = (line[37] == 'P')
+ if line[38] == 'A':
+ obj.access_layer = int(line[39:41])
+ if line[41] == 'X':
+ obj.x = settings.parse_ipc_length(line[42:49])
+ if line[49] == 'Y':
+ obj.y = settings.parse_ipc_length(line[50:57])
+ if line[57] == 'X':
+ obj.w = settings.parse_ipc_length(line[58:62])
+ if line[62] == 'Y':
+ obj.h = settings.parse_ipc_length(line[63:67])
+ if line[67] == 'R':
+ obj.h = math.radians(int(line[68:71]))
+ if line[72] == 'S':
+ obj.solder_mask = SoldermaskInfo(int(line[73]))
+ obj.leftover = line[74:].strip() or None
+
+ return obj
+
+ def format(self, settings, net_name_map):
+ x = settings.unit(self.x, self.unit)
+ y = settings.unit(self.y, self.unit)
+ w = settings.unit(self.w, self.unit)
+ h = settings.unit(self.h, self.unit)
+ # TODO: raise warning if any string is too long
+ ref_des = 'VIA' if self.is_via else (self.ref_des or '')
+ net_name = net_name_map.get(self.net_name, self.net_name)
+
+ yield ''.join((
+ '3',
+ str(self.pad_type.value),
+ '7',
+ f'{net_name or "":<14}'[:14],
+ ' ',
+ f'{ref_des or "":<6}'[:6],
+ '-',
+ f'{self.pin_num or "":<4}'[:4],
+ 'M' if self.is_middle else ' ',
+ settings.format_ipc_length(self.hole_dia, 4, 'D', self.unit),
+ {True: 'P', False: 'U', None: ' '}[self.is_plated],
+ settings.format_ipc_number(self.access_layer, 2, 'A'),
+ settings.format_ipc_length(self.x, 6, 'X', self.unit, sign=True),
+ settings.format_ipc_length(self.y, 6, 'Y', self.unit, sign=True),
+ settings.format_ipc_length(self.w, 4, 'X', self.unit),
+ settings.format_ipc_length(self.y, 4, 'Y', self.unit),
+ settings.format_ipc_number(math.degrees(self.rotation) if self.rotation is not None else None, 3, 'R'),
+ ' ',
+ settings.format_ipc_number(self.solder_mask, 1, 'S'),
+ f'{self.leftover or "":<6}'))
+
+class OutlineType(Enum):
+ BOARD_EDGE = 0
+ PANEL_EDGE = 1
+ SCORE_LINE = 2
+ OTHER_FAB = 3
+
+
+def parse_coord_chain(line, settings):
+ x, y = None, None
+ for segment in line.split('*'):
+ coords = []
+ for coord in segment.strip().split():
+ if not (m := re.match(r'(X[+-]?[0-9]+)?(Y[+-]?[0-9]+)?', coord)):
+ raise SyntaxError(f'Invalid IPC-356 coordinate {coord}')
+
+ x = settings.parse_ipc_length(match[1], x)
+ y = settings.parse_ipc_length(match[2], y)
+
+ if x is None or y is None:
+ raise SyntaxError('Outline or conductor coordinate chain is missing one coordinate in the beginning')
+
+ coords.append((x, y))
+ yield coords
+
+def format_coord_chain(line, settings, coords, cont):
+ for x, y in coords:
+ coord = settings.format_ipc_length(x, 6, 'X', unit=self.unit, sign=True)
+ coord += settings.format_ipc_length(y, 6, 'Y', unit=self.unit, sign=True)
+
+ if len(line) + len(coord) <= 80:
+ line += coord
+
+ else:
+ yield line
+ line = f'{cont} {coord}'
+ yield line
- scale = 0.0001 if settings.units == 'inch' else 0.001
- net_name = line[3:17].strip()
- layer = int(line[19:21])
- # Parse out aperture definiting
- raw_aperture = line[22:].split()[0]
- aperture_dict = _COORD.match(raw_aperture).groupdict()
- x = 0
- y = 0
- x = int(aperture_dict['x']) * \
- scale if aperture_dict['x'] != '' else None
- y = int(aperture_dict['y']) * \
- scale if aperture_dict['y'] != '' else None
- aperture = (x, y)
-
- # Parse out conductor shapes
- shapes = []
- coord_list = ' '.join(line[22:].split()[1:])
- raw_shapes = coord_list.split('*')
- for rshape in raw_shapes:
- x = 0
- y = 0
- shape = []
- coords = rshape.split()
- for coord in coords:
- coord_dict = _COORD.match(coord).groupdict()
- x = int(coord_dict['x']) if coord_dict['x'] != '' else x
- y = int(coord_dict['y']) if coord_dict['y'] != '' else y
- shape.append((x * scale, y * scale))
- shapes.append(tuple(shape))
- return cls(net_name, layer, aperture, tuple(shapes))
-
- def __init__(self, net_name, layer, aperture, shapes):
- self.net_name = net_name
- self.layer = layer
- self.aperture = aperture
- self.shapes = shapes
-
- def __repr__(self):
- return '<IPC-D-356 %s Conductor Record>' % self.net_name
-
-
-class IPC356_Adjacency(object):
+@dataclass
+class Outline:
+ outline_type : OutlineType
+ outline : [(float,)]
+ unit : KW_ONLY
@classmethod
- def from_line(cls, line):
- if line[0:3] != '379':
- raise ValueError('Not a valid IPC-D-356 Conductor statement')
- nets = line[3:].strip().split()
+ def parse(kls, line, settings):
+ outline_type = OutlineType[line[3:17].strip()]
+ for outline in parse_coord_chain(line[22:], settings):
+ yield kls(outline_type, outline, unit=settings.unit)
- return cls(nets[0], nets[1:])
+ def format(self, settings):
+ line = f'389{self.outline_type.name:<14} '
+ yield from format_coord_chain(line, settings, self.outline, '089')
- def __init__(self, net, adjacent_nets):
- self.net = net
- self.adjacent_nets = adjacent_nets
+ def __str__(self):
+ return f'<IPC-356 {self.outline_type.name} outline with {len(self.outline)} points>'
- def __repr__(self):
- return '<IPC-D-356 %s Adjacency Record>' % self.net
+@dataclass
+class Conductor:
+ net_name : str
+ layer : int
+ aperture : (float,)
+ coords : [(float,)]
+ unit : KW_ONLY
-class IPC356_EndOfFile(object):
-
- def __init__(self):
- pass
+ @classmethod
+ def parse(kls, line, settings, net_name_map={}):
+ net_name = line[3:17].strip() or None
+ net_name = net_name_map.get(net_name, net_name)
- def to_netlist(self):
- return '999'
+ if line[18] != 'L':
+ raise SytaxError(f'Invalid IPC-356 layer number specification for conductor in line "{line}"')
+ layer = int(line[19:21])
- def __repr__(self):
- return '<IPC-D-356 EOF>'
+ aperture_def, _, coords = line[22:].partition(' ')
+ if not (m := re.match(r'(X[+-]?[0-9]+)(Y[+-]?[0-9]+)?', coord)):
+ raise SyntaxError('Invalid IPC-356 aperture specification "{aperture_def"}')
+ aperture = settings.parse_ipc_length(m[1]), settings.parse_ipc_length(m[2])
+ for chain in parse_coord_chain(coords, settings):
+ yield kls(net_name, layer, aperture, chain, unit=settings.unit)
-class IPC356_Net(object):
+ def format(self, settings, net_name_map):
+ net_name = net_name_map.get(self.net_name, self.net_name)
+ net_name = f'{net_name:<14}[:14]'
+ line = f'378{net_name} L{self.layer:02d} '
+ yield from format_coord_chain(line, settings, self.outline, '078')
- def __init__(self, name, adjacent_nets):
- self.name = name
- self.adjacent_nets = set(
- adjacent_nets) if adjacent_nets is not None else set()
+ def __str__(self):
+ return f'<IPC-356 conductor {self.net_name} with {len(self.coords)} points>'
- def __repr__(self):
- return '<IPC-D-356 Net %s>' % self.name
diff --git a/gerbonara/gerber/layers.py b/gerbonara/gerber/layers.py
index d6f06b2..996bfec 100644
--- a/gerbonara/gerber/layers.py
+++ b/gerbonara/gerber/layers.py
@@ -24,7 +24,7 @@ from pathlib import Path
from .excellon import ExcellonFile
from .rs274x import GerberFile
-from .ipc356 import IPCNetlist
+from .ipc356 import Netlist
from .cam import FileSettings
from .layer_rules import MATCH_RULES
diff --git a/gerbonara/gerber/tests/test_ipc356.py b/gerbonara/gerber/tests/test_ipc356.py
index 77f0782..2a39a79 100644
--- a/gerbonara/gerber/tests/test_ipc356.py
+++ b/gerbonara/gerber/tests/test_ipc356.py
@@ -3,146 +3,59 @@
# Author: Hamilton Kibbe <ham@hamiltonkib.be>
import pytest
+
from ..ipc356 import *
from ..cam import FileSettings
-import os
-
-IPC_D_356_FILE = os.path.join(os.path.dirname(__file__), "resources/ipc-d-356.ipc")
-
-
-def test_read():
- ipcfile = read(IPC_D_356_FILE)
- assert isinstance(ipcfile, IPCNetlist)
-
-
-def test_parser():
- ipcfile = read(IPC_D_356_FILE)
- assert ipcfile.settings.units == "inch"
- assert ipcfile.settings.angle_units == "degrees"
- assert len(ipcfile.comments) == 3
- assert len(ipcfile.parameters) == 4
- assert len(ipcfile.test_records) == 105
- assert len(ipcfile.components) == 21
- assert len(ipcfile.vias) == 14
- assert ipcfile.test_records[-1].net_name == "A_REALLY_LONG_NET_NAME"
- assert ipcfile.outlines[0].type == "BOARD_EDGE"
- assert set(ipcfile.outlines[0].points) == {
- (0.0, 0.0),
- (2.25, 0.0),
- (2.25, 1.5),
- (0.0, 1.5),
- (0.13, 0.024),
- }
-
-
-def test_comment():
- c = IPC356_Comment("Layer Stackup:")
- assert c.comment == "Layer Stackup:"
- c = IPC356_Comment.from_line("C Layer Stackup: ")
- assert c.comment == "Layer Stackup:"
- pytest.raises(ValueError, IPC356_Comment.from_line, "P JOB")
- assert str(c) == "<IPC-D-356 Comment: Layer Stackup:>"
-
-
-def test_parameter():
- p = IPC356_Parameter("VER", "IPC-D-356A")
- assert p.parameter == "VER"
- assert p.value == "IPC-D-356A"
- p = IPC356_Parameter.from_line("P VER IPC-D-356A ")
- assert p.parameter == "VER"
- assert p.value == "IPC-D-356A"
- pytest.raises(ValueError, IPC356_Parameter.from_line, "C Layer Stackup: ")
- assert str(p) == "<IPC-D-356 Parameter: VER=IPC-D-356A>"
-
-
-def test_eof():
- e = IPC356_EndOfFile()
- assert e.to_netlist() == "999"
- assert str(e) == "<IPC-D-356 EOF>"
-
-
-def test_outline():
- type = "BOARD_EDGE"
- points = [(0.01, 0.01), (2.0, 2.0), (4.0, 2.0), (4.0, 6.0)]
- b = IPC356_Outline(type, points)
- assert b.type == type
- assert b.points == points
- b = IPC356_Outline.from_line(
- "389BOARD_EDGE X100Y100 X20000Y20000 X40000 Y60000",
- FileSettings(units="inch"),
- )
- assert b.type == "BOARD_EDGE"
- assert b.points == points
-
-
-def test_test_record():
- pytest.raises(ValueError, IPC356_TestRecord.from_line, "P JOB", FileSettings())
- record_string = (
- "317+5VDC VIA - D0150PA00X 006647Y 012900X0000 S3"
- )
- r = IPC356_TestRecord.from_line(record_string, FileSettings(units="inch"))
- assert r.feature_type == "through-hole"
- assert r.net_name == "+5VDC"
- assert r.id == "VIA"
- pytest.approx(r.hole_diameter, 0.015)
- assert r.plated
- assert r.access == "both"
- pytest.approx(r.x_coord, 0.6647)
- pytest.approx(r.y_coord, 1.29)
- assert r.rect_x == 0.0
- assert r.soldermask_info == "both"
- r = IPC356_TestRecord.from_line(record_string, FileSettings(units="metric"))
- pytest.approx(r.hole_diameter, 0.15)
- pytest.approx(r.x_coord, 6.647)
- pytest.approx(r.y_coord, 12.9)
- assert r.rect_x == 0.0
- assert str(r) == "<IPC-D-356 +5VDC Test Record: through-hole>"
-
- record_string = (
- "327+3.3VDC R40 -1 PA01X 032100Y 007124X0236Y0315R180 S0"
- )
- r = IPC356_TestRecord.from_line(record_string, FileSettings(units="inch"))
- assert r.feature_type == "smt"
- assert r.net_name == "+3.3VDC"
- assert r.id == "R40"
- assert r.pin == "1"
- assert r.plated
- assert r.access == "top"
- pytest.approx(r.x_coord, 3.21)
- pytest.approx(r.y_coord, 0.7124)
- pytest.approx(r.rect_x, 0.0236)
- pytest.approx(r.rect_y, 0.0315)
- assert r.rect_rotation == 180
- assert r.soldermask_info == "none"
- r = IPC356_TestRecord.from_line(record_string, FileSettings(units="metric"))
- pytest.approx(r.x_coord, 32.1)
- pytest.approx(r.y_coord, 7.124)
- pytest.approx(r.rect_x, 0.236)
- pytest.approx(r.rect_y, 0.315)
-
- record_string = (
- "317 J4 -M2 D0330PA00X 012447Y 008030X0000 S1"
- )
- r = IPC356_TestRecord.from_line(record_string, FileSettings(units="inch"))
- assert r.feature_type == "through-hole"
- assert r.id == "J4"
- assert r.pin == "M2"
- pytest.approx(r.hole_diameter, 0.033)
- assert r.plated
- assert r.access == "both"
- pytest.approx(r.x_coord, 1.2447)
- pytest.approx(r.y_coord, 0.8030)
- pytest.approx(r.rect_x, 0.0)
- assert r.soldermask_info == "primary side"
+from .utils import *
+from ..utils import Inch, MM
+
+
+REFERENCE_FILES = [
+ # TODO add more test files from github
+ 'allegro-2/MinnowMax_RevA1_IPC356A.ipc',
+ 'allegro/08_057494d-ipc356.ipc',
+ 'ipc-d-356.ipc',
+ ]
+
+
+@filter_syntax_warnings
+@pytest.mark.parametrize('reference', REFERENCE_FILES, indirect=True)
+def test_read(reference):
+ netlist = Netlist.open(reference)
+ assert netlist
+ assert netlist.test_records
+
+@filter_syntax_warnings
+@pytest.mark.parametrize('reference', REFERENCE_FILES, indirect=True)
+def test_idempotence(reference, tmpfile):
+ tmp_1 = tmpfile('First generation output', '.ipc')
+ tmp_2 = tmpfile('Second generation output', '.ipc')
+
+ Netlist.open(reference).save(tmp_1)
+ Netlist.open(tmp_1).save(tmp_2)
+
+ assert tmp_1.read_text() == tmp_2.read_text()
+
+@filter_syntax_warnings
+@pytest.mark.parametrize('reference', REFERENCE_FILES, indirect=True)
+def test_bells_and_whistles(reference):
+ netlist = Netlist.open(reference)
+ netlist.net_names()
+ netlist.vias()
+ netlist.reference_designators()
+ netlist.records_by_reference('C1')
+ netlist.records_by_net_name('n001')
+ netlist.conductors_by_net_name('n001')
+ netlist.conductors_by_layer(0)
+
+@filter_syntax_warnings
+@pytest.mark.parametrize('reference', REFERENCE_FILES, indirect=True)
+@pytest.mark.parametrize('other', REFERENCE_FILES, indirect=True)
+def test_merge(reference, other):
+ other = reference_path(other)
+ a = Netlist.open(reference)
+ b = Netlist.open(other)
+ a.merge(b, our_prefix='A')
+ # FIXME asserts
- record_string = "317SCL COMMUNICATION-1 D 40PA00X 34000Y 20000X 600Y1200R270 "
- r = IPC356_TestRecord.from_line(record_string, FileSettings(units="inch"))
- assert r.feature_type == "through-hole"
- assert r.net_name == "SCL"
- assert r.id == "COMMUNICATION"
- assert r.pin == "1"
- pytest.approx(r.hole_diameter, 0.004)
- assert r.plated
- pytest.approx(r.x_coord, 3.4)
- pytest.approx(r.y_coord, 2.0)
diff --git a/gerbonara/gerber/tests/test_pcb.py b/gerbonara/gerber/tests/test_pcb.py
deleted file mode 100644
index 5f513f3..0000000
--- a/gerbonara/gerber/tests/test_pcb.py
+++ /dev/null
@@ -1,7 +0,0 @@
-import os
-from ..pcb import PCB
-
-
-def test_from_directory():
- test_pcb = PCB.from_directory(os.path.join(os.path.dirname(__file__), 'resources/eagle_files'))
- assert len(test_pcb.layers) == 11 # Checks all the available layer files have been read or not.
diff --git a/gerbonara/gerber/tests/test_rs274x_backend.py b/gerbonara/gerber/tests/test_rs274x_backend.py
deleted file mode 100644
index ac39c8f..0000000
--- a/gerbonara/gerber/tests/test_rs274x_backend.py
+++ /dev/null
@@ -1,68 +0,0 @@
-#! /usr/bin/env python
-# -*- coding: utf-8 -*-
-
-# Author: Garret Fick <garret@ficksworkshop.com>
-
-import os
-
-from ..render.rs274x_backend import Rs274xContext
-from ..rs274x import read
-
-
-def test_render_two_boxes():
- """Umaco exapmle of two boxes"""
- _test_render(
- "resources/example_two_square_boxes.gbr", "golden/example_two_square_boxes.gbr"
- )
-
-def _resolve_path(path):
- return os.path.join(os.path.dirname(__file__), path)
-
-
-def _test_render(gerber_path, png_expected_path, create_output_path=None):
- """Render the gerber file and compare to the expected PNG output.
-
- Parameters
- ----------
- gerber_path : string
- Path to Gerber file to open
- png_expected_path : string
- Path to the PNG file to compare to
- create_output : string|None
- If not None, write the generated PNG to the specified path.
- This is primarily to help with
- """
-
- gerber_path = _resolve_path(gerber_path)
- png_expected_path = _resolve_path(png_expected_path)
- if create_output_path:
- create_output_path = _resolve_path(create_output_path)
-
- gerber = read(gerber_path)
-
- # Create GBR output from the input file
- ctx = Rs274xContext(gerber.settings)
- gerber.render(ctx)
-
- actual_contents = ctx.dump()
-
- # If we want to write the file bytes, do it now. This happens
- if create_output_path:
- with open(create_output_path, "wb") as out_file:
- out_file.write(actual_contents.getvalue())
- # Creating the output is dangerous - it could overwrite the expected result.
- # So if we are creating the output, we make the test fail on purpose so you
- # won't forget to disable this
- assert not True, (
- "Test created the output %s. This needs to be disabled to make sure the test behaves correctly"
- % (create_output_path,)
- )
-
- # Read the expected PNG file
-
- with open(png_expected_path, "r") as expected_file:
- expected_contents = expected_file.read()
-
- assert expected_contents == actual_contents.getvalue()
-
- return gerber
diff --git a/gerbonara/gerber/utils.py b/gerbonara/gerber/utils.py
index 5bc03e5..1aad900 100644
--- a/gerbonara/gerber/utils.py
+++ b/gerbonara/gerber/utils.py
@@ -75,6 +75,9 @@ class LengthUnit:
return unit.convert_from(self, value)
+ def format(self, value):
+ return f'{value:.3f}{self.shorthand}'
+
def __call__(self, value, unit):
return self.convert_from(unit, value)