Skip to content

Instantly share code, notes, and snippets.

@idt12312
Last active August 8, 2021 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idt12312/ab038dd71863a20302c0b5a5366fbf7b to your computer and use it in GitHub Desktop.
Save idt12312/ab038dd71863a20302c0b5a5366fbf7b to your computer and use it in GitHub Desktop.
This script is for KiCad. This renames leading reference number of selected components.
import pcbnew
import re
def _get_selected_modules():
modules = pcbnew.GetBoard().GetModules()
return filter(lambda m: m.IsSelected(), modules)
def _replace_leading_number(target, new):
match = re.search(r'[0-9]', target)
idx = match.start()
return target[:idx] + '{}'.format(new) + target[idx+1:]
def rename_leading_number_selected(new_number):
selected = _get_selected_modules()
for module in selected:
refdes = module.GetReference()
new_refdes = _replace_leading_number(refdes, new_number)
print('rename: ' + refdes + ' -> ' + new_refdes)
module.SetReference(new_refdes)
@idt12312
Copy link
Author

idt12312 commented Aug 8, 2021

Example

  1. Select components to be rename.

kicad_copylayout_aw9

  1. In the interactive Python console on KiCad, execute the following command.
>>> from rename_leading_number_selected import rename_leading_number_selected
>>> rename_leading_number_selected(4)

In the above command, the leading reference number of selected components will be changed to 4.

  1. Reference number of the components is changed.

kicad_copylayout_aw10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment