algorithmic modeling for Rhino
I have the following code in a grasshopper VB.Net Script component:
Private Sub RunScript(ByVal S_Vector As List(Of Vector3d), ByRef S_Mid As Object)
Dim S_MidVector As New List(Of Vector3d)
Dim i As Integer
For i = 0 To S_Vector.Count - 2
S_MidVector.Add((S_Vector.Item(i) / S_Vector.Item(i).length) + (S_Vector.Item(i +_
1) /S_Vector.Item(i + 1).length))
S_MidVector.Item(i) = S_MidVector.Item(i).Unitize()
Next
S_Mid = S_MidVector
End Sub
I get an error saying that type 'Boolean' cannot be converted to Rhino.Geometry.Vector3d, which apparently means that Unitize() is returning a boolean, rather than the unitized vector. How can I get the unitized vector?
Tags:
just do
S_MidVector.Item(i).Unitize()
instead of
S_MidVector.Item(i) = S_MidVector.Item(i).Unitize()
;)
I did try that, but the vectors don't come out Unitized. The component output still has the initial length vectors.
Vectors are value types, when you call:
S_MidVector.Item(i), you get a copy of the vector that's inside the list. Unitizing this copy won't affect the original vector. What you need to do:
Dim vec As Vector3d = S_MidVector.Item(i)
vec.Unitize()
S_MidVector.Item(i) = vec
--
David Rutten
david@mcneel.com
Poprad, Slovakia
Thank you for explaining that the .Item() call returns a copy. Explains why the .length property call worked in the previous line for what I was using it for and the .Unitize() method didn't work, which was why I was so confused.
Ideally value types should not have methods that change the type variables. Ideally Unitize() should return a unitized vector rather than changing the xyz values of the vector itself. However we decided to not adhere to this guideline for RhinoCommon.
Native framework classes like Rectangle and PointF are also mutable, so even Microsoft breaks this rule.
--
David Rutten
david@mcneel.com
Poprad, Slovakia
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