-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathpcbnew2boardview.py
executable file
·241 lines (199 loc) · 8.03 KB
/
pcbnew2boardview.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
#!/usr/bin/env python
import sys
import re
import argparse
import pcbnew
def skip_module(module, tp=False):
if module.GetPadCount() == 0:
return True
refdes = module.Reference().GetText()
if tp and not refdes.startswith("TP"):
return True
if not tp and refdes.startswith("TP"):
return True
return False
def coord(nanometers):
milliinches = nanometers * 5 // 127000
return milliinches
def y_coord(maxy, y, flipped):
# Adjust y-coordinate to start from the bottom of the board and account for flipped components
return coord(maxy - y) if not flipped else coord(y)
def natural_sort_key(s):
is_blank = s.strip() == ''
return (is_blank, [int(text) if text.isdigit() else text.casefold()
for text in re.compile('([0-9]+)').split(s)])
def convert_brd(pcb, brd):
# Board outline
outlines = pcbnew.SHAPE_POLY_SET()
pcb.GetBoardPolygonOutlines(outlines)
outline = outlines.Outline(0)
outline_points = [outline.GetPoint(n) for n in range(outline.PointCount())]
outline_maxx = max(map(lambda p: p.x, outline_points))
outline_maxy = max(map(lambda p: p.y, outline_points))
brd.write("0\n") # unknown
brd.write("BRDOUT: {count} {width} {height}\n"
.format(count =len(outline_points) + outline.IsClosed(),
width =coord(outline_maxx),
height=coord(outline_maxy)))
for point in outline_points:
brd.write("{x} {y}\n"
.format(x=coord(point.x),
y=y_coord(outline_maxy, point.y, False)))
if outline.IsClosed():
brd.write("{x} {y}\n"
.format(x=coord(outline_points[0].x),
y=y_coord(outline_maxy, outline_points[0].y, False)))
brd.write("\n")
# Nets
net_info = pcb.GetNetInfo()
net_items = [net_info.GetNetItem(n) for n in range(1, net_info.GetNetCount())]
brd.write("NETS: {count}\n"
.format(count=len(net_items)))
for net_item in net_items:
brd.write("{code} {name}\n"
.format(code=net_item.GetNetCode(),
name=net_item.GetNetname()))
brd.write("\n")
# Parts
module_list = pcb.GetFootprints()
modules = []
for module in module_list:
if not skip_module(module):
modules.append(module)
brd.write("PARTS: {count}\n"
.format(count=len(modules)))
pin_at = 0
for module in modules:
module_bbox = module.GetBoundingBox()
flipped = module.IsFlipped()
brd.write("{ref} {x1} {y1} {x2} {y2} {pin} {side}\n"
.format(ref=module.Reference().GetText(),
x1=coord(module_bbox.GetLeft()),
y1=y_coord(outline_maxy, module_bbox.GetTop(), flipped),
x2=coord(module_bbox.GetRight()),
y2=y_coord(outline_maxy, module_bbox.GetBottom(), flipped),
pin=pin_at,
side=1 + flipped))
pin_at += module.GetPadCount()
brd.write("\n")
# Pins
module_list = pcb.GetFootprints()
pads = []
for module in module_list:
if not skip_module(module):
pads_list = module.Pads()
for pad in sorted(pads_list, key=lambda pad: natural_sort_key(pad.GetName())):
pads.append(pad)
brd.write("PINS: {count}\n"
.format(count=len(pads)))
for pad in pads:
pad_pos = pad.GetPosition()
flipped = pad.IsFlipped()
brd.write("{x} {y} {net} {side}\n"
.format(x=coord(pad_pos.x),
y=y_coord(outline_maxy, pad_pos.y, flipped),
net=pad.GetNetCode(),
side=1 + flipped))
brd.write("\n")
# Nails
module_list = pcb.GetFootprints()
testpoints = []
for module in module_list:
if not skip_module(module, tp=True):
pads_list = module.Pads()
for pad in sorted(pads_list, key=lambda pad: natural_sort_key(pad.GetName())):
testpoints.append((module, pad))
brd.write("NAILS: {count}\n"
.format(count=len(testpoints)))
for module, pad in testpoints:
pad_pos = pad.GetPosition()
flipped = pad.IsFlipped()
brd.write("{probe} {x} {y} {net} {side}\n"
.format(probe=module.GetReference()[2:],
x=coord(pad_pos.x),
y=y_coord(outline_maxy, pad_pos.y, flipped),
net=pad.GetNetCode(),
side=1 + flipped))
brd.write("\n")
def convert_bvr(pcb, bvr):
bvr.write("BVRAW_FORMAT_3\n")
outlines = pcbnew.SHAPE_POLY_SET()
pcb.GetBoardPolygonOutlines(outlines)
outline = outlines.Outline(0)
outline_points = [outline.GetPoint(n) for n in range(outline.PointCount())]
outline_maxx = max(map(lambda p: p.x, outline_points))
outline_maxy = max(map(lambda p: p.y, outline_points))
module_list = pcb.GetFootprints()
modules = []
for module in module_list:
if not skip_module(module):
modules.append(module)
ref = module.GetReference()
flipped = module.IsFlipped()
side = "B" if flipped else "T"
mount = module.GetTypeName()
pads_list = module.Pads()
bvr.write("\n")
bvr.write(f"PART_NAME {ref}\n")
bvr.write(f" PART_SIDE {side}\n")
bvr.write(" PART_ORIGIN 0.000 0.000\n")
bvr.write(f" PART_MOUNT {mount}\n")
bvr.write("\n")
for pad in sorted(pads_list, key=lambda pad: natural_sort_key(pad.GetName())):
pin_num = pad.GetNumber()
net = pad.GetNetname()
pad_bbox = pad.GetBoundingBox()
pad_size = pad.GetSize()
x_center = (pad_bbox.GetLeft() + pad_bbox.GetRight()) / 2
y_center = (pad_bbox.GetTop() + pad_bbox.GetBottom()) / 2
x = coord(x_center)
y = y_coord(outline_maxy, y_center, flipped)
if flipped:
y = coord(outline_maxy - y_center)
if pad.GetShape() == pcbnew.PAD_SHAPE_CIRCLE:
radius = coord(pad_size.x / 1.6)
else:
smaller_dimension = min(pad_size.x, pad_size.y)
radius = coord(smaller_dimension / 1.6)
bvr.write(f" PIN_ID {ref}-{pin_num}\n")
bvr.write(f" PIN_NUMBER {pin_num}\n")
bvr.write(f" PIN_NAME {pin_num}\n")
bvr.write(f" PIN_SIDE {side}\n")
bvr.write(f" PIN_ORIGIN {x} {y}\n")
bvr.write(f" PIN_RADIUS {radius}\n")
bvr.write(f" PIN_NET {net}\n")
bvr.write(" PIN_TYPE 2\n")
bvr.write(" PIN_COMMENT\n")
bvr.write(" PIN_END\n")
bvr.write("\n")
bvr.write("PART_END\n")
bvr.write("\n")
first_point = outline_points[0]
outline_pts = ""
for point in outline_points:
x = coord(point.x)
y = y_coord(outline_maxy, point.y, False)
outline_pts += (f"{x} {y} ")
x = coord(first_point.x)
y = y_coord(outline_maxy, first_point.y, False)
outline_pts += (f"{x} {y}")
bvr.write("OUTLINE_POINTS ")
bvr.write(outline_pts)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"kicad_pcb_file", metavar="KICAD-PCB-FILE", type=str,
help="input in .kicad_pcb format")
parser.add_argument(
"brd_file", metavar="BRD-FILE", type=argparse.FileType("w"),
help="output in .brd format")
parser.add_argument(
"--bvr_file", metavar="BVR-FILE", type=argparse.FileType("w"),
help="output in .bvr format")
args = parser.parse_args()
board = pcbnew.LoadBoard(args.kicad_pcb_file)
convert_brd(board, args.brd_file)
if args.bvr_file:
convert_bvr(board, bvr_file)
if __name__ == "__main__":
main()