diff --git a/.scripts/pre_panel.py b/.scripts/pre_panel.py index 578eb90..d761e18 100644 --- a/.scripts/pre_panel.py +++ b/.scripts/pre_panel.py @@ -2,6 +2,9 @@ import json import sys from kikit.common import findBoardBoundingBox +from kikit.panelize_ui_impl import buildText +from kikit.panelize_ui_sections import ppText +from kikit.defs import * from pcbnewTransition.pcbnew import LoadBoard PCB_DIV_MM = 1000000 @@ -26,6 +29,7 @@ def float_to_deg(input: float, n: int=0) -> str: class Text: def __init__(self, text: str, anchor: str, offset: tuple[float, float], rot: float, size: tuple[float, float, float] = (1.0, 1.0, 0.16), just: tuple[str, str] = ('center', 'center')): + self.layer=Layer.F_SilkS self.text = text self.anchor = anchor self.hoff = float_to_str_mm(offset[0]) @@ -37,6 +41,13 @@ class Text: self.hjust = just[0] self.vjust = just[1] + def flip(self): + self.layer=Layer.B_SilkS + rot = float(self.rot.replace("deg", "")) + rot += 180.0 + self.rot = str(rot)+"deg" + return self + def to_dict(self, n: int) -> dict: if n == 0: str_n = '' @@ -56,6 +67,7 @@ class Text: "hjustify": self.hjust, "vjustify": self.vjust, "thickness": self.thickness, + "layer": int(self.layer), } } @@ -88,10 +100,7 @@ class Panel: self.grid = grid self.gaps = (grid[0] - 1, grid[1] - 1) self.frame = frame - self.text = None - self.text2 = None - self.text3 = None - self.text4 = None + self.text = {} self.dims = [self.gaps[i]*self.spacing + self.grid[i]*board.dims[i] + frame.extra_d[i] for i in range(0,2)] self.spacing = [self.spacing + board.dims[i] for i in range(0,2)] @@ -109,7 +118,7 @@ class Panel: txt_just = (('center', 'center'), ('left', 'center')) 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.update(Text("{boardTitle}-{boardRevision}", txt_loc[0], txt_off[0], txt_rot, just=txt_just[0]).to_dict(len(self.text))) spacer = " " txt =\ @@ -118,35 +127,58 @@ class Panel: "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" +\ "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.text.update(Text(txt, txt_loc[1], txt_off[1], txt_rot, just=txt_just[1]).to_dict(1)) 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.text4 = Text("ORIGX ->", "bl", (arrow_off[0], -arrow_off[1]), 90, just=("right", "center")).to_dict(3) + txt_orig = [ +Text("ORIGX ->", "bl", (arrow_off[0], -arrow_off[1]), 90, just=("right", "center")), +Text("ORIGY ->", "bl", (arrow_off[1], -arrow_off[0]), 0, just=("right", "center")) + ] + for txt in txt_orig: + self.text.update(txt.to_dict(len(self.text))) + self.text.update(txt.flip().to_dict(len(self.text))) -print("Starting pre_panel script") +def kikitPostprocess(panel, arg): + print("arg:", arg) + text_dict = eval(arg) + for t in text_dict.values(): + ppText(t) + print(t["thickness"]) + t["plugin"] = None + buildText(t,panel) + # panel.addText(t.text, position=, orientation=t.rot, + # width=t.width, height=t.height, thickness=t.thickness, + # hJustify=t.hjustify, + # vJustify=t.vjustify, + # layer=t.layer): -board = LoadBoard(pcb_file) -sourceArea = findBoardBoundingBox(board) -fids = [] -for foot in board.Footprints(): - if "FID" in foot.GetReference(): - cent = foot.GetBoundingBox().GetCenter() - fids.append(tuple([cent[i]/PCB_DIV_MM for i in range(0,2)])) + print("Finished post panel script") -json_file = open(panel_file) -json_str = json_file.read() -panel_json = json.loads(json_str) +if __name__ == '__main__': + print("Starting pre_panel script") -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)) -panel = Panel(panel_json["layout"]["space"], (int(panel_json["layout"]["rows"]), int(panel_json["layout"]["rows"])) ,frame, board) + board = LoadBoard(pcb_file) + sourceArea = findBoardBoundingBox(board) + fids = [] + for foot in board.Footprints(): + if "FID" in foot.GetReference(): + cent = foot.GetBoundingBox().GetCenter() + fids.append(tuple([cent[i]/PCB_DIV_MM for i in range(0,2)])) -for t in [panel.text, panel.text2, panel.text3, panel.text4]: - if t is not None: - panel_json.update(t) + json_file = open(panel_file) + json_str = json_file.read() + panel_json = json.loads(json_str) -json_file = open(panel_file, mode="w") -json.dump(panel_json, json_file, indent=4) + 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)) + panel = Panel(panel_json["layout"]["space"], (int(panel_json["layout"]["rows"]), int(panel_json["layout"]["rows"])) ,frame, board) -print("Finished pre_panel script") + import pathlib + + panel_json["post"]["script"] = str(pathlib.Path(__file__).resolve()) + panel_json["post"]["scriptarg"] = str(panel.text) + + json_file = open(panel_file, mode="w") + json.dump(panel_json, json_file, indent=4) + + print("Finished pre_panel script")