summaryrefslogtreecommitdiff
path: root/gerber/tests/test_layers.py
blob: 3a21a2c482fcf7a91e70c2fdb9ee0e83a95d4482 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#! /usr/bin/env python
# -*- coding: utf-8 -*-

# copyright 2016 Hamilton Kibbe <ham@hamiltonkib.be>
#
# 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 os

from .tests import *
from ..layers import *
from ..common import read

NCDRILL_FILE = os.path.join(os.path.dirname(__file__),
                            'resources/ncdrill.DRD')
NETLIST_FILE = os.path.join(os.path.dirname(__file__),
                            'resources/ipc-d-356.ipc')
COPPER_FILE = os.path.join(os.path.dirname(__file__),
                           'resources/top_copper.GTL')

def test_guess_layer_class():
    """ Test layer type inferred correctly from filename
    """

    # Add any specific test cases here (filename, layer_class)
    test_vectors = [(None, 'unknown'), ('NCDRILL.TXT', 'unknown'),
                    ('example_board.gtl', 'top'),
                    ('exampmle_board.sst', 'topsilk'),
                    ('ipc-d-356.ipc', 'ipc_netlist'), ]

    for hint in hints:
        for ext in hint.ext:
            assert_equal(hint.layer, guess_layer_class('board.{}'.format(ext)))
        for name in hint.name:
            assert_equal(hint.layer, guess_layer_class('{}.pho'.format(name)))

    for filename, layer_class in test_vectors:
        assert_equal(layer_class, guess_layer_class(filename))

def test_guess_layer_class_regex():
    """ Test regular expressions for layer matching
    """

    # Add any specific test case (filename, layer_class)
    test_vectors = [('test - top copper.gbr', 'top'),
                    ('test - copper top.gbr', 'top'), ]

    # Add custom regular expressions
    layer_hints = [
        Hint(layer='top',
                ext=[],
                name=[],
                regex=r'(.*)(\scopper top|\stop copper).gbr',
                content=[]
            ),
    ]
    hints.extend(layer_hints)

    for filename, layer_class in test_vectors:
        assert_equal(layer_class, guess_layer_class(filename))


def test_guess_layer_class_by_content():
    """ Test layer class by checking content
    """

    expected_layer_class = 'bottom'
    filename = os.path.join(os.path.dirname(__file__),
                               'resources/example_guess_by_content.g0')

    layer_hints = [
        Hint(layer='bottom',
                ext=[],
                name=[],
                regex='',
                content=['G04 Layer name: Bottom']
            )
    ]
    hints.extend(layer_hints)

    assert_equal(expected_layer_class, guess_layer_class_by_content(filename))


def test_sort_layers():
    """ Test layer ordering
    """
    layers = [
        PCBLayer(layer_class='drawing'),
        PCBLayer(layer_class='drill'),
        PCBLayer(layer_class='bottompaste'),
        PCBLayer(layer_class='bottomsilk'),
        PCBLayer(layer_class='bottommask'),
        PCBLayer(layer_class='bottom'),
        PCBLayer(layer_class='internal'),
        PCBLayer(layer_class='top'),
        PCBLayer(layer_class='topmask'),
        PCBLayer(layer_class='topsilk'),
        PCBLayer(layer_class='toppaste'),
        PCBLayer(layer_class='outline'),
    ]

    layer_order = ['outline', 'toppaste', 'topsilk', 'topmask', 'top',
                   'internal', 'bottom', 'bottommask', 'bottomsilk',
                   'bottompaste', 'drill', 'drawing']
    bottom_order = list(reversed(layer_order[:10])) + layer_order[10:]
    assert_equal([l.layer_class for l in sort_layers(layers)], layer_order)
    assert_equal([l.layer_class for l in sort_layers(layers, from_top=False)],
                 bottom_order)


def test_PCBLayer_from_file():
    layer = PCBLayer.from_cam(read(COPPER_FILE))
    assert_true(isinstance(layer, PCBLayer))
    layer = PCBLayer.from_cam(read(NCDRILL_FILE))
    assert_true(isinstance(layer, DrillLayer))
    layer = PCBLayer.from_cam(read(NETLIST_FILE))
    assert_true(isinstance(layer, PCBLayer))
    assert_equal(layer.layer_class, 'ipc_netlist')


def test_PCBLayer_bounds():
    source = read(COPPER_FILE)
    layer = PCBLayer.from_cam(source)
    assert_equal(source.bounds, layer.bounds)


def test_DrillLayer_from_cam():
    no_exceptions = True
    try:
        layer = DrillLayer.from_cam(read(NCDRILL_FILE))
        assert_true(isinstance(layer, DrillLayer))
    except:
        no_exceptions = False
    assert_true(no_exceptions)