-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcoil_plugin.py
261 lines (239 loc) · 11.3 KB
/
coil_plugin.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
import pcbnew
import json
import wx
import math
CENTER_X = 150
CENTER_Y = 100
def create_tracks(board, group, net, layer, thickness, coords):
last_x = None
last_y = None
for coord in coords:
x = coord["x"] + CENTER_X
y = coord["y"] + CENTER_Y
track = pcbnew.PCB_TRACK(board)
if last_x is not None:
track.SetStart(pcbnew.wxPointMM(float(last_x), float(last_y)))
track.SetEnd(pcbnew.wxPointMM(float(x), float(y)))
track.SetWidth(int(thickness * 1e6))
track.SetLayer(layer)
if net is not None:
track.SetNetCode(net.GetNetCode())
board.Add(track)
group.AddItem(track)
last_x = x
last_y = y
class CoilPlugin(pcbnew.ActionPlugin):
def defaults(self):
self.name = "Create coil"
self.category = "Coils"
self.description = "Creates a coil"
# self.show_toolbar_button = False # Optional, defaults to False
# self.icon_file_name = os.path.join(os.path.dirname(__file__), 'simple_plugin.png') # Optional, defaults to ""
def Run(self):
# launch a file picker dialog to get the coil file
dialog = wx.FileDialog(None, "Choose a coil file", "", "", "*.json", wx.FD_OPEN)
if dialog.ShowModal() == wx.ID_OK:
# read the file
with open(dialog.GetPath(), "r") as f:
board = pcbnew.GetBoard()
# load up the JSON with the coil parameters
coil_data = json.load(f)
# parameters
track_width = coil_data["parameters"]["trackWidth"]
pin_diameter = coil_data["parameters"]["pinDiameter"]
pin_drill = coil_data["parameters"]["pinDrillDiameter"]
via_diameter = coil_data["parameters"]["viaDiameter"]
via_drill_diameter = coil_data["parameters"]["viaDrillDiameter"]
# put everything in a group to make it easier to manage
pcb_group = pcbnew.PCB_GROUP(board)
board.Add(pcb_group)
# create tracks
for track in coil_data["tracks"]["f"]:
net = self.findNet(board, track)
create_tracks(
board, pcb_group, net, pcbnew.F_Cu, track["width"], track["pts"]
)
for track in coil_data["tracks"]["b"]:
net = self.findNet(board, track)
create_tracks(
board, pcb_group, net, pcbnew.B_Cu, track["width"], track["pts"]
)
pcb_layers = [
pcbnew.In1_Cu,
pcbnew.In2_Cu,
pcbnew.In3_Cu,
pcbnew.In4_Cu,
pcbnew.In5_Cu,
pcbnew.In6_Cu,
]
for i, track_list in enumerate(coil_data["tracks"]["in"]):
for track in track_list:
net = self.findNet(board, track)
create_tracks(
board, pcb_group, net, pcb_layers[i], track["width"], track["pts"]
)
# create the vias
for via in coil_data["vias"]:
net = self.findNet(board, via)
pcb_via = pcbnew.PCB_VIA(board)
pcb_via.SetPosition(
pcbnew.wxPointMM(via["x"] + CENTER_X, via["y"] + CENTER_Y)
)
pcb_via.SetWidth(int(via_diameter * 1e6))
pcb_via.SetDrill(int(via_drill_diameter * 1e6))
pcb_via.SetNetCode(net.GetNetCode())
board.Add(pcb_via)
pcb_group.AddItem(pcb_via)
# create the pins
# for pin in coil_data["pins"]:
# x = pin["x"] + CENTER_X
# y = pin["y"] + CENTER_Y
# module = pcbnew.FOOTPRINT(board)
# module.SetPosition(pcbnew.wxPointMM(x, y))
# board.Add(module)
# pcb_pad = pcbnew.PAD(module)
# pcb_pad.SetSize(pcbnew.wxSizeMM(pin_diameter, pin_diameter))
# pcb_pad.SetShape(pcbnew.PAD_SHAPE_CIRCLE)
# pcb_pad.SetAttribute(pcbnew.PAD_ATTRIB_PTH)
# pcb_pad.SetLayerSet(pcb_pad.PTHMask())
# pcb_pad.SetDrillSize(pcbnew.wxSizeMM(pin_drill, pin_drill))
# pcb_pad.SetPosition(pcbnew.wxPointMM(x, y))
# pcb_pad.SetNetCode(net.GetNetCode())
# module.Add(pcb_pad)
# create the pads
lset = pcbnew.LSET()
lset.AddLayer(pcbnew.B_Cu)
for pin in coil_data["pads"]:
net = self.findNet(board, pin)
x = pin["x"] + CENTER_X
y = pin["y"] + CENTER_Y
module = pcbnew.FOOTPRINT(board)
module.SetPosition(pcbnew.wxPointMM(x, y))
board.Add(module)
pcb_pad = pcbnew.PAD(module)
pcb_pad.SetSize(pcbnew.wxSizeMM(pin["width"], pin["height"]))
pcb_pad.SetShape(pcbnew.PAD_SHAPE_RECT)
pcb_pad.SetAttribute(pcbnew.PAD_ATTRIB_SMD)
pcb_pad.SetLayerSet(pcb_pad.SMDMask())
# pcb_pad.SetLayerSet(lset)
pcb_pad.SetPosition(pcbnew.wxPointMM(x, y))
pcb_pad.SetNetCode(net.GetNetCode())
pcb_pad.Flip(pcbnew.wxPointMM(x, y), False)
module.Add(pcb_pad)
# create any silk screen
for text in coil_data["silk"]:
x = text["x"] + CENTER_X
y = text["y"] + CENTER_Y
pcb_txt = pcbnew.PCB_TEXT(board)
pcb_txt.SetText(text["text"])
pcb_txt.SetPosition(pcbnew.wxPointMM(x, y))
pcb_txt.SetHorizJustify(pcbnew.GR_TEXT_HJUSTIFY_CENTER)
pcb_txt.Rotate(pcbnew.wxPointMM(x, y), text["angle"])
pcb_txt.SetTextSize(
pcbnew.wxSize(
text["size"] * pcbnew.IU_PER_MM,
text["size"] * pcbnew.IU_PER_MM,
)
)
pcb_txt.SetLayer(pcbnew.F_SilkS)
if text["layer"] == "b":
pcb_txt.Flip(pcbnew.wxPointMM(x, y), True)
board.Add(pcb_txt)
pcb_group.AddItem(pcb_txt)
# create the mounting holes
# for hole in coil_data["mountingHoles"]:
# x = hole["x"] + CENTER_X
# y = hole["y"] + CENTER_Y
# module = pcbnew.FOOTPRINT(board)
# module.SetPosition(pcbnew.wxPointMM(x, y))
# board.Add(module)
# pcb_pad = pcbnew.PAD(module)
# pcb_pad.SetSize(pcbnew.wxSizeMM(hole["diameter"], hole["diameter"]))
# pcb_pad.SetShape(pcbnew.PAD_SHAPE_CIRCLE)
# pcb_pad.SetAttribute(pcbnew.PAD_ATTRIB_NPTH)
# # pcb_pad.SetLayerSet(pcb_pad.NPTHMask())
# pcb_pad.SetDrillSize(
# pcbnew.wxSizeMM(hole["diameter"], hole["diameter"])
# )
# pcb_pad.SetPosition(pcbnew.wxPointMM(x, y))
# module.Add(pcb_pad)
# pcb_group.AddItem(pcb_hole)
# crate the edge cuts
for edge_cut in coil_data["edgeCuts"]:
ec = pcbnew.PCB_SHAPE(board)
ec.SetShape(pcbnew.SHAPE_T_POLY)
ec.SetFilled(False)
ec.SetLayer(pcbnew.Edge_Cuts)
ec.SetWidth(int(0.1 * pcbnew.IU_PER_MM))
v = pcbnew.wxPoint_Vector()
for point in edge_cut:
x = point["x"] + CENTER_X
y = point["y"] + CENTER_Y
v.append(pcbnew.wxPointMM(x, y))
ec.SetPolyPoints(v)
board.Add(ec)
# put it on the solder mask as well - who knows why...
for edge_cut in coil_data["edgeCuts"]:
ec = pcbnew.PCB_SHAPE(board)
ec.SetShape(pcbnew.SHAPE_T_POLY)
ec.SetFilled(False)
ec.SetLayer(pcbnew.F_Mask)
ec.SetWidth(int(0.1 * pcbnew.IU_PER_MM))
v = pcbnew.wxPoint_Vector()
for point in edge_cut:
x = point["x"] + CENTER_X
y = point["y"] + CENTER_Y
v.append(pcbnew.wxPointMM(x, y))
ec.SetPolyPoints(v)
board.Add(ec)
for edge_cut in coil_data["edgeCuts"]:
ec = pcbnew.PCB_SHAPE(board)
ec.SetShape(pcbnew.SHAPE_T_POLY)
ec.SetFilled(False)
ec.SetLayer(pcbnew.B_Mask)
ec.SetWidth(int(0.1 * pcbnew.IU_PER_MM))
v = pcbnew.wxPoint_Vector()
for point in edge_cut:
x = point["x"] + CENTER_X
y = point["y"] + CENTER_Y
v.append(pcbnew.wxPointMM(x, y))
ec.SetPolyPoints(v)
board.Add(ec)
# Add components
# coil_data["components"] = [
# {
# "ref": "LED1",
# "pads": [
# {"num": "1", "net": "LED_IO_36"},
# {"num": "2", "net": "GND"},
# {"num": "3", "net": "LED_IO_35"},
# {"num": "4", "net": "V+"},
# ],
# }
# ]
for i, component in enumerate(coil_data["components"]):
component_ref = component["ref"]
module = board.FindFootprintByReference(component_ref)
for pad in component["pads"]:
pad_num = str(pad["num"])
pcb_pad = module.FindPadByNumber(pad_num)
net = self.findNet(board, pad)
# wx.MessageBox("ref: " + component_ref + " Pad: " + pad_num + " Net: " + pad["net"] + " NetCode: " + str(net.GetNetCode()))
if net is not None:
pcb_pad.SetNetCode(net.GetNetCode())
module.Add(pcb_pad)
pcb_group.AddItem(pcb_pad)
def findNet(self, board, element):
# find the matching net for the track
net_name = ""
if "net" in element:
net_name = element["net"]
if net_name == "":
return None
net = board.FindNet(net_name)
if net is None:
net = pcbnew.NETINFO_ITEM(board, net_name)
board.Add(net)
# raise "Net not found: {}".format(net_name)
return net
CoilPlugin().register() # Instantiate and register to Pcbnew])