Grasshopper

algorithmic modeling for Rhino

Hey everyone, I have a functioning bit of code that I'm curious about simplifying. I'm constructing a datatree and I'd like to do the following in a single loop (Where J1 through J6 are functions that return a custom class that has a property "limIndx"):

For Each item As Integer In J1.limIndx
limOut.add(item, New GH_Path(0, 1))
Next
For Each item As Integer In J2.limIndx
limOut.add(item, New GH_Path(0, 2))
Next
For Each item As Integer In J3.limIndx
limOut.add(item, New GH_Path(0, 3))
Next
For Each item As Integer In J4.limIndx
limOut.add(item, New GH_Path(0, 4))
Next
For Each item As Integer In J5.limIndx
limOut.add(item, New GH_Path(0, 5))
Next
For Each item As Integer In J6.limIndx
limOut.add(item, New GH_Path(0, 6))
Next

The only thing I'm not clear on is how to address/loop through J1 through J6 since it's name has both a letter and number.

I know the following is wrong, but it possible to do something like:

For i as integer = 1 to 6
limOut.add("J(i)".limIndx(i), New GH_Path(0, i))
Next

the "J(i)" part is what I'm curious about.

Any help is appreciated.

-Brian Harms

Views: 380

Replies to This Discussion

I don't think it's possible to really automate this without going into reflection or Linq. I don't know enough about Linq to say anything more though. I think I'd write something like the following:

Dim indices As MyClass() = new MyClass() {J1.linIndx, J2.linIndx, ... , JN.linIndx}

For i As Int32 = 0 To indices.Length -1

  For Each item As Integer In indices(i)
    limOut.add(item, New GH_Path(0, i))
  Next

Next

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Incidentally, if you cannot aggregate the calls to J1, J2 etc. ahead of time (that is after all a potentially breaking logic change), then you can create an array of delegates instead and call those delegates from within the loop. That way you can post-pone calls to J1, J2 etc. until just before each item loop.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Y U NO


 For i As Int32 = 0 To indices.Length - 1
limOut.addrange(indices(i), New GH_Path(0, i))
Next

?

Even better.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

That's great, thanks guys.

-Brian

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service