Plugin to swap the location of two components

Quick script to just swap the locations of two components in Pcbnew.

Definitely something that would be cool to see built into the main application, because I find myself need this all the time. Anyway, be the change you want to see in the world, this is what I know how to do to fix the issue.

Select two components, press the toolbar icon, and the position/rotation of the objects will be swapped.

Two outstanding issues:
It won’t do layers/flips yet
The objects get deselected after the operation. This seems to be a weird KiCad behaviour, I guess the code would have to select them again afterwards

PRs welcome, I’m unlikely to come back to this until I need to use it again

1 Like

My mind has been churning trying to understand the application here. Why is this an issue?
Practical examples would help.

1 Like

Earlier today an issue was fixed that caused premature de-selection of objects in the PCB Editor. Maybe it’s related?

1 Like

Sure, I’m definitely laser focused on my use-cases.

Say you have an array of resistors on your schematic. Dump them into your PCB and you have a mess:

With about ten clicks, using alignment and distribution tools, you can clean that up:
image

So far so good, but now you’re stuck.

There’s no good way to untangle that ratsnest.

Now, you can select two crossed components:

And hit the button, and now they’re uncrossed:

That’s the most recent one, but honestly not the most common one.
Use-case number two:
This is much trickier, so I’m not going to easily be able to set up a demo. Lay out out a dense microcontroller fanout, you get 80% of the way done, and then you see a way to swap two of your output pins that still work within what those peripherals can do. You change the pins on the schematic, forward annotate to update your netlist on the PCB, but the resistors coming out of them are set in place already. Now they need to be swapped in location.

As I said, this isn’t a groundbreaking plugin, it’s just to implement a small quick feature that totally should be in the main package.

1 Like

The trouble with such simple plugins is that it’s easy to get lost in them and forget which one does what. I would not even consider a plugin for the first example, but instead just roughly sort them in the right order and then use align / distribute to make a row of them (More likely: just put them on a suitable grid). Sure, it’s more mouse clicks, but it really is such a small part of a PCB design that it’s hardly worth automating. Unless, maybe when you daily have such projects.

For your second example, I would just swap the resistors themselves:

  1. Move one resistor to outside of Edge.cuts.
  2. Grab the other by a pad, and snap it to the end of the track where the other resistor was.
  3. Do the same with the first resistor to put it on the location of the second.

Happy for you that this is not a common part of your day, it can be tedious

Are you aware of the Place Footprints plugin?

If you know in advance which order you want your footprints, then it may help if you start by annotating the footprints in that order.

I like such little helpers as user-addon (even if I don’t use all such addon-functions).
On the other hand I hope that not to much of these functions slip into the main-application. Typically they are useful only for for a minority of users (but for these users the function is really useful, such as in your case) or only for seldom used tasks but will clutter the user-interface for all. So experienced users have often a set of helper-scripts they use and like, but this is not necessary the same set as the next users wants/needs.

I’m hoping for gitlab-issue Hotkeys or shortcuts for python action scripts (#3837) · Issues · KiCad / KiCad Source Code / kicad · GitLab. This will allow to connect such scripts directly with the normal workflow.

2 Likes