-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkicad_pcb2png.py
More file actions
executable file
·513 lines (407 loc) · 14.5 KB
/
Copy pathkicad_pcb2png.py
File metadata and controls
executable file
·513 lines (407 loc) · 14.5 KB
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!/usr/bin/env python3
import os
import sys
from PIL import Image, ImageDraw, PngImagePlugin
import math
from pyparsing import *
import pprint
# some global config parameters
BORDER_MM = 0.5
PPI = 2000
#==============================================================================
class Segment:
def __init__(self, param_list):
self.params = dict()
del param_list[0] # remove the 'segment' element
# transform the params into keys of the object
for e in param_list:
key = e[0]
rest = e[1:]
if len(rest) == 1:
rest = rest[0]
self.params[key] = rest
return None
class Via:
def __init__(self, param_list):
self.params = dict()
del param_list[0] # remove the 'via' element
# transform the params into keys of the object
for e in param_list:
key = e[0]
rest = e[1:]
if len(rest) == 1:
rest = rest[0]
self.params[key] = rest
return None
class Module:
def __init__(self, param_list):
self.params = dict()
del param_list[0] # remove the 'module' element
param_list = list(filter(lambda f: type(f) is list, param_list)) # filter out all non-list items
# transform the params into keys of the object
for e in param_list:
key = e[0]
rest = e[1:]
if len(rest) == 1:
rest = rest[0]
if key == 'at' or key == 'layer': # only store at and layer
self.params[key] = rest
# aggregate pads separately
raw_pads = list(filter(lambda f: f[0] == 'pad', param_list))
pads = list()
for p in raw_pads:
pad = Pad(self.params['at'], p)
pads.append(pad)
self.params['pads'] = pads
return None
class Pad:
def __init__(self, module_location, param_list):
self.params = dict()
del param_list[0] # remove the 'pad' element
pad_type = param_list[1]
shape = param_list[2]
param_list = list(filter(lambda f: type(f) is list, param_list)) # filter out all non-list items
# transform the params into keys of the object
for e in param_list:
key = e[0]
rest = e[1:]
if len(rest) == 1:
rest = rest[0]
if key == 'at' or key == 'size' or key == 'drill' or key == 'layers': # only store at, size and drill
self.params[key] = rest
self.params['pad_type'] = pad_type
self.params['shape'] = shape
self.params['offset'] = module_location
return None
class Zone:
def __init__(self, param_list):
self.params = dict()
del param_list[0] # remove the 'zone' element
layer = list(filter(lambda f: f[0] == 'layer', param_list))
param_list = list(filter(lambda f: f[0] == 'filled_polygon', param_list)) # filter everythin out except 'filled_polygon'
polygons = list()
for p in param_list:
poly = p[1][1:]
poly = list(map(lambda f: f[1:], poly )) # iterate all coordiantes and strip the 'xy' (the first element)
polygons.append(poly)
self.params['polygons'] = polygons
self.params['layer'] = layer[0][1]
return None
class Outline:
def __init__(self, param_list):
self.params = dict()
if param_list[0] == 'gr_line':
self.outline_type = 'line'
elif param_list[0] == 'gr_arc':
self.outline_type = 'arc'
else:
print('ERR: Unknown outline type')
exit()
del param_list[0] # remove the 'gr_line' or 'gr_arg' element
for e in param_list:
key = e[0]
rest = e[1:]
if len(rest) == 1:
rest = rest[0]
self.params[key] = rest
return None
#==============================================================================
def mm2pix(mm, ppi):
return int(round(mm/25.4*ppi))
def create_image(filename, dimensions, offset, ppi, border_mm, segments, modules, zones, vias, inverted=False, flipped=True, layer='B.Cu'):
print('Generating image...', end="", flush=True)
border = mm2pix(border_mm, ppi)
w = dimensions[0] + 2 * border
h = dimensions[1] + 2 * border
im = Image.new("RGB", (w,h))
draw = ImageDraw.Draw(im)
black = 0x000000
white = 0xFFFFFF
fill = white
red = 0x0000FF
if inverted:
draw.rectangle( [0, 0, w, h], white)
fill = black
off_x = offset[0] - border
off_y = offset[1] - border
for z in zones:
if z.params['layer'] == layer:
for p in z.params['polygons']:
poly = list(map(lambda f: tuple(( mm2pix(f[0], ppi) - off_x, h - (mm2pix(f[1], ppi) - off_y ))), p ))
draw.polygon( poly, white, None)
for s in segments:
x1 = mm2pix(s.params['start'][0], ppi) - off_x
y1 = mm2pix(s.params['start'][1], ppi) - off_y
x2 = mm2pix(s.params['end'][0], ppi) - off_x
y2 = mm2pix(s.params['end'][1], ppi) - off_y
width = mm2pix(s.params['width'], ppi)
if s.params['layer'] == layer:
draw.line( [x1, h-y1, x2, h-y2], fill, width)
# draw circle on the start of the connection
x = x1 - int(round(width / 2))
y = h - (y1 + int(round(width / 2)))
draw.ellipse( [ x+1, y-1, x + width, y + width], fill)
# draw circle on the end of the connection
x = x2 - int(round(width / 2))
y = h - (y2 + int(round(width / 2)))
draw.ellipse( [ x+1, y-1, x + width, y + width], fill)
for m in modules:
for p in m.params['pads']:
# check if it's on the right layer
if "*.Cu" in p.params['layers'] or layer in p.params['layers']:
try:
rotation = 360 - p.params['at'][2]
except:
rotation = 0
angle = math.radians(rotation)
rot_x = p.params['at'][0] * math.cos(angle) - p.params['at'][1] * math.sin(angle)
rot_y = p.params['at'][0] * math.sin(angle) + p.params['at'][1] * math.cos(angle)
center_x = mm2pix( rot_x + p.params['offset'][0], ppi) - off_x
center_y = mm2pix( rot_y + p.params['offset'][1], ppi) - off_y
width_x = mm2pix(p.params['size'][0], ppi)
width_y = mm2pix(p.params['size'][1], ppi)
shape = p.params['shape']
if shape == 'oval':
None
if shape == 'circle':
x = center_x - int(round(width_x / 2.0))
y = h - (center_y + int(round(width_y / 2.0)))
draw.ellipse( [ x, y, x+width_x, y+width_y], fill)
if shape == 'rect':
x = center_x - int(round(width_x / 2.0))
y = h -(center_y + int(round(width_y / 2.0)))
poly = [x, y, x+width_x, y, x+width_x, y+width_y, x, y+width_y]
draw.polygon( poly, white, None)
# draw.ellipse( [ x, y, x+width_x, y+width_y], fill)
# check for drill hole in pad
if 'drill' in p.params:
drill = mm2pix(p.params['drill'], ppi)
# draw circle for drill hole
x = center_x - int(round(drill / 2.0))
y = h - (center_y + int(round(drill / 2.0)))
draw.ellipse( [ x, y, x + drill, y + drill], black)
for v in vias:
if layer in v.params['layers']:
center_x = mm2pix( v.params['at'][0], ppi) - off_x
center_y = mm2pix( v.params['at'][1], ppi) - off_y
width = mm2pix( v.params['size'], ppi) # vias are always round?
x = center_x - int(round(width / 2.0))
y = h - (center_y + int(round(width / 2.0)))
draw.ellipse( [ x, y, x+width, y+width], fill)
# check for drill hole in pad
if 'drill' in v.params:
drill = mm2pix(v.params['drill'], ppi)
# draw circle for drill hole
x = center_x - int(round(drill / 2.0))
y = h - (center_y + int(round(drill / 2.0)))
draw.ellipse( [ x, y, x + drill, y + drill], black)
del draw
print(' OK')
print('Writing', filename, '...', end="", flush=True)
im.save(filename, "PNG", dpi=(ppi,ppi))
print(' OK')
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# def distance(x1, y1, x2, y2):
# return mat.sqrt(math.pow(math.abs(x2-x1),2) + math.pow(math.abs(y2-y1),2))
def create_outline_image(filename, dimensions, offset, ppi, border_mm, outlines, modules, vias):
print('Generating image...', end="", flush=True)
border = mm2pix(border_mm, ppi)
w = dimensions[0] + 2 * border
h = dimensions[1] + 2 * border
im = Image.new("RGB", (w,h))
draw = ImageDraw.Draw(im)
black = 0x000000
white = 0xFFFFFF
fill = white
off_x = offset[0] - border
off_y = offset[1] - border
# create a continuous polygon from the separate outlines
poly = list()
poly.append(outlines[0].params['start'])
poly.append(outlines[0].params['end'])
del outlines[0]
while len(outlines) > 0:
i = 0
while i < len(outlines):
s = outlines[i].params['start']
e = outlines[i].params['end']
if s == poly[len(poly)-1]: # start matches
poly.append(e)
break
elif e == poly[len(poly)-1]: # end matches
poly.append(s)
break
i = i + 1
del outlines[i]
del poly[len(poly)-1]
poly_pix = list()
for p in poly:
x = mm2pix(p[0], ppi) - off_x
y = mm2pix(p[1], ppi) - off_y
poly_pix.append( tuple(( x, h-y )) )
draw.polygon( poly_pix, white, None)
for m in modules:
for p in m.params['pads']:
# no need to check for layer, only check for drill hole
try:
rotation = 360 - p.params['at'][2]
except:
rotation = 0
angle = math.radians(rotation)
rot_x = p.params['at'][0] * math.cos(angle) - p.params['at'][1] * math.sin(angle)
rot_y = p.params['at'][0] * math.sin(angle) + p.params['at'][1] * math.cos(angle)
center_x = mm2pix( rot_x + p.params['offset'][0], ppi) - off_x
center_y = mm2pix( rot_y + p.params['offset'][1], ppi) - off_y
# check for drill hole in pad
if 'drill' in p.params:
drill = mm2pix(p.params['drill'], ppi)
# draw circle for drill hole
x = center_x - int(round(drill / 2.0))
y = h - (center_y + int(round(drill / 2.0)))
draw.ellipse( [ x, y, x + drill, y + drill], black)
for v in vias:
center_x = mm2pix( v.params['at'][0], ppi) - off_x
center_y = mm2pix( v.params['at'][1], ppi) - off_y
# check for drill hole in pad
if 'drill' in v.params:
drill = mm2pix(v.params['drill'], ppi)
# draw circle for drill hole
x = center_x - int(round(drill / 2.0))
y = h - (center_y + int(round(drill / 2.0)))
draw.ellipse( [ x, y, x + drill, y + drill], black)
del draw
print(' OK')
print('Writing', filename, '...', end="", flush=True)
im.save(filename, "PNG", dpi=(ppi,ppi))
print(' OK')
#==============================================================================
def verifyLen(s,l,t):
t = t[0]
if t.len is not None:
t1len = len(t[1])
if t1len != t.len:
raise ParseFatalException(s,l,\
"invalid data of length %d, expected %s" % (t1len, t.len))
return t[1]
# define punctuation literals
LPAR, RPAR, LBRK, RBRK, LBRC, RBRC, VBAR = map(Suppress, "()[]{}|")
decimal = Regex(r'0|[1-9]\d*').setParseAction(lambda t: int(t[0]))
bytes = Word(printables)
raw = Group(decimal("len") + Suppress(":") + bytes).setParseAction(verifyLen)
token = Word(alphanums + "-./_:*+=")
qString = Group(Optional(decimal,default=None)("len") +
dblQuotedString.setParseAction(removeQuotes)).setParseAction(verifyLen)
# extended definitions
decimal = Regex(r'-?0|[1-9]\d*').setParseAction(lambda t: int(t[0]))
real = Regex(r"[+-]?\d+\.\d*([eE][+-]?\d+)?").setParseAction(lambda tokens: float(tokens[0]))
token = Word(alphanums + "-./_:*+=!<>")
simpleString = real | raw | decimal | token | qString
display = LBRK + simpleString + RBRK
string_ = Optional(display) + simpleString
sexp = Forward()
sexpList = Group(LPAR + ZeroOrMore(sexp) + RPAR)
sexp << ( string_ | sexpList )
#==============================================================================
def parse_pcblist(pcblist):
# map keys in the kicad_pcb file to objects that can parse the data
supported_list = dict()
supported_list['segment'] = Segment
supported_list['via'] = Via
supported_list['module'] = Module
supported_list['zone'] = Zone
supported_list['gr_line'] = Outline
supported_list['gr_arc'] = Outline
for i in pcblist:
name = i[0]
if name in supported_list:
instance = supported_list[name](i)
pcb_data[name].append(instance)
def get_outline_boundingbox(outlines):
if len(outlines) == 0:
raise NameError('HiThere') # Error, no outlines in board
max_x = outlines[0].params['start'][0]
max_y = outlines[0].params['start'][1]
min_x = max_x
min_y = max_y
for o in outlines:
xmin = min( o.params['start'][0], o.params['end'][0] )
xmax = max( o.params['start'][0], o.params['end'][0] )
ymin = min( o.params['start'][1], o.params['end'][1] )
ymax = max( o.params['start'][1], o.params['end'][1] )
max_x = max( max_x, xmax)
max_y = max( max_y, ymax)
min_x = min( min_x, xmin)
min_y = min( min_y, ymin)
return (min_x, min_y, max_x, max_y)
#==============================================================================
#==============================================================================
SEGMENTS = list()
VIAS = list()
MODULES = list()
ZONES = list()
OUTLINES = list()
pcb_data = dict()
pcb_data['segment'] = SEGMENTS
pcb_data['via'] = VIAS
pcb_data['module'] = MODULES
pcb_data['zone'] = ZONES
pcb_data['gr_line'] = OUTLINES
pcb_data['gr_arc'] = OUTLINES
print("\nkicad_pcb2png.py ver. 2 may 2016\n")
current_dir = os.getcwd()
if len(sys.argv) > 1:
project_name = sys.argv[1]
pcbfile = project_name + ".kicad_pcb"
if os.path.isfile(pcbfile):
print("Board File Found:", pcbfile)
else:
print("ERR: No board file found!")
exit()
else:
print("ERR: Project name missing!")
print("Usage: python3 kicad_pcb2png.py [project name]")
exit()
print('Reading...', end="", flush=True)
with open(pcbfile, 'r') as f:
data="".join(line.rstrip() for line in f)
print(' OK')
print('Parsing...', end="", flush=True)
try:
sexpr = sexp.parseString(data, parseAll=True)
pcblist = sexpr.asList()
parse_pcblist(pcblist[0])
except ParseFatalException as pfe:
print("ERR: Parse error!")
#print("Error:", pfe.msg)
#print(pfe.markInputline('^'))
print(' OK\n')
# filter out all outlines that are not on the Edge.Cuts layer
OUTLINES = list(filter(lambda f: f.params['layer'] == 'Edge.Cuts', OUTLINES))
print('outlines:',len(OUTLINES))
print('zones:',len(ZONES))
print('segments:',len(SEGMENTS))
print('modules:',len(MODULES))
pad_count = 0
for m in MODULES:
pad_count += len(m.params['pads'])
print('pads:',pad_count)
print('vias:',len(VIAS))
if len(OUTLINES) < 3:
print("ERR: Less than 3 board outlines found!")
exit()
try:
bbox = get_outline_boundingbox(OUTLINES)
width = bbox[2] - bbox[0]
height = bbox[3] - bbox[1]
dimensions = [ mm2pix(width, PPI), mm2pix(height, PPI) ]
offset = [ mm2pix(bbox[0], PPI), mm2pix(bbox[1], PPI) ]
print('\nBoard: {:.2f} x {:.2f} mm'.format(width, height))
print('Border: {:.2f} mm'.format(BORDER_MM))
border_pixels = int(round(BORDER_MM/25.4*PPI))
print('Image:', dimensions[0]+border_pixels*2, 'x', dimensions[1]+border_pixels*2, '@', PPI, 'ppi\n')
except:
print("ERR: No board outlines found!")
exit()
create_image(project_name+"-B.Cu_MILL-TRACES.png", dimensions, offset, PPI, BORDER_MM, SEGMENTS, MODULES, ZONES, VIAS)
create_outline_image(project_name+"-MILL-OUTLINE.png", dimensions, offset, PPI, BORDER_MM, OUTLINES, MODULES, VIAS)
print("Done\n")