Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IFRs on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to move all Assembly Components to particular layer(1) using journal 2

Status
Not open for further replies.

DesEngineer4

Mechanical
Feb 19, 2013
181
Hi,
This is shanmuk, new to NX7.5
I need your help in creating journal for assembly components.
Tool: UG/NX 7.5
Need:
I want to move all assembly components to layer 1 using journal.

Can you please help me in creating journal for the above need.



 
Replies continue below

Recommended for you

Here is a journal to move components to layer 1.

Code:
Option Strict Off
Imports System
Imports System.Collections
Imports NXOpen
Imports NXOpen.Utilities
Imports NXOpen.Assemblies

Module MoveComponents
    Dim s As Session = Session.GetSession()
    Dim lw As ListingWindow = s.ListingWindow
    Sub Main()
        Dim allComp1 As ArrayList = New ArrayList
        Dim displaypart As Part = s.Parts.Display
        Dim root As Component = s.Parts.Display.ComponentAssembly.RootComponent
        getAllComponents2(root, allComp1)
        Dim cnt1 As Integer = allComp1.Count
        Dim objectArray1(cnt1 - 1) As DisplayableObject
        For i As Integer = 0 To cnt1 - 1
            objectArray1(i) = allComp1(i)
        Next
        displaypart.Layers.MoveDisplayableObjects(1, objectArray1)
    End Sub

    Sub getAllComponents2(ByVal comp As Component, ByRef allComp As ArrayList)
        Dim child As Component = Nothing
        Dim space As String = Nothing
        For Each child In comp.GetChildren()
            allComp.Add(child)
            getAllComponents2(child, allComp)
        Next
    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
    End Function

End Module

Regards

Frank Swinkels
 
Hi,
thanks for your valuable reply and i tried your journal, it is working fine and thanks for that. I have some more queries i hope you will advise me in regarding.

