Python Assistant Prompts: Iterate a NamedGroupCollection


 

Prompt Used:

Python code to iterate a NamedGroupCollection

 

 

Python Assistant Generated Code:

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

def iterate_named_groups():
    """
    Iterates through all NamedGroups in the active model and prints their names.
    """
    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
    if ACTIVEMODEL is None:
        print("No active DGN model found.")
        return

    # Create a NamedGroupCollection for the active model
    named_group_collection = NamedGroupCollection(ACTIVEMODEL)
    count = 0

    for named_group in named_group_collection:
        name = named_group.GetName()
        print(f"NamedGroup {count}: {str(name)}")
        count += 1

    if count == 0:
        print("No NamedGroups found in the active model.")

if __name__ == "__main__":
    iterate_named_groups()

 

 

Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs