Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Journal Help - Trying to Select Component and add attributes 1

Status
Not open for further replies.

Kenja824

Automotive
Nov 5, 2014
949
A while back I combined two codes I had and I thought it was working. Nobody ever mentioned it wasnt working right so now that I needed it for myself, I find it isnt working.

When in an assembly, I want to execute a journal and it will request the user to select a component and hit OK. Then a box will pop-up with certain attributes for the user to fill out. When the user hits OK again, it will add those attributes to that component at the assembly level.

Currently I find this code to request the user to select a component and fill out the attributes, but it is adding the attributes to the assembly properties and not the component properties.

Can someone help me fix this so it adds the attributes to the selected component please?

NOTE**** -- This journal calls a DLX file. I am unsure why this is necessary. It is something someone created for me once. I do know when I have changed this to add different attributes in the past, I had to find them in the DLX file and change them there as well. Both files are attached in a zip folder in case it helps.



This is the Journal code.....

Code:
'==============================================================================
'  WARNING!!  This file is overwritten by the Block UI Styler while generating
'  the automation code. Any modifications to this file will be lost after
'  generating the code again.
'
'       Filename:  checkAttributes.vb
'
'        This file was generated by the NX Block UI Styler
'        Created by: vegetagaru
'              Version: NX 9
'              Date: 
'              Time: 
'
'==============================================================================

'==============================================================================
'  Purpose:  This TEMPLATE file contains VB.NET source to guide you in the
'  construction of your Block application dialog. The generation of your
'  dialog file (.dlx extension) is the first step towards dialog construction
'  within NX.  You must now create a NX Open application that
'  utilizes this file (.dlx).
'
'  The information in this file provides you with the following:
'
'  1.  Help on how to load and display your Block UI Styler dialog in NX
'      using APIs provided in NXOpen.BlockStyler namespace
'  2.  The empty callback methods (stubs) associated with your dialog items
'      have also been placed in this file. These empty methods have been
'      created simply to start you along with your coding requirements.
'      The method name, argument list and possible return values have already
'      been provided for you.
'==============================================================================

'------------------------------------------------------------------------------
'These imports are needed for the following template code
'------------------------------------------------------------------------------
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.BlockStyler
Imports System.Globalization

'------------------------------------------------------------------------------
'Represents Block Styler application class
'------------------------------------------------------------------------------
Public Class checkAttributes
    'class members
    Private Shared theSession As Session = Session.GetSession()
    Private Shared theUFSession As UFSession = UFSession.GetUFSession()
    Private Shared theUFSSession As UFSession = UFSession.GetUFSession()
    Private Shared theUI As UI = UI.GetUI()
    Private theDlxFileName As String
    Private theDialog As NXOpen.BlockStyler.BlockDialog
    Private groupAttr As NXOpen.BlockStyler.Group ' Block type: Group
    Private SPOT_ID As NXOpen.BlockStyler.StringBlock ' Block type: String
    Private ZONE_STA_ROBOT As NXOpen.BlockStyler.StringBlock ' Block type: String
    Private BLANK4 As NXOpen.BlockStyler.StringBlock ' Block type: String
    Private BLANK3 As NXOpen.BlockStyler.StringBlock ' Block type: String
    Private BLANK2 As NXOpen.BlockStyler.StringBlock ' Block type: String
    Private BLANK1 As NXOpen.BlockStyler.StringBlock ' Block type: String

    Private workPart As Part = theSession.Parts.Work
    Private displayPart As Part = theSession.Parts.Display
    Private lw As ListingWindow = theSession.ListingWindow

    Private bodyPart As Part = theSession.Parts.Display

#Region "Block Styler Dialog Designer generator code"
    '------------------------------------------------------------------------------
    'Constructor for NX Styler class
    '------------------------------------------------------------------------------
    Public Sub New()

        '**************************************************************************************
        '**************************************************************************************
        '       lw.Open()

        Dim theComponent As Assemblies.Component = Nothing

        If SelectComponent("select a component", theComponent) = Selection.Response.Cancel Then
            Return
        End If

        '     lw.WriteLine("component display name: " & theComponent.DisplayName)
        '     lw.WriteLine("component part path: " & theComponent.Prototype.OwningPart.FullPath)

        '    Const testAttribute As String = "thickness"

        'Dim thicknessAtt As NXObject.AttributeInformation = Nothing
        'If theComponent.HasUserAttribute(testAttribute, NXObject.AttributeType.Any, -1) Then
        '    thicknessAtt = theComponent.GetUserAttribute(testAttribute, NXObject.AttributeType.Any, -1)
        '    lw.WriteLine("""" & testAttribute & """" & " attribute found")
        '    lw.WriteLine("  attribute value: " & thicknessAtt.StringValue)
        'Else
        '    lw.WriteLine("component does NOT have an attribute named: " & testAttribute)
        'End If

        '      lw.Close()
        '**************************************************************************************
        '**************************************************************************************

        Try
            theDlxFileName = "R:\hms_tools\NX1872\Tooling\Journals\CYL_CODE_ATTR\checkAttributes.dlx"
            theDialog = theUI.CreateDialog(theDlxFileName)
            theDialog.AddApplyHandler(AddressOf apply_cb)
            theDialog.AddOkHandler(AddressOf ok_cb)
            theDialog.AddUpdateHandler(AddressOf update_cb)
            theDialog.AddInitializeHandler(AddressOf initialize_cb)
            theDialog.AddDialogShownHandler(AddressOf dialogShown_cb)

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            Throw ex
        End Try
    End Sub
