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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# copyright 2014 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
import re
from collections import namedtuple
from .excellon import ExcellonFile
from .ipc356 import IPCNetlist
Hint = namedtuple('Hint', 'layer ext name regex content')
hints = [
Hint(layer='top',
ext=['gtl', 'cmp', 'top', ],
name=['art01', 'top', 'GTL', 'layer1', 'soldcom', 'comp', 'F.Cu', ],
regex='',
content=[]
),
Hint(layer='bottom',
ext=['gbl', 'sld', 'bot', 'sol', 'bottom', ],
name=['art02', 'bottom', 'bot', 'GBL', 'layer2', 'soldsold', 'B.Cu', ],
regex='',
content=[]
),
Hint(layer='internal',
ext=['in', 'gt1', 'gt2', 'gt3', 'gt4', 'gt5', 'gt6',
'g1', 'g2', 'g3', 'g4', 'g5', 'g6', ],
name=['art', 'internal', 'pgp', 'pwr', 'gnd', 'ground',
'gp1', 'gp2', 'gp3', 'gp4', 'gt5', 'gp6',
'In1.Cu', 'In2.Cu', 'In3.Cu', 'In4.Cu',
'group3', 'group4', 'group5', 'group6', 'group7', 'group8',
'copper_top_l1', 'copper_inner_l2', 'copper_inner_l3', 'copper_bottom_l4', ],
regex='',
content=[]
),
Hint(layer='topsilk',
ext=['gto', 'sst', 'plc', 'ts', 'skt', 'topsilk'],
name=['sst01', 'topsilk', 'silk', 'slk', 'sst', 'F.SilkS', 'silkscreen_top'],
regex='',
content=[]
),
Hint(layer='bottomsilk',
ext=['gbo', 'ssb', 'pls', 'bs', 'skb', 'bottomsilk'],
name=['bsilk', 'ssb', 'botsilk', 'bottomsilk', 'B.SilkS', 'silkscreen_bottom'],
regex='',
content=[]
),
Hint(layer='topmask',
ext=['gts', 'stc', 'tmk', 'smt', 'tr', 'topmask', ],
name=['sm01', 'cmask', 'tmask', 'mask1', 'maskcom', 'topmask',
'mst', 'F.Mask', 'soldermask_top'],
regex='',
content=[]
),
Hint(layer='bottommask',
ext=['gbs', 'sts', 'bmk', 'smb', 'br', 'bottommask', ],
name=['sm', 'bmask', 'mask2', 'masksold', 'botmask', 'bottommask',
'msb', 'B.Mask', 'soldermask_bottom'],
regex='',
content=[]
),
Hint(layer='toppaste',
ext=['gtp', 'tm', 'toppaste', ],
name=['sp01', 'toppaste', 'pst', 'F.Paste', 'solderpaste_top'],
regex='',
content=[]
),
Hint(layer='bottompaste',
ext=['gbp', 'bm', 'bottompaste', ],
name=['sp02', 'botpaste', 'bottompaste', 'psb', 'B.Paste', 'solderpaste_bottom'],
regex='',
content=[]
),
Hint(layer='outline',
ext=['gko', 'outline', ],
name=['BDR', 'border', 'out', 'outline', 'Edge.Cuts', 'profile'],
regex='',
content=[]
),
Hint(layer='ipc_netlist',
ext=['ipc'],
name=[],
regex='',
content=[]
),
Hint(layer='drawing',
ext=['fab'],
name=['assembly drawing', 'assembly', 'fabrication',
'fab drawing', 'fab'],
regex='',
content=[]
),
]
def layer_signatures(layer_class):
for hint in hints:
if hint.layer == layer_class:
return hint.ext + hint.name
return []
def load_layer(filename):
return PCBLayer.from_cam(common.read(filename))
def load_layer_data(data, filename=None):
return PCBLayer.from_cam(common.loads(data, filename))
def guess_layer_class(filename):
try:
layer = guess_layer_class_by_content(filename)
if layer:
return layer
except:
pass
try:
directory, filename = os.path.split(filename)
name, ext = os.path.splitext(filename.lower())
for hint in hints:
if hint.regex:
if re.findall(hint.regex, filename, re.IGNORECASE):
return hint.layer
patterns = [r'^(\w*[.-])*{}([.-]\w*)?$'.format(x) for x in hint.name]
if ext[1:] in hint.ext or any(re.findall(p, name, re.IGNORECASE) for p in patterns):
return hint.layer
except:
pass
return 'unknown'
def guess_layer_class_by_content(filename):
try:
file = open(filename, 'r')
for line in file:
for hint in hints:
if len(hint.content) > 0:
patterns = [r'^(.*){}(.*)$'.format(x) for x in hint.content]
if any(re.findall(p, line, re.IGNORECASE) for p in patterns):
return hint.layer
except:
pass
return False
def sort_layers(layers, from_top=True):
layer_order = ['outline', 'toppaste', 'topsilk', 'topmask', 'top',
'internal', 'bottom', 'bottommask', 'bottomsilk',
'bottompaste']
append_after = ['drill', 'drawing']
output = []
drill_layers = [layer for layer in layers if layer.layer_class == 'drill']
internal_layers = list(sorted([layer for layer in layers
if layer.layer_class == 'internal']))
for layer_class in layer_order:
if layer_class == 'internal':
output += internal_layers
elif layer_class == 'drill':
output += drill_layers
else:
for layer in layers:
if layer.layer_class == layer_class:
output.append(layer)
if not from_top:
output = list(reversed(output))
for layer_class in append_after:
for layer in layers:
if layer.layer_class == layer_class:
output.append(layer)
return output
class PCBLayer(object):
""" Base class for PCB Layers
Parameters
----------
source : CAMFile
CAMFile representing the layer
Attributes
----------
filename : string
Source Filename
"""
@classmethod
def from_cam(cls, camfile):
filename = camfile.filename
layer_class = guess_layer_class(filename)
if isinstance(camfile, ExcellonFile) or (layer_class == 'drill'):
return DrillLayer.from_cam(camfile)
elif layer_class == 'internal':
return InternalLayer.from_cam(camfile)
if isinstance(camfile, IPCNetlist):
layer_class = 'ipc_netlist'
return cls(filename, layer_class, camfile)
def __init__(self, filename=None, layer_class=None, cam_source=None, **kwargs):
super(PCBLayer, self).__init__(**kwargs)
self.filename = filename
self.layer_class = layer_class
self.cam_source = cam_source
self.surface = None
self.primitives = cam_source.primitives if cam_source is not None else []
@property
def bounds(self):
if self.cam_source is not None:
return self.cam_source.bounds
else:
return None
def __repr__(self):
return '<PCBLayer: {}>'.format(self.layer_class)
class DrillLayer(PCBLayer):
@classmethod
def from_cam(cls, camfile):
return cls(camfile.filename, camfile)
def __init__(self, filename=None, cam_source=None, layers=None, **kwargs):
super(DrillLayer, self).__init__(filename, 'drill', cam_source, **kwargs)
self.layers = layers if layers is not None else ['top', 'bottom']
class InternalLayer(PCBLayer):
@classmethod
def from_cam(cls, camfile):
filename = camfile.filename
try:
order = int(re.search(r'\d+', filename).group())
except AttributeError:
order = 0
return cls(filename, camfile, order)
def __init__(self, filename=None, cam_source=None, order=0, **kwargs):
super(InternalLayer, self).__init__(filename, 'internal', cam_source, **kwargs)
self.order = order
def __eq__(self, other):
if not hasattr(other, 'order'):
raise TypeError()
return (self.order == other.order)
def __ne__(self, other):
if not hasattr(other, 'order'):
raise TypeError()
return (self.order != other.order)
def __gt__(self, other):
if not hasattr(other, 'order'):
raise TypeError()
return (self.order > other.order)
def __lt__(self, other):
if not hasattr(other, 'order'):
raise TypeError()
return (self.order < other.order)
def __ge__(self, other):
if not hasattr(other, 'order'):
raise TypeError()
return (self.order >= other.order)
def __le__(self, other):
if not hasattr(other, 'order'):
raise TypeError()
return (self.order <= other.order)
class PCB:
@classmethod
def from_directory(cls, directory, board_name=None, verbose=False):
layers = []
names = set()
# Validate
directory = os.path.abspath(directory)
if not os.path.isdir(directory):
raise TypeError('{} is not a directory.'.format(directory))
# Load gerber files
for filename in os.listdir(directory):
try:
camfile = gerber_read(os.path.join(directory, filename))
layer = PCBLayer.from_cam(camfile)
layers.append(layer)
name = os.path.splitext(filename)[0]
if len(os.path.splitext(filename)) > 1:
_name, ext = os.path.splitext(name)
if ext[1:] in layer_signatures(layer.layer_class):
name = _name
if layer.layer_class == 'drill' and 'drill' in ext:
name = _name
names.add(name)
if verbose:
print('[PCB]: Added {} layer <{}>'.format(layer.layer_class,
filename))
except ParseError:
if verbose:
print('[PCB]: Skipping file {}'.format(filename))
except IOError:
if verbose:
print('[PCB]: Skipping file {}'.format(filename))
# Try to guess board name
if board_name is None:
if len(names) == 1:
board_name = names.pop()
else:
board_name = os.path.basename(directory)
# Return PCB
return cls(layers, board_name)
def __init__(self, layers, name=None):
self.layers = sort_layers(layers)
self.name = name
def __len__(self):
return len(self.layers)
@property
def top_layers(self):
board_layers = [l for l in reversed(self.layers) if l.layer_class in
('topsilk', 'topmask', 'top')]
drill_layers = [l for l in self.drill_layers if 'top' in l.layers]
# Drill layer goes under soldermask for proper rendering of tented vias
return [board_layers[0]] + drill_layers + board_layers[1:]
@property
def bottom_layers(self):
board_layers = [l for l in self.layers if l.layer_class in
('bottomsilk', 'bottommask', 'bottom')]
drill_layers = [l for l in self.drill_layers if 'bottom' in l.layers]
# Drill layer goes under soldermask for proper rendering of tented vias
return [board_layers[0]] + drill_layers + board_layers[1:]
@property
def drill_layers(self):
return [l for l in self.layers if l.layer_class == 'drill']
@property
def copper_layers(self):
return list(reversed([layer for layer in self.layers if
layer.layer_class in
('top', 'bottom', 'internal')]))
@property
def outline_layer(self):
for layer in self.layers:
if layer.layer_class == 'outline':
return layer
@property
def layer_count(self):
""" Number of *COPPER* layers
"""
return len([l for l in self.layers if l.layer_class in
('top', 'bottom', 'internal')])
@property
def board_bounds(self):
for layer in self.layers:
if layer.layer_class == 'outline':
return layer.bounding_box
for layer in self.layers:
if layer.layer_class == 'top':
return layer.bounding_box
|