Building a decane molecule#

Decane molecule

This example combines what is written in the Building section and demonstrates how to create a structural model populated with atoms on an example of a decane molecule.

Example: building a decane molecule#
# make the operation undoable
SAMSON.beginHolding("Add nodes")

# create an instance of a structural model
structuralModel = SBStructuralModel()
# set the structural model's name
structuralModel.name = 'Structural model'

# hold the node to make it creation undoable
SAMSON.hold(structuralModel)
# create the node
structuralModel.create()
# add the visual model to the active document
SAMSON.getActiveDocument().addChild(structuralModel)

atomIndexer = SBNodeIndexer()

for i in range(10):

    # generate a position
    position = SBPosition3(
        i * SBQuantity.angstrom(1.1),
        (i % 2) * SBQuantity.angstrom(0.9),
        SBQuantity.angstrom(0))

    # instantiate a Carbon atom at the given position
    atom = SBAtom(SBElement.Carbon, position)

    # hold the node to make it creation undoable
    SAMSON.hold(atom)
    # create the node
    atom.create()

    # add the atom to the structuralModel
    structuralModel.addChild(atom)

    atomIndexer.addNode(atom)

# create covalent bonds
structuralModel.createCovalentBonds()

# add hydrogens to atoms based on their valence
for atom in atomIndexer:
    atom.addHydrogens()

# stop holding the undoable operation
SAMSON.endHolding()

# center the active camera on the system
SAMSON.getActiveCamera().center()