#End Region

    '------------------------------- DIALOG LAUNCHING ---------------------------------
    '
    '    Before invoking this application one needs to open any part/empty part in NX
    '    because of the behavior of the blocks.
    '
    '    Make sure the dlx file is in one of the following locations:
    '        1.) From where NX session is launched
    '        2.) $UGII_USER_DIR/application
    '        3.) For released applications, using UGII_CUSTOM_DIRECTORY_FILE is highly
    '            recommended. This variable is set to a full directory path to a file 
    '            containing a list of root directories for all custom applications.
    '            e.g., UGII_CUSTOM_DIRECTORY_FILE=$UGII_ROOT_DIR\menus\custom_dirs.dat
    '
    '    You can create the dialog using one of the following way:
    '
    '    1. Journal Replay
    '
    '        1) Replay this file through Tool->Journal->Play Menu.
    '
    '    2. USER EXIT
    '
    '        1) Create the Shared Library -- Refer "Block UI Styler programmer's guide"
    '        2) Invoke the Shared Library through File->Execute->NX Open menu.
    '
    '------------------------------------------------------------------------------
    Public Shared Sub Main()
        Dim thecheckAttributes As checkAttributes = Nothing

        Try
            If check_for_missing_display_part() = 0 Then
                thecheckAttributes = New checkAttributes()
                ' The following method shows the dialog immediately
                thecheckAttributes.Show()
            End If


        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
        Finally
            If thecheckAttributes IsNot Nothing Then
                thecheckAttributes.Dispose()
                thecheckAttributes = Nothing
            End If
        End Try
    End Sub
    '**************************************************************************************
    '**************************************************************************************
    Function SelectComponent(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select a component"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim cursor As Point3d
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
        Dim selectionMask_array(0) As Selection.MaskTriple

        With selectionMask_array(0)
            .Type = UFConstants.UF_component_type
            .Subtype = UFConstants.UF_component_subtype
        End With

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt,
        title, scope, selAction,
        includeFeatures, keepHighlighted, selectionMask_array,
        selObj, cursor)
        If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function



    '------------------------------------------------------------------------------
    ' This method specifies how a shared image is unloaded from memory
    ' within NX. This method gives you the capability to unload an
    ' internal NX Open application or user  exit from NX. Specify any
    ' one of the three constants as a return value to determine the type
    ' of unload to perform:
    '
    '
    '    Immediately : unload the library as soon as the automation program has completed
    '    Explicitly  : unload the library from the "Unload Shared Image" dialog
    '    AtTermination : unload the library when the NX session terminates
    '
    '
    ' NOTE:  A program which associates NX Open applications with the menubar
    ' MUST NOT use this option since it will UNLOAD your NX Open application image
    ' from the menubar.
    '------------------------------------------------------------------------------
    Public Shared Function GetUnloadOption(ByVal arg As String) As Integer
        'Return CType(Session.LibraryUnloadOption.Explicitly, Integer)
        Return CType(Session.LibraryUnloadOption.Immediately, Integer)
        ' Return CType(Session.LibraryUnloadOption.AtTermination, Integer)
    End Function
    '------------------------------------------------------------------------------
    ' Following method cleanup any housekeeping chores that may be needed.
    ' This method is automatically called by NX.
    '------------------------------------------------------------------------------
    Public Shared Sub UnloadLibrary(ByVal arg As String)
        Try

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
        End Try
    End Sub

    '------------------------------------------------------------------------------
    'This method shows the dialog on the screen
    '------------------------------------------------------------------------------
    Public Sub Show()
        Try

            theDialog.Show()

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
        End Try
    End Sub

    '------------------------------------------------------------------------------
    'Method Name: Dispose
    '------------------------------------------------------------------------------
    Public Sub Dispose()
        If theDialog IsNot Nothing Then
            theDialog.Dispose()
            theDialog = Nothing
            displayPart.Views.Refresh()
            theSession.Parts.SetWork(workPart)
        End If
    End Sub

    '------------------------------------------------------------------------------
    '---------------------Block UI Styler Callback Functions--------------------------
    '------------------------------------------------------------------------------

    '------------------------------------------------------------------------------
    'Callback Name: initialize_cb
    '------------------------------------------------------------------------------
    Public Sub initialize_cb()
        Try

            groupAttr = CType(theDialog.TopBlock.FindBlock("groupAttr"), NXOpen.BlockStyler.Group)
            SPOT_ID = CType(theDialog.TopBlock.FindBlock("SPOT_ID"), NXOpen.BlockStyler.StringBlock)
            ZONE_STA_ROBOT = CType(theDialog.TopBlock.FindBlock("ZONE_STA_ROBOT"), NXOpen.BlockStyler.StringBlock)
            BLANK4 = CType(theDialog.TopBlock.FindBlock("BLANK4"), NXOpen.BlockStyler.StringBlock)
            BLANK3 = CType(theDialog.TopBlock.FindBlock("BLANK3"), NXOpen.BlockStyler.StringBlock)
            BLANK2 = CType(theDialog.TopBlock.FindBlock("BLANK2"), NXOpen.BlockStyler.StringBlock)
            BLANK1 = CType(theDialog.TopBlock.FindBlock("BLANK1"), NXOpen.BlockStyler.StringBlock)

            theSession.Parts.SetWork(displayPart)
            displayPart.Views.Refresh()

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
        End Try
    End Sub

    '------------------------------------------------------------------------------
    'Callback Name: dialogShown_cb
    'This callback is executed just before the dialog launch. Thus any value set 
    'here will take precedence and dialog will be launched showing that value. 
    '------------------------------------------------------------------------------
    Public Sub dialogShown_cb()
        Try

            '---- Enter your callback code here -----

            SPOT_ID.Value = readAttribute(workPart, "SPOT_ID")
            If SPOT_ID.Value = "" Then SPOT_ID.Value = "---"

            ZONE_STA_ROBOT.Value = readAttribute(workPart, "ZONE_STA_ROBOT")
            If ZONE_STA_ROBOT.Value = "" Then ZONE_STA_ROBOT.Value = "---"

            BLANK4.Value = readAttribute(workPart, "BLANK4")
            If BLANK4.Value = "" Then BLANK4.Value = "---"

            BLANK3.Value = readAttribute(workPart, "BLANK3")
            If BLANK3.Value = "" Then BLANK3.Value = "---"

            BLANK2.Value = readAttribute(workPart, "BLANK2")
            If BLANK2.Value = "" Then BLANK2.Value = "---"

            BLANK1.Value = readAttribute(workPart, "BLANK1")
            If BLANK1.Value = "" Then BLANK1.Value = "---"

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
        End Try
    End Sub

    '------------------------------------------------------------------------------
    'Callback Name: apply_cb
    '------------------------------------------------------------------------------
    Public Function apply_cb() As Integer
        Dim errorCode As Integer = 0
        Try

            '---- Enter your callback code here -----

            If SPOT_ID.Value.ToUpper = "---" Then SPOT_ID.Value = ""
            If ZONE_STA_ROBOT.Value.ToUpper = "---" Then ZONE_STA_ROBOT.Value = ""
            If BLANK4.Value.ToUpper = "---" Then BLANK4.Value = ""
            If BLANK3.Value.ToUpper = "---" Then BLANK3.Value = ""
            If BLANK2.Value.ToUpper = "---" Then BLANK2.Value = ""
            If BLANK1.Value.ToUpper = "---" Then BLANK1.Value = ""

            Dim theObjects(0) As NXObject
            theObjects(0) = bodyPart
            createAttribute(workPart, theObjects, "SPOT_ID", SPOT_ID.Value)
            createAttribute(workPart, theObjects, "ZONE_STA_ROBOT", ZONE_STA_ROBOT.Value)
            createAttribute(workPart, theObjects, "BLANK4", BLANK4.Value)
            createAttribute(workPart, theObjects, "BLANK3", BLANK3.Value)
            createAttribute(workPart, theObjects, "BLANK2", BLANK2.Value)
            createAttribute(workPart, theObjects, "BLANK1", BLANK1.Value)



        Catch ex As Exception

            '---- Enter your exception handling code here -----
            errorCode = 1
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
        End Try
        apply_cb = errorCode
    End Function

    '------------------------------------------------------------------------------
    'Callback Name: update_cb
    '------------------------------------------------------------------------------
    Public Function update_cb(ByVal block As NXOpen.BlockStyler.UIBlock) As Integer
        Try

            If block Is SPOT_ID Then
                '---- Enter your code here -----

            ElseIf block Is ZONE_STA_ROBOT Then


            ElseIf block Is BLANK4 Then
                '---- Enter your code here -----

            ElseIf block Is BLANK3 Then
                '---- Enter your code here -----

            ElseIf block Is BLANK2 Then
                '---- Enter your code here -----

            ElseIf block Is BLANK1 Then
                '---- Enter your code here -----

            End If
        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
        End Try
        update_cb = 0
    End Function

    '------------------------------------------------------------------------------
    'Callback Name: ok_cb
    '------------------------------------------------------------------------------
    Public Function ok_cb() As Integer
        Dim errorCode As Integer = 0
        Try

            '---- Enter your callback code here -----
            errorCode = apply_cb()

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            errorCode = 1
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
        End Try
        ok_cb = errorCode
    End Function

    '------------------------------------------------------------------------------
    'Function Name: GetBlockProperties
    'Returns the propertylist of the specified BlockID
    '------------------------------------------------------------------------------
    Public Function GetBlockProperties(ByVal blockID As String) As PropertyList
        GetBlockProperties = Nothing
        Try

            GetBlockProperties = theDialog.GetBlockProperties(blockID)

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
        End Try
    End Function
    Public Shared Function check_for_missing_display_part() As Integer

        Dim dispPart As Part = Nothing

        dispPart = Session.GetSession().Parts.Display

        If dispPart Is Nothing Then
            Dim style As MsgBoxStyle
            Dim title As String = "Check for Displayed part"
            style = MsgBoxStyle.Information
            MsgBox("There is no displayed part   ", style, title)

            Return 1
        Else
            Return 0
        End If

    End Function
    Private Function readAttribute(ByVal selPart As Part, ByVal attrName As String) As String

        Dim attrValue As String = Nothing
        Dim iRes As Integer
        Dim attrInfor As NXOpen.NXObject.AttributeInformation = Nothing
        Dim attrtype As NXOpen.NXObject.AttributeType = NXObject.AttributeType.String

        theUFSession.Attr.FindAttribute(selPart.Tag, 5, attrName, iRes)
        If iRes <> 0 Then
            attrInfor = selPart.GetUserAttribute(attrName, attrtype, 2)
            attrValue = attrInfor.StringValue
        Else
            attrValue = "---"
        End If

        Return attrValue

    End Function
    '------------------------------------------------------------------------------
    'User Functions
    '------------------------------------------------------------------------------
    Private Sub createAttribute(ByVal thisPart As Part, ByVal theObjects() As NXObject, ByVal attrTitle As String, ByVal attrValue As String, Optional attrCategory As String = "")

        Dim attributePropertiesBuilder1 As AttributePropertiesBuilder = theSession.AttributeManager.CreateAttributePropertiesBuilder(thisPart, theObjects, AttributePropertiesBuilder.OperationType.Create)

        attributePropertiesBuilder1.IsArray = False
        attributePropertiesBuilder1.Category = attrCategory
        attributePropertiesBuilder1.Title = attrTitle
        attributePropertiesBuilder1.StringValue = attrValue
        attributePropertiesBuilder1.Commit()
        attributePropertiesBuilder1.Destroy()

    End Sub

End Class
 
 https://files.engineering.com/getfile.aspx?folder=2759c59f-f2e4-4514-8e93-f2a08a3f1233&file=Cylinder_Attributes.zip
Replies continue below

Recommended for you

Cowski, once helped me with this problem, or a near identical problem on and old thread. This was the advice he gave me....

Cowski said:
The function "apply_cb" calls the "createAttribute" sub and passes in "bodyPart" as the target; "bodyPart" is just another reference to the current display part (most likely your top level assembly). In these calls to "createAttribute", you will need to pass in your component of interest. Which leads us to the component variable: "theComponent" is currently declared inside of sub "New", which means when sub New is done executing, "theComponent" will go out of scope and will no longer be available for use. I suggest moving the variable declaration to the 'class member' section (after the class declaration, but before Sub New); this will make it available anywhere in the class. Keep the call to "SelectComponent" where it is in Sub New.

My guess is I messed up somewhere. I am thinking it is in this instruction.....

moving the variable declaration to the 'class member' section (after the class declaration, but before Sub New)

As I suck with code, in being honest, I dont know what "variable declaration" is. So I am thinking I must not have done this part? Can someone explain that to me better?


I think this might be what I was supposed to do??....

variable_kluc3n.jpg


Unfortunately it did not fix the problem. So I am guessing I didnt get it right. lol
 
Hello Kenja824,

Your journal is long and today I haven't time for look at it.
I add part of my old journal for add attribute to selected part of assembly.
Maybe it's help You.
Code:
Option Strict Off
Imports System
Imports System.IO
Imports System.Xml
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.Features
Imports NXOpenUI
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports System.Math
Imports System.Windows.Forms
Imports System.Collections



Module NXJournal
    Public ufs As UFSession = UFSession.GetUFSession()
    Public theUI As UI = UI.GetUI
    Public theSession As Session = Session.GetSession()
    Public workPart As Part = theSession.Parts.Work
    Public displayPart As Part = theSession.Parts.Display
    Public lw As ListingWindow = theSession.ListingWindow
    Public alreadyProcessed As Hashtable


    Sub Main(ByVal args() As String)
        Dim part1 As Part = theSession.Parts.Work
        Dim origPart As Part = part1

        Dim markId3 As Session.UndoMarkId
        markId3 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Object Display")
        Dim theUFS As NXOpen.UF.UFSession = NXOpen.UF.UFSession.GetUFSession()
        Dim IsTcEng As Boolean = False
        theUFS.UF.IsUgmanagerActive(IsTcEng)
        lw.Open()

        Dim ZbiorComp() As TaggedObject

        If SelectComponents("Wybierz elementy do zmiany atrybutów", ZbiorComp) = Selection.Response.Cancel Then
            Exit Sub
        End If

        alreadyProcessed = New Hashtable

        Dim allComp As ArrayList = New ArrayList
        Dim root As NXOpen.Assemblies.Component = theSession.Parts.Display.ComponentAssembly.RootComponent
        getAllComponents2(root, allComp)

        For Each komponent As NXOpen.Assemblies.Component In ZbiorComp

            displayPart.ModelingViews.WorkView.UpdateDisplay()
            Dim prototype As Part
            If TypeOf komponent.Prototype Is Part Then
                prototype = CType(komponent.Prototype, Part)
                workPart = prototype
                If Not alreadyProcessed.Contains(prototype) Then
                    ' Add this prototype to the hash table so that we don't process it again
                    alreadyProcessed.Add(prototype, prototype)
                    Dim text As String = InputBox("give me text of attribute TEST")

                    workPart.SetAttribute("TEST", text)  'old method
                    workPart.SetUserAttribute("TEST1", -1, text, Update.Option.Now) 'new method

                End If
            End If
        Next

        lw.Close()
    End Sub

    Function SelectComponents(ByVal prompt As String, ByRef selObj() As TaggedObject) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select components"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
        Dim selectionMask_array(0) As Selection.MaskTriple

        With selectionMask_array(0)
            .Type = UFConstants.UF_component_type
            .Subtype = UFConstants.UF_all_subtype
        End With

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt,
            title, scope, selAction,
            includeFeatures, keepHighlighted, selectionMask_array,
            selObj)
        If resp = Selection.Response.Ok Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function

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

End Module

Best regards

MANok
NX12
TX10
 
Thanks MANok

I appreciate the reply. They gave me something more pressing to do so I will get into this again later today I hope.

Yes it is long and to be honest, I get the feeling I dont even need a lot of it. I think when someone gave it to me, there was an added feature of looking for a specific attribute first, which I dont need to do anymore with this. Now I just need to be able to have the user select a component, have the pop-up offer specific attributes for them to fill out and when they hit OK it adds those attributes to the components at the assembly level. I hope any extra stuff thats in there wont cause problems.
 
Found the problem!

In this function....

