Submitted by Buczko Janos on Wed, 04/15/2020 - 04:35
Forums:
Hello,
I am beginner and I would need help. I have an assembly with several parts. These parts have some attributes. I would like to make a journal which provides possibility to select an attribute and then all parts are selected which have selected attribute. Can anyone support me in it, please?
Many Thanks
Janos
re: selection by attribute
What version of NX are you using?
There are functions available in NX 11 and later that make this easier.
Good news, I am using NX 12
Good news, I am using NX 12 verison.
re: select components
Try the following code. It will look through all the components in the current display part and select any with an attribute named "TEST". The attribute name is hard coded in the journal; edit the journal to look for your desired attribute before running it.
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Module Module3
ReadOnly theSession As Session = Session.GetSession()
ReadOnly theUfSession As UFSession = UFSession.GetUFSession()
ReadOnly theUI As UI = UI.GetUI()
Dim lw As ListingWindow = theSession.ListingWindow
'*********************************************
'* change "TEST" to the desired attribute name
Const findAttributeTitle As String = "TEST"
'*********************************************
Sub Main()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If
lw.Open()
If IsNothing(theSession.Parts.Display.ComponentAssembly.RootComponent) Then
'no components to process
Return
End If
Dim foundComponents As New List(Of Assemblies.Component)
theUI.SelectionManager.ClearGlobalSelectionList()
Try
Dim c As Assemblies.ComponentAssembly = theSession.Parts.Display.ComponentAssembly
processComponentChildren(c.RootComponent, foundComponents)
Catch e As Exception
theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
End Try
lw.Close()
theUI.SelectionManager.RequestSelections(foundComponents.ToArray)
lw.WriteLine("number of selected components: " & theUI.SelectionManager.GetNumSelectedObjects.ToString)
End Sub
Sub processComponentChildren(ByVal comp As Assemblies.Component, ByRef componentList As List(Of Assemblies.Component))
For Each child As Assemblies.Component In comp.GetChildren()
If child.HasUserAttribute(findAttributeTitle, NXObject.AttributeType.Any, -1) Then
componentList.Add(child)
End If
processComponentChildren(child, componentList)
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
It seems to work well. I need
It seems to work well. I need to extract little bit the code, but it is exactly what I wanted.
Thank you
Could we select by attribute
Could we select by attribute value as well? For now it selects based on attribute title, but I would like to select based on attribute value.
Unfortunately it is not enough to modify the findAttributeTitle to findAttributeValue. Could you help me please?
Many thanks in advance
re: select by attribute
There is some code here that shows how to get the attribute value:
http://www.nxjournaling.com/comment/2655#comment-2655