We have an assembly of parts inside Mechanical that have bodies of different different types, and we would like to create named selections where every named selection (ns) contains only bodies of the same type (e.g., solids), using scripting.
The below Mechanical python script does that automatically (copy/paste and run from the Mechanical scripting console):
model = ExtAPI.DataModel.Project.Model sections=model.CrossSections.Children # get all materials
Parts = model.Geometry.GetChildren(DataModelObjectCategory.Part,True) # get all parts selIdline = [] selIdsolid = [] selIdsurf = [] selIdres = []
SlMn = ExtAPI.SelectionManager SlMn.ClearSelection() Sel = SlMn.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
for Part in Parts: # loop over parts for Body in Part.Children: if (Body.GeometryType)==(GeometryType.Line): # if the part sec. name is same as sec in loop BId = Body.GetGeoBody().Id selIdline.append(BId) # add to array storing parts elif (Body.GeometryType)==(GeometryType.Solid): BId = Body.GetGeoBody().Id selIdsolid.append(BId) # add to array storing parts elif (Body.GeometryType)==(GeometryType.Surface): BId = Body.GetGeoBody().Id selIdsurf.append(BId) # add to array storing parts else: BId = Body.GetGeoBody().Id selIdres.append(BId) # add to array storing parts Sel.Ids=selIdline if len(selIdline)>0: ns=model.AddNamedSelection() ns.Location=Sel ns.Name='LInes' SlMn.ClearSelection() Sel.Ids=selIdsolid if len(selIdsolid)>0: ns=model.AddNamedSelection() ns.Location=Sel ns.Name='SOlids' SlMn.ClearSelection() Sel.Ids=selIdsurf if len(selIdsurf)>0: ns=model.AddNamedSelection() ns.Location=Sel ns.Name='Surfaces' SlMn.ClearSelection() Sel.Ids=selIdres if len(selIdres)>0: ns=model.AddNamedSelection() ns.Location=Sel ns.Name='Rest' SlMn.ClearSelection()