Public Function apply_cb() As Integer

I had to change....

theObjects(0) = bodyPart

to

theObjects(0) = theComponent

Once I did that and ran the code, it added the attribute to the component at the assembly level just as I wanted.
 
Though I have figured out the original problem, there is still a couple minor issues I would like to fix if anyone could please tell me how.


Currently when I hit my button, the first box will appear and I will select the component and hit OK....

cyl-code1_bcgpsc.png


However, two things are happening that are not a big deal but I would like to fix them if I can.
1) The component does not stay highlighted. I would prefer it stayed highlighted until the second box has the OK clicked.
2) If the component already has the attribute, it still shows "---" in the box. I would prefer it showed what the component already had filled out for that attribute. This way the user will know if it is done already.

cyl-code2_xpwmcz.png


If anyone can help with either of these, I would appreciate it.
 
If it helps, I have found that the code is looking for the attribute in the assembly instead of the selected part. I just havent figured out how to change it to find the attribute in the selected component.

My guess is it is in one of these two sections of code....

I keep looking at the first part, where it says "workpart", thinking it should say "selPart" or something, but nothing I try works.

Code:
   Public Sub dialogShown_cb()
        Try

            '---- Enter your callback code here -----

            CYL_CODE.Value = readAttribute(workPart, "CYL_CODE")
            If CYL_CODE.Value = "" Then CYL_CODE.Value = "---"

Code:
   Private Function readAttribute(ByVal selPart As Part, ByVal attrName As String) As String

        Dim attrValue As String = Nothing
        Dim iRes As Integer
        Dim attrInfor As NXOpen.NXObject.AttributeInformation = Nothing
        Dim attrtype As NXOpen.NXObject.AttributeType = NXObject.AttributeType.String

        theUFSession.Attr.FindAttribute(selPart.Tag, 5, attrName, iRes)
        If iRes <> 0 Then
            attrInfor = selPart.GetUserAttribute(attrName, attrtype, 2)
            attrValue = attrInfor.StringValue
        Else
            attrValue = "---"
        End If

        Return attrValue
 
@Kenja824
I don't know blockstyler, so my knowledge is poore about it, but I fixed some problems. I didn't have Your latest code, so I used it from first post.
The basic problem, like You figure out - You process workpart instead of selected part. Secund problem is part responsible to set values - it adds values to component and not to part, so when You switch to displayed part, there wasn't any attributes. I odn't know if it was Your intension or not, I changed it, but Your code is commented, so You can revert it any time.
Below is code fixed by myself.

Code:
'==============================================================================
'  WARNING!!  This file is overwritten by the Block UI Styler while generating
'  the automation code. Any modifications to this file will be lost after
'  generating the code again.
'
'       Filename:  checkAttributes.vb
'
'        This file was generated by the NX Block UI Styler
'        Created by: vegetagaru
'              Version: NX 9
'              Date: 
'              Time: 
'
'==============================================================================

'==============================================================================
'  Purpose:  This TEMPLATE file contains VB.NET source to guide you in the
'  construction of your Block application dialog. The generation of your
'  dialog file (.dlx extension) is the first step towards dialog construction
'  within NX.  You must now create a NX Open application that
'  utilizes this file (.dlx).
'
'  The information in this file provides you with the following:
'
'  1.  Help on how to load and display your Block UI Styler dialog in NX
'      using APIs provided in NXOpen.BlockStyler namespace
'  2.  The empty callback methods (stubs) associated with your dialog items
'      have also been placed in this file. These empty methods have been
'      created simply to start you along with your coding requirements.
'      The method name, argument list and possible return values have already
'      been provided for you.
'==============================================================================

'------------------------------------------------------------------------------
'These imports are needed for the following template code
'------------------------------------------------------------------------------
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.BlockStyler
Imports System.Globalization

'------------------------------------------------------------------------------
'Represents Block Styler application class
'------------------------------------------------------------------------------
Public Class checkAttributes
    'class members
    Private Shared theSession As Session = Session.GetSession()
    Private Shared theUFSession As UFSession = UFSession.GetUFSession()
    Private Shared theUFSSession As UFSession = UFSession.GetUFSession()
    Private Shared theUI As UI = UI.GetUI()
    Private theDlxFileName As String
    Private theDialog As NXOpen.BlockStyler.BlockDialog
    Private groupAttr As NXOpen.BlockStyler.Group ' Block type: Group
    Private SPOT_ID As NXOpen.BlockStyler.StringBlock ' Block type: String
    Private ZONE_STA_ROBOT As NXOpen.BlockStyler.StringBlock ' Block type: String
    Private BLANK4 As NXOpen.BlockStyler.StringBlock ' Block type: String
    Private BLANK3 As NXOpen.BlockStyler.StringBlock ' Block type: String
    Private BLANK2 As NXOpen.BlockStyler.StringBlock ' Block type: String
    Private BLANK1 As NXOpen.BlockStyler.StringBlock ' Block type: String

    Private workPart As Part = theSession.Parts.Work
    Private displayPart As Part = theSession.Parts.Display
    Private lw As ListingWindow = theSession.ListingWindow

    Private bodyPart As Part = theSession.Parts.Display
	Dim theComponent As Assemblies.Component = Nothing	
	Dim MyPart As NXOpen.Part = Nothing
	
#Region "Block Styler Dialog Designer generator code"
    '------------------------------------------------------------------------------
    'Constructor for NX Styler class
    '------------------------------------------------------------------------------
    Public Sub New()



        '**************************************************************************************
        '**************************************************************************************
        '       lw.Open()

        

        If SelectComponent("select a component", theComponent) = Selection.Response.Cancel Then
		
            Return
			
        End If

        '     lw.WriteLine("component display name: " & theComponent.DisplayName)
        '     lw.WriteLine("component part path: " & theComponent.Prototype.OwningPart.FullPath)

        '    Const testAttribute As String = "thickness"

        'Dim thicknessAtt As NXObject.AttributeInformation = Nothing
        'If theComponent.HasUserAttribute(testAttribute, NXObject.AttributeType.Any, -1) Then
        '    thicknessAtt = theComponent.GetUserAttribute(testAttribute, NXObject.AttributeType.Any, -1)
        '    lw.WriteLine("""" & testAttribute & """" & " attribute found")
        '    lw.WriteLine("  attribute value: " & thicknessAtt.StringValue)
        'Else
        '    lw.WriteLine("component does NOT have an attribute named: " & testAttribute)
        'End If

        '      lw.Close()
        '**************************************************************************************
        '**************************************************************************************


		MyPart = CType(theSession.Parts.FindObject(theComponent.displayName), NXOpen.Part)


        Try
		
            theDlxFileName = "R:\hms_tools\NX1872\Tooling\Journals\CYL_CODE_ATTR\checkAttributes.dlx"
            theDialog = theUI.CreateDialog(theDlxFileName)
            theDialog.AddApplyHandler(AddressOf apply_cb)
            theDialog.AddOkHandler(AddressOf ok_cb)
            theDialog.AddUpdateHandler(AddressOf update_cb)
            theDialog.AddInitializeHandler(AddressOf initialize_cb)
            theDialog.AddDialogShownHandler(AddressOf dialogShown_cb)

			Catch ex As Exception

            '---- Enter your exception handling code here -----
            Throw ex
			
        End Try
		
    End Sub
