-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_kicad_sym.py
244 lines (212 loc) · 7.22 KB
/
make_kicad_sym.py
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
import csv
import sys
import re
# Global variables
name = "MySymbol"
font = 1.27
size = (0, 0)
step = 0
side = ''
num = 1
dnum = 1
direction = 0
x, y = 0, 0
pos_cache = {} # Cache for storing positions based on size
props = {} # properties
prop_pos = 0
keywords=['name:', 'size:', 'sizep:', 'step:', 'side:', 'num:', 'rnum:', 'font:', 'prop:']
def split_by_whitespace_or_comma(s):
return re.split(r'[,\s]\s*', s)
def parse_line(line):
parts = line.split()
if parts[0] in keywords:
return parts[0][:-1], ' '.join(parts[1:])
else:
return 'pin', parts
def update_globals(key, value):
global size, step, side, num, x, y, name, props, dnum
if key == 'size':
new_size = tuple(map(float, split_by_whitespace_or_comma(value)))
size = new_size
if key == 'sizep':
new_size = tuple(map(float, split_by_whitespace_or_comma(value)))
size = (round(new_size[0]/2+1)*2 * step,
round(new_size[1]/2+1)*2 * step )
#print(step)
#print(size)
elif key == 'step':
step = float(value)
elif key == 'font':
font = float(value)
elif key == 'side':
reset_position(value)
side = value
elif key == 'num':
num = int(value)
dnum = 1
elif key == 'rnum':
num = int(value)
dnum = -1
elif key == 'name':
name = value
elif key == 'prop':
a = re.split(r'[,\s]\s*', value, maxsplit=1)
if(len(a) == 2):
props[a[0]] = a[1]
def cache_current_position():
global pos_cache, side, x, y, direction
pos_cache[side] = (x, y, direction)
def reset_position(new_side):
global pos_cache, size, x, y, step, direction, side
if(side == new_side):
return;
if new_side in pos_cache:
x, y, direction = pos_cache[new_side]
else:
if(side != ''):
cache_current_position()
#print(size)
if new_side == 'L':
x = -size[0] / 2 - step
y = size[1] / 2 - step
direction = 0
elif new_side == 'R':
x = size[0] / 2 + step
y = size[1] / 2 - step
direction = 180
elif new_side == 'T':
x = -size[0] / 2 + step
y = size[1] / 2 + step
direction = 270
elif new_side == 'B':
x = -size[0] / 2 + step
y = -size[1] / 2 - step
direction = 90
#print(f'{x} {y} {direction}')
def calculate_position():
global x, y, num
position = (x, y)
if side in ['L', 'R']:
y -= step
else: # 'T' or 'B'
x += step
return position
def print_prop(prop_name, val, col=-1, row=-1, hide=True):
global props, font, prop_pos
display = "hide"
x = font
y = prop_pos
if(row != -1):
y = round(size[1] / 2 + step - (step*row), 2)
if(col != -1):
x = round(-size[0] / 2 + step + (step*col), 2)
if(not hide):
display = "(justify left)"
s = f' (property "{prop_name}" "{val}" (at {x} {y:.2f} 0)\n' \
f' (effects (font (size {font} {font})) {display})\n' \
f' )\n'
prop_pos = y - step*2
return s
def generate_rectangle_record(_name, step, size):
global props, name
TLx, TLy = round(-size[0] / 2, 2), round(size[1] / 2, 2)
BRx, BRy = round(size[0] / 2, 2), round(-size[1] / 2, 2)
s = ""
for key, prop_v in props.items():
row = -1
col = -1
hide = (key != "Reference")
if(key == "Reference"):
row = 1
col = 0
if(key == "Value"):
name = prop_v
_name = prop_v
continue
s += print_prop(key, prop_v, col, -1, hide)
s += print_prop("Value", name, row=0, hide=False)
return s + \
f' (symbol "{name}_0_1"\n' \
f' (rectangle (start {TLx:.2f} {TLy:.2f}) (end {BRx:.2f} {BRy:.2f})\n' \
f' (stroke (width {round(step/10, 3):.2f}) (type default))\n' \
f' (fill (type background))\n' \
f' )\n' \
f' )\n'
def generate_pin_records(name, step, pin_data):
pin_records = ''
for num, pin_name, pin_type, x, y, direction in pin_data:
x_rounded = round(x, 2)
y_rounded = round(y, 2)
step_rounded = round(step, 2)
#step_half_rounded = round(step / 2, 2)
pin_records += f' (pin {pin_type} line (at {x_rounded:.2f} {y_rounded:.2f} {direction} ) (length {step_rounded:.2f})\n' \
f' (name "{pin_name}" (effects (font (size {font:.2f} {font:.2f}))))\n' \
f' (number "{num}" (effects (font (size {font:.2f} {font:.2f}))))\n' \
f' )\n'
return f' (symbol "{name}_1_1"\n' + pin_records + '\n )\n'
pin_type_map = {
'i': 'input',
'o': 'output',
'io': 'bidirectional',
'vin': 'power_in',
'v': 'power_in',
'n': 'passive',
'-': 'passive',
'vout': 'power_out',
'nc': 'no_connect'
}
def map_pin_type(pin_type):
global pin_type_map
#print(pin_type)
s = pin_type_map.get(pin_type.lower(), pin_type)
#print(s)
return s
def process_file(input_file, output_file):
global num, dnum, direction, prop_pos, size
results = []
with open(input_file, 'r') as infile:
for line in infile:
line = line.strip()
if(len(line) == 0):
continue
if(line[0] == '#'):
continue
if(line == '-' or line == '--'):
calculate_position()
continue
key, value = parse_line(line)
if key != 'pin':
update_globals(key, value)
else: # Treat as a pin name-type pair
pin_name, pin_type = value
pin_type = map_pin_type(pin_type)
position = calculate_position()
results.append([num, pin_name, pin_type,
round(position[0], 2), round(position[1], 2), direction])
num += dnum
#print(results)
if output_file:
with open(output_file, 'w', newline='') as outfile:
csv_writer = csv.writer(outfile)
csv_writer.writerow(['Pin Number', 'Pin Name', 'Pin Type', 'Position X', 'Position Y', 'Direction'])
csv_writer.writerows(results)
else:
#print('Pin Number, Pin Name, Pin Type, Position X, Position Y', 'Direction')
#for row in results:
# print(', '.join(map(str, row)))
prop_pos = round(size[1] / 2, 2) + step
print( f'(kicad_symbol_lib (version 20220914) (generator make_kicad_sym.py)\n' \
f' (symbol "{name}" (in_bom yes) (on_board yes)\n')
print(generate_rectangle_record(name, step, size))
print(generate_pin_records(name, step, results))
print( f' )\n)\n')
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage:")
print(" python kicad_symbol_generator.py <input_file> > <output_file.kicad_sym>")
print(" or")
print(" python kicad_symbol_generator.py <input_file> <output_file.csv>")
else:
input_file = sys.argv[1]
output_file = sys.argv[2] if len(sys.argv) > 2 else None
process_file(input_file, output_file)