How to use Python script / console to iterate through selected footprint elements on Pcbnew? Is it possible?
I just started with:
import pcbnew
board_handle = pcbnew.LoadBoard('foo.kicad_pcb')
board = board_handle.GetBoard()
board.GetFoorprint
board.GetFoorprints
Foorprint?
Did you copy & paste that Python snippet?
[Edit] Foo R print.
(I don’t know much about scripting in KiCad, so can’t help there.)
My intention is copy a part of a layout, rename some REF and update the link with Eeschema (F8).
Something less automatic than https://github.com/MitjaNemec/Kicad_action_plugins/issues/102 , that I could not complete fix it.
bobc
January 25, 2021, 5:30pm
4
You don’t say what version of KiCad you are using.
def SetText(Filename = None):
if Filename:
my_board = pcbnew.LoadBoard (Filename)
else:
my_board = pcbnew.GetBoard()
for module in my_board.GetModules():
print ("module ref %s %s" % ( module.GetReference(), my_board.GetLayerName(module.Reference().GetLayer())))
# set layer
module.Value().SetLayer (pcbnew.F_Fab)
This used to work, but the Great Renaming Event of 2020 may have scuppered it.
qu1ck
January 25, 2021, 9:09pm
5
board = pcbnew.GetBoard()
for fp in board.Footprints():
if fp.IsSelected():
# do the deed
pass
That is for nightly. For 5.1 do board.GetModules()
1 Like
I got. Yes, @qu1ck I just wrote the same code here. Thanks!
For future references, my final algorithm (I just realized that have to for change the tracks net also, before re-sync):
import pcbnew
import re
#board = pcbnew.LoadBoard(file_name)
board = pcbnew.GetBoard()
# Number used to offset the reference number to macth with news hierarchical block.
offset_ref = -800
for f in board.GetFootprints():
if f.IsSelected():
old_ref = f.GetReference()
ref_parts = re.findall(r'([A-Z]+)([0-9]+)', old_ref)[0]
new_ref = ref_parts[0] + str( int(ref_parts[1]) + offset_ref )
f.SetReference(new_ref)
print('Changed ref: "%s" -> "%s"' % (old_ref, new_ref))
for t in board.GetTracks():
if t.IsSelected():
old_net = t.GetNetname()
net_parts = re.findall(r'Net\-\(([A-Z]+)([0-9]+)\-Pad[0-9]+\)', old_net)
if net_parts:#Net-(R1606-Pad1)
print(ref_parts)
net_parts = net_parts[0]
new_net = net_parts[0] + str( int(net_parts[1]) + offset_ref )
new_net = old_net.replace(''.join(net_parts), new_net)
t.SetNet(new_net)
print('Changed net: "%s" -> "%s"' % (old_net, new_net))
Just missing fix the t.SetNet(new_net)
method use. Some ideas?
qu1ck
January 25, 2021, 9:59pm
8
No idea what new_ref
is supposed to be but if you meant new_net
then it will not work because SetNet() takes a NETINFO_ITEM object, not a string
Right @qu1ck about the variable (just a mistake here). I update the code above.
I am right now trying to understand how to get the NETINFO_ITEM
from my intended net name.
I could not fix the above mentioned (link) Pcbnew python plugin (my first intention). There was some wx
issues that I could make.
qu1ck
January 25, 2021, 11:55pm
10
The way you generate net name is not the right approach. Net names are not deterministic in kicad, they may change when you add/remove components to the net.
You can get list of nets from pcb by doing board.GetNetsByName()
which returns a map that you can index like a dictionary or iterate over using items()
method.
Now to find which net you need depends on what you are trying to achieve maybe looking at pads and their GetNet()
and GetNetname()
will help.
Thanks @qu1ck . At the end, the “net rename” wasn’t need. Only the references change and re-sync by reference was enough.
My mistake was just some difference in the Nightly behavior that allow me different unit sequence on OA at hierarchical pages.
system
Closed
April 26, 2021, 1:34pm
12
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.