#End Region

    '------------------------------- DIALOG LAUNCHING ---------------------------------
    '
    '    Before invoking this application one needs to open any part/empty part in NX
    '    because of the behavior of the blocks.
    '
    '    Make sure the dlx file is in one of the following locations:
    '        1.) From where NX session is launched
    '        2.) $UGII_USER_DIR/application
    '        3.) For released applications, using UGII_CUSTOM_DIRECTORY_FILE is highly
    '            recommended. This variable is set to a full directory path to a file 
    '            containing a list of root directories for all custom applications.
    '            e.g., UGII_CUSTOM_DIRECTORY_FILE=$UGII_ROOT_DIR\menus\custom_dirs.dat
    '
    '    You can create the dialog using one of the following way:
    '
    '    1. Journal Replay
    '
    '        1) Replay this file through Tool->Journal->Play Menu.
    '
    '    2. USER EXIT
    '
    '        1) Create the Shared Library -- Refer "Block UI Styler programmer's guide"
    '        2) Invoke the Shared Library through File->Execute->NX Open menu.
    '
    '------------------------------------------------------------------------------


    Public Shared Sub Main()


        Dim thecheckAttributes As checkAttributes = Nothing
		
        Try
		
            If check_for_missing_display_part() = 0 Then
			
                thecheckAttributes = New checkAttributes()
                ' The following method shows the dialog immediately
                thecheckAttributes.Show()
				
            End If


        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        Finally
		
            If thecheckAttributes IsNot Nothing Then
			
                thecheckAttributes.Dispose()
                thecheckAttributes = Nothing
				
            End If
			
        End Try
		
    End Sub
	
    '**************************************************************************************
    '**************************************************************************************
    Function SelectComponent(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select a component"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim cursor As Point3d
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
        Dim selectionMask_array(0) As Selection.MaskTriple

        With selectionMask_array(0)
		
            .Type = UFConstants.UF_component_type
            .Subtype = UFConstants.UF_component_subtype
			
        End With

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, title, scope, selAction, includeFeatures, keepHighlighted, selectionMask_array, selObj, cursor)
		
        If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
		
            Return Selection.Response.Ok
			
        Else
		
            Return Selection.Response.Cancel
			
        End If

    End Function



    '------------------------------------------------------------------------------
    ' This method specifies how a shared image is unloaded from memory
    ' within NX. This method gives you the capability to unload an
    ' internal NX Open application or user  exit from NX. Specify any
    ' one of the three constants as a return value to determine the type
    ' of unload to perform:
    '
    '
    '    Immediately : unload the library as soon as the automation program has completed
    '    Explicitly  : unload the library from the "Unload Shared Image" dialog
    '    AtTermination : unload the library when the NX session terminates
    '
    '
    ' NOTE:  A program which associates NX Open applications with the menubar
    ' MUST NOT use this option since it will UNLOAD your NX Open application image
    ' from the menubar.
    '------------------------------------------------------------------------------
    Public Shared Function GetUnloadOption(ByVal arg As String) As Integer
	
        'Return CType(Session.LibraryUnloadOption.Explicitly, Integer)
        Return CType(Session.LibraryUnloadOption.Immediately, Integer)
        ' Return CType(Session.LibraryUnloadOption.AtTermination, Integer)
		
    End Function
    '------------------------------------------------------------------------------
    ' Following method cleanup any housekeeping chores that may be needed.
    ' This method is automatically called by NX.
    '------------------------------------------------------------------------------
    Public Shared Sub UnloadLibrary(ByVal arg As String)
	
        Try

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        End Try
		
    End Sub

    '------------------------------------------------------------------------------
    'This method shows the dialog on the screen
    '------------------------------------------------------------------------------
    Public Sub Show()
	
        Try

            theDialog.Show()

			Catch ex As Exception

				'---- Enter your exception handling code here -----
				theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        End Try
		
    End Sub

    '------------------------------------------------------------------------------
    'Method Name: Dispose
    '------------------------------------------------------------------------------
    Public Sub Dispose()
	
        If theDialog IsNot Nothing Then
		
            theDialog.Dispose()
            theDialog = Nothing
            displayPart.Views.Refresh()
            theSession.Parts.SetWork(workPart)
			
        End If
		
    End Sub

    '------------------------------------------------------------------------------
    '---------------------Block UI Styler Callback Functions--------------------------
    '------------------------------------------------------------------------------

    '------------------------------------------------------------------------------
    'Callback Name: initialize_cb
    '------------------------------------------------------------------------------
    Public Sub initialize_cb()
	
        Try

            groupAttr = CType(theDialog.TopBlock.FindBlock("groupAttr"), NXOpen.BlockStyler.Group)
            SPOT_ID = CType(theDialog.TopBlock.FindBlock("SPOT_ID"), NXOpen.BlockStyler.StringBlock)
            ZONE_STA_ROBOT = CType(theDialog.TopBlock.FindBlock("ZONE_STA_ROBOT"), NXOpen.BlockStyler.StringBlock)
            BLANK4 = CType(theDialog.TopBlock.FindBlock("BLANK4"), NXOpen.BlockStyler.StringBlock)
            BLANK3 = CType(theDialog.TopBlock.FindBlock("BLANK3"), NXOpen.BlockStyler.StringBlock)
            BLANK2 = CType(theDialog.TopBlock.FindBlock("BLANK2"), NXOpen.BlockStyler.StringBlock)
            BLANK1 = CType(theDialog.TopBlock.FindBlock("BLANK1"), NXOpen.BlockStyler.StringBlock)

            theSession.Parts.SetWork(displayPart)
            displayPart.Views.Refresh()

			Catch ex As Exception

				'---- Enter your exception handling code here -----
				theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        End Try
		
    End Sub

    '------------------------------------------------------------------------------
    'Callback Name: dialogShown_cb
    'This callback is executed just before the dialog launch. Thus any value set 
    'here will take precedence and dialog will be launched showing that value. 
    '------------------------------------------------------------------------------
	
    Public Sub dialogShown_cb()
	
        Try

            '---- Enter your callback code here -----

            SPOT_ID.Value = readAttribute(MyPart, "SPOT_ID")
            If SPOT_ID.Value = "" Then SPOT_ID.Value = "---"

            ZONE_STA_ROBOT.Value = readAttribute(MyPart, "ZONE_STA_ROBOT")
            If ZONE_STA_ROBOT.Value = "" Then ZONE_STA_ROBOT.Value = "---"

            BLANK4.Value = readAttribute(MyPart, "BLANK4")
            If BLANK4.Value = "" Then BLANK4.Value = "---"

            BLANK3.Value = readAttribute(MyPart, "BLANK3")
            If BLANK3.Value = "" Then BLANK3.Value = "---"

            BLANK2.Value = readAttribute(MyPart, "BLANK2")
            If BLANK2.Value = "" Then BLANK2.Value = "---"

            BLANK1.Value = readAttribute(MyPart, "BLANK1")
            If BLANK1.Value = "" Then BLANK1.Value = "---"

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        End Try
		
    End Sub

    '------------------------------------------------------------------------------
    'Callback Name: apply_cb
    '------------------------------------------------------------------------------
    Public Function apply_cb() As Integer
	
        Dim errorCode As Integer = 0
		
        Try

            '---- Enter your callback code here -----

            If SPOT_ID.Value.ToUpper = "---" Then SPOT_ID.Value = ""
            If ZONE_STA_ROBOT.Value.ToUpper = "---" Then ZONE_STA_ROBOT.Value = ""
            If BLANK4.Value.ToUpper = "---" Then BLANK4.Value = ""
            If BLANK3.Value.ToUpper = "---" Then BLANK3.Value = ""
            If BLANK2.Value.ToUpper = "---" Then BLANK2.Value = ""
            If BLANK1.Value.ToUpper = "---" Then BLANK1.Value = ""

            Dim theObjects(0) As NXObject
            'theObjects(0) = bodyPart
			theObjects(0) = theComponent
            'createAttribute(MyPart, theObjects, "SPOT_ID", SPOT_ID.Value)
			'createAttribute(MyPart, theObjects, "ZONE_STA_ROBOT", ZONE_STA_ROBOT.Value)
            'createAttribute(MyPart, theObjects, "BLANK4", BLANK4.Value)
            'createAttribute(MyPart, theObjects, "BLANK3", BLANK3.Value)
            'createAttribute(MyPart, theObjects, "BLANK2", BLANK2.Value)
            'createAttribute(MyPart, theObjects, "BLANK1", BLANK1.Value)
			
			MyPart.SetUserAttribute("SPOT_ID", -1, SPOT_ID.Value, Update.Option.Now) 
			MyPart.SetUserAttribute("ZONE_STA_ROBOT", -1, ZONE_STA_ROBOT.Value, Update.Option.Now) 
			MyPart.SetUserAttribute("BLANK4", -1, BLANK4.Value, Update.Option.Now) 
			MyPart.SetUserAttribute("BLANK3", -1, BLANK3.Value, Update.Option.Now) 
			MyPart.SetUserAttribute("BLANK2", -1, BLANK2.Value, Update.Option.Now) 
			MyPart.SetUserAttribute("BLANK1", -1, BLANK1.Value, Update.Option.Now) 
			

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            errorCode = 1
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        End Try
		
        apply_cb = errorCode
		
    End Function

    '------------------------------------------------------------------------------
    'Callback Name: update_cb
    '------------------------------------------------------------------------------
    Public Function update_cb(ByVal block As NXOpen.BlockStyler.UIBlock) As Integer
	
        Try

            If block Is SPOT_ID Then
                '---- Enter your code here -----

            ElseIf block Is ZONE_STA_ROBOT Then


            ElseIf block Is BLANK4 Then
                '---- Enter your code here -----

            ElseIf block Is BLANK3 Then
                '---- Enter your code here -----

            ElseIf block Is BLANK2 Then
                '---- Enter your code here -----

            ElseIf block Is BLANK1 Then
                '---- Enter your code here -----

            End If
			
        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        End Try
		
        update_cb = 0
		
    End Function

    '------------------------------------------------------------------------------
    'Callback Name: ok_cb
    '------------------------------------------------------------------------------
    Public Function ok_cb() As Integer
	
        Dim errorCode As Integer = 0
		
        Try

            '---- Enter your callback code here -----
            errorCode = apply_cb()

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            errorCode = 1
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        End Try
		
        ok_cb = errorCode
		
    End Function

    '------------------------------------------------------------------------------
    'Function Name: GetBlockProperties
    'Returns the propertylist of the specified BlockID
    '------------------------------------------------------------------------------
    Public Function GetBlockProperties(ByVal blockID As String) As PropertyList
        GetBlockProperties = Nothing
		
        Try

            GetBlockProperties = theDialog.GetBlockProperties(blockID)

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        End Try
		
    End Function
	
	
    Public Shared Function check_for_missing_display_part() As Integer

        Dim dispPart As Part = Nothing

        dispPart = Session.GetSession().Parts.Display

        If dispPart Is Nothing Then
		
            Dim style As MsgBoxStyle
            Dim title As String = "Check for Displayed part"
            style = MsgBoxStyle.Information
            MsgBox("There is no displayed part   ", style, title)

            Return 1
			
        Else
		
            Return 0
			
        End If

    End Function
	
	
    Private Function readAttribute(ByVal selPart As Part, ByVal attrName As String) As String

        Dim attrValue As String = Nothing
        Dim iRes As Integer
        Dim attrInfor As NXOpen.NXObject.AttributeInformation = Nothing
        Dim attrtype As NXOpen.NXObject.AttributeType = NXObject.AttributeType.String

        theUFSession.Attr.FindAttribute(selPart.Tag, 5, attrName, iRes)
		
        If iRes <> 0 Then
		
            attrInfor = selPart.GetUserAttribute(attrName, attrtype, 2)
            attrValue = attrInfor.StringValue
			
        Else
		
            attrValue = "---"
			
        End If

        Return attrValue

    End Function
	
    '------------------------------------------------------------------------------
    'User Functions
    '------------------------------------------------------------------------------
    Private Sub createAttribute(ByVal thisPart As Part, ByVal theObjects() As NXObject, ByVal attrTitle As String, ByVal attrValue As String, Optional attrCategory As String = "")

        Dim attributePropertiesBuilder1 As AttributePropertiesBuilder = theSession.AttributeManager.CreateAttributePropertiesBuilder(thisPart, theObjects, AttributePropertiesBuilder.OperationType.Create)

        attributePropertiesBuilder1.IsArray = False
        attributePropertiesBuilder1.Category = attrCategory
        attributePropertiesBuilder1.Title = attrTitle
        attributePropertiesBuilder1.StringValue = attrValue
        attributePropertiesBuilder1.Commit()
        attributePropertiesBuilder1.Destroy()

    End Sub

End Class

With best regards
Michael
 
Thanks niedzviedz

I just noticed your signature has Michael. Would you rather be referred to as Michael on here?

I made the changes I needed to add the correct attributes now that I have more info from others. lol This works great.

I do have a question though and if it is a big change, dont worry about it. Currently, after filling out the attributes, if one hits "APPLY", that box stays open. This would never be needed as we would always need to select another component. Is there a simple way to make the "APPLY" button start the entire process over?

The user would hit the button to start the journal. It will ask him to select a component. He will select one and hit OK. Then it will pop up with a box with attributes to fill out, just as it all works right now. But after filling the attributes out, he could hit OK and it goes away, or he could hit Apply and it would start over asking him to select the next component.

The closest thing I could find on the internet to this was called recursion or something like that, but I didnt understand how it worked or if it was what I needed. My problem is that I am given a job where I need to do something with Journals once every so many months only. So every time I come back to it, I feel like I am learning it all over again.

These days my brain is a sponge. A sopping wet sponge. When I use it, it seems I lose more information than I soak in.

As I said, this isnt important as it is only the difference of having to hit a button again to do the next component.

 
try this code:

Code:
'==============================================================================
'  WARNING!!  This file is overwritten by the Block UI Styler while generating
'  the automation code. Any modifications to this file will be lost after
'  generating the code again.
'
'       Filename:  checkAttributes.vb
'
'        This file was generated by the NX Block UI Styler
'        Created by: vegetagaru
'              Version: NX 9
'              Date: 
'              Time: 
'
'==============================================================================

