API - how to find item selected in the PCBNew layout window

I would like to create a plugin that can operate on the currently selected item in the PCBNew window. For this purpose, I’d like to understand how a plugin would get the selected item(s) from the API.

For example, I would like to implement something that can quickly set the justification of a footprint text. Like one might with a button or hotkey in Excel or Word.

So the user would select the item, say the footprint’s Value field on the Fab layer, and run this plugin to apply FP_TEXT.SetHorizJustify() accordingly. (One would need three plugins, for left, right and center.)

I see that EDA_ITEMs (like FP_TEXT) have a IsSelected field. But I am hoping that iterating every single text of every footprint is not the way to discover which item is selected.

Is there some more direct way for the plugin to find the selected text?

I realize that implementing features like I described also runs into the problem of how the user activates the plugin conveniently, given that currently there’s no way to tie a hotkey to a plugin. However, Streamdeck. :slight_smile: And I guess if desperate I could have Streamdeck open the item’s edit dialog, and tab its way to the Justification combo box etc.

But in general I’d like to know the plugin way to operate on the selected footprint(s), texts and so on. And maybe at some point Kicad will offer hotkeys for plugins to complete the picture.

Thanks

I have no idea unfortunately, but maybe there’s another plugin doing something similar that you can look at?

Here’s a good collection of plugins:

I looked through the api, didn’t find anything to get current selected item list. So probably iterating over items and checking their status is the way.

The replicate layout plugin also works like this. Maybe you could use it as a reference.

Refer to:

My footprint swapper works exactly like this, and is extremely small and simple to understand:

Additionally, you can add plugin icons to the toolbar, so you could have left/right/centre justification buttons right there on the interface.

@albin A couple of those tools are indeed plugins that operate on the selection. And they find the selection by iterating all the relevant items on the board.

@qu1ck I too looked through the API and didn’t find anything obvious. The example plugins I saw indeed iterated over items, but generally were looking for footprints, whereas in my case my plugin would be visiting all the footprints and inspecting the subsidiary Reference(), Value() and GraphicalItems(), so possibly 10 or more times as many items. But perhaps that goes fast enough to be tractable.

@RacingJoe Thanks for that example.

@Jarrett Thanks for the example. Indeed yes I could enable a toolbar button for each plugin. Sadly there doesn’t appear to be a way to connect a hotkey to that button.

On a tangential note:

I discovered that PCBNew allows you to multi-select individual texts of several footprints, using Shift-Click. So for example just the F.Fab Value field of R1 and R2 and R3.

This does not allow you to open the edit dialog and adjust the justification, height etc of all at once. However I guess it’s useful for positioning, and Align and Distribute functions (not to be confused with justification). And will also work for a justification plugin.

Anyhow, thanks everyone!

2 Likes

Woo-hoo! Thanks @qu1ck!

This is just slightly OT but you should check two things:

  1. Does the Edit/Edit text & graphic properties tool, which is available in V6, solve your issue?
  2. Given that we are close to V7 release, you should check whether any of new functionalities about to be introduced solves your issue (2022 End-of-year Recap! - YouTube)

I used to have a plugin for V5 that calculated and showed track lengths for specific nets (in order to do length tuning for a bus) and then V6 introduced Net inspector making my plugin redundant. Kicad is developing quite rapidly with and discoverability of new features takes time especially since most web references (videos, …) lag behind.

Or maybe you could/should open a new issue on GitLab requesting an extension of `Edit text & Graphic properties to allow change of justification.

Text justification is available on footprint level but not board level.

Thanks for pointing out the Edit Text and Graphic Properties dialog.

I’m all in favor of being able to bulk-edit properties, including justification. However in this instance, I need to be able to set justification for individual footprint texts, or possibly a few that are nearby to each other.

I’m experimenting with adding additional information fields (on different layers) to footprints, and to avoid positioning them all by hand, I am using the position, direction, layer (F.Fab, B.Fab), height and justification of the Fab layer item FOOTPRINT.Value() as a guide. So once the Fab Values are positioned by hand, a script can position, size and orient my additional fields accordingly.

Justification is significant here because at many locations on the board, one end of the Value string abuts a component, while there’s open space on the other end. My added strings may be a bit longer than the Value string and can extend into that open space.

Take a look at the V7 presentation video. Specifically the properties panel. I think it should solve your issue

There is also this, as I mentioned above:

ksnip_20221229-230224

I’m also curious. Why the necessity for justification?
My normal practice is to drop the grid down to 1 mil. when the board is mostly finished, then push the text for the footprints to nearby, suitable, vacant real estate. Everything done with left finger mouse and zooming.

Edit: justification at this level is also available in 6.x.x.

@jmk FWIW, I described my current use case a few messages ago.

And yes, there are several ways to adjust footprint text justification etc, and more in K7. But all require opening a dialog, or at least moving the mouse, (eg: K7 to the properties panel) finding the desired item, clicking something or manipulating a combo box. The improving GUI is great for discoverability, but slow for a lot of changes.

By contrast, I’m hoping to get to the point of just selecting the item, and then a single button press on the keyboard or Stream Deck to make the change, here being left/center/right justify.

Related faster-UI improvements, I think still awaited, are:

  • Hot keys for plugins
  • Parameters for plugins
  • Hot keys for buttons added by plugins

3rd bullet point is subset of first. Not sure what 2nd is? What kind of parameters? For user input plugins are expected to have their own configuration dialogs.

What kind of parameters?

I withdraw that suggestion. I now see that “complex plugins” probably cover what I had in mind.

3rd bullet point is subset of first.

I guess so, depending on whether the hotkey mechanism ties to UI elements or to the underlying functions they perform.

IMO it would still be useful to have chained shortcuts/hotkeys, for example if A runs a plugin, it could be followed by B which would do action X while A followed by C would do action Y. This way it would be possible to write and install one plugin with several small functions, instead of writing and installing, say, 10 or 20 plugins for small tasks.

This is somewhat related to Stringed (chained) keyboard shortcuts (lp:#1616154) (#2031) · Issues · KiCad / KiCad Source Code / kicad · GitLab, at least conceptually.

Simple Plugin is adequate for what you want to do. Complex plugin is primarily for plugins needing resources/files that are called by the plugin but, it’s mostly to keep things unconfused as all that can be done by Simple plugin.

Regarding ‘finding the selected item’ well, pretty simple, just point to where to look, so to speak and what to look for.

You can have Pop-Up GUI panels for ‘Input’ and for displaying output…

Below shows my Simple Plugin that has Pop-up panel with Buttons that call Def’s in the plugin and call external App’s (this one shows a button for calling an SQL Database for Proj Mgmt…

What does " point to where to look, so to speak and what to look for" translate to in terms of code?