Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

visual basic string array problem

Status
Not open for further replies.

multicaduser

Industrial
Jan 29, 2013
261
I have a visual basic program that takes the file name and assigns it to an existing part attribute. This works well until the name gets long.

The error that pops up is

"System.Exception: Length of String array does not match with length provided."

First the attribute is read then if the option is chosen the file name is used to replace the attribute or something can be entered manually. This is the line of code it fails on.

usrAttr(2,2) = fnameAsDesc

Nothing unusual and it works well with most file names. My programming skills are not great and any hint on what to try would be appreciated.



NX 12.0.1.7 Windows 10
 
Replies continue below

Recommended for you

I assume that "usrAttr()" is a string array that you have created, how is it declared? How long is the name of the file? You might need to post more of your code for better feedback.

www.nxjournaling.com
 
Morning cowski, thank you for your reply.

Here is how the attribute is retrieved from the existing file.

Function getUsrAttr
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
Dim stpr As Integer
For stpr = 1 to 6
If workPart.HasUserAttribute(usrAttr(stpr,1), NXObject.AttributeType.String, -1) Then
Dim attrString As NXObject.AttributeInformation = workPart.GetUserAttribute(usrAttr(stpr,1), NXObject.AttributeType.Any, -1)
usrAttr(stpr,2) = attrString.StringValue
lw.writeline(usrAttr(stpr,1))
lw.writeline(usrAttr(stpr,2))
End If
Next
Return usrAttr
End function


And here is how the file name string is obtained.

fnameAsDesc = (IO.Path.GetFileNameWithoutExtension(workPart.FullPath)).Replace("_", " ")
Dim fnameLen As Integer = fnameAsDesc.Length
fnameAsDesc = fnameAsDesc.Substring(5,fnameLen-5)

This all works fine for most file names, however length and characters such as hyphens or a lot of underscores seem to mess it up. I use a lot of manual debugging such as writing the variable to screen at different points to check if there is something unusual with it but nothing other than a plain text string shows. Just stumped.


NX 12.0.1.7 Windows 10
 
The code that you provided is helpful, but it doesn't show how the "usrAttr" variable is declared. Based on the text of the error message, it seems that the array was declared with fewer elements than you intended (i.e. the array doesn't have an element 2,2 - but you try to use it anyway).

www.nxjournaling.com
 
Here's the program, a lot of junk in it for trying to figure out the problem. Thanks for your help.

Code:
Option Strict Off
Imports System
Imports System.Collections.Generic

Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI


Module NXJournal

    Dim ufs As UFSession = UFSession.GetUFSession()

    Dim shtAttrString As String = ""
    Dim dscAttrString As String = ""
    Dim sizAttrString As String = ""
    Dim venAttrString As String = ""
    Dim pnmAttrString As String = ""
    Dim fnameAsDesc As String = ""
	Dim afnameAsDesc As String = "" 'existing description attribute
    Dim usrAttr (6,2) As String

    Sub Main ()

        Dim theSession As Session = Session.GetSession()
        Dim theUfSession As UFSession = UFSession.GetUFSession()
        Dim ufs As UFSession = UFSession.GetUFSession()
        Dim workPart As Part = theSession.Parts.Work
        Dim displayPart As Part = theSession.Parts.Display
        Dim currentPath As String = IO.Path.GetDirectoryName(workPart.FullPath)
        Dim currentFile As String = IO.Path.GetFileNameWithoutExtension(workPart.FullPath)
        Dim lw As ListingWindow = theSession.ListingWindow


setUsrAttr 'set default attribute names and values

getUsrAttr 'get part attributes if exists

If usrAttr(6,2) = "true" Then
   lw.WriteLine("BOM Lock Is Set")
   Exit Sub
End If

'get file name and replace underscores with spaces
fnameAsDesc = (IO.Path.GetFileNameWithoutExtension(workPart.FullPath)).Replace("_", " ")
Dim fnameLen As Integer = fnameAsDesc.Length
fnameAsDesc = fnameAsDesc.Substring(5,fnameLen-5)