'==============================================================================
'  Purpose:  This TEMPLATE file contains VB.NET source to guide you in the
'  construction of your Block application dialog. The generation of your
'  dialog file (.dlx extension) is the first step towards dialog construction
'  within NX.  You must now create a NX Open application that
'  utilizes this file (.dlx).
'
'  The information in this file provides you with the following:
'
'  1.  Help on how to load and display your Block UI Styler dialog in NX
'      using APIs provided in NXOpen.BlockStyler namespace
'  2.  The empty callback methods (stubs) associated with your dialog items
'      have also been placed in this file. These empty methods have been
'      created simply to start you along with your coding requirements.
'      The method name, argument list and possible return values have already
'      been provided for you.
'==============================================================================

'------------------------------------------------------------------------------
'These imports are needed for the following template code
'------------------------------------------------------------------------------
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.BlockStyler
Imports System.Globalization



'------------------------------------------------------------------------------
'Represents Block Styler application class
'------------------------------------------------------------------------------
Public Class checkAttributes
    'class members
    Private Shared theSession As Session = Session.GetSession()
    Private Shared theUFSession As UFSession = UFSession.GetUFSession()
    Private Shared theUFSSession As UFSession = UFSession.GetUFSession()
    Private Shared theUI As UI = UI.GetUI()
    Private theDlxFileName As String
    Private theDialog As NXOpen.BlockStyler.BlockDialog
    Private groupAttr As NXOpen.BlockStyler.Group ' Block type: Group
    Private SPOT_ID As NXOpen.BlockStyler.StringBlock ' Block type: String
    Private ZONE_STA_ROBOT As NXOpen.BlockStyler.StringBlock ' Block type: String
    Private BLANK4 As NXOpen.BlockStyler.StringBlock ' Block type: String
    Private BLANK3 As NXOpen.BlockStyler.StringBlock ' Block type: String
    Private BLANK2 As NXOpen.BlockStyler.StringBlock ' Block type: String
    Private BLANK1 As NXOpen.BlockStyler.StringBlock ' Block type: String

    Private workPart As Part = theSession.Parts.Work
    Private displayPart As Part = theSession.Parts.Display
    Private lw As ListingWindow = theSession.ListingWindow

    Private bodyPart As Part = theSession.Parts.Display
	Dim theComponent As Assemblies.Component = Nothing	
	Dim MyPart As NXOpen.Part = Nothing
	Dim resp1 as integer = 0 
	
	
#Region "Block Styler Dialog Designer generator code"
    '------------------------------------------------------------------------------
    'Constructor for NX Styler class
    '------------------------------------------------------------------------------
    Public Sub New()



        '**************************************************************************************
        '**************************************************************************************
        '       lw.Open()

        
		If SelectComponent("select a component", theComponent) = Selection.Response.Cancel Then
		
           Return
			
        End If

        '     lw.WriteLine("component display name: " & theComponent.DisplayName)
        '     lw.WriteLine("component part path: " & theComponent.Prototype.OwningPart.FullPath)

        '    Const testAttribute As String = "thickness"

        'Dim thicknessAtt As NXObject.AttributeInformation = Nothing
        'If theComponent.HasUserAttribute(testAttribute, NXObject.AttributeType.Any, -1) Then
        '    thicknessAtt = theComponent.GetUserAttribute(testAttribute, NXObject.AttributeType.Any, -1)
        '    lw.WriteLine("""" & testAttribute & """" & " attribute found")
        '    lw.WriteLine("  attribute value: " & thicknessAtt.StringValue)
        'Else
        '    lw.WriteLine("component does NOT have an attribute named: " & testAttribute)
        'End If

        '      lw.Close()
        '**************************************************************************************
        '**************************************************************************************


		MyPart = CType(theSession.Parts.FindObject(theComponent.displayName), NXOpen.Part)


        Try
		
            theDlxFileName = "R:\hms_tools\NX1872\Tooling\Journals\CYL_CODE_ATTR\checkAttributes.dlx"
            theDialog = theUI.CreateDialog(theDlxFileName)
            theDialog.AddApplyHandler(AddressOf apply_cb)
            theDialog.AddOkHandler(AddressOf ok_cb)
            theDialog.AddUpdateHandler(AddressOf update_cb)
            theDialog.AddInitializeHandler(AddressOf initialize_cb)
            theDialog.AddDialogShownHandler(AddressOf dialogShown_cb)

			Catch ex As Exception

            '---- Enter your exception handling code here -----
            Throw ex
			
        End Try
		
    End Sub
