Top-Level Instance Names Matching Wildcard Pattern

author-image

By

This design example provides a Tcl procedure that returns top-level instance names that match a wildcard pattern. Copy and paste the procedure into a Tcl script, and then call the procedure.
The procedure returns the instance names in a list. If no top-level instance names match the wildcard pattern, the procedure returns an empty list. The instance names are returned in an arbitrary order. If you want the list of instance names returned in a particular order, such as sorted alphabetically, use the lsort command to sort it appropriately.
For example, if the top-level instance names in your design are foo1, foo2, foo3, bar1, bar2, and bar3, a procedure call of

get_top_level_instances_matching *2 would return { foo2 bar2 }.

proc get_top_level_instances_matching { wildcard } {

    # Make a variable to hold the top-level instances that match the wildcard
    catch { array unset names_to_return }
    array set names_to_return [list]
    
    # The collection of names is all the hierarchies in the design
    foreach_in_collection name_id [get_names -filter * -node_type hierarchy] {
    
        # The short_full_path option gets the name in the form
        # instance|instance|...
        # It uses only instances regardless of whether the
        # "Display entity name for node name" setting is on or off
        set short_full_name [get_name_info -info short_full_path $name_id]
        
        # Split the hierarchy into a list, breaking it apart on the
        # hierarchy separator |
        set short_full_pieces [split $short_full_name "|"]
        
        # Get the top-level instance name - the first one in the list
        set top_level_instance [lindex $short_full_pieces 0]
        
        # If the top-level instance name matches the wildcard, save it
        # to return at the end of the procedure
        if { [string match $wildcard $top_level_instance] } {
            set names_to_return($top_level_instance) 1
        }
    }
    
    return [array names names_to_return]
}