Python bindings - how to find item selected in the PCBNew layout window

@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?

Chained shortcuts

I suspect if Kicad had a mechanism for hotkeys to run plugins, then the first keystroke could invoke a plugin that pops up a small UI, which, having the focus, would then capture the second keystroke. That effect might be good enough.

Though we don’t have hotkeys for plugins, I might give this scheme a try in conjunction with Stream Deck, to create the effect of the initial “hotkey to run plugin”, then followed by another key. The user could key this second key on the keyboard, or it could be a subsequent step in the Stream Deck macro.

Well a single plugin python file can register multiple plugins. So from the point of view of managing the code, that avoids a proliferation of files. However, it still creates a proliferation of items on the Tools > External Plugins menu.

I am on the lookout for how to create subsidiary menu folders on the Tools > External Plugins menu, but haven’t seen how to do that. I thought that “complex plugin” might do that, but haven’t yet tried it. “complex” might just be for organizing a multi-python-file plugin as a python module (ie: subsidiary directory with __init__.py file). I’ll get to this at some point soon, but comments welcome on The Way to create Tools > External Plugins > MyManyFunctions folder.

If you’re a programmer (from Noob to Expert) you’ll glean the answers from image below. It contains Three “where to look’s” and “actions to perform”. That’s what I mean in terms of code…

Your best approach IMO is to look at several of the Plugins that come with Kicad and look at some that are posted online - there are common threads running through them…

The actual Action they take isn’t too important, what is important is that they refer to something (‘point to it’ so to speak) and then they ‘do something’ to or with something…

You indicated you want to

That is not supported by kicad at the moment.

@BlackCoffee your code snippets are missing some items (value and reference texts of footprint are not included in GraphicalItems() result). I’ve added an API to get selected items to kicad, merge request link above (not merged yet).

I’m not clear on what you’re saying but, doesn’t matter. All of my Plugins work verrrry well. Never a problem. I did Not intend to post full code details…