Option Strict On Imports System Imports System.Collections.Generic Imports NXOpen Imports System.IO Module Module2 Dim theSession As Session = Session.GetSession() Dim workPart As Part = theSession.Parts.Work Sub Main() For Each fname As String In Directory.GetFileSystemEntries("C:\tmp\REP\REP_J\4hk1_150323", "*.prt") Dim loadstat As PartLoadStatus = Nothing Dim tmppart As Part = theSession.Parts.OpenDisplay(fname, loadstat) workPart = tmppart Process() tmppart.Save(BasePart.SaveComponents.False, BasePart.CloseAfterSave.True) Next End Sub Sub Process() If IsNothing(theSession.Parts.Work) Then 'active part required Return End If Dim lw As ListingWindow = theSession.ListingWindow lw.Open() Const undoMarkName As String = "reference set from bodies on layer" Dim markId1 As Session.UndoMarkId markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName) '$$$$$ specify reference set name here Const refSetName As String = "REP" '$$$$$ specify layer Const refSetLayer As Integer = 1 Dim layerBodies As New List(Of Body) 'gather bodies on specified layer For Each tempBody As Body In workPart.Bodies If tempBody.Layer = refSetLayer Then layerBodies.Add(tempBody) End If Next Try 'find/create reference set Dim myRefSet As ReferenceSet = CreateReferenceSet(refSetName) 'remove all objects from reference set myRefSet.RemoveObjectsFromReferenceSet(myRefSet.AskMembersInReferenceSet) 'add bodies from specified layer myRefSet.AddObjectsToReferenceSet(layerBodies.ToArray) Catch ex As NXException theSession.UndoToMark(markId1, undoMarkName) MsgBox(ex.Message) Finally End Try lw.Close() End Sub Function CreateReferenceSet(ByVal refSetName As String) As ReferenceSet Dim theRefSets() As ReferenceSet = workPart.GetAllReferenceSets 'does the specified reference set exist? 'if so, return it For Each someRefSet As ReferenceSet In theRefSets If someRefSet.Name.ToUpper = refSetName.ToUpper Then Return someRefSet End If Next 'if we get here, the ref set does not exist 'create it Dim targetRefSet As ReferenceSet targetRefSet = workPart.CreateReferenceSet() targetRefSet.SetName(refSetName) Return targetRefSet End Function Public Function GetUnloadOption(ByVal dummy As String) As Integer 'Unloads the image when the NX session terminates GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination End Function End Module