2022-09-28 09:20:08 +00:00
|
|
|
import pcbnew
|
2022-10-04 17:43:35 +00:00
|
|
|
import json
|
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-06 09:18:11 +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):
|
|
|
|
board = pcbnew.GetBoard()
|
2022-10-04 17:43:35 +00:00
|
|
|
# load up the JSON with the coil parameters
|
2022-10-08 09:27:38 +00:00
|
|
|
coil_data = json.load(
|
|
|
|
open(
|
|
|
|
"/Users/chrisgreening/Work/projects/pcb_motor/kicad-coil-plugins/coil.json"
|
|
|
|
)
|
|
|
|
)
|
2022-10-04 17:43:35 +00:00
|
|
|
# parameters
|
2022-10-08 09:27:38 +00:00
|
|
|
track_width = coil_data["parameters"]["trackWidth"]
|
|
|
|
stator_hole_radius = coil_data["parameters"]["statorHoleRadius"]
|
|
|
|
via_diameter = coil_data["parameters"]["viaDiameter"]
|
|
|
|
via_drill_diameter = coil_data["parameters"]["viaDrillDiameter"]
|
2022-10-04 17:43:35 +00:00
|
|
|
|
|
|
|
# put everything in a group to make it easier to manage
|
|
|
|
pcb_group = pcbnew.PCB_GROUP(board)
|
2022-10-06 09:18:11 +00:00
|
|
|
# board.Add(pcb_group)
|
2022-10-04 17:43:35 +00:00
|
|
|
|
2022-09-28 09:20:08 +00:00
|
|
|
# create the center hole
|
|
|
|
arc = pcbnew.PCB_SHAPE(board)
|
|
|
|
arc.SetShape(pcbnew.SHAPE_T_ARC)
|
2022-10-04 17:43:35 +00:00
|
|
|
arc.SetStart(pcbnew.wxPointMM(stator_hole_radius, 0))
|
2022-09-28 09:20:08 +00:00
|
|
|
arc.SetCenter(pcbnew.wxPointMM(0, 0))
|
|
|
|
arc.SetArcAngleAndEnd(0, False)
|
|
|
|
arc.SetLayer(pcbnew.Edge_Cuts)
|
|
|
|
arc.SetWidth(int(0.1 * pcbnew.IU_PER_MM))
|
|
|
|
board.Add(arc)
|
2022-10-06 09:18:11 +00:00
|
|
|
# pcb_group.AddItem(arc)
|
2022-10-04 17:43:35 +00:00
|
|
|
|
|
|
|
# create tracks
|
2022-10-08 09:27:38 +00:00
|
|
|
for track in coil_data["tracks"]["f"]:
|
2022-10-04 17:43:35 +00:00
|
|
|
# find the matching net for the track
|
2022-10-08 09:27:38 +00:00
|
|
|
net = board.FindNet("coils")
|
|
|
|
if net is None:
|
|
|
|
raise "Net not found: {}".format(track["net"])
|
|
|
|
create_tracks(board, pcb_group, net, pcbnew.F_Cu, track_width, track)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
# create the vias
|
2022-10-08 09:27:38 +00:00
|
|
|
for via in coil_data["vias"]:
|
2022-10-04 17:43:35 +00:00
|
|
|
pcb_via = pcbnew.PCB_VIA(board)
|
2022-10-08 09:27:38 +00:00
|
|
|
pcb_via.SetPosition(pcbnew.wxPointMM(float(via["x"]), float(via["y"])))
|
2022-10-04 17:43:35 +00:00
|
|
|
pcb_via.SetWidth(int(via_diameter * 1e6))
|
|
|
|
pcb_via.SetDrill(int(via_drill_diameter * 1e6))
|
2022-10-08 09:27:38 +00:00
|
|
|
pcb_via.SetNetCode(net.GetNetCode())
|
2022-10-04 17:43:35 +00:00
|
|
|
board.Add(pcb_via)
|
2022-10-06 09:18:11 +00:00
|
|
|
# pcb_group.AddItem(pcb_via)
|
2022-10-04 17:43:35 +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-06 09:18:11 +00:00
|
|
|
# pcb_group.AddItem(pcb_txt)
|
2022-10-04 17:43:35 +00:00
|
|
|
|
2022-10-08 09:27:38 +00:00
|
|
|
|
|
|
|
CoilPlugin().register() # Instantiate and register to Pcbnew])
|