Forgot password?

Create an account!

Forum

« back

RhinoScript – Subscript out of range error - help

Messages

Please log in to write a message.

  • 7. Hanno (Nov 14, 2010 12.38):

    Hi,

    it is exactly as Johannes describes: All your indices are 0, so when you try to use j-1 on your arrMidPt assignments, you get -1 which is out of range.
    In the lines above you are correctly checking if your indices are > 0, so why not do the same thing here?

    Hanno

  • 6. workshopbennett (Nov 14, 2010 05.51):

    When i run the

    Call Msgbox("i: " & i & "  ,j: " & j & "  k: " & k)

    i get the values of 0 for i,j, and k. NOw the error is that K is out of range...no longer j. Below is the erroring script. I am trying to get a midpoint between all the lines that are created.


    Option Explicit
    'Script written by <insert name>
    'Script copyrighted by <insert company name>
    'Script version Monday, November 08, 2010 9:10:21 AM

    Call Main()

    Sub Main()

            Dim i : i = 0
            Dim y
            Dim z
            Dim x
            Dim j
            Dim k
            Dim imax, jmax, kmax
            Dim arrPt
           
            imax = 5
            jmax = 5
            kmax = 5
           
            ReDim arrPt(imax, jmax, kmax)
           
            'Call Rhino.EnableRedraw(False)

            For k = 0 To kmax
                    For j = 0 To jmax
                            For i = 0 To imax
                                                                   
                                    If k = 0 Then
                                            x = i * 2
                                            y = j * 2
                                    End If
                                                                                                   
                                    If k = 1 Then
                                            x = i * 1
                                            y = j * 1
                                    End If
                                                                                                   
                                    If k = 2 Then
                                            x = i * 3
                                            y = j * 3
                                    End If
                                                                                                   
                                    If k = 3 Then
                                            x = i * 6
                                            y = j * 6
                                    End If
                                                                                                   
                                    If k = 4 Then
                                            x = i * 8
                                            y = j * 8
                                    End If
                                                                   
                                    If k = 5 Then
                                            x = i * 3
                                            y = j * 3
                                    End If
                                    'possibility to change spacing in that x does not always have to equal i.  
                                    'define x,y,z in terms of i,j,k
                           
                                    'x = i * 2 +rnd*3
                                    'y = j * 2 +rnd*3
                                    z = k * 2 +rnd*3'step
                   
                                    'create the array
                                    arrPt(i,j,k)  = array(x,y,z)
                                    'create a point
                                    Call Rhino.AddPoint (arrPt(i,j,k))
                                    'if you use x,y,z you will get the point locations in the xyz world space
                                    'Call Rhino.AddTextDot(i&","&j&","&k&",", arrPt(i,j,k))
                                   
                                    'create lines
                                    If i > 0 Then
                                            Call Rhino.AddLine (arrPt (i-1,j,k), arrPt(i,j,k))     
                                    End If
                                   
                                    If j > 0 Then
                                            Call Rhino.AddLine (arrPt (i,j-1,k), arrPt(i,j,k))     
                                    End If
                                   
                                    If k > 0 Then
                                            Call Rhino.AddLine (arrPt (i,j,k-1), arrPt(i,j,k))     
                                    End If
                                   
                                    If i > 0 And j > 0 And k > 0 Then
                                            Call Rhino.AddSrfPt (array(arrPt(i,j,k),arrPt(i-1,j,k),arrPt(i-1,j-1,k), arrPt(i,j-1,k)))       
                                    End If
                                   
                                    ReDim arrMidPt(3)
                                    Call Msgbox("i: " & i & "  ,j: " & j & "  k: " & k)

                                    arrMidPt(0) = MidPt (arrPt(i,j,k), arrPt(i,j,k))
                                    Call Rhino.AddPoint (arrMidPt(0))
                                                                   
                                    arrMidPt(1) = MidPt (arrPt(i,j-1,k), arrPt(i-1,j-1,k))
                                    Call Rhino.AddPoint (arrMidPt(1))
                                                                                                   
                                    arrMidPt(3) = MidPt (arrPt(i,j-1,k), arrPt(i-1,j,k))
                                    Call Rhino.AddPoint (arrMidPt(3))
                                                                                           
                                    Call Rhino.AddCurve(array(arrMidPt(0), arrMidPt(1), arrMidPt(2), arrMidPt(3), arrMidPt (0)))
                            Next
           
                    Next
            Next
           
            'Call Rhino.EnableRedraw(True)  
           
           
    End Sub

    Function MidPt (pt1,pt2)
            'give you x values, then y values, then z values
            MidPt = array((pt1(0) + pt2(0))/2,_
                    (pt1(1) + pt2(1))/2,_
                    (pt1(2) + pt2(2))/2)
    End Function
  • 5. Johannes (Nov 13, 2010 21.10):

    i am not sure because i have no rhino with me... but it could be the index in that line:
    arrMidPt(0) = MidPt (arrPt(i,j,k), arrPt(i-1,j,k))
    what is the size of your array (arrPt)? in the first loop is i=0 and when you want to get the index of i-1 it equals -1... and the index of an array cannot be -1.

    try a messagebox before that lines and take a look on i, j and k

    Call Msgbox("i: " & i & "  ,j: " & j & "  k: " & k
  • 4. workshopbennett (Nov 13, 2010 15.26):

    The script below has commented out where i am trying to generate the midpoints. (ReDim MidPt) When it is uncommented, it will error.

  • 3. workshopbennett (Nov 13, 2010 15.24):

    Thanks for the reply and my apologies for the mess. I am trying to generate midpoints on the lines that are made, where the midpoints can then be used to generate new geometry. The error i get it Subscript out of range, j. I am new to Rhinoscript and am still trying to get my head around arrays and such.
    any input would be helpful.

    Thanks

    Option Explicit
    'Script written by <insert name>
    'Script copyrighted by <insert company name>
    'Script version Monday, November 08, 2010 9:10:21 AM

    Call Main()

    Sub Main()

    Dim i : i = 0
    Dim y
    Dim z
    Dim x
    Dim j
    Dim k
    Dim imax, jmax, kmax
    Dim arrPt

    imax = 5
    jmax = 5
    kmax = 5

    ReDim arrPt(imax, jmax, kmax)

    'Call Rhino.EnableRedraw(False)

    For k = 0 To kmax
    For j = 0 To jmax
    For i = 0 To imax

    If k = 0 Then
    x = i * 2
    y = j * 2
    End If

    If k = 1 Then
    x = i * 1
    y = j * 2
    End If

    If k = 2 Then
    x = i * 3
    y = j * 3
    End If

    If k = 3 Then
    x = i * 6
    y = j * 6
    End If

    If k = 4 Then
    x = i * 8
    y = j * 8
    End If

    If k = 5 Then
    x = i * 3
    y = j * 3
    End If
    'possibility to change spacing in that x does not always have to equal i.
    'define x,y,z in terms of i,j,k

    x = i * 2 +rnd*3
    y = j * 2 +rnd*3
    z = k * 2 +rnd*3'step

    'create the array
    arrPt(i,j,k) = array(x,y,z)
    'create a point
    Call Rhino.AddPoint (arrPt(i,j,k))
    'if you use x,y,z you will get the point locations in the xyz world space
    'Call Rhino.AddTextDot(i&","&j&","&k&",", arrPt(i,j,k))

    'create lines
    If i > 0 Then
    Call Rhino.AddLine (arrPt (i-1,j,k), arrPt(i,j,k))     
    End If

    If j > 0 Then
    Call Rhino.AddLine (arrPt (i,j-1,k), arrPt(i,j,k))     
    End If

    If k > 0 Then
    Call Rhino.AddLine (arrPt (i,j,k-1), arrPt(i,j,k))     
    End If

    If i > 0 And j > 0 And k > 0 Then
    Call Rhino.AddSrfPt (array(arrPt(i,j,k),arrPt(i-1,j,k),arrPt(i-1,j-1,k), arrPt(i,j-1,k)))       
    End If

    ReDim arrMidPt(3)

    '        arrMidPt(0) = MidPt (arrPt(i,j,k), arrPt(i-1,j,k))
    '        Call Rhino.AddPoint (arrMidPt(0))
    '       
    '        arrMidPt(1) = MidPt (arrPt(i,j-1,k), arrPt(i-1,j-1,k))
    '        Call Rhino.AddPoint (arrMidPt(1))
    '       
    '        arrMidPt(3) = MidPt (arrPt(i,j-1,k-1), arrPt(i-1,j,k-1))
    '        Call Rhino.AddPoint (arrMidPt(3))
    '       
    '        Call Rhino.AddCurve(array(arrMidPt(0), arrMidPt(1), arrMidPt(2), arrMidPt(3), arrMidPt (0)))
    Next

    Next
    Next

    'Call Rhino.EnableRedraw(True)


    End Sub

    Function MidPt (pt1,pt2)
    'give you x values, then y values, then z values
    MidPt = array((pt1(0) + pt2(0))/2,_
    (pt1(1) + pt2(1))/2,_
    (pt1(2) + pt2(2))/2)
    End Function
  • 2. Johannes (Nov 13, 2010 10.15):

    hi, please use syntax highlighting and post it again. its hard to read such a script withoout it.
    write a new message and see below for the description.

  • 1. workshopbennett (Nov 13, 2010 05.38):

    keep getting 'subscript out of range j". if lines 93 - 102 get commented out it runs fine. Thoughts?

    Option Explicit
    'Script written by <insert name>
    'Script copyrighted by <insert company name>
    'Script version Monday, November 08, 2010 9:10:21 AM

    Call Main()

    Sub Main()

    Dim i : i = 0
    Dim y
    Dim z
    Dim x
    Dim j
    Dim k
    Dim imax, jmax, kmax
    Dim arrPt

    imax = 5
    jmax = 5
    kmax = 5

    ReDim arrPt(imax, jmax, kmax)

    'Call Rhino.EnableRedraw(False)

    For k = 0 To kmax
    For j = 0 To jmax
    For i = 0 To imax

    If k = 0 Then
    x = i * 2
    y = j * 2
    End If

    If k = 1 Then
    x = i * 1
    y = j * 2
    End If

    If k = 2 Then
    x = i * 3
    y = j * 3
    End If

    If k = 3 Then
    x = i * 6
    y = j * 6
    End If

    If k = 4 Then
    x = i * 8
    y = j * 8
    End If

    If k = 5 Then
    x = i * 3
    y = j * 3
    End If
    'possibility to change spacing in that x does not always have to equal i.
    'define x,y,z in terms of i,j,k

    x = i * 2 +rnd*3
    y = j * 2 +rnd*3
    z = k * 2 +rnd*3'step

    'create the array
    arrPt(i,j,k) = array(x,y,z)
    'create a point
    Call Rhino.AddPoint (arrPt(i,j,k))
    'if you use x,y,z you will get the point locations in the xyz world space
    'Call Rhino.AddTextDot(i&","&j&","&k&",", arrPt(i,j,k))

    'create lines
    If i > 0 Then
    Call Rhino.AddLine (arrPt (i-1,j,k), arrPt(i,j,k))
    End If

    If j > 0 Then
    Call Rhino.AddLine (arrPt (i,j-1,k), arrPt(i,j,k))
    End If

    If k > 0 Then
    Call Rhino.AddLine (arrPt (i,j,k-1), arrPt(i,j,k))
    End If

    If i > 0 And j > 0 And k > 0 Then
    Call Rhino.AddSrfPt (array(arrPt(i,j,k),arrPt(i-1,j,k),arrPt(i-1,j-1,k), arrPt(i,j-1,k)))
    End If

    ReDim arrMidPt(3)

    ' arrMidPt(0) = MidPt (arrPt(i,j,k), arrPt(i-1,j,k))
    ' Call Rhino.AddPoint (arrMidPt(0))
    '
    ' arrMidPt(1) = MidPt (arrPt(i,j-1,k), arrPt(i-1,j-1,k))
    ' Call Rhino.AddPoint (arrMidPt(1))
    '
    ' arrMidPt(3) = MidPt (arrPt(i,j-1,k-1), arrPt(i-1,j,k-1))
    ' Call Rhino.AddPoint (arrMidPt(3))
    '
    ' Call Rhino.AddCurve(array(arrMidPt(0), arrMidPt(1), arrMidPt(2), arrMidPt(3), arrMidPt (0)))
    Next

    Next
    Next

    'Call Rhino.EnableRedraw(True)


    End Sub

    Function MidPt (pt1,pt2)
    'give you x values, then y values, then z values
    MidPt = array((pt1(0) + pt2(0))/2,_
    (pt1(1) + pt2(1))/2,_
    (pt1(2) + pt2(2))/2)
    End Function

Recommend

Why are these buttons gray?