1. From the above journal it was moving all assembly components to layer 1. (that's really helpful to me) But, I have a range that all assembly components should be in between layer (1 to 10), some components which are already in layer below 10 those components are also moving to layer 1. Please suggest me, (in leaving those components which are in layer <10 same as previous but changing the component layer >10 to layer 1).

2. I am trying the below journal to find out the (referenceset_entire part or referenceset_empty) only. But, this journal showing all reference set which are in assembly, I need only specific reference set.

Example: An assembly containing 10 sub assembly components (out of 10 sub assemblies, 4 sub assembly components having reference set name as "S" and another 4 sub assembly components having reference set name as "S-T/L" and remaining two having reference set as Entire Part (which is none)). I need the journal to list only those two components which having reference set as Entire part. "Please have a look on below journal"



Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.Assemblies
Imports NXOpen.UF

Module report_components

Dim s As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = s.ListingWindow

Sub Main()

lw.Open()
Dim root As Component = _
s.Parts.Display.ComponentAssembly.RootComponent
reportComponentChildren(root, 0)

End Sub

Sub reportComponentChildren(ByVal comp As Component, _
ByVal indent As Integer)

Dim child As Component = Nothing
Dim space As String = Nothing
Dim c_part As Part = Nothing
Dim c_obj As NXObject = Nothing


For ii As Integer = 1 To indent
space = space & " "
Next
For Each child In comp.GetChildren()

'c_obj = child.Prototype
'lw.WriteLine(space & child.Name)
'c_part = DirectCast(child.Prototype, Part)
'lw.WriteLine(space & child.Name & " " & c_part.FullPath)

Dim cpTag As NXOpen.Tag = child.Tag()
Dim partName As String = ""
Dim refsetName As String = ""
Dim instName As String = ""
Dim origin(2) As Double
Dim csysMatrix(8) As Double
Dim xform(3, 3) As Double

ufs.Assem.AskComponentData(cpTag, partName, refsetName, instName, origin, csysMatrix, Xform)
lw.WriteLine(child.Name & " " & refsetName)

reportComponentChildren(child, indent + 1)
Next
End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function

End Module



 
The first journal has been extended to only change the component layer to 1 if its layer number > 10.

Code:
Option Strict Off
Imports System
Imports System.Collections
Imports NXOpen
Imports NXOpen.Utilities
Imports NXOpen.Assemblies

Module MoveComponents
    Dim s As Session = Session.GetSession()
    Dim lw As ListingWindow = s.ListingWindow
    Sub Main()
        Dim allComp1 As ArrayList = New ArrayList
        Dim displaypart As Part = s.Parts.Display
        Dim root As Component = s.Parts.Display.ComponentAssembly.RootComponent
        getAllComponents2(root, allComp1)
        Dim dispobj As DisplayableObject = Nothing
        Dim objectArray1(-1) As DisplayableObject
        Dim objlayer As Integer = Nothing
        Dim cnt2 As Integer = 0
        For i As Integer = 0 To cnt1 - 1
            dispobj = DirectCast(allComp1(i), DisplayableObject)
            objlayer = dispobj.Layer
            If objlayer > 10 Then
                ReDim Preserve objectArray1(cnt2)
                objectArray1(cnt2) = allComp1(i)
                cnt2 += 1
            End If
        Next
        If cnt2 > 0 Then
            displaypart.Layers.MoveDisplayableObjects(1, objectArray1)
        End If
    End Sub

    Sub getAllComponents2(ByVal comp As Component, ByRef allComp As ArrayList)
        Dim child As Component = Nothing
        Dim space As String = Nothing
        For Each child In comp.GetChildren()
            allComp.Add(child)
            getAllComponents2(child, allComp)
        Next
    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
    End Function

End Module

I have changed your second journal just to make it similar to the first layer in that I use the array list of components. This makes it much easier to combine/change any journal. Since in this journal we are only looking for reference sets I have used an alternative method.

Code:
Option Strict Off

Imports System
Imports System.Collections
Imports NXOpen
Imports NXOpen.Assemblies
Imports NXOpen.UF

Module ReportReferenceSet

    Dim s As Session = Session.GetSession()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = s.ListingWindow

    Sub Main()
        Dim allComp1 As ArrayList = New ArrayList
        Dim displaypart As Part = s.Parts.Display
        lw.Open()
        Dim root As Component = s.Parts.Display.ComponentAssembly.RootComponent
        getAllComponents2(root, allComp1)
        Dim referenceSet1 As String = "Entire Part"
        reportComponentReferenceSet(allComp1, referenceSet1)
    End Sub
    Sub reportComponentReferenceSet(ByVal allComp1 As ArrayList, ByVal referenceSet1 As String)
        For Each comp As Component In allComp1
            If comp.ReferenceSet = referenceSet1 Then
                lw.WriteLine(comp.Name & " " & comp.ReferenceSet)
            End If
        Next
    End Sub
    Sub getAllComponents2(ByVal comp As Component, ByRef allComp As ArrayList)
        Dim child As Component = Nothing
        Dim space As String = Nothing
        For Each child In comp.GetChildren()
            allComp.Add(child)
            getAllComponents2(child, allComp)
        Next
    End Sub
    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        Return Session.LibraryUnloadOption.Immediately
    End Function

End Module

Regards

Frank Swinkels
 
Thank you very much.
I will try these journals and come back to you with results
 
Hi,
For second journal for reference sets it was working perfectly, thank you very much.

for first journal it was showing an error like [ cnt1 is not defined ] please see the attached snap shot.
some what i tried to define (cnt1) but i didnt get any result. Please suggest.

 
 http://files.engineering.com/getfile.aspx?folder=b0550821-adc9-4791-8b8a-53ec456b565d&file=journal_error.JPG
Sorry I deleted a line of code that I should have not deleted.

After the line

getAllComponents2(root, allComp1)

in main sub insert a line

Dim cnt1 As Integer = allComp1.Count

Regards

Frank Swinkels
 
Yeah its ok..

Now it is working for me..
thank you..
 
Hi

I need your help and suggestion in regarding.

Is there any program in journal or grip to the below activity ?

ex:

I use 5 .prt files in my daily use of work which was saved in some location. Can we call that .prt file through icon in NX 7.5, If it is possible please guide me. How i need is if i click on icon in toolbar. it should show the list of 5 .prt files. So, that i can use the need one in single click.

Thanks in Advance.
 
It is possible to do what you want. You could have an icon for each of the 5 part files that you use daily. What it would need is a simple program for the toolbar such that the part is opened. Alternatively you can have a single icon which start a program from which you would select a part file to open.

I have attached two images. Image1.jpg shows the selection dialog for 5 parts which whould run from the added file open icon on image2.jpg. The five blank icons would be separate programs to open parts.

Frank Swinkels
 
 http://files.engineering.com/getfile.aspx?folder=588c619e-0f8a-4326-a3c4-045f777295bd&file=imagesMon4March2013.zip
Yes,

I need like image 1. What is the program for that ?

 
Hi

You have given the below journal to Find out the Reference Set "Entire Part". What changes to be done to the following journal for replacing the "Entire Part" to Reference Set "S". Please advice.

Option Strict Off

Imports System
Imports System.Collections
Imports NXOpen
Imports NXOpen.Assemblies
Imports NXOpen.UF

Module ReportReferenceSet

Dim s As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = s.ListingWindow

Sub Main()
Dim allComp1 As ArrayList = New ArrayList
Dim displaypart As Part = s.Parts.Display
lw.Open()
Dim root As Component = s.Parts.Display.ComponentAssembly.RootComponent
getAllComponents2(root, allComp1)
Dim referenceSet1 As String = "Entire Part"
reportComponentReferenceSet(allComp1, referenceSet1)
End Sub
Sub reportComponentReferenceSet(ByVal allComp1 As ArrayList, ByVal referenceSet1 As String)
For Each comp As Component In allComp1
If comp.ReferenceSet = referenceSet1 Then
lw.WriteLine(comp.Name & " " & comp.ReferenceSet)
End If
Next
End Sub
Sub getAllComponents2(ByVal comp As Component, ByRef allComp As ArrayList)
Dim child As Component = Nothing
Dim space As String = Nothing
For Each child In comp.GetChildren()
allComp.Add(child)
getAllComponents2(child, allComp)
Next
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function

End Module

 
Here is the journal to replace a reference set.

Code:
Option Strict Off

Imports System
Imports System.Collections
Imports NXOpen
Imports NXOpen.Assemblies
Imports NXOpen.UF

Module ReportReferenceSet

    Dim s As Session = Session.GetSession()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = s.ListingWindow
    Dim displaypart As Part = s.Parts.Display

    Sub Main()
        Dim allComp1 As ArrayList = New ArrayList
        '   lw.Open()
        Dim root As Component = s.Parts.Display.ComponentAssembly.RootComponent
        getAllComponents2(root, allComp1)
        Dim referenceSet1 As String = "Entire Part"
        Dim referenceSet2 As String = "S"
        '   reportComponentReferenceSet(allComp1, referenceSet1)
        ChangeComponentReferenceSet(allComp1, referenceSet1, referenceSet2)
    End Sub
    Sub reportComponentReferenceSet(ByVal allComp1 As ArrayList, ByVal referenceSet1 As String)
        For Each comp As Component In allComp1
            If comp.ReferenceSet = referenceSet1 Then
                lw.WriteLine(comp.Name & " " & comp.ReferenceSet)
            End If
        Next
    End Sub
    Sub ChangeComponentReferenceSet(ByVal allComp1 As ArrayList, ByVal referenceSet1 As String, _
                                    ByVal referenceSet2 As String)
        Dim errorList1 As ErrorList
        Dim comp1(0) As Component
        For Each comp As Component In allComp1

            If comp.ReferenceSet = referenceSet1 Then
                comp1(0) = comp
                errorList1 = displaypart.ComponentAssembly.ReplaceReferenceSetInOwners(referenceSet2, comp1)
            End If
        Next
    End Sub
    Sub getAllComponents2(ByVal comp As Component, ByRef allComp As ArrayList)
        Dim child As Component = Nothing
        Dim space As String = Nothing
        For Each child In comp.GetChildren()
            allComp.Add(child)
            getAllComponents2(child, allComp)
        Next
    End Sub
    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        Return Session.LibraryUnloadOption.Immediately
    End Function

End Module

Regards

Frank Swinkels
 
Below is your reply,

It is possible to do what you want. You could have an icon for each of the 5 part files that you use daily. What it would need is a simple program for the toolbar such that the part is opened. Alternatively you can have a single icon which start a program from which you would select a part file to open.

I have attached two images. Image1.jpg shows the selection dialog for 5 parts which whould run from the added file open icon on image2.jpg. The five blank icons would be separate programs to open parts.

Frank Swinkels


I need program for image 1, please advice and thanks.
 
HI
I want to run a journal, Which will create one reference set and will move all the components to the same reference set.

Example:
If I modeled an assembly which contains 20 sub assemblies. After completing my model I need the following with a single click. (If it is possible please guide me)

My Need:
I need a journal to create Reference Set name as "Assembly", after creating reference set it should move all the assembly components to the same "Assembly" reference set.

Thanks in Advance.

 
Regarding the open part from icon this takes a few steps since it involves some system setup as the journal/program consists of to files. Leaving it for the moment as a journal when you run the attached journal SelectPart3.vb it needs to find a dialog file SelectPart3.dlg. For the journal to find this dialog file it must be located in a directory where it can be found. NX allows several ways of setting this up. Here is but one.

Set up a directory such as

C:\myprograms Instead of myprograms you can have whatever you want.

Create next level directories as

C:\myprograms\application
C:\myprograms\startup

In your UGII_BASE_DIR\ugii\menus directory you have a file custom_dirs.dat Edit this file and add a line

C:\myprograms
Finally create an environment variable

UGII_CUSTOM_DIRECTORY_FILE
which points to your UGII_BASE_DIR\ugii\menus\custom_dirs.dat file

If all is setup correctly then when you run the journal it will display the dialog. If it is not set up correctly you will get a message that the dialog cannot be found.

Remember that at this point the program does not do anything yet. First you need to have it set up correctly then we need to add the toolbar and finally you will need to supply the file names and full file paths.

On the other issue regarding assembly reference sets I suggest you look at the thread. I have nothing to add.

Frank Swinkels
 
 http://files.engineering.com/getfile.aspx?folder=089c63c9-c6bd-4f91-9bc9-d636000c4151&file=SelectPart3Tues5March2013.zip
thanks for your reply..

I will come back to you with result
 
Regarding assembly reference set. I need the changes to the below journal. Instead of replacing reference set Entire part to some other reference set. can we do the modifications to this journal. so, that what ever default reference set it may contain to the assyembly components, all components should move automatically to one reference set (example: say "Toplevel") with single click.



Option Strict Off

Imports System
Imports System.Collections
Imports NXOpen
Imports NXOpen.Assemblies
Imports NXOpen.UF

Module ReportReferenceSet

Dim s As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = s.ListingWindow
Dim displaypart As Part = s.Parts.Display

Sub Main()
Dim allComp1 As ArrayList = New ArrayList
' lw.Open()
Dim root As Component = s.Parts.Display.ComponentAssembly.RootComponent
getAllComponents2(root, allComp1)
Dim referenceSet1 As String = "Entire Part"
Dim referenceSet2 As String = "S"
' reportComponentReferenceSet(allComp1, referenceSet1)
ChangeComponentReferenceSet(allComp1, referenceSet1, referenceSet2)
End Sub
Sub reportComponentReferenceSet(ByVal allComp1 As ArrayList, ByVal referenceSet1 As String)
For Each comp As Component In allComp1
If comp.ReferenceSet = referenceSet1 Then
lw.WriteLine(comp.Name & " " & comp.ReferenceSet)
End If
Next
End Sub
Sub ChangeComponentReferenceSet(ByVal allComp1 As ArrayList, ByVal referenceSet1 As String, _
ByVal referenceSet2 As String)
Dim errorList1 As ErrorList
Dim comp1(0) As Component
For Each comp As Component In allComp1

If comp.ReferenceSet = referenceSet1 Then
comp1(0) = comp
errorList1 = displaypart.ComponentAssembly.ReplaceReferenceSetInOwners(referenceSet2, comp1)
End If
Next
End Sub
Sub getAllComponents2(ByVal comp As Component, ByRef allComp As ArrayList)
Dim child As Component = Nothing
Dim space As String = Nothing
For Each child In comp.GetChildren()
allComp.Add(child)
getAllComponents2(child, allComp)
Next
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function

End Module



Regards,
Sam
 
Hi Frank,

Now I will try to explain you better about my reference set requirements (Please see the attachments)

I have attached one model images for example:

A model contains 5 sub level assemblies and each sub assembly contain there individual reference sets say model ( Please See image 1). Now I will create a new reference set say T/L (Top Level) and i will move all sub assembly components to that "T/L" reference set. (Please See Image 2 & 3)

Now my requirement is Can we do the above procedure automatically. That i am using below journal which was recorded through NX, This journal is working for current model only. But, I need this journal to use for any model or any assembly part. (which creates new reference set and should move all components to that reference set). Please guide me. I hope so, you understood my requirement. Thanks for your patience.




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




 
 http://files.engineering.com/getfile.aspx?folder=006a7137-4f51-4e78-9764-a0ff0910272a&file=Images.zip
Hi Frank,

Please look at my above issue.


Thanks & Regards,
Sam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor