Cleaned up script, integrated into CI

This commit is contained in:
ac
2025-06-03 14:50:54 +10:00
parent 538c109b25
commit f73d59d2e3
2 changed files with 8 additions and 44 deletions

View File

@@ -1,16 +1,13 @@
import json import json
import sys import sys
import shutil
from kikit.panelize import Origin,getOriginCoord
from kikit.common import findBoardBoundingBox from kikit.common import findBoardBoundingBox
from pcbnewTransition.pcbnew import LoadBoard from pcbnewTransition.pcbnew import LoadBoard
PCB_DIV_MM = 1000000 PCB_DIV_MM = 1000000
proj_file = sys.argv[1] pcb_file = sys.argv[1]
pcb_file = sys.argv[2] panel_file = sys.argv[2]
panel_file = sys.argv[3]
def str_mm_to_float(input: str) -> float: def str_mm_to_float(input: str) -> float:
input = input.lower() input = input.lower()
@@ -45,7 +42,6 @@ class Text:
str_n = '' str_n = ''
else: else:
str_n = str(n + 1) str_n = str(n + 1)
return { return {
"text" + str_n: "text" + str_n:
{ {
@@ -87,7 +83,7 @@ class Board:
class Panel: class Panel:
def __init__(self, spacing: str, grid: (int, int), frame: Frame, board: Board): def __init__(self, spacing: str, grid: tuple[int, int], frame: Frame, board: Board):
self.spacing = str_mm_to_float(spacing) self.spacing = str_mm_to_float(spacing)
self.grid = grid self.grid = grid
self.gaps = (grid[0] - 1, grid[1] - 1) self.gaps = (grid[0] - 1, grid[1] - 1)
@@ -96,7 +92,6 @@ class Panel:
self.text2 = None self.text2 = None
self.text3 = None self.text3 = None
self.text4 = None self.text4 = None
self.dims = [self.gaps[i]*self.spacing + self.grid[i]*board.dims[i] + frame.extra_d[i] for i in range(0,2)] self.dims = [self.gaps[i]*self.spacing + self.grid[i]*board.dims[i] + frame.extra_d[i] for i in range(0,2)]
arrow_off = [frame.spacing + frame.width, frame.width] arrow_off = [frame.spacing + frame.width, frame.width]
@@ -109,21 +104,20 @@ class Panel:
txt_rot = 0 txt_rot = 0
else: else:
txt_loc = ('ml', 'br') txt_loc = ('ml', 'br')
txt_off = ((text_off[1], 0), (text_off[1], text_off[0])) txt_off = ((text_off[1], 0), (-text_off[1], -text_off[0]))
txt_just = (('center', 'center'), ('center', 'center')) txt_just = (('center', 'center'), ('left', 'center'))
txt_rot = 90 txt_rot = 90
self.text = Text("{boardTitle}-{boardRevision}", txt_loc[0], txt_off[0], txt_rot, just=txt_just[0]).to_dict(0) self.text = Text("{boardTitle}-{boardRevision}", txt_loc[0], txt_off[0], txt_rot, just=txt_just[0]).to_dict(0)
spacer = " " spacer = " "
txt =\ txt =\
"PNL (x,y) : (" + "x: " +float_to_str_mm(self.dims[0],4) + ", " + float_to_str_mm(self.dims[1],4) + ")\n" +\ "PNL (x,y) : (" + float_to_str_mm(self.dims[0],4) + ", " + float_to_str_mm(self.dims[1],4) + ")\n" +\
"BRD (x,y),r: (" + float_to_str_mm(board.dims[0],4) + ", " + float_to_str_mm(board.dims[1],4) + "), " +\ "BRD (x,y),r: (" + float_to_str_mm(board.dims[0],4) + ", " + float_to_str_mm(board.dims[1],4) + "), " +\
float_to_deg(board.rot, 3) + "\n" +\ float_to_deg(board.rot, 3) + "\n" +\
"FIDS (x,y) : " + board.fids "FIDS (x,y) : " + board.fids
self.text2 = Text(txt, txt_loc[1], txt_off[1], txt_rot, just=txt_just[1]).to_dict(1) self.text2 = Text(txt, txt_loc[1], txt_off[1], txt_rot, just=txt_just[1]).to_dict(1)
if "frame" in self.frame.ftype: if "frame" in self.frame.ftype:
self.text3 = Text("ORIGY ->", "bl", (arrow_off[1], -arrow_off[0]), 0, just=("right", "center")).to_dict(2) self.text3 = Text("ORIGY ->", "bl", (arrow_off[1], -arrow_off[0]), 0, just=("right", "center")).to_dict(2)
self.text4 = Text("ORIGX ->", "bl", (arrow_off[0], -arrow_off[1]), 90, just=("right", "center")).to_dict(3) self.text4 = Text("ORIGX ->", "bl", (arrow_off[0], -arrow_off[1]), 90, just=("right", "center")).to_dict(3)
@@ -132,55 +126,24 @@ print("Starting pre_panel script")
board = LoadBoard(pcb_file) board = LoadBoard(pcb_file)
sourceArea = findBoardBoundingBox(board) sourceArea = findBoardBoundingBox(board)
# bottomOrig = [(a - b)/10**6 for a,b in zip(*[list(getOriginCoord(o, sourceArea)) for o in [Origin.BottomRight, Origin.BottomLeft]])]
# print("bottom Orig: ", bottomOrig)
fids = [] fids = []
for foot in board.Footprints(): for foot in board.Footprints():
if "FID" in foot.GetReference(): if "FID" in foot.GetReference():
cent = foot.GetBoundingBox().GetCenter() cent = foot.GetBoundingBox().GetCenter()
fids.append(tuple([cent[i]/PCB_DIV_MM for i in range(0,2)])) fids.append(tuple([cent[i]/PCB_DIV_MM for i in range(0,2)]))
print("Fids:", fids)
dest_folder = '/'.join(proj_file.split('/')[:-1])
dest_name = proj_file.split('/')[-1].removesuffix(".kicad_pro")
src_name = pcb_file.removesuffix(".kicad_pcb")
src_proj = src_name + ".kicad_pro"
try:
shutil.copy(src_name + ".kicad_dru", dest_folder + "/" + dest_name + ".kicad_dru")
except Exception:
pass
json_file = open(panel_file) json_file = open(panel_file)
json_str = json_file.read() json_str = json_file.read()
panel_json = json.loads(json_str) panel_json = json.loads(json_str)
print(panel_json)
frame = Frame(panel_json["framing"]["type"], panel_json["framing"]["width"], panel_json["framing"]["space"]) frame = Frame(panel_json["framing"]["type"], panel_json["framing"]["width"], panel_json["framing"]["space"])
board = Board(fids, panel_json["layout"].get("rotation", "0deg"), (sourceArea.GetWidth()/PCB_DIV_MM, sourceArea.GetHeight()/PCB_DIV_MM)) board = Board(fids, panel_json["layout"].get("rotation", "0deg"), (sourceArea.GetWidth()/PCB_DIV_MM, sourceArea.GetHeight()/PCB_DIV_MM))
panel = Panel(panel_json["layout"]["space"], (int(panel_json["layout"]["rows"]), int(panel_json["layout"]["rows"])) ,frame, board) panel = Panel(panel_json["layout"]["space"], (int(panel_json["layout"]["rows"]), int(panel_json["layout"]["rows"])) ,frame, board)
for t in [panel.text, panel.text2, panel.text3, panel.text4]: for t in [panel.text, panel.text2, panel.text3, panel.text4]:
if t is not None: if t is not None:
panel_json.update(t) panel_json.update(t)
# panel_json.update(panel.text3)
# json_file = open(src_proj)
# json_str = json_file.read()
# src = json.loads(json_str)
#
# json_file = open(proj_file)
# json_str = json_file.read()
# dest = json.loads(json_str)
#
json_file = open(panel_file, mode="w") json_file = open(panel_file, mode="w")
json.dump(panel_json, json_file, indent=4) json.dump(panel_json, json_file, indent=4)

View File

@@ -105,6 +105,7 @@ image:
echo "mkdring" echo "mkdring"
mkdir -p panels/$NAME mkdir -p panels/$NAME
echo "panelising" echo "panelising"
python3 .gitlab/.scripts/pre_panel.py $PCB $JSON
kikit panelize -p $JSON $PCB panels/$NAME/$NAME.kicad_pcb kikit panelize -p $JSON $PCB panels/$NAME/$NAME.kicad_pcb
cp .gitlab/micromelon_default/micromelon_default.kicad_sch panels/$NAME/$NAME.kicad_sch cp .gitlab/micromelon_default/micromelon_default.kicad_sch panels/$NAME/$NAME.kicad_sch
cp $d/fp-lib-table panels/$NAME/ cp $d/fp-lib-table panels/$NAME/