Function to change font for all labels in the PCB

I would like to add the ability to change the font of all text elements in my PCB in my script but this code is not working:

def Run(self):
        board = pcbnew.GetBoard()

        # Define the font name to change to
        new_font_name = "Arial Bold"

        for footprint in board.GetFootprints():
            for graphicText in footprint.GraphicTexts():
                # Change the font name to Arial Bold
                graphicText.SetFont(new_font_name)

        pcbnew.UpdateUserInterface()

but I get:

for graphicText in footprint.GraphicTexts():

AttributeError: 'FOOTPRINT' object has no attribute 'GraphicTexts

I have no knowledge of python or the pcbnew library, how could I correct the function?

How are you writing a Python script, then? How did you write this initial code?

• You can set Font’s from the Edit_Text_and_Graphic_Properties panel (example below - I selected and changed only one item for this example)

• You can delve into Code stuff here

Screen Shot 2024-01-15 at 08.17.03

Thank you Coffee, I do this every time to change the font for everything, but I wanted to do it in a single step with a script, but I searched for it and I don’t think there are functions to do this.
I tried a more practical solution, this code generated by AI correctly modifies the .KiCad_pcb file and changes the font for all elements.

# Leggi il contenuto del file del PCB
        file_path = board.GetFileName()
        with open(file_path, "r") as file:
            file_content = file.read()

        # Definisci il pattern da cercare e il testo con cui sostituire
        pattern = r"\(effects \(font \(size 1 1\) \(thickness [0-9.]+\)\)\)"
        replacement_text = (
            '        (effects (font (face "Arial") (size 1 1) (thickness 0.2) bold))'
        )

        # Esegui la sostituzione utilizzando espressioni regolari
        new_content = re.sub(pattern, replacement_text, file_content)

        # Sovrascrivi il file del PCB con il nuovo contenuto
        with open(file_path, "w") as file:
            file.write(new_content)

        # Salva il file del PCB
        board.Save(file_path)
       
        pcbnew.Refresh()

        pcbnew.UpdateUserInterface()

Is pcbnew.Refresh() and pcbnew.UpdateUserInterface() not refreshing the changes, is there a way to reload the pcb from the modified file (even with an external editor)?

I don’t keep up with Kicad’s changes - I tend to do things my way and that often means by Brute-Force programming (meaning, I let the computer do the work even if it takes longer to run codes…). Thus, I don’t know if Refresh does or, does-not work but, it seems to work for my plugins. They all work and never crash.
Not long ago, some started crashing upon updating to v7.0.9 but, not with v7.0.10

Screenshot show example with Refresh inside “def’s” and at end of them.

The PCB window has a Recycle/Refresh button that may help…

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.