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 …
Get the named groups collection for a specified DGN model like this …
dgn_model: DgnModel = ISessionMgr.ActiveDgnModelRef named_groups = NamedGroupCollection(dgn_model)
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 NamedGroups.common import (VersionInfo, collect_named_groups, named_group_collection_to_dictionary, named_group_collection_to_list, )
from NamedGroups.named_group_proxy import NamedGroupProxy
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_groups = 0
assert dgn_model is not None, "No DGN model reference supplied"
# Create a NamedGroupCollection for the specified model
n_groups, named_group_collection = collect_named_groups(dgn_model)
if 0 == n_groups:
msg = "No Named Groups found in the DGN model"
MessageCenter.ShowErrorMessage(msg, msg, False)
return 0
msg = f"{n_groups} Named Groups in the DGN model:"
MessageCenter.ShowInfoMessage(msg, msg, False)
group_list = named_group_collection_to_list(named_group_collection)
print("Enumerate group_list...")
for i, proxy in enumerate (group_list):
msg = f"[{i}] {proxy}"
print(msg)
MessageCenter.ShowInfoMessage(msg, msg, False)
# Convert the list to a dictionary, for no particular reason
# (We perform this conversion in the write-to-JSON module)
print("Enumerate groups_dict...")
groups_dict = named_group_collection_to_dictionary(named_group_collection)
for i, proxy in enumerate (groups_dict.values()):
msg = f"[{i}] {proxy}"
MessageCenter.ShowInfoMessage(msg, msg, False)
return n_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_groups = len(list(named_group_collection))
print(f"{n_groups} 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_groups:
print("No Named Groups found in the DGN model")
return (n_groups, named_group_collection)
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])
'''
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, 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. Read
more here.
Python-NamedGroupProxy is a small class I use to help with Named Group programming.
Class NamedGroupProxy is explained
here.
Post questions about MicroStation programming to the MicroStation Programming Forum.