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

Nested Named Groups

As well as individual named groups, you can create a hierarchy of them by adding “child” named groups to a “parent”. When you have a hierarchy, manipulating the “parent” named group will include any attached “child” named groups. You can add and drop elements from individual named groups (including those that are part of a hierarchy), as well as manipulate the elements within each named group (again, including those that are part of a hierarchy).

Programming Named Groups

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

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

Enumerate that collection to see each named group …

count = 0
for i, group in enumerate(named_groups):
    msg += f"  [{1 + i}] Name: {group.GetName()}, Description: {group.GetDescription()}\n"
    count = i + 1

You can create, modify, and delete named groups using the Python API.

You can visit members of a named group using the TraverseMembers() and TraverseRelatedMembers() functions.

Get the size of a NamedGroupCollection like this …

n_groups = len(list(named_groups))

Named Group Collection Problem

Warning: Don't assume that you can convert a NamedGroupedCollection to a Python list

If you take this reasonable step and then attempt to enumerate the resulting list, it won't work. Something happens in the conversion and the data become garbled …

dgn_model: DgnModel = ISessionMgr.ActiveDgnModelRef
named_groups = NamedGroupCollection(dgn_model)
named_group_list = list(named_groups)
for group in named_group_list:
    name = group.GetName() # Gives odd error message

The NamedGroupCollection is not Pythonic: it has no __len__() member and no __iter__() method. We get its length by converting it to a list and measuring the length of that list …

n_members = len(list(named_group_collection))

But don't attempt to use that list for enumeration! See what follows …

The lack of an iteration (__iter__()) method means that we can't use the usual Python ways to enumerate that list. That's one reason I wrote NamedGroupProxy. Here's how to get a list of NamedGroupProxy from a NamedGroupCollection …

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

    Returns: tuple (int, list[NamedGroupProxy])
    '''
    dgn_model = named_group_collection.GetModelRef().GetDgnModel()
    ng_list = []

    for i, group in enumerate (named_group_collection):
        proxy = NamedGroupProxy.from_named_group(group, dgn_model)
        #print(f"[{i}] {proxy}") # For developers only
        ng_list.append(proxy)
    return len(ng_list), ng_list

Named Group Flags

Class NamedGroupFlags get/sets various named flags that specify such things as propagation protocols. They are described in the Named Group Flags page.

Questions

Post questions about MicroStation programming to the MicroStation Programming Forum.