forked from atomic14/kicad-coil-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcb_json.py
256 lines (222 loc) · 6.62 KB
/
pcb_json.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
import json
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from helpers import rotate
def create_pin(radius, angle, name, net_name):
return {
"x": radius * np.cos(np.deg2rad(angle)),
"y": radius * np.sin(np.deg2rad(angle)),
"name": name,
"net": net_name,
}
def create_pad(point, width, height, layer, net_name, angle=0):
return {
"x": point[0],
"y": point[1],
"width": width,
"height": height,
"layer": layer,
"angle": angle,
"net": net_name,
}
def create_silk(point, text, layer="f", size=1, angle=0):
return {
"x": point[0],
"y": point[1],
"text": text,
"layer": layer,
"size": size,
"angle": angle,
}
def create_via(point, net_name):
return {"x": point[0], "y": point[1], "net": net_name}
# def create_track(points, net_name):
# return [{"x": x, "y": y, "net": net_name} for x, y in points]
def create_mounting_hole(point, diameter):
return {
"x": point[0],
"y": point[1],
"diameter": diameter,
}
def create_track_json(points):
return [{"x": x, "y": y} for x, y in points]
def dump_json(
filename,
track_width,
pin_diam,
pin_drill,
via_diam,
via_drill,
vias,
pins,
pads,
silk,
tracks_f,
tracks_in,
tracks_b,
mounting_holes,
edge_cuts,
components
):
tracks_inner = [[
{
"net":track_info["net"],
"width": track_info["width"] if "width" in track_info else track_width,
"pts":create_track_json(track_info["pts"]),
}
for track_info in track_vals]
for i, track_vals in enumerate(tracks_in)]
tracks = {
"b": [
{
"net":track_info["net"],
"width": track_info["width"] if "width" in track_info else track_width,
"pts":create_track_json(track_info["pts"]),
}
for track_info in tracks_b],
"f": [
{
"net":track_info["net"],
"width": track_info["width"] if "width" in track_info else track_width,
"pts":create_track_json(track_info["pts"]),
}
for track_info in tracks_f],
"in": tracks_inner
}
# dump out the results to json
json_result = {
"parameters": {
"trackWidth": track_width,
"viaDiameter": via_diam,
"viaDrillDiameter": via_drill,
"pinDiameter": pin_diam,
"pinDrillDiameter": pin_drill,
},
"vias": vias,
"pins": pins,
"pads": pads,
"silk": silk,
"tracks": tracks,
"mountingHoles": mounting_holes,
"edgeCuts": [create_track_json(points) for points in edge_cuts],
"components": components,
}
json.dump(json_result, open(filename, "w"))
return json_result
def plot_json(json_result):
pin_diam = json_result["parameters"]["pinDiameter"]
pin_drill = json_result["parameters"]["pinDrillDiameter"]
# track_width = json_result["parameters"]["trackWidth"]
via_dim = json_result["parameters"]["viaDiameter"]
via_drill = json_result["parameters"]["viaDrillDiameter"]
# plot the back tracks
ax = None
for track in json_result["tracks"]["b"]:
df = pd.DataFrame(track["pts"], columns=["x", "y"])
ax = df.plot.line(x="x", y="y", color="blue", ax=ax)
ax.axis("equal")
colors = ["green", "orange", "cyan", "magenta"]
color_index = 0
# plot the inner tracks
for index in json_result["tracks"]["in"]:
for track in index:
df = pd.DataFrame(track["pts"], columns=["x", "y"])
ax = df.plot.line(x="x", y="y", color=colors[color_index % 4], ax=ax)
ax.axis("equal")
color_index += 1
# plot the front tracks
for track in json_result["tracks"]["f"]:
df = pd.DataFrame(track["pts"], columns=["x", "y"])
ax = df.plot.line(x="x", y="y", color="red", ax=ax)
ax.axis("equal")
# set the axis range
ax.set_xlim(-30, 30)
ax.set_ylim(-30, 30)
ax.invert_yaxis() # match KiCAD where y-axis is inverted (numbers inrease DOWN the screen)
# plot the pads
for pad in json_result["pads"]:
color = "red"
if pad["layer"] == "b":
color = "blue"
ax.add_patch(
plt.Rectangle(
(pad["x"] - pad["width"] / 2, pad["y"] - pad["height"] / 2),
pad["width"],
pad["height"],
fill=True,
color=color,
# rotate by the angle
angle=pad["angle"],
)
)
# plot the pins
for pin in json_result["pins"]:
ax.add_patch(
plt.Circle(
(pin["x"], pin["y"]),
radius=pin_diam / 2,
fill=True,
color="orange",
)
)
ax.add_patch(
plt.Circle(
(pin["x"], pin["y"]),
radius=pin_drill / 2,
fill=True,
color="white",
)
)
# plot the silk
for silk in json_result["silk"]:
color = "cyan"
if silk["layer"] == "b":
color = "magenta"
ax.text(
silk["x"],
silk["y"],
silk["text"],
horizontalalignment="center",
verticalalignment="center",
color=color,
fontsize=silk["size"] * 10,
)
# plot the vias
for via in json_result["vias"]:
ax.add_patch(
plt.Circle(
(via["x"], via["y"]),
radius=via_dim / 2,
fill=True,
color="black",
)
)
ax.add_patch(
plt.Circle(
(via["x"], via["y"]),
radius=via_drill / 2,
fill=True,
color="white",
)
)
# plot the mounting holes
for hole in json_result["mountingHoles"]:
ax.add_patch(
plt.Circle(
(hole["x"], hole["y"]),
radius=hole["diameter"] / 2,
fill=False,
color="orange",
)
)
# plot the edge cuts
for edge_cut in json_result["edgeCuts"]:
df = pd.DataFrame(edge_cut, columns=["x", "y"])
ax = df.plot.line(x="x", y="y", color="orange", ax=ax)
# hide the legend
ax.legend().set_visible(False)
# make the plot bigger
ax.figure.set_size_inches(11, 11)
# save to file
ax.figure.savefig("coils.png")