#End Region

    '------------------------------- DIALOG LAUNCHING ---------------------------------
    '
    '    Before invoking this application one needs to open any part/empty part in NX
    '    because of the behavior of the blocks.
    '
    '    Make sure the dlx file is in one of the following locations:
    '        1.) From where NX session is launched
    '        2.) $UGII_USER_DIR/application
    '        3.) For released applications, using UGII_CUSTOM_DIRECTORY_FILE is highly
    '            recommended. This variable is set to a full directory path to a file 
    '            containing a list of root directories for all custom applications.
    '            e.g., UGII_CUSTOM_DIRECTORY_FILE=$UGII_ROOT_DIR\menus\custom_dirs.dat
    '
    '    You can create the dialog using one of the following way:
    '
    '    1. Journal Replay
    '
    '        1) Replay this file through Tool->Journal->Play Menu.
    '
    '    2. USER EXIT
    '
    '        1) Create the Shared Library -- Refer "Block UI Styler programmer's guide"
    '        2) Invoke the Shared Library through File->Execute->NX Open menu.
    '
    '------------------------------------------------------------------------------


    Public Shared Sub Main()


        Dim thecheckAttributes As checkAttributes = Nothing
		
        Try
		
            If check_for_missing_display_part() = 0 Then
			
                thecheckAttributes = New checkAttributes()
                ' The following method shows the dialog immediately
                thecheckAttributes.Show()
				
            End If


        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        Finally
		
            If thecheckAttributes IsNot Nothing Then
			
                thecheckAttributes.Dispose()
                thecheckAttributes = Nothing
				
            End If
			
        End Try
		
    End Sub
	
    '**************************************************************************************
    '**************************************************************************************
    Function SelectComponent(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

		Dim resp As Selection.Response = Selection.Response.Cancel
        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select a component"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim cursor As Point3d
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
        Dim selectionMask_array(0) As Selection.MaskTriple

        With selectionMask_array(0)
		
            .Type = UFConstants.UF_component_type
            .Subtype = UFConstants.UF_component_subtype
			
        End With

        'Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, title, scope, selAction, includeFeatures, keepHighlighted, selectionMask_array, selObj, cursor)
		resp = theUI.SelectionManager.SelectTaggedObject(prompt, title, scope, selAction, includeFeatures, keepHighlighted, selectionMask_array, selObj, cursor)
		
        If resp = Selection.Response.ObjectSelected Or resp = Selection.Response.ObjectSelectedByName Then
		
            Return Selection.Response.Ok
			
        Else
		
            Return Selection.Response.Cancel
			
        End If

    End Function



    '------------------------------------------------------------------------------
    ' This method specifies how a shared image is unloaded from memory
    ' within NX. This method gives you the capability to unload an
    ' internal NX Open application or user  exit from NX. Specify any
    ' one of the three constants as a return value to determine the type
    ' of unload to perform:
    '
    '
    '    Immediately : unload the library as soon as the automation program has completed
    '    Explicitly  : unload the library from the "Unload Shared Image" dialog
    '    AtTermination : unload the library when the NX session terminates
    '
    '
    ' NOTE:  A program which associates NX Open applications with the menubar
    ' MUST NOT use this option since it will UNLOAD your NX Open application image
    ' from the menubar.
    '------------------------------------------------------------------------------
    Public Shared Function GetUnloadOption(ByVal arg As String) As Integer
	
        'Return CType(Session.LibraryUnloadOption.Explicitly, Integer)
        Return CType(Session.LibraryUnloadOption.Immediately, Integer)
        ' Return CType(Session.LibraryUnloadOption.AtTermination, Integer)
		
    End Function
    '------------------------------------------------------------------------------
    ' Following method cleanup any housekeeping chores that may be needed.
    ' This method is automatically called by NX.
    '------------------------------------------------------------------------------
    Public Shared Sub UnloadLibrary(ByVal arg As String)
	
        Try

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        End Try
		
    End Sub

    '------------------------------------------------------------------------------
    'This method shows the dialog on the screen
    '------------------------------------------------------------------------------
    Public Sub Show()
	
        Try

            theDialog.Show()

			Catch ex As Exception

				'---- Enter your exception handling code here -----
				theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        End Try
		
    End Sub

    '------------------------------------------------------------------------------
    'Method Name: Dispose
    '------------------------------------------------------------------------------
    Public Sub Dispose()
	
        If theDialog IsNot Nothing Then
		
            theDialog.Dispose()
            theDialog = Nothing
            displayPart.Views.Refresh()
            theSession.Parts.SetWork(workPart)
			
        End If
		
    End Sub

    '------------------------------------------------------------------------------
    '---------------------Block UI Styler Callback Functions--------------------------
    '------------------------------------------------------------------------------

    '------------------------------------------------------------------------------
    'Callback Name: initialize_cb
    '------------------------------------------------------------------------------
    Public Sub initialize_cb()
	
        Try

            groupAttr = CType(theDialog.TopBlock.FindBlock("groupAttr"), NXOpen.BlockStyler.Group)
            SPOT_ID = CType(theDialog.TopBlock.FindBlock("SPOT_ID"), NXOpen.BlockStyler.StringBlock)
            ZONE_STA_ROBOT = CType(theDialog.TopBlock.FindBlock("ZONE_STA_ROBOT"), NXOpen.BlockStyler.StringBlock)
            BLANK4 = CType(theDialog.TopBlock.FindBlock("BLANK4"), NXOpen.BlockStyler.StringBlock)
            BLANK3 = CType(theDialog.TopBlock.FindBlock("BLANK3"), NXOpen.BlockStyler.StringBlock)
            BLANK2 = CType(theDialog.TopBlock.FindBlock("BLANK2"), NXOpen.BlockStyler.StringBlock)
            BLANK1 = CType(theDialog.TopBlock.FindBlock("BLANK1"), NXOpen.BlockStyler.StringBlock)

            theSession.Parts.SetWork(displayPart)
            displayPart.Views.Refresh()

			Catch ex As Exception

				'---- Enter your exception handling code here -----
				theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        End Try
		
    End Sub

    '------------------------------------------------------------------------------
    'Callback Name: dialogShown_cb
    'This callback is executed just before the dialog launch. Thus any value set 
    'here will take precedence and dialog will be launched showing that value. 
    '------------------------------------------------------------------------------
	
    Public Sub dialogShown_cb()
	
        Try

            '---- Enter your callback code here -----

            SPOT_ID.Value = readAttribute(MyPart, "SPOT_ID")
            If SPOT_ID.Value = "" Then SPOT_ID.Value = "---"

            ZONE_STA_ROBOT.Value = readAttribute(MyPart, "ZONE_STA_ROBOT")
            If ZONE_STA_ROBOT.Value = "" Then ZONE_STA_ROBOT.Value = "---"

            BLANK4.Value = readAttribute(MyPart, "BLANK4")
            If BLANK4.Value = "" Then BLANK4.Value = "---"

            BLANK3.Value = readAttribute(MyPart, "BLANK3")
            If BLANK3.Value = "" Then BLANK3.Value = "---"

            BLANK2.Value = readAttribute(MyPart, "BLANK2")
            If BLANK2.Value = "" Then BLANK2.Value = "---"

            BLANK1.Value = readAttribute(MyPart, "BLANK1")
            If BLANK1.Value = "" Then BLANK1.Value = "---"

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        End Try
		
    End Sub

    '------------------------------------------------------------------------------
    'Callback Name: apply_cb
    '------------------------------------------------------------------------------
    Public Function apply_cb() As Integer
		
		Dim response1 As Selection.Response = Selection.Response.Cancel
        Dim errorCode As Integer = 0
		

					
        Try
				
			response1 = SelectComponent("select a component", theComponent)
				
			'---- Enter your callback code here -----

            If SPOT_ID.Value.ToUpper = "---" Then SPOT_ID.Value = ""
            If ZONE_STA_ROBOT.Value.ToUpper = "---" Then ZONE_STA_ROBOT.Value = ""
            If BLANK4.Value.ToUpper = "---" Then BLANK4.Value = ""
            If BLANK3.Value.ToUpper = "---" Then BLANK3.Value = ""
            If BLANK2.Value.ToUpper = "---" Then BLANK2.Value = ""
            If BLANK1.Value.ToUpper = "---" Then BLANK1.Value = ""

            Dim theObjects(0) As NXObject
            'theObjects(0) = bodyPart
			theObjects(0) = theComponent
            'createAttribute(MyPart, theObjects, "SPOT_ID", SPOT_ID.Value)
			'createAttribute(MyPart, theObjects, "ZONE_STA_ROBOT", ZONE_STA_ROBOT.Value)
            'createAttribute(MyPart, theObjects, "BLANK4", BLANK4.Value)
            'createAttribute(MyPart, theObjects, "BLANK3", BLANK3.Value)
            'createAttribute(MyPart, theObjects, "BLANK2", BLANK2.Value)
            'createAttribute(MyPart, theObjects, "BLANK1", BLANK1.Value)
			
			MyPart.SetUserAttribute("SPOT_ID", -1, SPOT_ID.Value, Update.Option.Now) 
			MyPart.SetUserAttribute("ZONE_STA_ROBOT", -1, ZONE_STA_ROBOT.Value, Update.Option.Now) 
			MyPart.SetUserAttribute("BLANK4", -1, BLANK4.Value, Update.Option.Now) 
			MyPart.SetUserAttribute("BLANK3", -1, BLANK3.Value, Update.Option.Now) 
			MyPart.SetUserAttribute("BLANK2", -1, BLANK2.Value, Update.Option.Now) 
			MyPart.SetUserAttribute("BLANK1", -1, BLANK1.Value, Update.Option.Now) 
			

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            errorCode = 1
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        End Try
		
			
        apply_cb = errorCode
		
    End Function

    '------------------------------------------------------------------------------
    'Callback Name: update_cb
    '------------------------------------------------------------------------------
    Public Function update_cb(ByVal block As NXOpen.BlockStyler.UIBlock) As Integer
	
        Try

            If block Is SPOT_ID Then
                '---- Enter your code here -----

            ElseIf block Is ZONE_STA_ROBOT Then


            ElseIf block Is BLANK4 Then
                '---- Enter your code here -----

            ElseIf block Is BLANK3 Then
                '---- Enter your code here -----

            ElseIf block Is BLANK2 Then
                '---- Enter your code here -----

            ElseIf block Is BLANK1 Then
                '---- Enter your code here -----

            End If
			
        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        End Try
		
        update_cb = 0
		
    End Function

    '------------------------------------------------------------------------------
    'Callback Name: ok_cb
    '------------------------------------------------------------------------------
    Public Function ok_cb() As Integer
	
        Dim errorCode As Integer = 0
		
        Try

            '---- Enter your callback code here -----
            'errorCode = apply_cb()

            If SPOT_ID.Value.ToUpper = "---" Then SPOT_ID.Value = ""
            If ZONE_STA_ROBOT.Value.ToUpper = "---" Then ZONE_STA_ROBOT.Value = ""
            If BLANK4.Value.ToUpper = "---" Then BLANK4.Value = ""
            If BLANK3.Value.ToUpper = "---" Then BLANK3.Value = ""
            If BLANK2.Value.ToUpper = "---" Then BLANK2.Value = ""
            If BLANK1.Value.ToUpper = "---" Then BLANK1.Value = ""

            Dim theObjects(0) As NXObject
            'theObjects(0) = bodyPart
			theObjects(0) = theComponent
            'createAttribute(MyPart, theObjects, "SPOT_ID", SPOT_ID.Value)
			'createAttribute(MyPart, theObjects, "ZONE_STA_ROBOT", ZONE_STA_ROBOT.Value)
            'createAttribute(MyPart, theObjects, "BLANK4", BLANK4.Value)
            'createAttribute(MyPart, theObjects, "BLANK3", BLANK3.Value)
            'createAttribute(MyPart, theObjects, "BLANK2", BLANK2.Value)
            'createAttribute(MyPart, theObjects, "BLANK1", BLANK1.Value)
			
			MyPart.SetUserAttribute("SPOT_ID", -1, SPOT_ID.Value, Update.Option.Now) 
			MyPart.SetUserAttribute("ZONE_STA_ROBOT", -1, ZONE_STA_ROBOT.Value, Update.Option.Now) 
			MyPart.SetUserAttribute("BLANK4", -1, BLANK4.Value, Update.Option.Now) 
			MyPart.SetUserAttribute("BLANK3", -1, BLANK3.Value, Update.Option.Now) 
			MyPart.SetUserAttribute("BLANK2", -1, BLANK2.Value, Update.Option.Now) 
			MyPart.SetUserAttribute("BLANK1", -1, BLANK1.Value, Update.Option.Now) 

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            errorCode = 1
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        End Try
		
        ok_cb = errorCode	

	
    End Function

    '------------------------------------------------------------------------------
    'Function Name: GetBlockProperties
    'Returns the propertylist of the specified BlockID
    '------------------------------------------------------------------------------
    Public Function GetBlockProperties(ByVal blockID As String) As PropertyList
        GetBlockProperties = Nothing
		
        Try

            GetBlockProperties = theDialog.GetBlockProperties(blockID)

        Catch ex As Exception

            '---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)
			
        End Try
		
    End Function
	
	
    Public Shared Function check_for_missing_display_part() As Integer

        Dim dispPart As Part = Nothing

        dispPart = Session.GetSession().Parts.Display

        If dispPart Is Nothing Then
		
            Dim style As MsgBoxStyle
            Dim title As String = "Check for Displayed part"
            style = MsgBoxStyle.Information
            MsgBox("There is no displayed part   ", style, title)

            Return 1
			
        Else
		
            Return 0
			
        End If

    End Function
	
	
    Private Function readAttribute(ByVal selPart As Part, ByVal attrName As String) As String

        Dim attrValue As String = Nothing
        Dim iRes As Integer
        Dim attrInfor As NXOpen.NXObject.AttributeInformation = Nothing
        Dim attrtype As NXOpen.NXObject.AttributeType = NXObject.AttributeType.String

        theUFSession.Attr.FindAttribute(selPart.Tag, 5, attrName, iRes)
		
        If iRes <> 0 Then
		
            attrInfor = selPart.GetUserAttribute(attrName, attrtype, 2)
            attrValue = attrInfor.StringValue
			
        Else
		
            attrValue = "---"
			
        End If

        Return attrValue

    End Function
	
    '------------------------------------------------------------------------------
    'User Functions
    '------------------------------------------------------------------------------
    Private Sub createAttribute(ByVal thisPart As Part, ByVal theObjects() As NXObject, ByVal attrTitle As String, ByVal attrValue As String, Optional attrCategory As String = "")

        Dim attributePropertiesBuilder1 As AttributePropertiesBuilder = theSession.AttributeManager.CreateAttributePropertiesBuilder(thisPart, theObjects, AttributePropertiesBuilder.OperationType.Create)

        attributePropertiesBuilder1.IsArray = False
        attributePropertiesBuilder1.Category = attrCategory
        attributePropertiesBuilder1.Title = attrTitle
        attributePropertiesBuilder1.StringValue = attrValue
        attributePropertiesBuilder1.Commit()
        attributePropertiesBuilder1.Destroy()

    End Sub

End Class



With best regards
Michael
 
Absolutely awesome. Works perfect. Thank you very much.

 
Hey niedzviedz;

I have come across a major issue with this code.

Not sure how familiar you are with automotive tooling, but we will create assemblies we call units. One complete tool will have multiple units in it. So basically we can have anywhere from 2 to 50 assemblies that will build up one top level assembly. The top level assembly is called the key sheet and all the assemblies inside that key sheet are called units.

In this particular case, we have a keysheet that is made up of 30 units. We might have ten instances of the same power clamp being used within those units. The designer needs to add two particular attributes to each instance separately. This way we can list what Unit it is found in and which order the cylinders activate.

Currently, if I use this code to give a single instance of a component these attributes, it is giving it to all instances of the component used in all the units. I need it to only give it to the instance of the component that is selected.

When you get time, could you please help me with this?

Also, I have updated the code so it now adds the attributes we need. When we started this, I didnt have that info yet and we were working with examples. I am attaching a zip with the current code and the DLX file it calls.

Sorry for the hassle and thanks



Ken
My brain is like a sponge. A sopping wet sponge. When I use it, I seem to lose more than I soak in.
 
Try this code:

Code:
'==============================================================================
'  WARNING!!  This file is overwritten by the Block UI Styler while generating
'  the automation code. Any modifications to this file will be lost after
'  generating the code again.
'
'	   Filename:  checkAttributes.vb
'
'		This file was generated by the NX Block UI Styler
'		Created by: vegetagaru
'			  Version: NX 9
'			  Date: 
'			  Time: 
'
'==============================================================================

'==============================================================================
'  Purpose:  This TEMPLATE file contains VB.NET source to guide you in the
'  construction of your Block application dialog. The generation of your
'  dialog file (.dlx extension) is the first step towards dialog construction
'  within NX.  You must now create a NX Open application that
'  utilizes this file (.dlx).
'
'  The information in this file provides you with the following:
'
'  1.  Help on how to load and display your Block UI Styler dialog in NX
'	  using APIs provided in NXOpen.BlockStyler namespace
'  2.  The empty callback methods (stubs) associated with your dialog items
'	  have also been placed in this file. These empty methods have been
'	  created simply to start you along with your coding requirements.
'	  The method name, argument list and possible return values have already
'	  been provided for you.
'==============================================================================

'------------------------------------------------------------------------------
'These imports are needed for the following template code
'------------------------------------------------------------------------------
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.BlockStyler
Imports System.Globalization



'------------------------------------------------------------------------------
'Represents Block Styler application class
'------------------------------------------------------------------------------
Public Class checkAttributes
	'class members
	Private Shared theSession As Session = Session.GetSession()
	Private Shared theUFSession As UFSession = UFSession.GetUFSession()
	Private Shared theUFSSession As UFSession = UFSession.GetUFSession()
	Private Shared theUI As UI = UI.GetUI()
	Private theDlxFileName As String
	Private theDialog As NXOpen.BlockStyler.BlockDialog
	Private groupAttr As NXOpen.BlockStyler.Group ' Block type: Group
	Private UNIT_No As NXOpen.BlockStyler.StringBlock ' Block type: String
	Private CYL_CODE As NXOpen.BlockStyler.StringBlock ' Block type: String
	Private BLANK4 As NXOpen.BlockStyler.StringBlock ' Block type: String
	Private BLANK3 As NXOpen.BlockStyler.StringBlock ' Block type: String
	Private BLANK2 As NXOpen.BlockStyler.StringBlock ' Block type: String
	Private BLANK1 As NXOpen.BlockStyler.StringBlock ' Block type: String

	Private workPart As Part = theSession.Parts.Work
	Private displayPart As Part = theSession.Parts.Display
	Private lw As ListingWindow = theSession.ListingWindow

	Private bodyPart As Part = theSession.Parts.Display
	Dim theComponent As Assemblies.Component = Nothing
	Dim MyPart As NXOpen.Part = Nothing
	Dim resp1 As Integer = 0

#Region "Block Styler Dialog Designer generator code"
	'------------------------------------------------------------------------------
	'Constructor for NX Styler class
	'------------------------------------------------------------------------------
	Public Sub New()



		'**************************************************************************************
		'**************************************************************************************
		'	   lw.Open()


		If SelectComponent("select a component", theComponent) = Selection.Response.Cancel Then

			Return

		End If

		'	 lw.WriteLine("component display name: " & theComponent.DisplayName)
		'	 lw.WriteLine("component part path: " & theComponent.Prototype.OwningPart.FullPath)

		'	Const testAttribute As String = "thickness"

		'Dim thicknessAtt As NXObject.AttributeInformation = Nothing
		'If theComponent.HasUserAttribute(testAttribute, NXObject.AttributeType.Any, -1) Then
		'	thicknessAtt = theComponent.GetUserAttribute(testAttribute, NXObject.AttributeType.Any, -1)
		'	lw.WriteLine("""" & testAttribute & """" & " attribute found")
		'	lw.WriteLine("  attribute value: " & thicknessAtt.StringValue)
		'Else
		'	lw.WriteLine("component does NOT have an attribute named: " & testAttribute)
		'End If

		'	  lw.Close()
		'**************************************************************************************
		'**************************************************************************************


		MyPart = CType(theSession.Parts.FindObject(theComponent.displayName), NXOpen.Part)


		Try

			theDlxFileName = "R:\hms_tools\NX1872\Tooling\Journals\CYL_CODE_ATTR\Cyl Attributes.dlx"
			theDialog = theUI.CreateDialog(theDlxFileName)
			theDialog.AddApplyHandler(AddressOf apply_cb)
			theDialog.AddOkHandler(AddressOf ok_cb)
			theDialog.AddUpdateHandler(AddressOf update_cb)
			theDialog.AddInitializeHandler(AddressOf initialize_cb)
			theDialog.AddDialogShownHandler(AddressOf dialogShown_cb)

		Catch ex As Exception

			'---- Enter your exception handling code here -----
			Throw ex

		End Try

	End Sub
#End Region

	'------------------------------- DIALOG LAUNCHING ---------------------------------
	'
	'	Before invoking this application one needs to open any part/empty part in NX
	'	because of the behavior of the blocks.
	'
	'	Make sure the dlx file is in one of the following locations:
	'		1.) From where NX session is launched
	'		2.) $UGII_USER_DIR/application
	'		3.) For released applications, using UGII_CUSTOM_DIRECTORY_FILE is highly
	'			recommended. This variable is set to a full directory path to a file 
	'			containing a list of root directories for all custom applications.
	'			e.g., UGII_CUSTOM_DIRECTORY_FILE=$UGII_ROOT_DIR\menus\custom_dirs.dat
	'
	'	You can create the dialog using one of the following way:
	'
	'	1. Journal Replay
	'
	'		1) Replay this file through Tool->Journal->Play Menu.
	'
	'	2. USER EXIT
	'
	'		1) Create the Shared Library -- Refer "Block UI Styler programmer's guide"
	'		2) Invoke the Shared Library through File->Execute->NX Open menu.
	'
	'------------------------------------------------------------------------------


	Public Shared Sub Main()


		Dim thecheckAttributes As checkAttributes = Nothing

		Try

			If check_for_missing_display_part() = 0 Then

				thecheckAttributes = New checkAttributes()
				' The following method shows the dialog immediately
				thecheckAttributes.Show()

			End If


		Catch ex As Exception

			'---- Enter your exception handling code here -----
			theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)

		Finally

			If thecheckAttributes IsNot Nothing Then

				thecheckAttributes.Dispose()
				thecheckAttributes = Nothing

			End If

		End Try

	End Sub

	'**************************************************************************************
	'**************************************************************************************
	Function SelectComponent(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

		Dim resp As Selection.Response = Selection.Response.Cancel
		Dim theUI As UI = UI.GetUI
		Dim title As String = "Select a component"
		Dim includeFeatures As Boolean = False
		Dim keepHighlighted As Boolean = False
		Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
		Dim cursor As Point3d
		Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
		Dim selectionMask_array(0) As Selection.MaskTriple

		With selectionMask_array(0)

			.Type = UFConstants.UF_component_type
			.Subtype = UFConstants.UF_component_subtype

		End With

		'Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, title, scope, selAction, includeFeatures, keepHighlighted, selectionMask_array, selObj, cursor)
		resp = theUI.SelectionManager.SelectTaggedObject(prompt, title, scope, selAction, includeFeatures, keepHighlighted, selectionMask_array, selObj, cursor)

		If resp = Selection.Response.ObjectSelected Or resp = Selection.Response.ObjectSelectedByName Then

			Return Selection.Response.Ok

		Else

			Return Selection.Response.Cancel

		End If

	End Function



	'------------------------------------------------------------------------------
	' This method specifies how a shared image is unloaded from memory
	' within NX. This method gives you the capability to unload an
	' internal NX Open application or user  exit from NX. Specify any
	' one of the three constants as a return value to determine the type
	' of unload to perform:
	'
	'
	'	Immediately : unload the library as soon as the automation program has completed
	'	Explicitly  : unload the library from the "Unload Shared Image" dialog
	'	AtTermination : unload the library when the NX session terminates
	'
	'
	' NOTE:  A program which associates NX Open applications with the menubar
	' MUST NOT use this option since it will UNLOAD your NX Open application image
	' from the menubar.
	'------------------------------------------------------------------------------
	Public Shared Function GetUnloadOption(ByVal arg As String) As Integer

		'Return CType(Session.LibraryUnloadOption.Explicitly, Integer)
		Return CType(Session.LibraryUnloadOption.Immediately, Integer)
		' Return CType(Session.LibraryUnloadOption.AtTermination, Integer)

	End Function
	'------------------------------------------------------------------------------
	' Following method cleanup any housekeeping chores that may be needed.
	' This method is automatically called by NX.
	'------------------------------------------------------------------------------
	Public Shared Sub UnloadLibrary(ByVal arg As String)

		Try

		Catch ex As Exception

			'---- Enter your exception handling code here -----
			theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)

		End Try

	End Sub

	'------------------------------------------------------------------------------
	'This method shows the dialog on the screen
	'------------------------------------------------------------------------------
	Public Sub Show()

		Try

			theDialog.Show()

		Catch ex As Exception

			'---- Enter your exception handling code here -----
			theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)

		End Try

	End Sub

	'------------------------------------------------------------------------------
	'Method Name: Dispose
	'------------------------------------------------------------------------------
	Public Sub Dispose()

		If theDialog IsNot Nothing Then

			theDialog.Dispose()
			theDialog = Nothing
			displayPart.Views.Refresh()
			theSession.Parts.SetWork(workPart)

		End If

	End Sub

	'------------------------------------------------------------------------------
	'---------------------Block UI Styler Callback Functions--------------------------
	'------------------------------------------------------------------------------

	'------------------------------------------------------------------------------
	'Callback Name: initialize_cb
	'------------------------------------------------------------------------------
	Public Sub initialize_cb()

		Try

			groupAttr = CType(theDialog.TopBlock.FindBlock("groupAttr"), NXOpen.BlockStyler.Group)
			UNIT_No = CType(theDialog.TopBlock.FindBlock("UNIT_No"), NXOpen.BlockStyler.StringBlock)
			CYL_CODE = CType(theDialog.TopBlock.FindBlock("CYL_CODE"), NXOpen.BlockStyler.StringBlock)

			theSession.Parts.SetWork(displayPart)
			displayPart.Views.Refresh()

		Catch ex As Exception

			'---- Enter your exception handling code here -----
			theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)

		End Try

	End Sub

	'------------------------------------------------------------------------------
	'Callback Name: dialogShown_cb
	'This callback is executed just before the dialog launch. Thus any value set 
	'here will take precedence and dialog will be launched showing that value. 
	'------------------------------------------------------------------------------

	Public Sub dialogShown_cb()

		Try

			'---- Enter your callback code here -----

			UNIT_No.Value = readAttribute(myPart, "UNIT_No")
			If UNIT_No.Value = "" Then UNIT_No.Value = "---"

			CYL_CODE.Value = readAttribute(myPart, "CYL_CODE")
			If CYL_CODE.Value = "" Then CYL_CODE.Value = "---"

		Catch ex As Exception

			'---- Enter your exception handling code here -----
			theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)

		End Try

	End Sub

	'------------------------------------------------------------------------------
	'Callback Name: apply_cb
	'------------------------------------------------------------------------------
	Public Function apply_cb() As Integer

		Dim response1 As Selection.Response = Selection.Response.Cancel
		Dim errorCode As Integer = 0


		Try

			response1 = SelectComponent("select a component", theComponent)

			'---- Enter your callback code here -----

			If UNIT_No.Value.ToUpper = "---" Then UNIT_No.Value = ""
			If CYL_CODE.Value.ToUpper = "---" Then CYL_CODE.Value = ""


			Dim theObjects(0) As NXObject
			'theObjects(0) = bodyPart
			theObjects(0) = theComponent

			'createAttribute(myPart, theObjects, "UNIT_No", UNIT_No.Value)
			'createAttribute(myPart, theObjects, "CYL_CODE", CYL_CODE.Value)
			
			'MyPart.SetUserAttribute("UNIT_No", -1, UNIT_No.Value, Update.Option.Now)
			'MyPart.SetUserAttribute("CYL_CODE", -1, CYL_CODE.Value, Update.Option.Now)

			Dim attributePropertiesBuilder1 As AttributePropertiesBuilder
			attributePropertiesBuilder1 = workpart.PropertiesManager.CreateAttributePropertiesBuilder(theobjects)
			attributePropertiesBuilder1.ObjectPicker = AttributePropertiesBaseBuilder.ObjectOptions.Occurrence
			attributePropertiesBuilder1.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String
			attributePropertiesBuilder1.Title = "UNIT_No"
			attributePropertiesBuilder1.IsArray = False
			attributePropertiesBuilder1.StringValue = UNIT_No.Value
			
			Dim nXObject2 As NXObject
			
			nXObject2 = attributePropertiesBuilder1.Commit()
			attributePropertiesBuilder1.Destroy()
			
			Dim attributePropertiesBuilder2 As AttributePropertiesBuilder
			attributePropertiesBuilder2 = workpart.PropertiesManager.CreateAttributePropertiesBuilder(theobjects)
			attributePropertiesBuilder2.ObjectPicker = AttributePropertiesBaseBuilder.ObjectOptions.Occurrence
			attributePropertiesBuilder2.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String
			attributePropertiesBuilder2.Title = "CYL_CODE"
			attributePropertiesBuilder2.IsArray = False
			attributePropertiesBuilder2.StringValue = CYL_CODE.Value
			
			Dim nXObject3 As NXObject
			
			nXObject3 = attributePropertiesBuilder2.Commit()
			attributePropertiesBuilder2.Destroy()


		Catch ex As Exception

			'---- Enter your exception handling code here -----
			errorCode = 1
			theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)

		End Try


		apply_cb = errorCode

	End Function

	'------------------------------------------------------------------------------
	'Callback Name: update_cb
	'------------------------------------------------------------------------------
	Public Function update_cb(ByVal block As NXOpen.BlockStyler.UIBlock) As Integer

		Try

			If block Is UNIT_No Then
				'---- Enter your code here -----

			ElseIf block Is CYL_CODE Then


			End If

		Catch ex As Exception

			'---- Enter your exception handling code here -----
			theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)

		End Try

		update_cb = 0

	End Function

	'------------------------------------------------------------------------------
	'Callback Name: ok_cb
	'------------------------------------------------------------------------------
	Public Function ok_cb() As Integer

		Dim errorCode As Integer = 0

		Try

			'---- Enter your callback code here -----
			'errorCode = apply_cb()

			If UNIT_No.Value.ToUpper = "---" Then UNIT_No.Value = ""
			If CYL_CODE.Value.ToUpper = "---" Then CYL_CODE.Value = ""


			Dim theObjects(0) As NXObject
			'theObjects(0) = bodyPart
			theObjects(0) = theComponent
			
			Dim attributePropertiesBuilder1 As AttributePropertiesBuilder
			attributePropertiesBuilder1 = workpart.PropertiesManager.CreateAttributePropertiesBuilder(theobjects)
			attributePropertiesBuilder1.ObjectPicker = AttributePropertiesBaseBuilder.ObjectOptions.Occurrence
			attributePropertiesBuilder1.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String
			attributePropertiesBuilder1.Title = "UNIT_No"
			attributePropertiesBuilder1.IsArray = False
			attributePropertiesBuilder1.StringValue = UNIT_No.Value
			
			Dim nXObject2 As NXObject
			
			nXObject2 = attributePropertiesBuilder1.Commit()
			attributePropertiesBuilder1.Destroy()
			
			Dim attributePropertiesBuilder2 As AttributePropertiesBuilder
			attributePropertiesBuilder2 = workpart.PropertiesManager.CreateAttributePropertiesBuilder(theobjects)
			attributePropertiesBuilder2.ObjectPicker = AttributePropertiesBaseBuilder.ObjectOptions.Occurrence
			attributePropertiesBuilder2.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String
			attributePropertiesBuilder2.Title = "CYL_CODE"
			attributePropertiesBuilder2.IsArray = False
			attributePropertiesBuilder2.StringValue = CYL_CODE.Value
			
			Dim nXObject3 As NXObject
			
			nXObject3 = attributePropertiesBuilder2.Commit()
			attributePropertiesBuilder2.Destroy()
			
						
			'createAttribute(myPart, theObjects, "UNIT_No", UNIT_No.Value)
			'createAttribute(myPart, theObjects, "CYL_CODE", CYL_CODE.Value)

			'MyPart.SetUserAttribute("UNIT_No", -1, UNIT_No.Value, Update.Option.Now)
			'MyPart.SetUserAttribute("CYL_CODE", -1, CYL_CODE.Value, Update.Option.Now)


		Catch ex As Exception

			'---- Enter your exception handling code here -----
			errorCode = 1
			theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)

		End Try

		ok_cb = errorCode


	End Function

	'------------------------------------------------------------------------------
	'Function Name: GetBlockProperties
	'Returns the propertylist of the specified BlockID
	'------------------------------------------------------------------------------
	Public Function GetBlockProperties(ByVal blockID As String) As PropertyList
		GetBlockProperties = Nothing

		Try

			GetBlockProperties = theDialog.GetBlockProperties(blockID)

		Catch ex As Exception

			'---- Enter your exception handling code here -----
			theUI.NXMessageBox.Show("Check Attributes", NXMessageBox.DialogType.Error, ex.ToString)

		End Try

	End Function


	Public Shared Function check_for_missing_display_part() As Integer

		Dim dispPart As Part = Nothing

		dispPart = Session.GetSession().Parts.Display

		If dispPart Is Nothing Then

			Dim style As MsgBoxStyle
			Dim title As String = "Check for Displayed part"
			style = MsgBoxStyle.Information
			MsgBox("There is no displayed part   ", style, title)

			Return 1

		Else

			Return 0

		End If

	End Function


	Private Function readAttribute(ByVal selPart As Part, ByVal attrName As String) As String

		Dim attrValue As String = Nothing
		Dim iRes As Integer
		Dim attrInfor As NXOpen.NXObject.AttributeInformation = Nothing
		Dim attrtype As NXOpen.NXObject.AttributeType = NXObject.AttributeType.String

		theUFSession.Attr.FindAttribute(selPart.Tag, 5, attrName, iRes)

		If iRes <> 0 Then

			attrInfor = selPart.GetUserAttribute(attrName, attrtype, 2)
			attrValue = attrInfor.StringValue

		Else

			attrValue = "---"

		End If

		Return attrValue

	End Function

	'------------------------------------------------------------------------------
	'User Functions
	'------------------------------------------------------------------------------
	Private Sub createAttribute(ByVal thisPart As Part, ByVal theObjects() As NXObject, ByVal attrTitle As String, ByVal attrValue As String, Optional attrCategory As String = "")

		Dim attributePropertiesBuilder1 As AttributePropertiesBuilder = theSession.AttributeManager.CreateAttributePropertiesBuilder(thisPart, theObjects, AttributePropertiesBuilder.OperationType.Create)

		attributePropertiesBuilder1.IsArray = False
		attributePropertiesBuilder1.Category = attrCategory
		attributePropertiesBuilder1.Title = attrTitle
		attributePropertiesBuilder1.StringValue = attrValue
		attributePropertiesBuilder1.Commit()
		attributePropertiesBuilder1.Destroy()

	End Sub

