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

Named Groups

For an introduction to Named Groups for Python programmers, see Named Groups for Programmers.

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", ""]
}

Read Named Groups from Python

First, import some Python modules that supply functions and classes …

from MSPyBentley import *
from MSPyDgnPlatform import *
from MSPyMstnPlatform import *

from  NamedGroups.common import ( VersionInfo, compose_temp_json_file_path, compose_temp_file_path,
                                  named_group_collection_to_dictionary, named_group_collection_to_list,
                                  collect_named_groups, NamedGroupFile, )
from  NamedGroups.named_group_proxy import  NamedGroupProxy

import json

Function read_json_named_groups()

Read from a JSON file to create a list of NamedGroupProxy …

def read_json_named_groups()->(int, list[NamedGroupProxy]):
    '''
    Read NamedGroup data from a JSON file.

    Returns: tuple (Number of Named Groups read, list of NamdGroupProxy data)
    '''
    json_file_path = compose_temp_file_path(NamedGroupFile.transfer_file_name(), '.json')
    msg = f"Read JSON file '{json_file_path}'"
    MessageCenter.ShowInfoMessage(msg, msg, False)
    print(msg)
    n_groups = 0
    groups = []
    with open(json_file_path, 'r') as fp:
        data = json.load(fp)
        n_groups = len(data)
        msg = f"Read {n_groups}"
        MessageCenter.ShowInfoMessage(msg, msg, False)
        for i, ng in enumerate(data.values()):
            print(f"[{i}] {ng}")
            _NAME = 0
            _DESCR = 1
            _TYPE = 2
            _FLAGS = 3
            proxy = NamedGroupProxy(ng[_NAME], ng[_DESCR], ng[_TYPE], ng[_FLAGS])
            print(f"proxy [{i}] {proxy}")
            groups.append(proxy)
        return (n_groups, groups)

    if 0 == n_groups:
        msg = f"Failed to read JSON file '{json_file_path}'"
        MessageCenter.ShowErrorMessage(msg, msg, False)
        return n_groups, []

Function flag_if_present()

We have a list of Named Groups imported from JSON. We want to find those not present in the active DGN file, so we can create those Named Groups that we need.

def flag_if_present(proxy_list: list[NamedGroupProxy], dgn_model: DgnModel = ISessionMgr.ActiveDgnModelRef)->int:
    '''
    For each member of the NamedGroupProxy list, check whether that NG is already defined in the specified DGN model.
    If it exists, set the NamedGroupProxy 'present' flag.
    '''
    n_existing, existing_group_collection = collect_named_groups(dgn_model)
    (n_existing, existing_group_list) = named_group_collection_to_list(existing_group_collection)
    print(existing_group_list)
    for proxy in proxy_list:
        for existing in existing_group_list:
            print(f"NG compare {proxy} with {existing}")
            if proxy == existing:
                proxy.set_present()
                print(f"proxy {proxy} is present")
    print(f"new_groups_set after check against existing_group_list...")
    # Convert lists to sets then compute their difference...
    new_groups_set = set(proxy_list).difference(set(existing_group_list))
    print(f"new_groups_set size={len(new_groups_set)} {new_groups_set}")
    for proxy in new_groups_set:
        print(proxy)

Class Python-NamedGroupProxy

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

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.