-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpanelizer.py
executable file
·343 lines (270 loc) · 13.2 KB
/
panelizer.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
#!/usr/bin/env python3
import os
import sys
from argparse import ArgumentParser
from pathlib import Path
import pcbnew
from pcbnew import *
"""
A simple script to create a v-scored panel of a KiCad board.
Author: Willem Hillier
This script is very much in-progress, and so here's an extensive TODO list:
- Put report in panel file in a text field
- Put logo/text block on panel border
- Put fuducials on border
- Auto-calculate distance from line to text center ("V-SCORE") based on text size
- Is there a way to pull back copper layers to the pullback distances so if the user presses "b" on the panel, it doesn't get wrecked (by copper getting too close to V-scores)
- (maybe) Make a "DRC" that checks if copper is too close to V-score lines
"""
# set up command-line arguments parser
parser = ArgumentParser(description="A script to panelize KiCad files.")
parser.add_argument(dest="sourceBoardFile", help='Path to the *.kicad_pcb file to be panelized')
parser.add_argument(dest="x", type=int, help='Number of boards in X direction')
parser.add_argument(dest="y", type=int, help='Number of boards in Y direction')
parser.add_argument("-v", "--verbose", action='store_true', help='Show progress')
args = parser.parse_args()
sourceBoardFile = args.sourceBoardFile
NUM_X = args.x
NUM_Y = args.y
#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()
# Output file name is format {inputFile}_panelized.kicad_pcb
panelOutputFile = os.path.splitext(sourceBoardFile)[0] + "_panelized.kicad_pcb"
# To scale KiCad's nm to mm
# All dimension parameters used by this script are mm unless otherwise noted
SCALE = 1000000
# edge rail dimensions
HORIZONTAL_EDGE_RAIL_WIDTH = 0
VERTICAL_EDGE_RAIL_WIDTH = 5
#v-scoring parameters
V_SCORE_LAYER = "Eco1.User"
V_SCORE_LINE_LENGTH_BEYOND_BOARD = 20
V_SCORE_TEXT_CENTER_TO_LINE_LENGTH = 10
V_SCORE_TEXT = "V-SCORE"
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
# print("{} {}".format(i, board.GetLayerName(i)))
return layertable
#Used to create progress bar, from here: https://gist.github.com/vladignatyev/06860ec2040cb497f0f3
def progress(count, total, suffix=''):
bar_len = 60
filled_len = int(round(bar_len * count / float(total)))
percents = round(100.0 * count / float(total), 1)
bar = '=' * filled_len + '-' * (bar_len - filled_len)
sys.stdout.write('[%s] %s%s %s\r' % (bar, percents, '%', suffix))
sys.stdout.flush() # As suggested by Rom Ruben
#load source board
board = LoadBoard(sourceBoardFile)
if args.verbose: print('Loaded board\n')
# set up layer table
layertable = get_layertable()
#get dimensions of board
boardWidth = board.GetBoardEdgesBoundingBox().GetWidth()
boardHeight = board.GetBoardEdgesBoundingBox().GetHeight()
# Now it's time to make an array of tracks, drawings, modules (footprints), and zones.
# array of tracks
tracks = board.GetTracks()
#get total number of tracks for progress bar
n = 0
for track in tracks:
n += 1
i = 0
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
i += 1
if((x!=0)or(y!=0)): # Don't 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
if args.verbose: progress(i ,n*NUM_X*NUM_Y,'Positioning tracks') # Progress bar
if args.verbose: print("\n")
i=0
for track in newTracks: # add new tracks to board
tracks.Append(track)
i += 1
if args.verbose: progress(i, len(newTracks), 'Adding tracks to panel' )
# array of drawing objects
drawings = board.GetDrawings()
n = 0
for drawing in drawings:
n += 1
newDrawings = []
i = 0
for drawing in drawings: # Iterate through each object 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
i += 1
if((x!=0)or(y!=0)): # Don't duplicate source object to location
newDrawing = drawing.Duplicate()
newDrawing.Move(wxPoint(x*boardWidth, y*boardHeight)) # Move to correct location
newDrawings.append(newDrawing) # Add to temporary list of objects
if args.verbose: progress(i ,n*NUM_X*NUM_Y,'Positioning drawings') # Progress bar
if args.verbose: print("\n")
i=0
for drawing in newDrawings: # add new objects to board
board.Add(drawing)
i += 1
if args.verbose: progress(i ,len(newDrawings),'Adding drawings to panel') # Progress bar
if args.verbose: print("\n")
# array of modules
modules = board.GetModules()
n = 0
for module in modules:
n += 1
newModules = []
i = 0
for sourceModule in modules: # Iterate through each object 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
i += 1
if((x!=0)or(y!=0)): # Don't duplicate source object to location
newModule = pcbnew.MODULE(sourceModule)
newModule.SetPosition(wxPoint(x*boardWidth + sourceModule.GetPosition().x, y*boardHeight + sourceModule.GetPosition().y)) # Move to correct location
newModules.append(newModule) # Add to temporary list of objects
if args.verbose: progress(i ,n*NUM_X*NUM_Y,'Positioning modules') # Progress bar
if args.verbose: print("\n")
i = 0
for module in newModules: # add new objects to board
board.Add(module)
i += 1
if args.verbose: progress(i, len(newModules), 'Adding modules to panel')
if args.verbose: print("\n")
# array of zones
modules = board.GetModules() #used to extract nets for zones...
newZones = []
#total for progress bar
n = 0
for a in range(0, board.GetAreaCount()):
for x in range(0, NUM_X):
for y in range(0, NUM_Y):
n += 1
i = 0
for a in range(0,board.GetAreaCount()): # Iterate through each object to be copied
sourceZone = board.GetArea(a)
for x in range(0,NUM_X): # Iterate through x direction
for y in range(0, NUM_Y): # Iterate through y direction
i += 1
if args.verbose: progress(i, n, 'Determining nets of zones')
if((x!=0)or(y!=0)): # Don't duplicate source object to location
newZone = sourceZone.Duplicate()
newZone.SetNet(sourceZone.GetNet())
newZone.Move(wxPoint(x*boardWidth, y*boardHeight)) # Move to correct location
# simulate pullback distance
newZones.append(newZone) # Add to temporary list of objects
if args.verbose: print("\n")
i = 0
for zone in newZones: # add new objects to board
board.Add(zone)
i += 1
if args.verbose: progress(i, len(newZones), 'Adding new zones to board')
if args.verbose: print("\n")
# 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: # Iterate through each object to be copied
if(drawing.IsOnLayer(layertable["Edge.Cuts"])):
drawing.DeleteStructure()
if args.verbose: print("Existing Edge.Cuts lines deleted.")
# Create actual Edge.Cuts perimeter
# top
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 = 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 = 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 = 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"])
if args.verbose: print("New Edge.Cuts created.")
# re-calculate board dimensions with new edge cuts
panelWidth = board.GetBoardEdgesBoundingBox().GetWidth()
panelHeight = board.GetBoardEdgesBoundingBox().GetHeight()
panelCenter = arrayCenter #should be the same as arrayCenter
# V-scoring lines
# 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
for x in range(1,NUM_X):
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.SetPosition(wxPoint(x_loc, vscore_top - V_SCORE_TEXT_CENTER_TO_LINE_LENGTH*SCALE))
v_score_text.SetTextSize(pcbnew.wxSize(SCALE*V_SCORE_TEXT_SIZE,SCALE*V_SCORE_TEXT_SIZE))
#v_score_text.SetThickness(SCALE*1)
v_score_text.SetLayer(layertable[V_SCORE_LAYER])
v_score_text.SetTextAngle(900)
board.Add(v_score_text)
if args.verbose: print("Vertical v-scores created.")
# horizontal v-scores
for y in range(0,NUM_Y+1):
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.SetPosition(wxPoint(vscore_left - V_SCORE_TEXT_CENTER_TO_LINE_LENGTH*SCALE, y_loc))
v_score_text.SetTextSize(pcbnew.wxSize(SCALE*V_SCORE_TEXT_SIZE,SCALE*V_SCORE_TEXT_SIZE))
#v_score_text.SetThickness(SCALE*1)
v_score_text.SetLayer(layertable[V_SCORE_LAYER])
v_score_text.SetTextAngle(0)
board.Add(v_score_text)
if args.verbose: print("Horizontal v-scores created.")
# In order to make sure copper is pulled-back from the v-score, we can move the v-scores to the edge.cuts layer, re-fill zones, then move them back.
# Also, lock all the zones to ensure they can't get refilled and therefore messed up.
# move vscores to edge.cuts layer
for vscore in v_scores:
vscore.SetLayer(layertable["Edge.Cuts"])
board.Add(vscore)
# refill zones
#pcbnew.ZONE_FILLER(board).Fill(board.Zones())
# move back to correct layer
for vscore in v_scores:
vscore.SetLayer(layertable[V_SCORE_LAYER])
# Save output
board.Save(panelOutputFile)
print("Board output saved to " + panelOutputFile)
# Print report
print("\nREPORT:")
print("Board dimensions: " + str(boardWidth/SCALE) + "x" + str(boardHeight/SCALE) + "mm")
print("Panel dimensions: " + str(panelWidth/SCALE) + "x" + str(panelHeight/SCALE) + "mm")