Developer: RhinoScript
Summary: Demonstrates how to use the .NET Framework to sort arrays in RhinoScript.
One of the big limitations in VBScript is the fact that there is no easy way to sort a list of items. To put a list in alphabetical order requires you to either use of the pre-canned RhinoScript methods, such as SortNumbers, SortPoints or SortStrings, or write sorting function of your own, like the following bubble sort code:
For i = (UBound(arrNames) - 1) to 0 Step -1 For j= 0 to i If UCase(arrNames(j)) > UCase(arrNames(j+1)) Then strHolder = arrNames(j+1) arrNames(j+1) = arrNames(j) arrNames(j) = strHolder End If Next Next
There is another option and that is to use the .NET Framework to help. The majority of .NET Framework classes unusable in VBScript. However, there are a large number of .NET classes that have COM-callable wrappers. This means these classes include a COM interface enabling them to be accessed from VBScript.
For example, consider the following script:
Set DataList = CreateObject("System.Collections.ArrayList") DataList.Add "B" DataList.Add "C" DataList.Add "E" DataList.Add "D" DataList.Add "A" DataList.Sort() For Each strItem in DataList Rhino.Print strItem Next
Notice that we have created an instance of the .NET Framework class System.Collections.ArrayList. We then use the Add method to add five items to the list. To sort the list, all we need to do is call the ArrayList's Sort method.
Now, what if you really wanted those values in descending order? In this case, just call ArrayList's Sort method after calling the Sort method.
Set DataList = CreateObject("System.Collections.ArrayList") DataList.Add "B" DataList.Add "C" DataList.Add "E" DataList.Add "D" DataList.Add "A" DataList.Sort() DataList.Reverse() For Each strItem in DataList Rhino.Print strItem Next
Also, have you ever tried to remove an item from a VBScript array? It is no easy task. But, with the .NET Framework's ArrayList, you just need to call the Remove method. The following script an ArrayList, sorts it, and then removes the entry for D.
Set DataList = CreateObject("System.Collections.ArrayList") DataList.Add "B" DataList.Add "C" DataList.Add "E" DataList.Add "D" DataList.Add "A" DataList.Sort() DataList.Remove("D") For Each strItem in DataList Rhino.Print strItem Next
Copying items VBScript arrays and an ArrayList is just as easy. In the following script, an ArrayList is used to sort a list of layer names.
LayerNames = Rhino.LayerNames Set DataList = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(LayerNames) DataList.Add DataList(i) Next DataList.Sort() For i = 0 To UBound(LayerNames) LayerNames(i) = DataList(i) Next Layer = Rhino.ListBox(LayerNames, "Layer to set current")