Created
July 21, 2022 22:57
-
-
Save JarrettR/d4bbafea86a54daa1c742ceb007567e7 to your computer and use it in GitHub Desktop.
KiCad extension for swapping components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Swaps the location of two selected components | |
""" | |
import pcbnew | |
class ComponentSwap(pcbnew.ActionPlugin): | |
""" | |
Select two (or more!) components and press the button | |
""" | |
def defaults(self): | |
self.name = "Swap the location of components" | |
self.category = "Modify PCB" | |
self.description = "Swap the coordinates of selected components" | |
self.show_toolbar_button = True | |
def Run(self): | |
pcb = pcbnew.GetBoard() | |
# | |
selectedMods = [] | |
for mod in pcb.GetModules(): | |
if mod.IsSelected(): | |
selectedMods.append(mod) | |
length = len(selectedMods) | |
newPos = [] | |
for i in range(length): | |
pos = selectedMods[i].GetPosition() | |
orient = selectedMods[i].GetOrientation() | |
layer = selectedMods[i].GetLayer() | |
flip = selectedMods[i].IsFlipped() | |
param = { 'pos': pos, 'orient': orient, 'layer': layer, 'flip': flip } | |
newPos.append(param) | |
newPos = newPos[1:] + newPos[:1] | |
for i in range(length): | |
selectedMods[i].SetPosition(newPos[i]['pos']) | |
selectedMods[i].SetOrientation(newPos[i]['orient']) | |
selectedMods[i].SetLayer(newPos[i]['layer']) | |
# if selectedMods[i].IsFlipped() && !newPos[i]['flip'] | |
pcbnew.Refresh() | |
ComponentSwap().register() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment