-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathicgen.py
More file actions
executable file
·438 lines (347 loc) · 12.9 KB
/
icgen.py
File metadata and controls
executable file
·438 lines (347 loc) · 12.9 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
#!/usr/bin/python3
# coding=utf-8
#-------------------------------------------------------------------------------
#
# Project: KiCad Tools
#
# Name: Schematic IC Symbols Generator
#
# Purpose: Generate schematic symbols for integrated circuit components
# from YAML descripion file
#
# Copyright (c) 2016, emb-lib Project Team
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
# THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#-------------------------------------------------------------------------------
import sys
import os
import getopt
import yaml
#-------------------------------------------------------------------------------
#
# Settings
#
PIN_LEN = 200 # mils
FONT_SIZE = 100 # mils
#-------------------------------------------------------------------------------
def namegen(fullpath, ext):
basename = os.path.basename(fullpath)
name = os.path.splitext(basename)[0]
return name + os.path.extsep + ext
#-------------------------------------------------------------------------------
def sections(d, pattern):
res = []
for i in d.keys():
if pattern in i:
res.append(i)
res.sort(key=lambda s, npos=len(pattern): int(s[npos:]))
return res
#-------------------------------------------------------------------------------
def join_rec(l):
res = ''
for idx, i in enumerate(l, start = 1):
sep = ' '
if idx == len(l):
sep = ''
res += str(i) + sep
return res
#-------------------------------------------------------------------------------
def draw_pline(part, *vertexes):
N = int(len(vertexes[0])/2)
C = 0
Th = 0
CC = 'N'
return join_rec( ['P', N, part, C, Th] + vertexes[0] + [CC] ) + os.linesep
#-------------------------------------------------------------------------------
def draw_line(part, x0, y0, x1, y1):
N = 2
C = 0
Th = 0
CC = 'N'
return join_rec( ['P', N, part, C, Th, x0, y0, x1, y1, CC] ) + os.linesep
#-------------------------------------------------------------------------------
def draw_amp_symbol(part_id, capt):
x0, y0 = capt[1:]
vertexes = [x0, y0, x0+100, y0+50, x0, y0+100, x0, y0]
return draw_pline(part_id, vertexes)
#-------------------------------------------------------------------------------
def draw_text(part_id, capt):
text, x0, y0 = capt
Orientation = 0
CommonBodyStyle = 0
params = ['T', Orientation, x0, y0, FONT_SIZE, 0, part_id, CommonBodyStyle, text, 'Normal 0 C C']
return join_rec( params) + os.linesep
#-------------------------------------------------------------------------------
def create_header(ic):
header = '#' + os.linesep
header += '# ' + ic['Name'] + os.linesep
header += '#' + os.linesep
Name = 'DEF ' + ic['Name']
Ref = ic['Ref']
Unused = '0'
PinNameOffset = ic['PinNameOffset']
DrawPinNum = 'Y'
DrawPinName = 'Y'
UnitCount = len( sections(ic, 'Part') )
UnitsLocked = 'L' # L: locked; F: free
OptionFlag = 'N' # N: normal; P: power
l = [Name, Ref, Unused, PinNameOffset, DrawPinNum, DrawPinName, UnitCount, UnitsLocked, OptionFlag]
header += join_rec(l) + os.linesep
return header
#-------------------------------------------------------------------------------
def create_field(field, name, pos_x, pos_y, font=FONT_SIZE, visibility='V'):
l = ['F'+str(field), '"'+name+'"', pos_x, pos_y, str(font), 'H', visibility, 'L CNN']
return join_rec(l) + os.linesep
#-------------------------------------------------------------------------------
def draw_pin(ic, part_id, pin, org):
STEP = ic['Spacing']
if org[0] < 0:
orient = 'R'
else:
orient = 'L'
C = '0'
Etype = 'P' # passive
org[1] -= pin[2]*STEP
rec = join_rec( ['X', pin[1], pin[0], org[0], org[1], ic['PinLen'], orient, FONT_SIZE, FONT_SIZE, part_id, C, Etype] ) + os.linesep
return org, rec
#-------------------------------------------------------------------------------
def draw_part(ic, part_name):
STEP = ic['Spacing']
part = ic[part_name]
vsect = part['Sections']
if ic['Filled']:
filled = 'f'
else:
filled = 'n'
part_id = part_name[4:]
pgroups = sections(part, 'PinGroup')
#-----------------------------------
#
# Calculate part height
#
h_l = 0
h_r = 0
for i in pgroups:
h = 0
pgroup = part[i]
if 'Height' in pgroup.keys():
h = int( pgroup['Height'] )
else:
for p in pgroup['Pins']:
if len(p) != 3:
print( 'E: invalid pin description: ' + str(p) +\
', must have 3 fields but only ' + str(len(p)) + ' found' )
print( ' Part: ' + part_name)
print( ' PinGroup: ' + i)
sys.exit(2)
h += p[2]
h += 1
if pgroup['Side'] == 'Left':
h_l += h
elif pgroup['Side'] == 'Right':
h_r += h
else:
print('E: invalid PinGroup Side in ' + i)
height = max(h_l, h_r)*STEP
width = sum(vsect)
if filled:
f = 'f'
else:
f = 'n'
#-----------------------------------
#
# Outline
#
Type = 'S'
X0 = '0'
Y0 = '0'
C = '0'
Th = '0'
draw = join_rec( [Type, X0, Y0, width, -height, part_id, C, Th, filled] ) + os.linesep
#-----------------------------------
#
# Caption
#
capt = part['Caption']
if capt:
if capt[0] == '|>':
draw += draw_amp_symbol(part_id, capt)
else:
draw += draw_text(part_id, capt)
#-----------------------------------
#
# Vertical lines
#
X = 0
for i in range( len(vsect) - 1):
X0 = X + vsect[i]
Y0 = 0
X1 = X0
Y1 = -height
draw += draw_line(part_id, X0, Y0, X1, Y1)
X = X0
#-----------------------------------
#
# Pins
#
sep_l_x0 = 0
sep_l_x1 = vsect[0]
if len(vsect) == 1:
sep_r_x0 = 0
elif len(vsect) == 2:
sep_r_x0 = vsect[0]
else:
sep_r_x0 = sum(vsect[:-1])
pin_org_l = [ -int(ic['PinLen']), 0]
pin_org_r = [ width + int(ic['PinLen']), 0]
for idx, pg_name in enumerate(pgroups, start=1):
pgroup = part[pg_name]
if 'Height' in pgroup.keys():
pg_height = -int( pgroup['Height'] )
else:
pg_height = -1
h = 0
if pgroup['Side'] == 'Left':
h = pin_org_l[1]
for p in pgroup['Pins']:
pin_org_l, pin_rec = draw_pin(ic, part_id, p, pin_org_l)
draw += pin_rec
if pg_height == -1:
pin_org_l[1] += pg_height*STEP
else:
pin_org_l[1] = h + pg_height*STEP
if pgroup['Sep']:
draw += draw_line(part_id, sep_l_x0, pin_org_l[1], sep_l_x1, pin_org_l[1])
else:
h = pin_org_r[1]
for p in pgroup['Pins']:
pin_org_r, pin_rec = draw_pin(ic, part_id, p, pin_org_r)
draw += pin_rec
if pg_height == -1:
pin_org_r[1] += pg_height*STEP
else:
pin_org_r[1] = h + pg_height*STEP
if pgroup['Sep']:
draw += draw_line(part_id, sep_r_x0, pin_org_r[1], width, pin_org_r[1])
return draw
#-------------------------------------------------------------------------------
def create_drawings(ic):
parts = sections(ic, 'Part')
draw = 'DRAW' + os.linesep
for i in parts:
draw += draw_part(ic, i)
draw += 'ENDDRAW' + os.linesep
return draw
#-------------------------------------------------------------------------------
def check_cmp_params(ic):
params = ['Name', 'Ref', 'PinLen', 'PinNameOffset', 'Filled']
success = True
for i in params:
if not i in ic.keys():
print('E: component description has no "' + i + '" parameter')
success = False
parts = sections(ic, "Part")
part_params = ['Caption', 'Sections']
for part_name in parts:
part = ic[part_name]
for p in part_params:
if not p in part.keys():
print('E: "' + part_name + '" part description has no "' + p + '" parameter')
success = False
sect_count = len(part['Sections'])
if sect_count < 1 or sect_count > 3:
print('E: "' + part_name + '" part has invalid sections count: ' + str(sect_count) + ' (must be 1..3)')
success = False
pgroups = sections(part, 'PinGroup')
pgroup_params = ['Side', 'Sep', 'Pins']
for pg_name in pgroups:
pgroup = part[pg_name]
for p in pgroup_params:
if not p in pgroup.keys():
print('E: "' + pg_name + '" pin group description has no "' + p + '" parameter')
success = False
return success
#-------------------------------------------------------------------------------
def create_cmp_desc(ic, infile, outpath):
desc = 'Description' in ic.keys()
kwd = 'Keywords' in ic.keys()
if not desc and not kwd:
return
dcmp = '#' + os.linesep
dcmp += '$CMP ' + ic['Name'] + os.linesep
if desc:
dcmp += 'D ' + ic['Description'] + os.linesep
if kwd:
dcmp += 'K ' + ic['Keywords'] + os.linesep
dcmp += '$ENDCMP' + os.linesep
cname = namegen(infile, 'dcmp')
with open( os.path.join(outpath, cname), 'wb' ) as f:
f.write( bytes(dcmp, 'UTF-8') )
#-------------------------------------------------------------------------------
def create_cmp(yml, outpath, silent):
ic = yaml.load( open(yml) )
if not check_cmp_params(ic):
sys.exit(2)
name = ic['Name']
if 'NameOffset' in ic.keys():
name_offset = ic['NameOffset']
else:
name_offset = 1000 # default offset
if 'Footprint' in ic.keys():
footprint = ic['Footprint']
else:
footprint = '' # default footprint
rec = create_header(ic)
rec += create_field( field=0, name=ic['Ref'], pos_x=0, pos_y=100 )
rec += create_field( field=1, name=ic['Name'], pos_x=name_offset, pos_y=100 )
rec += create_field( field=2, name=footprint, pos_x=name_offset, pos_y=250, visibility='I' )
rec += create_field( field=3, name='', pos_x=name_offset, pos_y=500, visibility='I' )
rec += create_drawings(ic)
rec += 'ENDDEF' + os.linesep
cname = namegen(yml, 'cmp')
if not silent:
print('I: create component file ' + cname)
with open( os.path.join(outpath, cname ), 'wb') as f:
f.write( bytes(rec, 'UTF-8') )
create_cmp_desc(ic, yml, outpath)
def main():
#-------------------------------------------------
#
# Process options
#
optlist, fl = getopt.gnu_getopt(sys.argv[1:], 'o:s')
yml = fl[0]
if not yml:
print('I: usage: icgen.py [options] <filename.yml>')
print(' where options are:')
print(' -o: directory for output files')
sys.exit(1)
outpath = ''
silent = False
for i in optlist:
if i[0] == '-o':
outpath = i[1]
if i[0] == '-s':
silent = True
create_cmp(yml, outpath, silent)
if __name__ == '__main__':
main()
#-------------------------------------------------------------------------------