Electric has two scripting languages: Java (using the Bean Shell) and Python (using Jython). These languages enable you to load custom code that adds functionality to Electric. Neither of these languages is part of the default Electric distribution. You must add them as "plug ins" (see Section 1-5 for more on plug-ins).

To run a Java script, use the Run Java Bean Shell Script... command (in menu Tools / Languages). To run a Python script, use the Run Jython Script... command. Note that during execution of these scripts, Electric may give warning messages about preferences, which can be ignored.

You can attach a script to the Tools / Languages menu by using the Manage Scripts... command. Scripts can have mnemonic letters assigned to them (see Section 1-9 for more on mnemonics). |

#### Java Script Examples

Here are some example scripts in the Java Bean Shell. For more information about accessing the internals of Electric, read the Javadoc in the source code.

`import com.sun.electric.database.hierarchy.Cell;
import com.sun.electric.database.topology.NodeInst;
import com.sun.electric.tool.Job;
import java.util.Iterator;
// get the current cell
Cell c = Job.getUserInterface().getCurrentCell();
// find all transistors
for(Iterator it = c.getNodes(); it.hasNext(); ) {
NodeInst ni = it.next();
if (ni.getFunction().isTransistor())
System.out.println("Found transistor: " + ni.describe(false));
}
// find all exports that start with "A"
for(Iterator it = c.getPorts(); it.hasNext(); ) {
com.sun.electric.database.hierarchy.Export e = (com.sun.electric.database.hierarchy.Export)it.next();
if (e.getName().toLowerCase().startsWith("a"))
System.out.println("Found export: " + e.getName());
}`

This example searches the current cell, printing all transistors and all exports that start with the letter "a".

Notice that Electric's "Export" object must be a fully-qualified name, because the name "Export" is used for other reasons in the Bean Shell. This also applies to Electric's "EPoint" class.

`import com.sun.electric.database.hierarchy.Cell;
import com.sun.electric.database.topology.NodeInst;
import com.sun.electric.technology.PrimitiveNode;
import com.sun.electric.technology.Technology;
import com.sun.electric.tool.lang.EvalJavaBsh;
import java.awt.geom.Point2D;
Cell newCell = Cell.makeInstance(Library.getCurrent(), "samp1{lay}");
Technology tech = Technology.findTechnology("mocmos");
PrimitiveNode trP = tech.findNodeProto("P-Transistor");
NodeInst tP = NodeInst.makeInstance(trP, new Point2D.Double(10, 10),
trP.getDefWidth(), trP.getDefHeight(), newCell);
EvalJavaBsh.displayCell(newCell);`

This example creates a new cell, places a transistor in it, and displays the cell.

`import com.sun.electric.database.hierarchy.Cell;
import com.sun.electric.database.geometry.Orientation;
import com.sun.electric.database.topology.ArcInst;
import com.sun.electric.database.topology.NodeInst;
import com.sun.electric.technology.ArcProto;
import com.sun.electric.technology.PrimitiveNode;
import com.sun.electric.technology.Technology;
import java.awt.geom.Point2D;
// create the new cell
Cell newCell = Cell.makeInstance(Library.getCurrent(), "samp2{lay}");
Technology tech = Technology.findTechnology("mocmos");
// place a rotated transistor
PrimitiveNode trP = tech.findNodeProto("P-Transistor");
NodeInst tP = NodeInst.makeInstance(trP, new Point2D.Double(0, 20),
trP.getDefWidth(), trP.getDefHeight(), newCell,
Orientation.R, "T1");
// place a metal-Active contact
PrimitiveNode coP = tech.findNodeProto("Metal-1-P-Active-Con");
NodeInst maP = NodeInst.makeInstance(coP, new Point2D.Double(8, 20),
coP.getDefWidth(), coP.getDefHeight(), newCell);
// wire the transistor to the contact
ArcProto aP = tech.findArcProto("P-Active");
ArcInst.makeInstance(aP, tP.findPortInst("diff-bottom"),
maP.findPortInst("metal-1-p-act"));
// export the contact
com.sun.electric.database.hierarchy.Export.newInstance(newCell,
maP.findPortInst("metal-1-p-act"), "IN", PortCharacteristic.IN);`

This example goes a bit further: it creates a rotated transistor and a contact, wires them together, and exports the contact. The transistor is named "T1."

#### Python Script Examples

`from com.sun.electric.database.hierarchy import Cell
from com.sun.electric.database.topology import NodeInst
from com.sun.electric.tool import Job
from java.util import Iterator
# get the current cell
c = Job.getUserInterface().getCurrentCell()
# find all transistors
it = c.getNodes()
while it.hasNext():
ni = it.next()
if ni.getFunction().isTransistor():
print "Found transistor: " + ni.describe(0)
# find all exports that start with "A"
it = c.getPorts()
while it.hasNext():
e = it.next()
if e.getName().lower().startswith("a"):
print "Found export: " + e.getName()
`

This example searches the current cell, printing all transistors and all exports that start with the letter "a".

`from com.sun.electric.database.hierarchy import Cell
from com.sun.electric.database.hierarchy import Library
from com.sun.electric.database.topology import NodeInst
from com.sun.electric.technology import Technology
from com.sun.electric.tool.lang import EvalJython
from java.awt.geom import Point2D
newCell = Cell.makeInstance(Library.getCurrent(), "sample1{lay}")
tech = Technology.findTechnology("mocmos")
trP = tech.findNodeProto("P-Transistor")
tP = NodeInst.makeInstance(trP, Point2D.Double(10, 10), trP.getDefWidth(), trP.getDefHeight(), newCell)
EvalJython.displayCell(newCell)
`

This example creates a new cell, places a transistor in it, and displays the cell.

`from com.sun.electric.database.geometry import Orientation
from com.sun.electric.database.hierarchy import Cell
from com.sun.electric.database.hierarchy import Library
from com.sun.electric.database.hierarchy import Export
from com.sun.electric.database.prototype import PortCharacteristic
from com.sun.electric.database.topology import ArcInst
from com.sun.electric.database.topology import NodeInst
from com.sun.electric.technology import Technology
from java.awt.geom import Point2D
# create the new cell
newCell = Cell.makeInstance(Library.getCurrent(), "sample2{lay}")
tech = Technology.findTechnology("mocmos")
# place a rotated transistor
trP = tech.findNodeProto("P-Transistor")
tP = NodeInst.makeInstance(trP, Point2D.Double(0, 20), trP.getDefWidth(), trP.getDefHeight(), newCell, Orientation.R, "T1")
# place a metal-Active contact
coP = tech.findNodeProto("Metal-1-P-Active-Con")
maP = NodeInst.makeInstance(coP, Point2D.Double(8, 20), coP.getDefWidth(), coP.getDefHeight(), newCell)
# wire the transistor to the contact
aP = tech.findArcProto("P-Active")
ArcInst.makeInstance(aP, tP.findPortInst("diff-bottom"), maP.findPortInst("metal-1-p-act"))
# export the contact
Export.newInstance(newCell, maP.findPortInst("metal-1-p-act"), "IN", PortCharacteristic.IN)
`

This example goes a bit further: it creates a rotated transistor and a contact, wires them together, and exports the contact. The transistor is named "T1."