Select atoms and bonds by location#
This example combines what is written in the Selecting nodes section and demonstrates how to select atoms and bonds based on their location.
# get an indexer of all atoms in the active document
# here we use SAMSON Node Specification Language
atom_indexer = SAMSON.getNodes('node.type atom')
# two planes
x_min = SBQuantity.angstrom(0.0)
x_max = SBQuantity.angstrom(5.0)
# an indexer in which we will be adding the desired nodes
atom_selection_indexer = SBNodeIndexer()
# a loop over an indexer
for atom in atom_indexer:
x_coord = atom.getX()
# check whether the atom lies in between the desired planes
if x_coord >= x_min and x_coord <= x_max:
# add the atom to the indexer
atom_selection_indexer.addNode(atom)
# add bonds between the atoms to the selection
bond_selection_indexer = SBNodeIndexer()
for atom in atom_selection_indexer:
# go through all the bonds of an atom
for bond in atom.getBondList():
# skip bonds that are already in the indexer
if bond in bond_selection_indexer: continue
# check if the opposite atom should also be selected
if bond.getOppositeAtom(atom) in atom_selection_indexer:
bond_selection_indexer.addNode(bond)
# make the operation undoable
SAMSON.beginHolding('Select atoms')
# clear the current selection in the active document
SAMSON.getActiveDocument().clearSelection()
# select nodes by changing their selectionFlag
for node in atom_selection_indexer:
node.selectionFlag = True
for node in bond_selection_indexer:
node.selectionFlag = True
# stop the undoable operation
SAMSON.endHolding()