'choice menu
   Dim menuChoice As String = ""
   Do
      menuChoice = opChoice
      If menuChoice = usrAttr(1,1) Then inputSheet
      If menuChoice = usrAttr(2,1) Then inputDescription
      If menuChoice = usrAttr(3,1) Then inputSize
      If menuChoice = usrAttr(4,1) Then inputVendor
      If menuChoice = usrAttr(5,1) Then inputMaterial
   Loop Until menuChoice = "done"

   Dim lstpr As Integer
   For lstpr = 1 to 5
      workPart.SetUserAttribute(usrAttr(lstpr,1), -1, usrAttr(lstpr,2), Update.Option.Now)
   Next

   End Sub


    Function opChoice() As String
        Dim theUfSession As UFSession = UFSession.GetUFSession()
        Dim opList As New List(Of String)
        opList.Add(usrAttr(1,1) & " : " & usrAttr(1,2))
        opList.Add(usrAttr(2,1) & " : " & usrAttr(2,2))
        opList.Add(usrAttr(3,1) & " : " & usrAttr(3,2))
        opList.Add(usrAttr(4,1) & " : " & usrAttr(4,2))
        opList.Add(usrAttr(5,1) & " : " & usrAttr(5,2))
        opList.Add("Done")

        Dim retVal As Integer = 0
        Dim title As String = "Select file output type"
        theUfSession.Ui.LockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
        retVal = theUFSession.Ui.DisplayMenu(title, 1, opList.ToArray, opList.Count)
        theUFSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)

        '1=back
        '2=cancel
        '5-18=menu item selected
        '19=disallowed state, unable to bring up dialog

        Select Case retVal
            Case Is = 5
                Return usrAttr(1,1)
            Case Is = 6
                Return usrAttr(2,1)
            Case Is = 7
                Return usrAttr(3,1)
            Case Is = 8
                Return usrAttr(4,1)
            Case Is = 9
                Return usrAttr(5,1)
            Case Is = 10
                Return "done"
        End Select

    End Function


   Function inputSheet() As String
      Dim answer As String = usrAttr(1,2)
      Dim myInt As Integer
        Do
            answer = NXInputBox.GetInputString("Enter Sheet Number", "Enter Sheet Number", usrAttr(1,2))
        Loop Until Integer.TryParse(answer, myInt)
        usrAttr(1,2) = myInt.ToString(0)
      return answer
    End Function


   Function inputDescription() As String
        Dim theUfSession As UFSession = UFSession.GetUFSession()
		
		Dim theSession As Session = Session.GetSession()
		Dim lw As ListingWindow = theSession.ListingWindow
		
        Dim dsList As New List(Of String)
        dsList.Add("Use File Name : " ) '& fnameAsDesc)
        dsList.Add("Enter Text")
        dsList.Add("Done")

        Dim retVal As Integer = 0
        Dim title As String = "Select file output type"
        theUfSession.Ui.LockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
        retVal = theUFSession.Ui.DisplayMenu(title, 1, dsList.ToArray, dsList.Count)
        theUFSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)

        Select Case retVal
            Case Is = 5
			    lw.writeline (fnameAsDesc.toupper())
				fnameAsDesc = fnameAsDesc.ToUpper()
                usrAttr(2,2) = fnameAsDesc
                Return usrAttr(2,2)
            Case Is = 6
                Dim answer As String = ""
                Do
                   answer = NXInputBox.GetInputString("Enter Description","Enter Description",usrAttr(2,2))
                Loop Until answer.Trim.Length > 0
                usrAttr(2,2) = answer.ToUpper()
                Return usrAttr(2,2)
            Case Is = 7
                usrAttr(2,2) = usrAttr(2,2).ToUpper()
                Return usrAttr(2,2)
        End Select
   End Function


   Function inputSize() As String
        Dim theUfSession As UFSession = UFSession.GetUFSession()
        Dim szList As New List(Of String)
        szList.Add("Pick Body")
        szList.Add("Enter Size")
        szList.Add("Done")

        Dim retVal As Integer = 0
        Dim title As String = usrAttr(3,1) & "  " & usrAttr(3,2)
        theUfSession.Ui.LockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
        retVal = theUFSession.Ui.DisplayMenu(title, 1, szList.ToArray, szList.Count)
        theUFSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)

        Select Case retVal
             Case Is = 5
                Dim answer As String = sizAttrString
                Dim s As Session = Session.GetSession()
                Dim ufs As UFSession = UFSession.GetUFSession()
                Dim a_body As NXOpen.Tag = NXOpen.Tag.Null
                Dim csys As NXOpen.Tag = NXOpen.Tag.Null
                Dim target As NXOpen.Tag = NXOpen.Tag.Null
                Dim blockFeature As NXOpen.Tag = NXOpen.Tag.Null
                Dim min_corner(2) As Double
                Dim directions(2, 2) As Double
                Dim distances(2) As Double
                Dim edge_len(2) As String
                While select_a_body(a_body) = Selection.Response.Ok
                   ufs.Csys.AskWcs(csys)
                   ufs.Modl.AskBoundingBoxExact(a_Body, csys, min_corner, directions, distances)
                   usrAttr(3,2) = (distances(0).ToString("f4") & " x " & distances(1).ToString("f4") & " x " & distances(2).ToString("f4"))
                   Return usrAttr(3,2)
               End While
            Case Is = 6
               Dim Answer As String = usrAttr(3,2)
               Do
                  answer = NXInputBox.GetInputString("Enter Size","Enter Size",usrAttr(3,2))
               Loop Until answer.Trim.Length > 0
               usrAttr(3,2) = answer.ToUpper()
               Return usrAttr(3,2)
            Case Is = 7
                usrAttr(3,2) = usrAttr(3,2).ToUpper
                Return usrAttr(3,2)
        End Select
   End Function


   Function inputVendor() As String
      Dim answer As String = ""
        Do
            answer = NXInputBox.GetInputString("Enter Vendor", "Enter Vendor", usrAttr(4,2))
        Loop Until answer.Trim.Length > 0
        usrAttr(4,2) = answer.ToUpper()
        Return usrAttr(4,2)

   End Function


   Function inputMaterial() As String
      Dim answer As String = ""
        Do
            answer = NXInputBox.GetInputString("Enter Part Number/Material", "Enter Part Number/Material", usrAttr(5,2))
        Loop Until answer.Trim.Length > 0
        usrAttr(5,2) = answer.ToUpper()
        Return usrAttr(5,2)
   End Function


    Function select_a_body(ByRef a_body As NXOpen.Tag) As Selection.Response

        Dim message As String = "Select a body"
        Dim title As String = "Select a body"
        Dim scope As Integer = UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY
        Dim response As Integer

        Dim view As NXOpen.Tag
        Dim cursor(2) As Double
        Dim ip As UFUi.SelInitFnT = AddressOf body_init_proc

        ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)

        Try
            ufs.Ui.SelectWithSingleDialog(message, title, scope, ip, _
                         Nothing, response, a_body, cursor, view)
        Finally
            ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)
        End Try

        If response <> UFConstants.UF_UI_OBJECT_SELECTED And _
           response <> UFConstants.UF_UI_OBJECT_SELECTED_BY_NAME Then
            Return Selection.Response.Cancel
        Else
            ufs.Disp.SetHighlight(a_body, 0)
            Return Selection.Response.Ok
        End If

    End Function

    Function body_init_proc(ByVal select_ As IntPtr, _
                           ByVal userdata As IntPtr) As Integer

        Dim num_triples As Integer = 1
        Dim mask_triples(0) As UFUi.Mask
        mask_triples(0).object_type = UFConstants.UF_solid_type
        mask_triples(0).object_subtype = UFConstants.UF_solid_body_subtype
        mask_triples(0).solid_type = UFConstants.UF_UI_SEL_FEATURE_BODY

        ufs.Ui.SetSelMask(select_, _
                           UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, _
                           num_triples, mask_triples)
        Return UFConstants.UF_UI_SEL_SUCCESS

    End Function

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

    End Function


    Function setUsrAttr
       usrAttr(1,1) = "sheet"
       usrAttr(1,2) = "1"
       usrAttr(2,1) = "description"
       usrAttr(2,2) = "now this is going to be a very long file name"
       usrAttr(3,1) = "size"
       usrAttr(3,2) = "0 x 0 x 0"
       usrAttr(4,1) = "vendor"
       usrAttr(4,2) = "NA"
       usrAttr(5,1) = "part_num"
       usrAttr(5,2) = "-"
       usrAttr(6,1) = "input_bom_lock"
       usrAttr(6,2) = "false"
       Return usrAttr
    End Function


    Function getUsrAttr
        Dim theSession As Session = Session.GetSession()
        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        Dim stpr As Integer
        For stpr = 1 to 6
        If workPart.HasUserAttribute(usrAttr(stpr,1), NXObject.AttributeType.String, -1) Then
            Dim attrString As NXObject.AttributeInformation = workPart.GetUserAttribute(usrAttr(stpr,1), NXObject.AttributeType.Any, -1)
            usrAttr(stpr,2) = attrString.StringValue
            lw.writeline(usrAttr(stpr,1))
            lw.writeline(usrAttr(stpr,2))
        End If
        Next
        Return usrAttr
    End function


End Module

NX 12.0.1.7 Windows 10
 
Wow, never would have thought of that but should have, grip has the same limitations. Thank you very much.

NX 12.0.1.7 Windows 10
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor