Assume I have some Named Selections with the same name prefix, for example 'CONT_1', 'CONT_2', etc. How can I merge them into a new Named Selection using Mechanical Scripting?
You can use the following script to merge the Named Selections with the same name prefix provided by the user.
nsMergeList = ['CONT_', 'TARG_'] nsList = DataModel.Project.Model.NamedSelections.Children nsToMerge = {key: None for key in nsMergeList} for nsPrefix in nsMergeList: nsToMerge[nsPrefix] = [] for ns in nsList: if ns.Name.StartsWith(nsPrefix): nsToMerge[nsPrefix].append(ns) for key, value in nsToMerge.items(): nsMerge = DataModel.Project.Model.NamedSelections.AddNamedSelection() nsMerge.Name = key nsMergeIds = [ns.Location.Ids for ns in value] nsMergeLoc = [] for sublist in nsMergeIds: for item in sublist: nsMergeLoc.append(item) selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities) selection.Ids = nsMergeLoc nsMerge.Location=selection