Python scripting add text to board

I was wondering if someone could post an example of a simple script to add some text to the board, for example to the user drawing layer.

I have been digging through the API and haven’t been able to work out how to do this. With four classes that seem to be related to this, (TEXTE_PCB, EDA_TEXT, TEXTE_MODULE and TEXT_EFFECTS) and no real description as to their purposes it is quite confusing.

I think I need to make a TEXTE_PCB object containing an EDA_TEXT object, but so far I see TEXTE_PCB required a BOARD_ITEM and also I can’t board.Add() a TEXTE_PCB instance.

Thanks.

Have a look at this (courtesy of @bobc)

Hi John, thanks very much for your quick reply.

That is helpful, and I have looked through the code but I am not out of the woods yet. It seems that code deals with adding text to an existing module. I think what I am looking for is to add text independently as a drawing item.

For example, I can add some new text in the GUI and then find it by iterating through board.GetDrawings() and finding items of type TEXTE_PCB. That works fine. Seems like it should be quite simple but I can’t find how to make my own TEXTE_PCB and have it show up.

What would probably help a lot is simply knowing the purpose and logic behind the naming of each of these text related classes that I mentioned earlier. This kind of thing is really not obvious from the names and API documentation.

EDA_TEXT is a common super class for all text type things to be drawn on canvas (not just pcb but schematic and gerber)
TEXTE_PCB is a standalone text that can be placed on pcb
TEXTE_MODULE is text that is part of a module (footprint) i.e. reference, value and others.

What you want is TEXTE_PCB, this is how to use it:

import pcbnew

board = pcbnew.BOARD()
txt = pcbnew.TEXTE_PCB(board)
txt.SetText("Hello")
txt.SetPosition(pcbnew.wxPoint(40000000, 20000000))
txt.SetHorizJustify(pcbnew.GR_TEXT_HJUSTIFY_CENTER)
txt.SetTextSize(pcbnew.wxSize(10000000, 10000000))
txt.SetThickness(1000000)
board.Add(txt)
board.Save('board.kicad_pcb')

You get this:

How did I get this info? I read the code. Cpp source code. Also I use a good IDE (intellij idea) that helps with fuzzy matching autocomplete so when I type pcbnew.HJUSTIFY it will show me that pcbnew.GR_TEXT_HJUSTIFY_CENTER is probably what I meant. 60% of the time it works every time ™, if not than back to reading code.

Hope this helps.

1 Like

Thank you very much. Exactly what I was looking for and works perfectly.

Kicad scripting is super useful but I guess we are still at the stage of having to dig into the C++ to figure things out as you say.

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