The procedure for requesting AWR support has changed. Please read all about the new AWR product support process.
Page tree
Skip to end of metadata
Go to start of metadata

A common task for scripts is to search through a MWO database and find all elements of a certain type. This example script does this and highlights some useful scripting features and tips:

 Option Explicit
Sub Main
    Dim s As Schematic, e As Element
    For Each s In Project.Schematics
        For Each e In s.Elements
            If Split(e.Name, ".")(0) = "MLIN" Then
                Debug.Print s.Name & "::" & e.Name
            End If
        Next e
    Next s
End Sub

Some things to note about this code:

Option Explicit

Use of the Option Explict command at the top of a script is highly recommended. This option requires that you declare all variables before using them, so it helps catch syntax errors and misspellings in variable names. It adds a small amount of extra work, but for all but the most trivial scripts you are likely to get this time back in reduced debugging time. Here's an example similar to a problem found in a user script that did not use Option Explicit:

 Sub Main
    sml = Project.Name
    Debug.Print sm1 'prints nothing, instead of the project file name as expected
End Sub

Can you spot the error? The first variable ends in lowercase L, while the 2nd variable ends in the number 1. Option Explicit would catch this.

Dim s As Schematic, e As Element

It's fine to declare more than one variable on a single line. Also, variables do not need to be declared at the top of a Function or Sub; instead you can declare them just before they are used in the function, which can make the code more readable.

For Each

Using the For Each keywords to loop over AWR collections is easier to write and more readable than using an index. The alternative would be:

Dim i as Long, s as Schematic
For i = 1 to Project.Schematics.Count
    Set s = Project.Schematics(i)
    ' stuff....
Next i

Getting the Model Name

The code phrase

Split(e.Name, ".")(0)

is a common shortcut for getting the element model name. The Element name is always formatted like "<modelName>.<elementID>", so

 Split(e.Name, ".")


returns an array like {"RES","R1"}. You don't need to create a temporary variable to hold this array however, you can use it directly by adding a "(0)" after the Split() function to get the first index of the array ("RES").