Python

Named Groups

Named Groups provide a way for a MicroStation user to connect logically a set of MicroStation graphic elements. Named groups can include elements from the active file and those from directly attached references. Elements in a named group can be manipulated collectively: read MicroStation help for more information. The MicroStation user interface makes it easy to create a named group and add and remove elements in that group …

MicroStation: Named Groups dialog

JSON

JSON is a popular text file format for data interchange. I use it in this example to write a simple representation of a named group. For example, given two named groups NG 1, NG 2 in a DGN file, their JSON respresentation would be something like …

{
    "0": ["NG 1", "Named Group 1", ""],
    "1": ["NG 2", "Named Group 2", ""]
}

Enumerate Named Groups Using Python

Get the named groups collection for a specified DGN model like this …

dgn_model: DgnModel = ISessionMgr.ActiveDgnModelRef
named_groups = NamedGroupCollection(dgn_model)

Function write_json_named_groups()

def write_json_named_groups(named_groups: NamedGroupCollection, list_type: ListType)->int:
    '''
    Write to a JSON file all NamedGroups in the active DGN model.

    Param ListType: Lets you use a Python dictionary or list to create the JSON data.

    Returns: The number of Named Groups written.
    '''
    print (f"named_groups type {type(named_groups)}")
    dgn_model = ISessionMgr.ActiveDgnModelRef
    json_file_path = compose_temp_json_file_path(dgn_model)
    msg = f"Create JSON file '{json_file_path}'"
    MessageCenter.ShowInfoMessage(msg, msg, False)
    n_groups = 0
    (n_groups, ng_dict) = named_group_collection_to_dictionary (named_groups)
    with open(json_file_path, 'w') as fp:
        json.dump(ng_dict, fp)
        msg = f"Created JSON file '{json_file_path}' from dictionary"
    MessageCenter.ShowInfoMessage(msg, msg, False)
    return n_groups

Function collect_named_groups()

def collect_named_groups(dgn_model: DgnModel = ISessionMgr.ActiveDgnModelRef)->(int, NamedGroupCollection):
    """
    Create a list of Named Groups found in a DGN model.
    Each Named Group is collected as a dictionary of tuples,
    each containing  ('Name': name, 'Value': value, 'Type': ng_type)

    Returns: tuple (int, NamedGroupCollection collection of Named Groups).  NamedGroupCollection is not a Python list.
    """
    assert dgn_model is not None, "No DGN model reference supplied"

    # Create a NamedGroupCollection for the specified model
    named_group_collection = NamedGroupCollection(dgn_model)
    n_members = len(list(named_group_collection))
    print(f"{n_members} Named Groups in the DGN model...")
    for i, group in enumerate (named_group_collection):
        assert group is not None
        proxy = NamedGroupProxy(group, dgn_model)
        print(f"[{i}] {proxy}")

    if 0 == n_members:
        print("No Named Groups found in the DGN model")

    return (n_members, named_group_collection)

Class Python-NamedGroupProxy

Python-NamedGroupProxy is a small class I use to help with Named Group programming. Class NamedGroupProxy is explained here.

Function named_group_collection_to_list()

def named_group_collection_to_list(named_group_collection: NamedGroupCollection)->(int, list):
    '''
    Convert a NamedGroupCollection to a Python list.

    Returns: tuple (int, list[NamedGroupProxy])
    '''
    print("named_group_collection_to_list...")
    n_groups = 0
    dgn_model = named_group_collection.GetModelRef().GetDgnModel()
    ng_list = []

    for i, group in enumerate (named_group_collection):
        proxy = NamedGroupProxy(group, dgn_model)
        print(f"[{i}] {proxy}")
        ng_list.append(proxy)
    n_groups = len(ng_list)
    print(f"named_group_collection_to_list {len(ng_list)} groups")
    return n_groups, ng_list
if __name__ == "__main__":
    vinfo = VersionInfo("List Named Groups", 26, 2, 26, "Show Named Groups in a DGN file")
    MessageCenter.ShowInfoMessage(vinfo.brief, vinfo.verbose, False)
    list_named_groups()

Class VersionInfo

VersionInfo is a small class I use to store version information. Read more here.

Questions

Post questions about MicroStation programming to the MicroStation Programming Forum.