End Class

With best regards
Michael
 
Seems perfect so far. Thank you so much!!!!

Ken
My brain is like a sponge. A sopping wet sponge. When I use it, I seem to lose more than I soak in.
 
So it turns out this code is doing something funky that doesnt make sense to me.

I was trying to figure out how to keep the parts highlighted even if you select "APPLY" and wait until someone hits "OK" or "CANCEL" before they unhighlighted (I was trying not to hassle anyone for that), when I noticed this problem during testing.

I added attributes like this...

Component1 = 1111
hit apply
Component2 = 2222
hit apply
Component3 = 3333
hit apply
Component4 = 4444
hit OK

Then going back and checking the components, I get the following results.

Component1 =
Component2 = 1111
Component3 = 2222
Component4 = 4444

It seems to ignore the first component and add its attributes to the next component and then overrides the second to last set of attributes and adds the correct attributes for the component I select OK on.

Just to be clear, I was playing with a copy of the code. I went to the original code and tried it and got the same results.



Ken
My brain is like a sponge. A sopping wet sponge. When I use it, I seem to lose more than I soak in.
 
Just an update. I have found a way to fix the highlighting problem at least.

I changed this code in the SelectComponent function to "True"

Code:
 Dim keepHighlighted As Boolean = True

This kept it all highlighted but didnt unhighlight the components after I hit OK. So recorded a part cleanup with the unhighlight optioned turned on and used some code from that and added it to the public Sub Dispose code...

