forked from willemcvu/kicad-panelizer
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpanelizer5.py
executable file
·356 lines (303 loc) · 14.5 KB
/
panelizer5.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
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
#!/usr/bin/env python3
import os
import sys
from argparse import ArgumentParser
import pcbnew
from pcbnew import *
"""
A simple script to create a v-scored panel of a KiCad board.
Original author: Willem Hillier
"""
__version__ = '1.5'
# set up command-line arguments parser
parser = ArgumentParser(description="A script to panelize KiCad files.")
parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + __version__)
parser.add_argument(dest="sourceBoardFile", help='Path to the *.kicad_pcb file to be panelized')
parser.add_argument('--numx', type=int, help='Number of boards in X direction')
parser.add_argument('--numy', type=int, help='Number of boards in Y direction')
parser.add_argument('--panelx', type=int, help='Maximum panel size in X direction')
parser.add_argument('--panely', type=int, help='Maximum panel size in Y direction')
parser.add_argument('--hrail', type=int, default=0, help='Horizontal edge rail width')
parser.add_argument('--vrail', type=int, default=0, help='Vertical edge rail width')
parser.add_argument('--hrailtext', help='Text to put on the horizontal edge rail')
parser.add_argument('--vrailtext', help='Text to put on the vertical edge rail')
parser.add_argument('--htitle', action='store_true', help='Print title info on horizontal edge rail')
parser.add_argument('--vtitle', action='store_true', help='Print title info on vertical edge rail')
parser.add_argument('--vscorelayer', default='Edge.Cuts', help='Layer to put v-score lines on')
parser.add_argument('--vscoretextlayer', default='Cmts.User', help='Layer to put v-score text on')
parser.add_argument('--vscoretext', default='V-SCORE', help='Text used to indicate v-scores')
parser.add_argument('--vscoreextends', type=float, default='-0.05', help='How far past the board to extend the v-score lines, defaults to -0.05')
args = parser.parse_args()
sourceBoardFile = args.sourceBoardFile
NUM_X = args.numx
NUM_Y = args.numy
PANEL_X = args.panelx
PANEL_Y = args.panely
HORIZONTAL_EDGE_RAIL_WIDTH = args.hrail
VERTICAL_EDGE_RAIL_WIDTH = args.vrail
HORIZONTAL_EDGE_RAIL_TEXT = args.hrailtext
VERTICAL_EDGE_RAIL_TEXT = args.vrailtext
V_SCORE_LAYER = args.vscorelayer
V_SCORE_TEXT_LAYER = args.vscoretextlayer
V_SCORE_TEXT = args.vscoretext
V_SCORE_LINE_LENGTH_BEYOND_BOARD = args.vscoreextends
# check that input board is a *.kicad_pcb file
sourceFileExtension = os.path.splitext(sourceBoardFile)[1]
if not(sourceFileExtension == '.kicad_pcb'):
print(sourceBoardFile + " is not a *.kicad_pcb file. Quitting.")
quit()
# check that railtext is at least 1mm
if ( ((HORIZONTAL_EDGE_RAIL_TEXT or args.htitle) and HORIZONTAL_EDGE_RAIL_WIDTH < 2) or ((VERTICAL_EDGE_RAIL_TEXT or args.vtitle) and VERTICAL_EDGE_RAIL_WIDTH < 2) ):
print("Rail width must be at least 2mm if using rail text. Quitting.")
quit()
# only allow numbers or panels
if (PANEL_X or PANEL_Y) and (NUM_X or NUM_Y):
print("Specify number of boards or size of panel, not both. Quitting.")
quit()
# expect panel size or number of boards
if (not PANEL_X or not PANEL_Y) and (not NUM_X or not NUM_Y):
print("Specify number of boards or size of panel. Quitting.")
quit()
# warn if user has specified both rails
if (HORIZONTAL_EDGE_RAIL_WIDTH and VERTICAL_EDGE_RAIL_WIDTH):
print("Warning: do you really want both edge rails?")
# output file name is format {inputFile}_panelized.kicad_pcb
panelOutputFile = os.path.splitext(sourceBoardFile)[0] + "_panelized.kicad_pcb"
# all dimension parameters used by this script are mm unless otherwise noted
SCALE = 1000000
# v-scoring parameters
V_SCORE_TEXT_SIZE = 2
V_SCORE_TEXT_THICKNESS = 0.1
# creates a list that can be used to lookup layer numbers by their name
def get_layertable():
layertable = {}
numlayers = pcbnew.PCB_LAYER_ID_COUNT
for i in range(numlayers):
layertable[board.GetLayerName(i)] = i
return layertable
# load source board
board = LoadBoard(sourceBoardFile)
# set up layer table
layertable = get_layertable()
# get dimensions of board
boardWidth = board.GetBoardEdgesBoundingBox().GetWidth()
boardHeight = board.GetBoardEdgesBoundingBox().GetHeight()
# how many whole boards can we fit on the panel
if (PANEL_X):
NUM_X = int( (PANEL_X*SCALE - 2*HORIZONTAL_EDGE_RAIL_WIDTH*SCALE) / boardWidth )
if (PANEL_Y):
NUM_Y = int( (PANEL_Y*SCALE - 2*VERTICAL_EDGE_RAIL_WIDTH*SCALE) / boardHeight )
# check we can actually panelize the board
if (NUM_X == 0 or NUM_Y == 0):
print("Panel size is too small for board. Quitting.")
quit()
# array of tracks
tracks = board.GetTracks()
newTracks = []
for sourceTrack in tracks: # iterate through each track to be copied
for x in range(0,NUM_X): # iterate through x direction
for y in range(0, NUM_Y): # iterate through y direction
if((x!=0)or(y!=0)): # do not duplicate source object to location
newTrack = sourceTrack.Duplicate()
newTrack.Move(wxPoint(x*boardWidth, y*boardHeight)) # move to correct location
newTracks.append(newTrack) # add to temporary list of tracks
for track in newTracks:
tracks.Append(track)
# array of drawing objects
drawings = board.GetDrawings()
newDrawings = []
for drawing in drawings:
for x in range(0,NUM_X):
for y in range(0, NUM_Y):
if((x!=0)or(y!=0)):
newDrawing = drawing.Duplicate()
newDrawing.Move(wxPoint(x*boardWidth, y*boardHeight))
newDrawings.append(newDrawing)
for drawing in newDrawings:
board.Add(drawing)
# array of modules
modules = board.GetModules()
newModules = []
for sourceModule in modules:
for x in range(0,NUM_X):
for y in range(0, NUM_Y):
if((x!=0)or(y!=0)):
newModule = pcbnew.MODULE(sourceModule)
newModule.SetPosition(wxPoint(x*boardWidth + sourceModule.GetPosition().x, y*boardHeight + sourceModule.GetPosition().y))
newModules.append(newModule)
for module in newModules:
board.Add(module)
# array of zones
modules = board.GetModules()
newZones = []
for a in range(0,board.GetAreaCount()):
sourceZone = board.GetArea(a)
for x in range(0,NUM_X):
for y in range(0, NUM_Y):
if((x!=0)or(y!=0)):
newZone = sourceZone.Duplicate()
newZone.SetNet(sourceZone.GetNet())
newZone.Move(wxPoint(x*boardWidth, y*boardHeight))
newZones.append(newZone)
for zone in newZones:
board.Add(zone)
# get dimensions and center coordinate of entire array (without siderails to be added shortly)
arrayWidth = board.GetBoardEdgesBoundingBox().GetWidth()
arrayHeight = board.GetBoardEdgesBoundingBox().GetHeight()
arrayCenter = board.GetBoardEdgesBoundingBox().GetCenter()
# erase all existing edgeCuts objects (individual board outlines)
drawings = board.GetDrawings()
for drawing in drawings:
if(drawing.IsOnLayer(layertable["Edge.Cuts"])):
drawing.DeleteStructure()
# top Edge.Cuts
edge = pcbnew.DRAWSEGMENT(board)
board.Add(edge)
edge.SetStart(pcbnew.wxPoint(arrayCenter.x - arrayWidth/2 - HORIZONTAL_EDGE_RAIL_WIDTH*SCALE, arrayCenter.y - arrayHeight/2 - VERTICAL_EDGE_RAIL_WIDTH*SCALE))
edge.SetEnd(pcbnew.wxPoint(arrayCenter.x + arrayWidth/2 + HORIZONTAL_EDGE_RAIL_WIDTH*SCALE, arrayCenter.y - arrayHeight/2 - VERTICAL_EDGE_RAIL_WIDTH*SCALE))
edge.SetLayer(layertable["Edge.Cuts"])
# right Edge.Cuts
edge = pcbnew.DRAWSEGMENT(board)
board.Add(edge)
edge.SetStart(pcbnew.wxPoint(arrayCenter.x + arrayWidth/2 + HORIZONTAL_EDGE_RAIL_WIDTH*SCALE, arrayCenter.y - arrayHeight/2 - VERTICAL_EDGE_RAIL_WIDTH*SCALE))
edge.SetEnd(pcbnew.wxPoint(arrayCenter.x + arrayWidth/2 + HORIZONTAL_EDGE_RAIL_WIDTH*SCALE, arrayCenter.y + arrayHeight/2 + VERTICAL_EDGE_RAIL_WIDTH*SCALE))
edge.SetLayer(layertable["Edge.Cuts"])
# bottom Edge.Cuts
edge = pcbnew.DRAWSEGMENT(board)
board.Add(edge)
edge.SetStart(pcbnew.wxPoint(arrayCenter.x + arrayWidth/2 + HORIZONTAL_EDGE_RAIL_WIDTH*SCALE, arrayCenter.y + arrayHeight/2 + VERTICAL_EDGE_RAIL_WIDTH*SCALE))
edge.SetEnd(pcbnew.wxPoint(arrayCenter.x - arrayWidth/2 - HORIZONTAL_EDGE_RAIL_WIDTH*SCALE, arrayCenter.y + arrayHeight/2 + VERTICAL_EDGE_RAIL_WIDTH*SCALE))
edge.SetLayer(layertable["Edge.Cuts"])
# left Edge.Cuts
edge = pcbnew.DRAWSEGMENT(board)
board.Add(edge)
edge.SetStart(pcbnew.wxPoint(arrayCenter.x - arrayWidth/2 - HORIZONTAL_EDGE_RAIL_WIDTH*SCALE, arrayCenter.y + arrayHeight/2 + VERTICAL_EDGE_RAIL_WIDTH*SCALE))
edge.SetEnd(pcbnew.wxPoint(arrayCenter.x - arrayWidth/2 - HORIZONTAL_EDGE_RAIL_WIDTH*SCALE, arrayCenter.y - arrayHeight/2 - VERTICAL_EDGE_RAIL_WIDTH*SCALE))
edge.SetLayer(layertable["Edge.Cuts"])
# re-calculate board dimensions with new edge cuts
panelWidth = board.GetBoardEdgesBoundingBox().GetWidth()
panelHeight = board.GetBoardEdgesBoundingBox().GetHeight()
panelCenter = arrayCenter # should be the same as arrayCenter
# absolute edges of v-scoring
vscore_top = panelCenter.y - panelHeight/2 - V_SCORE_LINE_LENGTH_BEYOND_BOARD*SCALE
vscore_bottom = panelCenter.y + panelHeight/2 + V_SCORE_LINE_LENGTH_BEYOND_BOARD*SCALE
vscore_right = panelCenter.x + panelWidth/2 + V_SCORE_LINE_LENGTH_BEYOND_BOARD*SCALE
vscore_left = panelCenter.x - panelWidth/2 - V_SCORE_LINE_LENGTH_BEYOND_BOARD*SCALE
v_scores = []
# vertical v-scores
if HORIZONTAL_EDGE_RAIL_WIDTH >0:
rangeStart = 0
rangeEnd = NUM_X+1
else:
rangeStart = 1
rangeEnd = NUM_X
for x in range(rangeStart, rangeEnd):
x_loc = panelCenter.x - panelWidth/2 + HORIZONTAL_EDGE_RAIL_WIDTH*SCALE + boardWidth*x
v_score_line = pcbnew.DRAWSEGMENT(board)
v_scores.append(v_score_line)
v_score_line.SetStart(pcbnew.wxPoint(x_loc, vscore_top))
v_score_line.SetEnd(pcbnew.wxPoint(x_loc, vscore_bottom))
v_score_line.SetLayer(layertable[V_SCORE_LAYER])
v_score_text = pcbnew.TEXTE_PCB(board)
v_score_text.SetText(V_SCORE_TEXT)
v_score_text.SetHorizJustify(GR_TEXT_HJUSTIFY_LEFT)
v_score_text.SetPosition(wxPoint(x_loc, vscore_top - V_SCORE_TEXT_SIZE*SCALE))
v_score_text.SetTextSize(pcbnew.wxSize(SCALE*V_SCORE_TEXT_SIZE,SCALE*V_SCORE_TEXT_SIZE))
v_score_text.SetLayer(layertable[V_SCORE_TEXT_LAYER])
v_score_text.SetTextAngle(900)
board.Add(v_score_text)
# horizontal v-scores
if VERTICAL_EDGE_RAIL_WIDTH >0:
rangeStart = 0
rangeEnd = NUM_Y+1
else:
rangeStart = 1
rangeEnd = NUM_Y
for y in range(rangeStart, rangeEnd):
y_loc = panelCenter.y - panelHeight/2 + VERTICAL_EDGE_RAIL_WIDTH*SCALE + boardHeight*y
v_score_line = pcbnew.DRAWSEGMENT(board)
v_scores.append(v_score_line)
v_score_line.SetStart(pcbnew.wxPoint(vscore_left, y_loc))
v_score_line.SetEnd(pcbnew.wxPoint(vscore_right, y_loc))
v_score_line.SetLayer(layertable[V_SCORE_LAYER])
v_score_text = pcbnew.TEXTE_PCB(board)
v_score_text.SetText(V_SCORE_TEXT)
v_score_text.SetHorizJustify(GR_TEXT_HJUSTIFY_RIGHT)
v_score_text.SetPosition(wxPoint(vscore_left - V_SCORE_TEXT_SIZE*SCALE, y_loc))
v_score_text.SetTextSize(pcbnew.wxSize(SCALE*V_SCORE_TEXT_SIZE,SCALE*V_SCORE_TEXT_SIZE))
v_score_text.SetLayer(layertable[V_SCORE_TEXT_LAYER])
v_score_text.SetTextAngle(0)
board.Add(v_score_text)
# move vscores to edge.cuts layer
for vscore in v_scores:
vscore.SetLayer(layertable["Edge.Cuts"])
board.Add(vscore)
# move back to correct layer
for vscore in v_scores:
vscore.SetLayer(layertable[V_SCORE_LAYER])
# add text to rail
if args.hrailtext:
hrail_text = pcbnew.TEXTE_PCB(board)
hrail_text.SetText(HORIZONTAL_EDGE_RAIL_TEXT)
hrail_text.SetTextSize(pcbnew.wxSize(SCALE*1,SCALE*1))
hrail_text.SetLayer(F_SilkS)
hrail_text.SetHorizJustify(GR_TEXT_HJUSTIFY_LEFT)
hrail_text.SetPosition(wxPoint(panelCenter.x - panelWidth/2 + HORIZONTAL_EDGE_RAIL_WIDTH/2*SCALE, panelCenter.y + panelHeight/2 - SCALE*1))
hrail_text.SetTextAngle(900) # rotate if on hrail
board.Add(hrail_text)
if args.vrailtext:
vrail_text = pcbnew.TEXTE_PCB(board)
vrail_text.SetText(VERTICAL_EDGE_RAIL_TEXT)
vrail_text.SetTextSize(pcbnew.wxSize(SCALE*1,SCALE*1))
vrail_text.SetLayer(F_SilkS)
vrail_text.SetHorizJustify(GR_TEXT_HJUSTIFY_LEFT)
vrail_text.SetPosition(wxPoint(panelCenter.x - panelWidth/2 + SCALE*1, panelCenter.y - panelHeight/2 + VERTICAL_EDGE_RAIL_WIDTH/2*SCALE))
board.Add(vrail_text)
# add title text to rail
TITLE_TEXT = ""
if board.GetTitleBlock().GetTitle():
TITLE_TEXT += str(board.GetTitleBlock().GetTitle())
if board.GetTitleBlock().GetRevision():
TITLE_TEXT += " Rev. " + str(board.GetTitleBlock().GetRevision())
if board.GetTitleBlock().GetDate():
TITLE_TEXT += ", " + str(board.GetTitleBlock().GetDate())
if board.GetTitleBlock().GetCompany():
TITLE_TEXT += " (c) " + str(board.GetTitleBlock().GetCompany())
if args.htitle:
titleblock_text = pcbnew.TEXTE_PCB(board)
titleblock_text.SetText(TITLE_TEXT)
titleblock_text.SetTextSize(pcbnew.wxSize(SCALE*1,SCALE*1))
titleblock_text.SetLayer(F_SilkS)
titleblock_text.SetHorizJustify(GR_TEXT_HJUSTIFY_LEFT)
titleblock_text.SetPosition(wxPoint(panelCenter.x + panelWidth/2 - HORIZONTAL_EDGE_RAIL_WIDTH/2*SCALE, panelCenter.y + panelHeight/2 - SCALE*1))
titleblock_text.SetTextAngle(900)
board.Add(titleblock_text)
if args.vtitle:
titleblock_text = pcbnew.TEXTE_PCB(board)
titleblock_text.SetText(TITLE_TEXT)
titleblock_text.SetTextSize(pcbnew.wxSize(SCALE*1,SCALE*1))
titleblock_text.SetLayer(F_SilkS)
titleblock_text.SetHorizJustify(GR_TEXT_HJUSTIFY_LEFT)
titleblock_text.SetPosition(wxPoint(panelCenter.x - panelWidth/2 + SCALE*1, panelCenter.y + panelHeight/2 - VERTICAL_EDGE_RAIL_WIDTH/2*SCALE))
board.Add(titleblock_text)
# print report to panel
report_text = pcbnew.TEXTE_PCB(board)
report_args = str(panelOutputFile) + " (" + str(NUM_X) + "x" + str(NUM_Y) + " panel) generated with:\n./panelizer.py"
for x in sys.argv[1:]:
report_args += " " + x
report_text.SetText(report_args)
report_text.SetTextSize(pcbnew.wxSize(SCALE*1,SCALE*1))
report_text.SetLayer(layertable["Cmts.User"])
report_text.SetHorizJustify(GR_TEXT_HJUSTIFY_CENTER)
report_text.SetPosition(wxPoint(panelCenter.x, vscore_bottom + 10*SCALE))
board.Add(report_text)
# save output
board.Save(panelOutputFile)
# warn if panel is under 70x70mm
if (panelWidth/SCALE < 70 or panelHeight/SCALE < 70):
print("Warning: panel is under 70x70mm")
# print report
if (PANEL_X or PANEL_Y):
print("You can fit " + str(NUM_X) +" x " + str(NUM_Y) + " boards on the panel")
print("Board dimensions: " + str(boardWidth/SCALE) + "x" + str(boardHeight/SCALE) + "mm")
print("Panel dimensions: " + str(panelWidth/SCALE) + "x" + str(panelHeight/SCALE) + "mm")