JOURNAL TO CREATE REFERENCE SETS.

Hi,

I have tried to record a journal, which creates a new reference set and add all components and objects to that reference set.
But, I have a error with the below journal, this is working only for the part from where i created. when i used the same journal to other parts, it is just creating the reference set and not adding components to that reference set.

Please help me in using this journal to any parts.

Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display

' ----------------------------------------------
' Menu: Format->Reference Sets...
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

theSession.SetUndoMarkName(markId1, "Reference Sets Dialog")

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Create New Reference Set")

Dim referenceSet1 As ReferenceSet
referenceSet1 = workPart.CreateReferenceSet()

Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Add Components to Reference Set")

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId2)

Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Name of Reference Set")

referenceSet1.SetName("T/L")

Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(markId4)

Dim markId5 As Session.UndoMarkId
markId5 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Objects of Reference Set")

Dim components1(0) As NXObject
Dim component1 As Assemblies.Component = CType(workPart.ComponentAssembly.RootComponent.FindObject("COMPONENT sub level 1 1"), Assemblies.Component)

components1(0) = component1
referenceSet1.AddObjectsToReferenceSet(components1)

Dim nErrs3 As Integer
nErrs3 = theSession.UpdateManager.DoUpdate(markId5)

Dim markId6 As Session.UndoMarkId
markId6 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Objects of Reference Set")

Dim components2(0) As NXObject
Dim component2 As Assemblies.Component = CType(workPart.ComponentAssembly.RootComponent.FindObject("COMPONENT sub level 2 1"), Assemblies.Component)

components2(0) = component2
referenceSet1.AddObjectsToReferenceSet(components2)

Dim nErrs4 As Integer
nErrs4 = theSession.UpdateManager.DoUpdate(markId6)

Dim markId7 As Session.UndoMarkId
markId7 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Objects of Reference Set")

Dim components3(0) As NXObject
Dim component3 As Assemblies.Component = CType(workPart.ComponentAssembly.RootComponent.FindObject("COMPONENT sub level 3 1"), Assemblies.Component)

components3(0) = component3
referenceSet1.AddObjectsToReferenceSet(components3)

Dim nErrs5 As Integer
nErrs5 = theSession.UpdateManager.DoUpdate(markId7)

Dim markId8 As Session.UndoMarkId
markId8 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Objects of Reference Set")

Dim components4(0) As NXObject
Dim component4 As Assemblies.Component = CType(workPart.ComponentAssembly.RootComponent.FindObject("COMPONENT sub level 4 1"), Assemblies.Component)

components4(0) = component4
referenceSet1.AddObjectsToReferenceSet(components4)

Dim nErrs6 As Integer
nErrs6 = theSession.UpdateManager.DoUpdate(markId8)

Dim markId9 As Session.UndoMarkId
markId9 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Objects of Reference Set")

Dim components5(0) As NXObject
Dim component5 As Assemblies.Component = CType(workPart.ComponentAssembly.RootComponent.FindObject("COMPONENT sub level 5 1"), Assemblies.Component)

components5(0) = component5
referenceSet1.AddObjectsToReferenceSet(components5)

Dim nErrs7 As Integer
nErrs7 = theSession.UpdateManager.DoUpdate(markId9)

theSession.DeleteUndoMarksUpToMark(markId3, Nothing, False)

theSession.SetUndoMarkName(markId1, "Reference Sets")

theSession.DeleteUndoMark(markId1, Nothing)

' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module

We ironed out some details through email, here is the resulting code for anyone else interested:

[vbnet]
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Create New Reference Set")

Dim referenceSet1 As ReferenceSet
referenceSet1 = workPart.CreateReferenceSet()
referenceSet1.SetName("T/L")
'add all components (existing and future)
referenceSet1.SetAddComponentsAutomatically(True, True)
'create list variable for solid bodies
Dim mySolids As List(Of Body) = New List(Of Body)
'workPart.Bodies collection contains both solid and sheet bodies
'filter out solid bodies and add them to the list
For Each solid As Body In workPart.Bodies
If solid.IsSolidBody Then
mySolids.Add(solid)
End If
Next
'add solid bodies to reference set
referenceSet1.AddObjectsToReferenceSet(mySolids.ToArray)

End Sub

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[/vbnet]

Hi,

first of all, thanks for the journal. Thats almost the thing I was searching for. I would like to modify the code a little bit but unfortunately i´m not very good in writing codes but i understand the most things. I hope someone can help me.

First modification i would like to add is:
The journal is always creating the "T/L" reference set. But if the "T/L" is already existing, the execution of the journal gives a error message. How can i handle that? (something like: if "T/L" exists, put bodys in "T/L", else create "T/L" and put bodys in "T/L".

Second modification i would like to add is:
Put only all bodies from e.g. layer 1 to the reference set "T/L". Background is that we often have surrounding geometrie on a different layer in the part or assembly and this should no go to the reference set. We want only the final released geometrie in there.

3rd modification:
If already geometrie (all layers, all kind of geometrie like sketches or planes) is in the reference set "T/L" remove them all and add only layer 1 bodies

I hope someone can help me.
Thanks
Best regards

Try the code below:

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen

Module Module2

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Sub Main()

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 = "T/L"

'$$$$$ 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

Wow! Thats exactly what i need. Thank you so much! You´re brilliant. It works perfectly!

Thanks a lot!
Best regards

I have used the above journal to make reference set but it will add component and assemblies in reference set it won't include the solid bodies in to reference set.May I get journal to include bodies.

The journal code above was designed to only add bodies on layer 1 to the reference set. If you would like to add all the bodies in the part, look for the code:

For Each tempBody As Body In workPart.Bodies
If tempBody.Layer = refSetLayer Then
layerBodies.Add(tempBody)
End If
Next

and comment out or remove the layer test.

For Each tempBody As Body In workPart.Bodies
layerBodies.Add(tempBody)
Next

thank you i will use it

Hello all,

thank you very much for this hints here. It works in my issue, but I would miss one additional feature and I am not good enough in programming neither :-)

My wish is to add the new reference set and its data on layer 1 to all files in one directory. I will post the changed Journal here that I have now with some help in another forum.
This loads all part files (as I see in history) and seves and closes them, but none of them has the new Reference Set.
Can anyone help me in that issue please?

If yes, thank you very much in advance for your efforts :)

-------------------------------------------------------------------------------------

Option Strict Off
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()

Dim loadstat As PartLoadStatus = Nothing
For Each fname As String In Directory.GetFileSystemEntries("C:\tmp\REP2", "*.prt")
Dim tmppart As Part = theSession.Parts.Open(fname, loadstat)
ProcessPart()
tmppart.Save(BasePart.SaveComponents.False, BasePart.CloseAfterSave.True)
Next
End Sub

Sub ProcessPart()

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

The ProcessPart() subroutine uses the workPart variable that is created when the journal code initializes. You need to update the workPart reference before calling the ProcessPart() subroutine.

Change the loop in the Main sub to the following:

For Each fname As String In Directory.GetFileSystemEntries("C:\tmp\REP2", "*.prt")
Dim tmppart As Part = theSession.Parts.OpenDisplay(fname, loadstat)
workPart = tmppart
ProcessPart()
tmppart.Save(BasePart.SaveComponents.False, BasePart.CloseAfterSave.True)
Next

*edit*

Thank you very much, this works :)
Just one row is missed in your posting:
---------------------------------------------
Sub Main()

For Each fname As String In Directory.GetFileSystemEntries("C:\tmp\REP2", "*.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
-----------------------------------

Best regards,
Alex

hello I want to send some pics which can give you idea what I am saying about so, can you give mail ID.