Grasshopper

algorithmic modeling for Rhino

index was out of range must be non-negative and less than the size of the collection

hello. I'm new to grasshopper and vb script as well. I've been having the following problem lately with vb script. I keep getting the following error:

index was out of range must be non-negative and less than the size of the collection

it usually appears when i try working with lists of points. it's pretty hard to learn, with this getting in the way all the time..

here, for example i tried going through one of andy payne's tutorials :

   Dim i As Integer
    Dim j As Integer
    Dim grid As New ArrayList
    For i = 0 To pts.count() - 1 Step gs
      Dim Row As New List (Of on3dpoint)
      For j = i To i + gs - 1
        Dim pt As on3dpoint
        pt = pts(j)
        row.Add(pt)
      Next
      grid.Add(row)
    Next

    Dim mid_pt As New list(Of On3dPoint)
    For i = 0 To grid.Count() - 1
      Dim row0 As List(Of On3dPoint)
      row0 = grid(i - 1)
      Dim row1 As List(Of on3dpoint)
      row1 = grid(i)
      For j = 1 To row0.count() - 1
        Dim mid_p As New On3dPoint
        mid_p = (Row0(j - 1) + Row0(j) + Row1(i - 1) + Row1(i)) / 4
        mid_pt.add(mid_p)
      Next
    Next
    a = mid_pt

thanks for your time











Views: 3670

Replies to This Discussion

Hi G,

row0 = grid(i - 1)

This seems to be the problem to me after a cursory perusal. This code is inside a loop that starts at zero:

For i = 0 To grid.Count() - 1

therefore the first time this loops runs, i will equal zero and you'll access the grid at an index of -1, which is not allowed.

I would also advice against the use of ArrayList. That class is a leftover from .NET 1.0 when generic types were not available. I'd say you should always use List(Of T) instead. So grid should be defined as:

Dim grid As New List(Of List(Of On3dPoint))


Lastly, since you're learning VB, I'd highly recommend you use the new Rhino SDK as it is a much friendlier development environment. You'll automatically get to use the new SDK if you switch to Grasshopper 0.7 and use the new Scripting components. The old scripting components are still there as well for legacy purposes so you won't lose any functionality.

I realize you're merely following a tutorial here.

I'll run this code after dinner, see if I can find all the problems.

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Ah, of course if you supply the wrong number for gs it will also fail.

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Here's my attempt, rewritten for RhinoCommon:

Dim grid As New List(Of List(Of Point3d))
Dim row As New List(Of Point3d)

'Iterate over all points
For i As Integer = 0 To pts.Count - 1
  'Add the current point to the current row.
  row.Add(pts(i))

  'If the row has gs items in it, it is complete and we move on.
  If (row.Count = gs) Then
    grid.Add(row)
    row = New List(Of Point3d)
  End If
Next

'If the last row is not empty, we have a wrong value of gs
If (row.Count <> 0) Then
  Print("the gs value was wrong, it does not provide a complete set of rows")
  Return
End If

Dim mid_pts As New List(Of Point3d)

For i As Integer = 0 To grid.Count - 2
  Dim row0 As List(Of Point3d) = grid(i)
  Dim row1 As List(Of Point3d) = grid(i + 1)

  For j As Integer = 0 To row0.count() - 2
    Dim mid_pt As Point3d = (row0(j) + row0(j + 1) + row1(j) + row1(j + 1)) / 4.0
    mid_pts.add(mid_pt)
  Next
Next

a = mid_pts


--
David Rutten
david@mcneel.com
Poprad, Slovakia
thanks for answering. i tried it and it returns no values...
Is there a message in the [out] field?
Can you post the points you're working on and the value of gs you're using?

--
David Rutten
david@mcneel.com
Poprad, Slovakia

Dear David.

I'm trying to create a triply periodic minimal surface in Grasshopper but I have this message in milipede component index was out of range must be non-negative and less than the size of the collection. do YOU HAVE A solution for this problem.

Attachments:

I'm not the developer of millipede, nor do I have it installed. I think you need to ask someone else.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service