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

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 list_named_groups()

Here's a function that uses that call and then enumerates the NamedGroup objects in that collection …

from MSPyBentley import *
from MSPyBentleyGeom import *
from MSPyDgnPlatform import *
from MSPyDgnView import *
from MSPyMstnPlatform import *

from la_solutions.version_info import VersionInfo

def list_named_groups(dgn_model: DgnModel = ISessionMgr.ActiveDgnModelRef)->int:
    """
    List the Named Groups found in a DGN model.

    Returns: The number of Named Groups found.
    """
    n_members = 0
    assert dgn_model is not None, "No DGN model reference supplied"

    # Create a NamedGroupCollection for the specified model
    named_group_collection = NamedGroupCollection(dgn_model)
    #  NamedGroupCollection has no __len()__ method, so a workaround it to convert it to a list
    #  then get the length of that list
    n_members = len(list(named_group_collection))

    print(f"{n_members} Named Groups in the DGN model:")
    found = False
    for i, group in enumerate (named_group_collection):
        name = group.GetName()
        description = group.GetDescription()
        print(f"  [{i}] Name: {str(name)} | Description: {str(description)}")
        found = True

    if not found:
        print("  (No Named Groups found in the DGN model.)")
    return n_members

Copy the above code to a Python file, and add the following to invoke it in the MicroStation Python manager …

if __name__ == "__main__":
    vinfo = VersionInfo("List Named Groups", 26, 1, 23, "Show Named Groups in a DGN file")
    MessageCenter.ShowInfoMessage(vinfo.brief, vinfo.verbose, False)
    list_named_groups()

VersionInfo is a small class I use to store version information …

from collections import namedtuple

class VersionInfo (namedtuple('VersionInfo', 'name, major, minor, sub_minor, description')):
    """ VersionInfo: a read-only class to store and format version information. """

    @property
    def app_name (self)->str:
        return self.name

    @property
    def brief (self)->str:
        """ Get a brief description of this version. """
        return f"{self.name}: {self.major}.{self.minor}.{self.sub_minor}"

    @property
    def verbose (self)->str:
        """ Get a verbose description of this version. """
        return f"{self.name}: {self.major}.{self.minor}.{self.sub_minor} {self.description}"

    def __str__(self):
        return verbose

Questions

Post questions about MicroStation programming to the MicroStation Programming Forum.