Can you write a script which creates a new named group and
adds all currently selected elements to it.
from MSPyBentley import * from MSPyECObjects import * from MSPyBentleyGeom import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import * import tkinter as tk from tkinter import simpledialog, messagebox def create_named_group_from_selection(group_name="MyNamedGroup"): """ Creates a named group in the active DGN model and adds all currently selected elements to it. :param group_name: The name of the named group to create. :type group_name: str :returns: True if the named group is successfully created and elements are added, False otherwise. :rtype: bool """ ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef if ACTIVEMODEL is None: messagebox.showerror("Error", "No active DGN model.") return False namedGroups = NamedGroupCollection(ACTIVEMODEL) if namedGroups is None: messagebox.showerror("Error", "Could not get NamedGroupCollection.") return False # Create named group flag = NamedGroupFlags() ret = NamedGroup.Create(group_name, "", flag, ACTIVEMODEL) if BentleyStatus.eSUCCESS != ret[0]: messagebox.showerror("Error", f"Failed to create named group: {group_name}") return False group = ret[1] # Get the selected elements selSetManager = SelectionSetManager.GetManager() agenda = ElementAgenda() selSetManager.BuildAgenda(agenda) if agenda.GetCount() == 0: messagebox.showinfo("Info", "No elements selected.") return False # Add each selected element to the named group memberFlag = NamedGroupMemberFlags() for i in range(agenda.GetCount()): editElementHandle = agenda.GetEntry(i) if BentleyStatus.eSUCCESS != group.AddMember(editElementHandle.GetElementId(), ACTIVEMODEL,
memberFlag): messagebox.showerror("Error", "Failed to add an element to the named group.") return False if BentleyStatus.eSUCCESS != group.WriteToFile(True): messagebox.showerror("Error", "Failed to write named group to file.") return False messagebox.showinfo("Success", f"Named group '{group_name}' created and elements added.") return True def main(): # Prompt user for group name using tkinter root = tk.Tk() root.withdraw() group_name = simpledialog.askstring("Named Group", "Enter name for the new Named Group:",
initialvalue="MyNamedGroup") if group_name: create_named_group_from_selection(group_name) else: messagebox.showinfo("Cancelled", "Operation cancelled by user.") if __name__ == "__main__": main()
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs