Attribute type check (integer/string)

I have a problem that I'd like to at least throw up a warning and close the journal file at least. At best, I'd like it to delete the attribute and recreate it as a string if it is an integer.

How would I go about checking the type of a specific attribute (integer vs string)? NX 9 preferably

The code below will check for the existence of an attribute; if it is not of type "string", it will attempt to delete the existing attribute and create a string attribute in its place. Change the value of the "aTitle" constant (the attribute title you want to look for) before running the journal.

'attribute_change_type

Option Strict Off
Imports System
Imports NXOpen

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If

Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Const undoMarkName As String = "nes386 change attribute"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

'$$ title of the attribute you want to change
Const aTitle As String = "test"

Dim aInfo As NXObject.AttributeInformation
Dim aStrValue As String = "default value"
Dim isStringAttribute As Boolean = False

If workPart.HasUserAttribute(aTitle, NXObject.AttributeType.Any, -1) Then
'attribute exists, retrieve information
aInfo = workPart.GetUserAttribute(aTitle, NXObject.AttributeType.Any, -1)
Select Case aInfo.Type
Case Is = NXObject.AttributeType.Boolean
aStrValue = aInfo.BooleanValue.ToString
Case Is = NXObject.AttributeType.Integer
aStrValue = aInfo.IntegerValue.ToString
Case Is = NXObject.AttributeType.Real
aStrValue = aInfo.RealValue.ToString
Case Is = NXObject.AttributeType.String
isStringAttribute = True
Case Else
'add other cases as necessary
End Select

If Not isStringAttribute Then
'delete existing non-string attribute and create a string attribute

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "delete attribute")

Try
workPart.DeleteUserAttribute(aInfo.Type, aInfo.Title, False, Update.Option.Now)
workPart.SetUserAttribute(aInfo.Title, -1, aStrValue, Update.Option.Now)
Catch ex As NXException
If ex.ErrorCode = 512007 Then
'attribute value is referenced by something and cannot be deleted without
'first breaking the links
lw.WriteLine("The attribute: '" & aTitle & "' is referenced by another object.")
lw.WriteLine("The attribute type cannot be changed until these links are broken.")
Else
lw.WriteLine(ex.ErrorCode & ": " & ex.Message)

End If

theSession.UndoToMark(markId1, "delete attribute")
End Try

End If

Else
'attribute does not exist
lw.WriteLine("attribute does not exist, creating it...")
'create string attribute
workPart.SetUserAttribute(aTitle, -1, aStrValue, Update.Option.Now)

End If

lw.Close()

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

End Function

End Module

Thank you, I was able to append my code with some lines from yours. Your programming is very impressive.