Code:
    Public Sub Dispose()

        If theDialog IsNot Nothing Then
            theDialog.Dispose()
            theDialog = Nothing
            displayPart.Views.Refresh()
            theSession.Parts.SetWork(workPart)

            Dim partCleanup1 As NXOpen.PartCleanup = Nothing
            partCleanup1 = theSession.NewPartCleanup()
            partCleanup1.TurnOffHighlighting = True
            partCleanup1.DoCleanup()
            partCleanup1.Dispose()

        End If
    End Sub

Likely there is a better way. This seems to add a few seconds of run time to the code after I hit OK, but at least it works. Believe it or not, this is a huge accomplishment for me when it comes to code. lol

Ken
My brain is like a sponge. A sopping wet sponge. When I use it, I seem to lose more than I soak in.
 
The Power of Prayer! I have been trying to figure this out for days and growing frustrated. I prayed at lunch and just figured it out. :)

I cant believe how easy it was to fix too.

I only had to move this code...

Code:
response1 = SelectComponent("select a component", theComponent)

and place it right after....

Code:
Dim nXObject3 As NXObject

nXObject3 = attributePropertiesBuilder2.Commit()
attributePropertiesBuilder2.Destroy()

Now there is only one last thing I need to do with this journal. Get it so when I select a component and the box appears to fill out the attributes, if it already has these attributes they want it to show those attributes. This way the user will know if it was already done.

Ken
My brain is like a sponge. A sopping wet sponge. When I use it, I seem to lose more than I soak in.
 
And it is complete. I have had a good day! lol

For this last part, all I had to do was change "selPart" to "theComponent" in two places within the "readAttribute" function.


What doesnt make sense to me is that in the top line....

Code:
Private Function readAttribute(ByVal selPart As Part, ByVal attrName As String) As String

I had to leave it as "selPart" for it to work. Even though that is the only place in the journal "selPart" shows up.

But it works and I am happy. lol

Ken
My brain is like a sponge. A sopping wet sponge. When I use it, I seem to lose more than I soak in.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor