I’m solving following issue. I have a set of objects (in my case 3 dimensional points) where I need to add some custom information to the object, measure the distance in Z axis from 0 and make a text visible in the model.
All works but I don’t know how to sort the array. If it would be only points I could use the Rhino.SortPoints method but I need objects. The problem is that I need to number the components going from Left point in X - axis to the right. This is my script
Call Ketlovak_new()
Sub Ketlovak_new()
Dim arrObjects, strObject, StrNewObject, strName, arrPt
Dim poc, z
arrObjects = Rhino.GetObjects("Select points of components", 1)
If IsArray(arrObjects) Then
For Each strObject In arrObjects
poc = poc + 1
arrPt = rhino.PointCoordinates(strObject)
Rhino.AddText poc, arrPt, 5
strName = Rhino.GetAttributeData(strObject, "Ketlovak", "Komponent")
strNewObject = Rhino.AddPoint(rhino.PointCoordinates(strObject))
If Not IsNull(strNewObject) Then
z = -arrPt(2)
Rhino.ObjectName strNewObject, "Point " & CStr(poc) & " " & strName
Rhino.SetUserText strNewObject, "Length", CStr(z)
Rhino.SetUserText strNewObject, "Compo", strName
End If
Next
End If
End Sub
Option Explicit
Call Ketlovak_new()
Sub Ketlovak_new()
Dim arrObjects, strObject, StrNewObject, strName, arrPt
Dim poc, z
arrObjects = Rhino.GetObjects("Select points of components", 1)
If IsArray(arrObjects) Then
'sort objects based on the X coordinate-----------------
Dim X,i : ReDim X(Ubound(arrObjects))
For i=0 To Ubound(arrObjects)
X(i) = Rhino.PointCoordinates(arrObjects(i))(0)
Next
'-------------------------------------------------------
Call Sort2ListsNumber(X, arrObjects)
'-------------------------------------------------------
Rhino.EnableRedraw False
For Each strObject In arrObjects
poc = poc + 1
arrPt = rhino.PointCoordinates(strObject)
Rhino.AddText poc, arrPt, 5
strName = Rhino.GetAttributeData(strObject, "Ketlovak", "Komponent")
'-------------------------------------------------------
If isnull(strName) Then strName = ""
'-------------------------------------------------------
strNewObject = Rhino.AddPoint(rhino.PointCoordinates(strObject))
If Not IsNull(strNewObject) Then
z = -arrPt(2)
Rhino.ObjectName strNewObject, "Point " & CStr(poc) & " " & strName
Rhino.SetUserText strNewObject, "Length", CStr(z)
Rhino.SetUserText strNewObject, "Compo", strName
End If
Next
Rhino.EnableRedraw True
End If
End Sub
Sub Sort2ListsNumber(ByRef arrNumbers, ByRef arrSecond) 'sorts 2 same size arrays based of sorting 1st one ( Numbers )
Dim arrTemp,i,n, arrSort2 : ReDim arrSort2(Ubound(arrSecond))
'creating dictionary object
Dim Dict : Set Dict = CreateObject("Scripting.Dictionary")
'making indexed dictionary objects
For i=0 To Ubound(arrNumbers)
If Not Dict.Exists(arrNumbers(i)) Then
Dict.Add arrNumbers(i), array(i) 'checking if key exists
Else 'if key exists, appent new value at the end of the array:
Dict.Item(arrNumbers(i)) = Rhino.JoinArrays(Dict.Item(arrNumbers(i)), array(i))
End If
Next
'sorting strings
arrNumbers = Rhino.SortNumbers(arrNumbers)
'assigning values to 2nd array
For i=0 To Ubound(arrNumbers)
arrTemp = Dict.Item(arrNumbers(i)) 'reading key items array ( unsorted indices )
n = arrTemp(Ubound(arrTemp)) 'getting last index
ReDim Preserve arrTemp(Ubound(arrTemp)-1) 'shrinking items
Dict.Item(arrNumbers(i)) = arrTemp
arrSort2(i) = arrSecond(n)
Next
arrSecond = arrSort2
Erase arrSort2 'cleanup
End Sub