algorithmic modeling for Rhino
I've searched this forum a little and found a few helpful tips, but I'm still struggling creating a data tree in my VB component.
Here's what I have so far:
Private Sub RunScript(ByVal sortedNumberList As List(Of Double), ByVal tolerance As Double, ByRef binnedList As Object)
I'm specifically struggling with line 42:
bucketOfBins.Add(bin, New GH_Path(bucketCounter))
I declared buckOfBins on line 17:
Dim bucketOfBins As New DataTree(Of Object)
From the readout on the "out" output of the VB component, the script is working as I intended, grouping the numbers in the list into bins. And the return value on line 53 is a data tree with the correct branching structure, but instead of the numbers in the lists, it says:
System.Collections.Generic.List`1[System.Object]
Any help would be appreciated. Also, any better resources for learning VB.NET specifically for Grasshopper rather than the Grasshopper Primer would be appreciated as well.
Tags:
You're adding List(Of Object) to the datatree, that's why it fails.
I rewrote your script, it now works on non-sorted numbers and it can handle multiple lists of numbers:
Private Sub RunScript(ByVal Numbers As List(Of Double), ByVal Tolerance As Double, ByRef Bins As Object)
If (Numbers.Count = 0) Then Return
Dim sortedNumbers As New List(Of Double)(Numbers)
sortedNumbers.Sort()
Dim min = sortedNumbers(0)
Dim max = sortedNumbers(sortedNumbers.Count - 1)
Dim binCount As Integer = Convert.ToInt32(Math.Floor((max - min) / Tolerance))
Print("binCount = " & binCount)
Dim binBoundaries As New List(Of Double)
For i As Integer = 1 To binCount
binBoundaries.Add(min + i * Tolerance)
Next
Print("Bin : Number")
Dim binTree As New DataTree(Of Double)
For Each number As Double In Numbers
Dim binIndex As Int32 = 0
For Each boundary As Double In binBoundaries
If (number < boundary) Then
Print(" {0} : {1}", binIndex, number)
binTree.Add(number, New GH_Path(Iteration, binIndex))
Exit For
End If
binIndex += 1
Next
Next
Bins = binTree
End Sub
--
David Rutten
david@mcneel.com
David,
Thank you so much. It is great to have a working bin sort. FYI, it drops the last number in the list.
Would you help me understand this line?
binTree.Add(number, New GH_Path(Iteration, binIndex))
I get that you are adding the number to the data tree, but I don't understand the New GH_Path(Iteration, binIndex) part. I'm reading the SDK help file and I understand that the GH_Path Class is the structure or path of a item or list of items. However, more explanation would really help. I don't understand the Iteration Property or how you are using it.
Thanks,
Damon
A GH_Path is an array of integers, since I use two integers in the constructor, we'll end up with paths that have two parts; {0;0} instead of just {0}
The Integer value is a member variable of the class itself (it gets set by Grasshopper). Every time the RunScript method is called, the Iteration value will be one higher. Then it gets reset whenever a new solution starts.
By including the Iteration in the paths, I make sure that the datatree I create inside RunScript() doesn't overlap with a previous one, which makes the script capable of running multiple times.
As for the dropped value, perhaps you should add double.MaxValue to the binBoundaries list, just to ensure all possible values are always included. I haven't actually tested this fix though.
--
David Rutten
david@mcneel.com
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
© 2024 Created by Scott Davidson. Powered by