pcb-stator-coil-generator/coil_plugin.py

137 lines
5.9 KiB
Python
Raw Normal View History

2022-09-28 09:20:08 +00:00
import pcbnew
2022-10-04 17:43:35 +00:00
import json
2022-10-08 16:59:50 +00:00
import wx
2022-09-28 09:20:08 +00:00
2022-10-04 17:43:35 +00:00
def create_tracks(board, group, net, layer, thickness, coords):
2022-09-28 09:20:08 +00:00
last_x = None
last_y = None
2022-10-04 17:43:35 +00:00
for coord in coords:
2022-10-08 09:27:38 +00:00
x = coord["x"]
y = coord["y"]
2022-09-28 09:20:08 +00:00
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)))
2022-10-04 17:43:35 +00:00
track.SetWidth(int(thickness * 1e6))
2022-09-28 09:20:08 +00:00
track.SetLayer(layer)
2022-10-06 09:18:11 +00:00
if net is not None:
track.SetNetCode(net.GetNetCode())
2022-09-28 09:20:08 +00:00
board.Add(track)
2022-10-09 19:46:45 +00:00
# group.AddItem(track)
2022-09-28 09:20:08 +00:00
last_x = x
last_y = y
class CoilPlugin(pcbnew.ActionPlugin):
def defaults(self):
2022-10-04 17:43:35 +00:00
self.name = "Create coil 3"
2022-09-28 09:20:08 +00:00
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):
2022-10-08 16:59:50 +00:00
# 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"]
stator_hole_radius = coil_data["parameters"]["statorHoleRadius"]
2022-10-08 18:17:54 +00:00
stator_radius = coil_data["parameters"]["statorRadius"]
pad_diameter = coil_data["parameters"]["padDiameter"]
pad_drill = coil_data["parameters"]["padDrillDiameter"]
2022-10-08 16:59:50 +00:00
via_diameter = coil_data["parameters"]["viaDiameter"]
via_drill_diameter = coil_data["parameters"]["viaDrillDiameter"]
2022-10-04 17:43:35 +00:00
2022-10-08 16:59:50 +00:00
# put everything in a group to make it easier to manage
pcb_group = pcbnew.PCB_GROUP(board)
2022-10-09 19:46:45 +00:00
# board.Add(pcb_group)
2022-10-04 17:43:35 +00:00
2022-10-08 16:59:50 +00:00
# create tracks
for track in coil_data["tracks"]["f"]:
# find the matching net for the track
net = board.FindNet("coils")
if net is None:
2022-10-09 19:46:45 +00:00
net = pcbnew.NETINFO_ITEM(board, "coils")
board.Add(net)
# raise "Net not found: {}".format(track["net"])
2022-10-08 16:59:50 +00:00
create_tracks(
board, pcb_group, net, pcbnew.F_Cu, track_width, track
)
2022-10-08 09:27:38 +00:00
2022-10-08 16:59:50 +00:00
for track in coil_data["tracks"]["b"]:
create_tracks(
board, pcb_group, net, pcbnew.B_Cu, track_width, track
)
2022-10-04 17:43:35 +00:00
2022-10-08 16:59:50 +00:00
# create the vias
for via in coil_data["vias"]:
pcb_via = pcbnew.PCB_VIA(board)
pcb_via.SetPosition(
pcbnew.wxPointMM(float(via["x"]), float(via["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)
2022-10-09 19:46:45 +00:00
# pcb_group.AddItem(pcb_via)
# create the pads
for pad in coil_data["pads"]:
module = pcbnew.FOOTPRINT(board)
module.SetPosition(pcbnew.wxPointMM(pad["x"], pad["y"]))
2022-10-09 19:46:45 +00:00
board.Add(module)
pcb_pad = pcbnew.PAD(module)
pcb_pad.SetSize(pcbnew.wxSizeMM(pad_diameter, pad_diameter))
2022-10-09 19:46:45 +00:00
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(pad_drill, pad_drill))
2022-10-09 19:46:45 +00:00
pcb_pad.SetPosition(pcbnew.wxPointMM(pad["x"], pad["y"]))
pcb_pad.SetNetCode(net.GetNetCode())
module.Add(pcb_pad)
2022-10-04 17:43:35 +00:00
2022-10-08 16:59:50 +00:00
# create any silk screen
for text in coil_data["silk"]:
pcb_txt = pcbnew.PCB_TEXT(board)
pcb_txt.SetText(text["text"])
pcb_txt.SetPosition(pcbnew.wxPointMM(text["x"], text["y"]))
pcb_txt.SetHorizJustify(pcbnew.GR_TEXT_HJUSTIFY_CENTER)
pcb_txt.SetTextSize(pcbnew.wxSize(5000000, 5000000))
pcb_txt.SetLayer(pcbnew.F_SilkS)
board.Add(pcb_txt)
2022-10-09 19:46:45 +00:00
# pcb_group.AddItem(pcb_txt)
2022-10-04 17:43:35 +00:00
2022-10-09 22:27:22 +00:00
# create the stator outline
arc = pcbnew.PCB_SHAPE(board)
arc.SetShape(pcbnew.SHAPE_T_CIRCLE)
arc.SetFilled(False)
arc.SetStart(pcbnew.wxPointMM(0, 0))
arc.SetEnd(pcbnew.wxPointMM(stator_radius, 0))
arc.SetCenter(pcbnew.wxPointMM(0, 0))
arc.SetLayer(pcbnew.Edge_Cuts)
arc.SetWidth(int(0.1 * pcbnew.IU_PER_MM))
board.Add(arc)
# pcb_group.AddItem(arc)
# create the center hole
arc = pcbnew.PCB_SHAPE(board)
arc.SetShape(pcbnew.SHAPE_T_CIRCLE)
arc.SetFilled(False)
arc.SetStart(pcbnew.wxPointMM(0, 0))
arc.SetEnd(pcbnew.wxPointMM(stator_hole_radius, 0))
arc.SetCenter(pcbnew.wxPointMM(0, 0))
arc.SetLayer(pcbnew.Edge_Cuts)
arc.SetWidth(int(0.1 * pcbnew.IU_PER_MM))
board.Add(arc)
# pcb_group.AddItem(arc)
2022-10-08 09:27:38 +00:00
CoilPlugin().register() # Instantiate and register to Pcbnew])