Skip to content

Instantly share code, notes, and snippets.

@Bolukan
Created September 11, 2024 22:15
Show Gist options
  • Select an option

  • Save Bolukan/aad16c8ca38d71efb57aabe1f6a6bb16 to your computer and use it in GitHub Desktop.

Select an option

Save Bolukan/aad16c8ca38d71efb57aabe1f6a6bb16 to your computer and use it in GitHub Desktop.
Christmas LED star
import pcbnew
import math
# Get the board object
board = pcbnew.GetBoard()
# Convert coordinates to KiCad internal units (nanometers)
def mm_to_nm(val):
return int(val * 1e6)
def cutline(start, end):
start_point = pcbnew.VECTOR2I(mm_to_nm(start[0]), mm_to_nm(start[1]))
end_point = pcbnew.VECTOR2I(mm_to_nm(end[0]), mm_to_nm(end[1]))
edge_cut_line = pcbnew.PCB_SHAPE(board)
edge_cut_line.SetShape(pcbnew.SHAPE_T_SEGMENT)
edge_cut_line.SetLayer(pcbnew.Edge_Cuts)
edge_cut_line.SetStart(start_point)
edge_cut_line.SetEnd(end_point)
board.Add(edge_cut_line)
def cutarc(start, end, center):
arc_start = pcbnew.VECTOR2I(mm_to_nm(start[0]), mm_to_nm(start[1]))
arc_end = pcbnew.VECTOR2I(mm_to_nm(end[0]), mm_to_nm(end[1]))
arc_center = pcbnew.VECTOR2I(mm_to_nm(center[0]), mm_to_nm(center[1]))
arc = pcbnew.PCB_SHAPE(board)
arc.SetShape(pcbnew.SHAPE_T_ARC)
arc.SetLayer(pcbnew.Edge_Cuts)
arc.SetStart(arc_end)
arc.SetEnd(arc_start)
arc.SetCenter(arc_center)
board.Add(arc)
def nextline(point, len):
mysin = math.sin(math.radians(point[2]))
mycos = math.cos(math.radians(point[2]))
return (point[0] + mycos * len, point[1] + mysin * len, (point[2] + 90) % 360)
def edge_cuts(XY0, width, length):
XY1 = nextline(XY0, length)
XY2 = nextline(XY1, width)
XY3 = nextline(XY2, length)
XY3b = nextline(XY2, length-2)
XY4 = (XY3[0], XY3[1], XY0[2]+30)
XY4b = nextline(XY4, 2)
XY5 = nextline(XY4, length)
XY6 = nextline(XY5, width)
XY7 = nextline(XY6, length)
XY6a = (XY6[0], XY6[1], XY4[2]-12) # XY6
XY9 = nextline(XY6a, width)
XY34 = (XY3[0], XY3[1], XY0[2]+15)
XY34b = nextline(XY34, 2.1)
cutline(XY0, XY1)
cutline(XY1, XY2)
cutline(XY2, XY3b)
cutarc(XY3b, XY4b, XY34b)
cutline(XY4b, XY5)
cutarc(XY9, XY5, XY6)
cutline(XY9, XY6)
cutline(XY6, XY7)
cutarc(XY0, XY7, XY3)
width = 11
length = 90
XY0 = (20, 130, 270)
edge_cuts(XY0, width, length)
XY0b = (120, 50, 90)
edge_cuts(XY0b, width, length)
# Refresh the board view in KiCad
pcbnew.Refresh()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment