forked from LordBlick/kicadPanelizer
/
generate_panelized_pcb.py
executable file
·230 lines (163 loc) · 6.73 KB
/
generate_panelized_pcb.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
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
# -*- coding: utf-8 -*-
# Copyright Lordblick, Christoph Riehl
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# This panelizer script is a modified and simplified form of the one from https://github.com/LordBlick/kicadPanelizer
# Christoph Riehl, f4eru <at> free.fr
# this version works without a GUI.
# the project settings are stored directly in this script file, please see/edit settings a few lines down from here.
##############################################################################################
# libs to load
##############################################################################################
from pcbnew import LoadBoard, PCB_TEXT, FOOTPRINT, \
Edge_Cuts as Cutout, wxPoint as wxPt, FromMils, FromMM
from os import path as pt
import re
from parse import *
import pcbnew
import numbers
##############################################################################################
# user settings
# please edit here
##############################################################################################
# put the source file name here
fileNamePCB = 'demo_files/demo_layout.kicad_pcb'
# comment one of those, depending on which units to use
#Units=FromMils
Units=FromMM
# umber of boards in each dimension
nx = 3
ny = 2
# stepping width
step_x = 65
step_y = 142 + 2 # 2 is the router tool width
# general offset
offset_x = 0
offset_y = 0
# rotation definition
angles = [0,180] # multiple angle steps can be defined for panels with multiple orientations
center_x = 82.5
center_y = 121
# how to renumber components, choose one option:
enumeration_base =0 # 0 to deactivate renumbering function
#enumeration_base ='!' # '!' or any other key character to replace that character with the number. Components will hve to have this key in the number like R!01 for R101, R102, etc...
#enumeration_base =100 # components will have numbers in 101,201,301, ...
##############################################################################################
# subfunctions
##############################################################################################
# listing objects in a PCB
def brdItemize(pcb):
lsItems = []
nullType = type(None)
for ItemStr in ('Drawings', 'Tracks', 'Footprints'):
for idx, Item in enumerate(getattr(pcb, 'Get'+ItemStr)()):
if type(Item)==nullType:
raise(TypeError, "Null Object Error#%i, expected %s Type…" % (idx+1, ItemStr[:-1]))
lsItems.append(Item)
nZones = pcb.GetAreaCount()
if nZones:
for idx in range(nZones):
Zone = pcb.GetArea(idx)
if type(Zone)==nullType:
raise(TypeError, "Null Object Error#%i, expected Zone/Area Type…" % (idx+1,))
lsItems.append(Zone)
return lsItems
# instanciates one object list on the board, with rotation and displacement
def pcb_instanciate_one_board(pcb, lsItems, vector, angle, center):
global boardcount
boardcount = boardcount+1;
for Item in lsItems:
if type(Item) is FOOTPRINT:
newItem = pcbnew.FOOTPRINT(Item) # creates new FOOTPRINT n as a copy of m
pcb.Add(newItem)
newItem.SetPosition(Item.GetPosition())
else:
newItem = Item.Duplicate()
pcb.Add(newItem)
if angle:
newItem.Rotate(center, angle*10) # kicad uses decidegrees ....
newItem.Move(vector)
# replace the marker "##PCBNUMBER##" by tha ctual number of this copy
if type(newItem) is PCB_TEXT:#
text = newItem.GetText()
text = re.sub(r'\##PCBNUMBER##',str(boardcount),text)
newItem.SetText(text)
# renumerate FOOTPRINTs including the PCB number
if type(newItem) is FOOTPRINT:
if enumeration_base:
ref = newItem.GetReference()
if isinstance(enumeration_base, numbers.Number):
match = re.match(r"(\D+)(\d+)", ref)
if match:
items = match.groups()
ref_letter = (items[0]).encode('ascii', 'ignore')
ref_num = int(items[1])
ref = "%s%i"%(ref_letter, ref_num +boardcount * enumeration_base)
else:
ref = re.sub(enumeration_base,str(boardcount),ref)
newItem.SetReference(ref)
return
# instanciates multiple object lists on the board, stepping displacements
def pcb_instanciate_multiple_boards(pcb, lsItems, nx, ny, step_vector, offset_vector, angle, center):
if nx<1 or(ny<1):
raise(ValueError, "Columns(%i) or Rows(%i) cannot be less than 1…" % (nx, ny))
for y in range(ny):
for x in range(nx):
vector_to_place = wxPt(x*step_vector.x + offset_vector.x, y*step_vector.y + offset_vector.y)
pcb_instanciate_one_board(pcb, lsItems, vector_to_place, angle, center)
return
##############################################################################################
# panelizing script
##############################################################################################
# generate vectors from dimensions
step_vector = wxPt(Units(step_x),Units(step_y))
offset_vector = wxPt(Units(offset_x),Units(offset_y))
center = wxPt(Units(center_x),Units(center_y))
# derived file names
fileNameSave = "%s-panelized.%s" % tuple(fileNamePCB.rsplit('.', 1))
fileNameCommonParts = "%s-panel-common.%s" % tuple(fileNamePCB.rsplit('.', 1))
# load PCB
print("Reading file: '%s'\n"% fileNamePCB)
pcb = LoadBoard(fileNamePCB)
print("\n")
# get all items
lsItems = brdItemize(pcb)
# delete original items
for Item in lsItems:
pcb.Remove(Item)
boardcount =0; # global variable is fine here for counting boards
# instantiate boards @ different rotations
for angle in angles:
pcb_instanciate_multiple_boards(pcb, lsItems, nx, ny, step_vector, offset_vector, angle, center)
print("Instantiate %i boards\n"% boardcount)
# try to load and add common parts
print("trying to read file: '%s'\n"% fileNameCommonParts)
try:
pcb_common = LoadBoard(fileNameCommonParts)
print("\n")
except:
print("File '%s' not found, Ignoring common parts\n"% fileNameCommonParts)
pass
else:
# get all items
lsItems_common = brdItemize(pcb_common)
# add common parts
print("Add common parts from file: '%s'\n"% fileNameCommonParts)
pcb_instanciate_one_board(pcb, lsItems_common, wxPt(0,0), 0, wxPt(0,0))
# save finished panel
print("\nSave panel to file:'%s'\n"% fileNameSave)
pcb.Save(fileNameSave)