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!

NX Journal Errors out in TeamCenter, but not for Native application

Status
Not open for further replies.

cmick31

Mechanical
Jun 1, 2021
19
Looking for any help here on this..

The code I have runs great in Native application of NX, but when ran in TeamCenter it errors out.

Quick run down of the code - it allows me to select a component, which then creates an attribute and let's you manually type in the value for that attribute.

With Native it looks like this:
native_p1t9vn.jpg


With TeamCenter when I select it's giving me this:
teams_pq8mod.jpg


Anyone know how I can troubleshoot this?

My code is provided below:


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 = True
        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)

            Dim partCleanup1 As NXOpen.PartCleanup = Nothing
            partCleanup1 = theSession.NewPartCleanup()
            partCleanup1.TurnOffHighlighting = True
            partCleanup1.DoCleanup()
            partCleanup1.Dispose()
        End If
    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
            '---- 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()

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

        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: 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

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

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

    '------------------------------------------------------------------------------
    '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(theComponent.Tag, 5, attrName, iRes)

        If iRes <> 0 Then
            'attrInfor = selPart.GetUserAttribute(attrName, attrtype, 2)
            attrInfor = theComponent.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
 
Replies continue below

Recommended for you

For anyone taking a look at this, I seem to have fixed the issue incase someone's having a similar problem. However, I'm unsure if it's going to cause any complications or not.

I revised line 105 from:

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

To:

MyPart = CType(theComponent.Prototype, NXOpen.Part)

Is this a fix that will be sustainable? Or should I be revising further with this in regards to the entirety of the written code.

Any advice is much appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor