From b3e0ceb5c3ec755b09d2f005b8e3dcbed22d45a1 Mon Sep 17 00:00:00 2001
From: Hamilton Kibbe <hamilton.kibbe@gmail.com>
Date: Fri, 20 Feb 2015 22:24:34 -0500
Subject: Add IPC-D-356 Netlist Parsing

---
 gerber/cam.py                        |  17 +-
 gerber/ipc356.py                     | 314 +++++++++++++++++++++++++++++++++++
 gerber/tests/resources/ipc-d-356.ipc | 114 +++++++++++++
 gerber/tests/test_ipc356.py          | 116 +++++++++++++
 4 files changed, 559 insertions(+), 2 deletions(-)
 create mode 100644 gerber/ipc356.py
 create mode 100644 gerber/tests/resources/ipc-d-356.ipc
 create mode 100644 gerber/tests/test_ipc356.py

(limited to 'gerber')

diff --git a/gerber/cam.py b/gerber/cam.py
index 243070d..31b6d2f 100644
--- a/gerber/cam.py
+++ b/gerber/cam.py
@@ -53,7 +53,8 @@ class FileSettings(object):
     and vice versa
     """
     def __init__(self, notation='absolute', units='inch',
-                 zero_suppression=None, format=(2, 5), zeros=None):
+                 zero_suppression=None, format=(2, 5), zeros=None,
+                 angle_units='degrees'):
         if notation not in ['absolute', 'incremental']:
             raise ValueError('Notation must be either absolute or incremental')
         self.notation = notation
@@ -84,6 +85,10 @@ class FileSettings(object):
             raise ValueError('Format must be a tuple(n=2) of integers')
         self.format = format
 
+        if angle_units not in ('degrees', 'radians'):
+            raise ValueError('Angle units may be degrees or radians')
+        self.angle_units = angle_units
+
     @property
     def zero_suppression(self):
         return self._zero_suppression
@@ -114,6 +119,8 @@ class FileSettings(object):
             return self.zeros
         elif key == 'format':
             return self.format
+        elif key == 'angle_units':
+            return self.angle_units
         else:
             raise KeyError()
 
@@ -144,6 +151,11 @@ class FileSettings(object):
                 raise ValueError('Format must be a tuple(n=2) of integers')
             self.format = value
 
+        elif key == 'angle_units':
+            if value not in ('degrees', 'radians'):
+                raise ValueError('Angle units may be degrees or radians')
+            self.angle_units = value
+
         else:
             raise KeyError('%s is not a valid key' % key)
 
@@ -151,7 +163,8 @@ class FileSettings(object):
         return (self.notation == other.notation and
                 self.units == other.units and
                 self.zero_suppression == other.zero_suppression and
-                self.format == other.format)
+                self.format == other.format and
+                self.angle_units == other.angle_units)
 
 
 class CamFile(object):
diff --git a/gerber/ipc356.py b/gerber/ipc356.py
new file mode 100644
index 0000000..2b6f1f6
--- /dev/null
+++ b/gerber/ipc356.py
@@ -0,0 +1,314 @@
+#! /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.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import math
+import re
+from .cam import 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]*)?')
+
+
+def read(filename):
+    """ Read data from filename and return an IPC_D_356
+    Parameters
+        ----------
+    filename : string
+        Filename of file to parse
+
+    Returns
+    -------
+    file : :class:`gerber.ipc356.IPC_D_356`
+        An IPC_D_356 object created from the specified file.
+
+    """
+    # File object should use settings from source file by default.
+    return IPC_D_356.from_file(filename)
+
+
+class IPC_D_356(object):
+
+    @classmethod
+    def from_file(self, filename):
+        p = IPC_D_356_Parser()
+        return p.parse(filename)
+
+
+    def __init__(self, statements, settings):
+        self.statements = statements
+        self.units = settings.units
+        self.angle_units = settings.angle_units
+
+    @property
+    def settings(self):
+        return FileSettings(units=self.units, angle_units=self.angle_units)
+
+    @property
+    def comments(self):
+        return [record for record in self.statements
+                if isinstance(record, IPC356_Comment)]
+
+    @property
+    def parameters(self):
+        return [record for record in self.statements
+                if isinstance(record, IPC356_Parameter)]
+
+    @property
+    def test_records(self):
+        return [record for record in self.statements
+                if isinstance(record, IPC356_TestRecord)]
+
+    @property
+    def nets(self):
+        return list(set([rec.net_name for rec in self.test_records
+                         if rec.net_name is not None]))
+
+    @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']
+
+    @property
+    def board_outline(self):
+        outline = [stmt for stmt in self.statements if isinstance(stmt, IPC356_BoardEdge)]
+        if len(outline):
+            return outline[0].points
+        else:
+            return None
+
+class IPC_D_356_Parser(object):
+    # TODO: Allow multi-line statements (e.g. Altium board edge)
+    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:
+            for line in f:
+
+                if line[0] == 'C':
+                    # Comment
+                    self.statements.append(IPC356_Comment.from_line(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
+
+                elif line[0] == '3' and line[2] == '7':
+                    # Test Record
+                    record = IPC356_TestRecord.from_line(line, self.settings)
+
+                    # 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] == '389':
+                    # Altium Board Edge Info
+                    self.statements.append(IPC356_BoardEdge.from_line(line, self.settings))
+
+                elif line[0] == '9':
+                    self.multiline = False
+                    self.statements.append(IPC356_EndOfFile())
+
+        return IPC_D_356(self.statements, self.settings)
+
+
+class IPC356_Comment(object):
+    @classmethod
+    def from_line(cls, line):
+        if line[0] != 'C':
+            raise ValueError('Not a valid comment statment')
+        comment = line[2:].strip()
+        return cls(comment)
+
+    def __init__(self, comment):
+        self.comment = comment
+
+    def __repr__(self):
+        return '<IPC-D-356 Comment: %s>' % self.comment
+
+
+class IPC356_Parameter(object):
+    @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)
+
+    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 IPC356_TestRecord(object):
+    @classmethod
+    def from_line(cls, line, settings):
+        units = settings.units
+        angle = settings.angle_units
+        feature_types = {'1':'through-hole', '2': 'smt',
+                         '3':'tooling-feature', '4':'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()
+
+        end = len(line) - 1 if len(line) < 27 else 26
+        record['id'] = line[20:end].strip()
+
+        end = len(line) - 1 if len(line) < 32 else 31
+        record['pin'] = (line[27:end].strip() if line[27:end].strip() != ''
+                         else None)
+
+        record['location'] = 'middle' if line[31] == 'M' else 'end'
+        if line[32] == 'D':
+            end = len(line) - 1 if len(line) < 38 else 37
+            dia = int(line[33:end].strip())
+            record['hole_diameter'] = (dia * 0.0001 if units == 'inch'
+                                       else dia * 0.001)
+        if len(line) >= 38:
+            record['plated'] = (line[37] == 'P')
+
+        if len(line) >= 40:
+            end = len(line) - 1 if len(line) < 42 else 41
+            record['access'] = access[int(line[39:end])]
+
+        if len(line) >= 43:
+            end = len(line) - 1 if len(line) < 50 else 49
+            coord = int(line[42:49].strip())
+            record['x_coord'] = (coord *  0.0001 if units == 'inch'
+                                 else coord * 0.001)
+
+        if len(line) >= 51:
+            end = len(line) - 1 if len(line) < 58 else 57
+            coord = int(line[50:57].strip())
+            record['y_coord'] = (coord *  0.0001 if units == 'inch'
+            else coord * 0.001)
+
+        if len(line) >= 59:
+            end = len(line) - 1 if len(line) < 63 else 62
+            dim = line[58:62].strip()
+            if dim != '':
+                record['rect_x'] = (int(dim) * 0.0001 if units == 'inch'
+                                else int(dim) * 0.001)
+
+        if len(line) >= 64:
+            end = len(line) - 1 if len(line) < 68 else 67
+            dim = line[63:67].strip()
+            if dim != '':
+                record['rect_y'] = (int(dim) * 0.0001 if units == 'inch'
+                                    else int(dim) * 0.001)
+
+        if len(line) >= 69:
+            end = len(line) - 1 if len(line) < 72 else 71
+            rot = line[68:71].strip()
+            if rot != '':
+                record['rect_rotation'] = (int(rot) if angle == 'degrees'
+                                           else math.degrees(rot))
+
+        if len(line) >= 74:
+            end = len(line) - 1 if len(line) < 75 else 74
+            record['soldermask_info'] = line[73:74].strip()
+
+        if len(line) >= 76:
+            end = len(line) - 1 if len(line < 80) else 79
+            record['optional_info'] = line[75:end]
+
+        return cls(**record)
+
+    def __init__(self, **kwargs):
+        for key in kwargs:
+            setattr(self, key, kwargs[key])
+
+    def __repr__(self):
+        return '<IPC-D-356 Test Record: Net: %s Type: %s>' % (self.net_name,
+                                                            self.feature_type)
+
+class IPC356_BoardEdge(object):
+
+    @classmethod
+    def from_line(cls, line, settings):
+        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'] is not '' else x
+            y = int(coord_dict['y']) if coord_dict['y'] is not '' else y
+            points.append((x * scale, y * scale))
+        return cls(points)
+
+    def __init__(self, points):
+        self.points = points
+
+    def __repr__(self):
+        return '<IPC-D-356 Board Edge Definition>'
+
+
+
+class IPC356_EndOfFile(object):
+    def __init__(self):
+        pass
+
+    def to_netlist(self):
+        return '999'
+
+    def __repr__(self):
+        return '<IPC-D-356 EOF>'
diff --git a/gerber/tests/resources/ipc-d-356.ipc b/gerber/tests/resources/ipc-d-356.ipc
new file mode 100644
index 0000000..b0086c9
--- /dev/null
+++ b/gerber/tests/resources/ipc-d-356.ipc
@@ -0,0 +1,114 @@
+C  IPC-D-356 generated by EAGLE Version 7.1.0 Copyright (c) 1988-2014 CadSoft
+C  Database /Some/Path/To/File
+C
+P  JOB EAGLE 7.1 NETLIST, DATE: 2/20/15 12:00 AM
+P  UNITS CUST 0
+P  DIM N
+P  NNAME1 A_REALLY_LONG_NET_NAME
+317GND              VIA         D  24PA00X  14900Y   1450X 396Y 396
+317GND              VIA         D  24PA00X   3850Y   8500X 396Y 396
+317GND              VIA         D  24PA00X   6200Y  10650X 396Y 396
+317GND              VIA         D  24PA00X   8950Y   1000X 396Y 396
+317GND              VIA         D  24PA00X  11800Y   2250X 396Y 396
+317GND              VIA         D  24PA00X  15350Y   3200X 396Y 396
+317GND              VIA         D  24PA00X  13200Y   3800X 396Y 396
+317GND              VIA         D  24PA00X   9700Y  12050X 396Y 396
+317GND              VIA         D  24PA00X  13950Y  11900X 396Y 396
+317GND              VIA         D  24PA00X  13050Y   7050X 396Y 396
+317GND              VIA         D  24PA00X  13000Y   8400X 396Y 396
+317N$3              VIA         D  24PA00X  11350Y  10100X 396Y 396
+317N$3              VIA         D  24PA00X  13250Y   5700X 396Y 396
+317VCC              VIA         D  24PA00X  15550Y   6850X 396Y 396
+327N$3              C1    -+          A01X   9700Y  10402X1575Y 630R270
+327GND              C1    --          A01X   9700Y  13198X1575Y 630R270
+327VCC              C2    -+          A01X  13950Y   9677X1535Y 630R270
+327GND              C2    --          A01X  13950Y  13023X1535Y 630R270
+327VCC              C3    -1          A01X   3850Y   9924X 512Y 591R270
+327GND              C3    -2          A01X   3850Y   9176X 512Y 591R270
+327VCC              C4    -1          A01X  10374Y   1000X 512Y 591R180
+327GND              C4    -2          A01X   9626Y   1000X 512Y 591R180
+327VCC              C5    -1          A01X  14700Y   3924X 512Y 591R270
+327GND              C5    -2          A01X  14700Y   3176X 512Y 591R270
+317DMX+             DMX   -1    D  40PA00X   5050Y  13900X 600Y1200R 90
+317DMX-             DMX   -2    D  40PA00X   6050Y  13900X 600Y1200R 90
+317GND              DMX   -3    D  40PA00X   7050Y  13900X 600Y1200R 90
+317PIC_MCLR         J1    -1    D  35PA00X  16900Y   6400X 554Y 554R 90
+317VCC              J1    -2    D  35PA00X  17900Y   6900X 554Y 554R 90
+317GND              J1    -3    D  35PA00X  16900Y   7400X 554Y 554R 90
+317PIC_PGD          J1    -4    D  35PA00X  17900Y   7900X 554Y 554R 90
+317PIC_PGC          J1    -5    D  35PA00X  16900Y   8400X 554Y 554R 90
+317                 J1    -6    D  35PA00X  17900Y   8900X 554Y 554R 90
+327N$4              L1    -1          A01X  13950Y   6382X 748Y1339R 90
+327VCC              L1    -2          A01X  13950Y   7918X 748Y1339R 90
+327N$5              LED1  -A          A01X  16313Y   1450X 472Y 472R  0
+327GND              LED1  -C          A01X  15487Y   1450X 472Y 472R  0
+317                 MIDI  -1    D  40PA00X   1200Y   9500X 600Y1200R  0
+317                 MIDI  -2    D  40PA00X   1200Y   8500X 600Y1200R  0
+317                 MIDI  -3    D  40PA00X   1200Y   7500X 600Y1200R  0
+317N$9              MIDI  -4    D  40PA00X   1200Y   6500X 600Y1200R  0
+317N$10             MIDI  -5    D  40PA00X   1200Y   5500X 600Y1200R  0
+317N$3              PWR   -1    D  40PA00X  17050Y  13750X 600Y1200R 90
+317GND              PWR   -2    D  40PA00X  18050Y  13750X 600Y1200R 90
+327DMX+             R1    -1          A01X   5076Y  11500X 512Y 591R  0
+327DMX-             R1    -2          A01X   5824Y  11500X 512Y 591R  0
+327VCC              R2    -1          A01X  14376Y   5300X 512Y 591R  0
+327PIC_MCLR         R2    -2          A01X  15124Y   5300X 512Y 591R  0
+327N$9              R3    -1          A01X   3126Y   6500X 512Y 591R  0
+327N$6              R3    -2          A01X   3874Y   6500X 512Y 591R  0
+327PIC_RX           R4    -1          A01X   9600Y   2624X 512Y 591R270
+327VCC              R4    -2          A01X   9600Y   1876X 512Y 591R270
+327VCC              R5    -1          A01X  17974Y   1450X 512Y 591R180
+327N$5              R5    -2          A01X  17226Y   1450X 512Y 591R180
+327N$3              U1    -1          A01X  12330Y   5710X 420Y 850R 90
+327N$4              U1    -2          A01X  12330Y   6380X 420Y 850R 90
+327GND              U1    -3          A01X  12330Y   7050X 420Y 850R 90
+327VCC              U1    -4          A01X  12330Y   7720X 420Y 850R 90
+327GND              U1    -5          A01X  12330Y   8390X 420Y 850R 90
+327                 U1    -6          A01X   9050Y   7050X4252Y4098R 90
+327PIC_MCLR         U2    -1          A01X  11123Y   4063X 157Y 591R270
+327                 U2    -2          A01X  11123Y   3807X 157Y 591R270
+327                 U2    -3          A01X  11123Y   3552X 157Y 591R270
+327N$1              U2    -4          A01X  11123Y   3296X 157Y 591R270
+327N$2              U2    -5          A01X  11123Y   3040X 157Y 591R270
+327PIC_RX           U2    -6          A01X  11123Y   2784X 157Y 591R270
+327                 U2    -7          A01X  11123Y   2528X 157Y 591R270
+327GND              U2    -8          A01X  11123Y   2272X 157Y 591R270
+327                 U2    -9          A01X  11123Y   2016X 157Y 591R270
+327                 U2    -10         A01X  11123Y   1760X 157Y 591R270
+327                 U2    -11         A01X  11123Y   1504X 157Y 591R270
+327                 U2    -12         A01X  11123Y   1248X 157Y 591R270
+327VCC              U2    -13         A01X  11123Y    993X 157Y 591R270
+327                 U2    -14         A01X  11123Y    737X 157Y 591R270
+327                 U2    -15         A01X  13977Y    737X 157Y 591R270
+327                 U2    -16         A01X  13977Y    993X 157Y 591R270
+327                 U2    -17         A01X  13977Y   1248X 157Y 591R270
+327                 U2    -18         A01X  13977Y   1504X 157Y 591R270
+327                 U2    -19         A01X  13977Y   1760X 157Y 591R270
+327                 U2    -20         A01X  13977Y   2016X 157Y 591R270
+327PIC_PGD          U2    -21         A01X  13977Y   2272X 157Y 591R270
+327PIC_PGC          U2    -22         A01X  13977Y   2528X 157Y 591R270
+327                 U2    -23         A01X  13977Y   2784X 157Y 591R270
+327                 U2    -24         A01X  13977Y   3040X 157Y 591R270
+327                 U2    -25         A01X  13977Y   3296X 157Y 591R270
+327                 U2    -26         A01X  13977Y   3552X 157Y 591R270
+327GND              U2    -27         A01X  13977Y   3807X 157Y 591R270
+327VCC              U2    -28         A01X  13977Y   4063X 157Y 591R270
+327N$2              U3    -1          A01X   4700Y   7540X 260Y 800R  0
+327VCC              U3    -2          A01X   5200Y   7540X 260Y 800R  0
+327VCC              U3    -3          A01X   5700Y   7540X 260Y 800R  0
+327N$1              U3    -4          A01X   6200Y   7540X 260Y 800R  0
+327GND              U3    -5          A01X   6200Y   9960X 260Y 800R  0
+327DMX-             U3    -6          A01X   5700Y   9960X 260Y 800R  0
+327DMX+             U3    -7          A01X   5200Y   9960X 260Y 800R  0
+327VCC              U3    -8          A01X   4700Y   9960X 260Y 800R  0
+327                 U4    -1          A01X   4704Y   3850X 394Y 500R  0
+327N$6              U4    -2          A01X   4704Y   2800X 394Y 500R  0
+327N$10             U4    -3          A01X   4704Y   1800X 394Y 500R  0
+327                 U4    -4          A01X   4704Y    750X 394Y 500R  0
+327GND              U4    -5          A01X   8396Y    750X 394Y 500R  0
+327PIC_RX           U4    -6          A01X   8396Y   1800X 394Y 500R  0
+327                 U4    -7          A01X   8396Y   2800X 394Y 500R  0
+327VCC              U4    -8          A01X   8396Y   3850X 394Y 500R  0
+327NNAME1           NA    -69         A01X   8396Y   3850X 394Y 500R  0
+389BOARD_EDGE         X0Y0 X22500 Y15000 X0
+999
diff --git a/gerber/tests/test_ipc356.py b/gerber/tests/test_ipc356.py
new file mode 100644
index 0000000..760608c
--- /dev/null
+++ b/gerber/tests/test_ipc356.py
@@ -0,0 +1,116 @@
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Author: Hamilton Kibbe <ham@hamiltonkib.be>
+from ..ipc356 import  *
+from ..cam import FileSettings
+from .tests import *
+
+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, IPC_D_356))
+
+def test_parser():
+    ipcfile = read(IPC_D_356_FILE)
+    assert_equal(ipcfile.settings.units, 'inch')
+    assert_equal(ipcfile.settings.angle_units, 'degrees')
+    assert_equal(len(ipcfile.comments), 3)
+    assert_equal(len(ipcfile.parameters), 4)
+    assert_equal(len(ipcfile.test_records), 105)
+    assert_equal(len(ipcfile.components), 21)
+    assert_equal(len(ipcfile.vias), 14)
+    assert_equal(ipcfile.test_records[-1].net_name, 'A_REALLY_LONG_NET_NAME')
+    assert_equal(set(ipcfile.board_outline),
+                 {(0., 0.), (2.25, 0.), (2.25, 1.5), (0., 1.5)})
+
+def test_comment():
+    c = IPC356_Comment('Layer Stackup:')
+    assert_equal(c.comment, 'Layer Stackup:')
+    c = IPC356_Comment.from_line('C  Layer Stackup:   ')
+    assert_equal(c.comment, 'Layer Stackup:')
+    assert_raises(ValueError, IPC356_Comment.from_line, 'P  JOB')
+    assert_equal(str(c), '<IPC-D-356 Comment: Layer Stackup:>')
+
+def test_parameter():
+    p = IPC356_Parameter('VER', 'IPC-D-356A')
+    assert_equal(p.parameter, 'VER')
+    assert_equal(p.value, 'IPC-D-356A')
+    p = IPC356_Parameter.from_line('P  VER IPC-D-356A    ')
+    assert_equal(p.parameter, 'VER')
+    assert_equal(p.value, 'IPC-D-356A')
+    assert_raises(ValueError, IPC356_Parameter.from_line, 'C  Layer Stackup:   ')
+    assert_equal(str(p), '<IPC-D-356 Parameter: VER=IPC-D-356A>')
+
+def test_eof():
+    e = IPC356_EndOfFile()
+    assert_equal(e.to_netlist(), '999')
+    assert_equal(str(e), '<IPC-D-356 EOF>')
+
+def test_board_edge():
+    points = [(0.01, 0.01), (2., 2.), (4., 2.), (4., 6.)]
+    b = IPC356_BoardEdge(points)
+    assert_equal(b.points, points)
+    b = IPC356_BoardEdge.from_line('389BOARD_EDGE         X100Y100 X20000Y20000'
+                                   ' X40000 Y60000', FileSettings(units='inch'))
+    assert_equal(b.points, points)
+
+def test_test_record():
+    assert_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_equal(r.feature_type, 'through-hole')
+    assert_equal(r.net_name, '+5VDC')
+    assert_equal(r.id, 'VIA')
+    assert_almost_equal(r.hole_diameter, 0.015)
+    assert_true(r.plated)
+    assert_equal(r.access, 'both')
+    assert_almost_equal(r.x_coord, 0.6647)
+    assert_almost_equal(r.y_coord, 1.29)
+    assert_equal(r.rect_x, 0.)
+    assert_equal(r.soldermask_info, '3')
+    r = IPC356_TestRecord.from_line(record_string, FileSettings(units='metric'))
+    assert_almost_equal(r.hole_diameter, 0.15)
+    assert_almost_equal(r.x_coord, 6.647)
+    assert_almost_equal(r.y_coord, 12.9)
+    assert_equal(r.rect_x, 0.)
+    assert_equal(str(r),
+                 '<IPC-D-356 Test Record: Net: +5VDC Type: through-hole>')
+
+    record_string = '327+3.3VDC          R40   -1         PA01X 032100Y 007124X0236Y0315R180 S0'
+    r = IPC356_TestRecord.from_line(record_string, FileSettings(units='inch'))
+    assert_equal(r.feature_type, 'smt')
+    assert_equal(r.net_name, '+3.3VDC')
+    assert_equal(r.id, 'R40')
+    assert_equal(r.pin, '1')
+    assert_true(r.plated)
+    assert_equal(r.access, 'top')
+    assert_almost_equal(r.x_coord, 3.21)
+    assert_almost_equal(r.y_coord, 0.7124)
+    assert_almost_equal(r.rect_x, 0.0236)
+    assert_almost_equal(r.rect_y, 0.0315)
+    assert_equal(r.rect_rotation, 180)
+    assert_equal(r.soldermask_info, '0')
+    r = IPC356_TestRecord.from_line(record_string, FileSettings(units='metric'))
+    assert_almost_equal(r.x_coord, 32.1)
+    assert_almost_equal(r.y_coord, 7.124)
+    assert_almost_equal(r.rect_x, 0.236)
+    assert_almost_equal(r.rect_y, 0.315)
+
+
+    record_string = '317                 J4    -M2   D0330PA00X 012447Y 008030X0000          S0'
+    r = IPC356_TestRecord.from_line(record_string, FileSettings(units='inch'))
+    assert_equal(r.feature_type, 'through-hole')
+    assert_equal(r.id, 'J4')
+    assert_equal(r.pin, 'M2')
+    assert_almost_equal(r.hole_diameter, 0.033)
+    assert_true(r.plated)
+    assert_equal(r.access, 'both')
+    assert_almost_equal(r.x_coord, 1.2447)
+    assert_almost_equal(r.y_coord, 0.8030)
+    assert_almost_equal(r.rect_x, 0.)
+    assert_equal(r.soldermask_info, '0')
+
-- 
cgit