algorithmic modeling for Rhino
Hi all,
I just started to learn gh VB.net. I cannot run thorough a easy code because of some bug in it.
I got a group of lines with different length as input, just wanna put lines with the length that bigger than some number into another arraylist as output.
if anyone of you has an idea to debug, really appreciate your comment and suggestion. Many thanks!
Below is the code:
Private Sub RunScript(ByVal y As List(Of Line), ByVal x As Double, ByRef A As Object)
Dim Input_Line As New Line
Dim New_Line As New Line
Dim New_Lines As List( Of Line )
Dim i As Integer
For i = 0 To y.Count()
Input_Line = y(i)
If Input_Line.Length < x Then
New_Line = Input_Line
End If
New_Lines.Add(New_Line)
Next
A = New_Lines
End Sub
Tags:
For i = 0 To y.Count()
This is the problem. If the list contains 10 lines, then this loop will run from zero to ten:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
which -if you count it- is actually eleven times. Your loop should go to y.Count - 1. Or, you could use a For...Each loop, circumventing the problem altogether:
Dim shortLines As New List(Of Line)
For Each segment As Line in y
If (segment.Length < x) Then
shortLines.Add(segment)
End If
Next
A = shortLines
--------------------------------
Another problem is this line of code:
New_Lines.Add(New_Line)
It is located inside the loop but outside the If statement, meaning it gets run every single iteration. This fills up the short line list with duplicates.
-------------------------------
Here's something else which is redundant:
Dim Input_Line As New Line
Apart from the fact that you don't need a special variable for this at all, you also don't need to add a New keyword. The type Line in RhinoCommon (just like Point3d, Vector3d, Plane, BoundingBox etc. etc.) are Structures, not Classes. Structures always exist when they are defined, whereas Classes can be null ("Nothing" in VB).
-------------------------------
Some more advice:
Dim i As Integer
For i = 0 To y.Count()
You can merge these two lines into one. VB.NET allows you to declare your iteration variable inside the loop:
For i As Integer = 0 To y.Count - 1
--------------------------------
If you don't like the For...Each approach at the top of this answer, here's how to write this using a For...To loop:
Dim shortLines As New List(Of Line)
For i As Integer = 0 To y.Count - 1
If (y(i).Length < x) Then
shortLines.Add(y(i))
End If
Next
A = shortLines
ps. A personal preference of mine is that I always encase the expressions inside If...Then statements in brackets. You technically don't need to do this, but I find it makes the code more readable.
--
David Rutten
david@mcneel.com
Poprad, Slovakia
Such a professional reply!~ Thanks very much! !
Actually, i did a lot monkey rvb before, and now I am struggling with the transition from monkey's vb to grasshopper's vb as a study. It seems a long way to go for me~~, even though I know the basic logic... Hopefully it would be better when I get more familiar with syntax and data transfer in grasshopper vb.net.
Anyway, thanks again!
YW
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by