Python: Restore Selection After Action in PCBNew?

Hello,

I am developing a small plugin action for pcbnew; it’s working well but I have a question about how to restore the previous selection after the plugin exits.

For context, the plugin action uses Layer Alignment Targets to make it easier to quickly align many items simultaneously. With one Layer Alignment Target set to an “X” shape, and all others set to a “+” shape, and with the same number of components selected as there are targets, the closest footprint to the “X” target serves as the reference for the others. Running the action sets the position offset from the other targets by proximity, and sets the rotation to match.

It’s easier to see it in a before/after screenshot than it is to describe in words…

All that is working well, but in the case where the user hasn’t selected enough components, I show a dialog and exit, but the selection is lost. Does anyone know how to restore the set of selected items when a Python action ends?

I’ve tried using “SetSelected()” on the footprints after the Dialog, but it seems “footprint.SetSelected()” does nothing. I’ve also tried following it with “pcbnew.UpdateUserInterface()” and “pcbnew.Refresh()”, also with no result.

Here is the complete plugin if anyone wants to give it a try / have a look…
com_zambetti_PositionFootprintsWithTargets.zip (402.4 KB)

If I remember correctly pcbnew itself does full refresh and deselects after plugin action ends and you can’t control it. Workaround here is to keep your window open and let the user amend selection while your plugin is still running.

Thank you for your reply. This action doesn’t create or make use of any windows, so it seems like a pretty heavy workaround to create a dummy window just to retain the selection.

I can understand why pcbnew refreshes automatically after an action ends, but automatically deselecting everything on action-end makes for frustrating UI if the user is expected to re-create a complicated selection before trying the action again.

You have a message dialog in the screenshot right there. A dialog window with one button is few more lines of code.

But I agree that it’s inconvenient, I’ll see if this is easy to fix.

1 Like

It was relatively easy to fix

2 Likes

Wow, you rock. Reading the conversation on GitLab, it looks like it’s in for 7.0. That release is so exciting. Many thanks!

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