When I’m in OpenGL Canvas, I can just make a normal mouse left-click - drag - release to make a rectangular selection, and all elements underneath (pads, zones, modules, tracks, drawings) are selected, and this is indicated with a highlight of the elements.
Now what I want is to “invert selection” - that is, starting from the previously made rectangular selection, deselect all selected elements, and select those elements on the board that are not selected.
I have noticed that KiCAD pcbnew scripting: pcbnew.EDA_ITEM Class Reference - has SetSelected
/ClearSelected
/IsSelected
, so I tried this script:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# this script intended to be called from within KiCad's Pcbnew: Tools / Scripting Console (Python Shell)
# NB: that python shell might start from pwd: ~/kicad/kicommand
# >>> execfile('/path/to/kicad-pcbnew-invertselection.py')
# (here InvertSelection should end up in locals() namespace)
# >>> InvertSelection()
import sys
import os
import pcbnew
board = pcbnew.GetBoard()
def InvertSelection():
print("InvertSelection from kicad-pcbnew-invertselection.py:")
for drw in board.GetDrawings():
print("{} {}".format(drw, drw.IsSelected())) # pcbnew.DIMENSION False; pcbnew.DRAWSEGMENT True;
if drw.IsSelected():
drw.ClearSelected()
else:
drw.SetSelected()
for trk in board.GetTracks():
print("{} {}".format(trk, trk.IsSelected())) # pcbnew.TRACK
if trk.IsSelected():
trk.ClearSelected()
else:
trk.SetSelected()
for mod in board.GetModules():
print("{} {}".format(mod, mod.IsSelected())) # pcbnew.MODULE
if mod.IsSelected():
mod.ClearSelected()
else:
mod.SetSelected()
for pad in board.GetPads():
print("{} {}".format(pad, pad.IsSelected())) # pcbnew.MODULE
if pad.IsSelected():
pad.ClearSelected()
else:
pad.SetSelected()
for i in range(0, board.GetAreaCount()):
area = board.GetArea(i); # pcbnew.ZONE_CONTAINER
print("{} {} {}".format(i, area, area.IsSelected())) # pcbnew.MODULE
if area.IsSelected():
area.ClearSelected()
else:
area.SetSelected()
I notice that pretty much everything selected does get deselected (you have to scroll once with the mouse to force a re-rendering) except silkscreen texts; but no modules that were previously unselected are selected.
Furthermore, board.GetAreaCount()
for the zones gives me 8, but I can iterate 9 times through it in my board - and yet there are only two pointer addresses mentioned?! This is what I get as printout:
0 <pcbnew.ZONE_CONTAINER; proxy of <Swig Object of type 'ZONE_CONTAINER *' at 0xef60e9b0> > True
1 <pcbnew.ZONE_CONTAINER; proxy of <Swig Object of type 'ZONE_CONTAINER *' at 0xef60e848> > True
2 <pcbnew.ZONE_CONTAINER; proxy of <Swig Object of type 'ZONE_CONTAINER *' at 0xef60e9b0> > True
3 <pcbnew.ZONE_CONTAINER; proxy of <Swig Object of type 'ZONE_CONTAINER *' at 0xef60e848> > True
4 <pcbnew.ZONE_CONTAINER; proxy of <Swig Object of type 'ZONE_CONTAINER *' at 0xef60e9b0> > True
5 <pcbnew.ZONE_CONTAINER; proxy of <Swig Object of type 'ZONE_CONTAINER *' at 0xef60e848> > True
6 <pcbnew.ZONE_CONTAINER; proxy of <Swig Object of type 'ZONE_CONTAINER *' at 0xef60e9b0> > True
7 <pcbnew.ZONE_CONTAINER; proxy of <Swig Object of type 'ZONE_CONTAINER *' at 0xef60e848> > True
8 <pcbnew.ZONE_CONTAINER; proxy of <Swig Object of type 'ZONE_CONTAINER *' at 0xef60e9b0> > True
Notice only 0xef60e848 and 0xef60e9b0 appear ?!
And finally - even if I do get a visual indication (de-highlight) of the previously selected elements after running this script (and refreshing the screen with mouse scroll/zoom in), when I hit Del key afterwards, the entire original selection is gone?!
So, how can I properly perform a “selection” and “deselection” of a Pcbnew footprint/module, or rather, inverting selection, from a Pcbnew python script?
Furthermore, how can I iterate through all (visible) elements on board (so I can check their selection status), without having to specify their type? There is board.GetVisibleElements()
, but it returns an int, not an array…
EDIT: this is the version I’m using:
Application: kicad
Version: (2017-11-13 revision d98fc85)-master, release build
Libraries:
wxWidgets 3.0.2
libcurl/7.35.0 OpenSSL/1.0.1f zlib/1.2.8 libidn/1.28 librtmp/2.3
Platform: Linux 4.4.0-112-generic x86_64, 64 bit, Little endian, wxGTK
Build Info:
wxWidgets: 3.0.2 (wchar_t,wx containers,compatible with 2.8) GTK+ 2.24
Boost: 1.54.0
Curl: 7.35.0
Compiler: GCC 4.8.4 with C++ ABI 1002
Build settings:
USE_WX_GRAPHICS_CONTEXT=OFF
USE_WX_OVERLAY=OFF
KICAD_SCRIPTING=ON
KICAD_SCRIPTING_MODULES=ON
KICAD_SCRIPTING_WXPYTHON=ON
KICAD_SCRIPTING_ACTION_MENU=ON
BUILD_GITHUB_PLUGIN=ON
KICAD_USE_OCE=OFF
KICAD_SPICE=OFF