Grasshopper

algorithmic modeling for Rhino

Hi,

 

I'm new to VB in Grasshopper.  I recently started taking a look at tutorials and just reproducing scripts to get a feel for code.  One of the first scripts I tried was out of the Woo Jae Sung tutorial that takes some built-up geometry composed in Grasshopper and then arrays it in a specific pattern on a host surface.

I got the Rhinocommon SDK help file, and because this Woo tutorial was written using a much earlier version of Grasshopper I have been trying to adjust all of the commands to fit Rhinocommon.

So in this script I changed a few On3dPoint commands to Point3d and because the script is not too complicated, I thought these should be the only necessary changes...

However when I made the changes, the output was that there was a problem with the "pts" funtion on one of the lines in the code.

 

Dim n As Integer = 0

    Dim ptsList As New List(Of List(Of Point3d))

    For i As Integer = 0 To DivU

      Dim ptsRow As New List(Of Point3d)

      For j As Integer = 0 To divV

        Dim pt As New Point3d(pts(n))

        ptsRow.Add(pt)

        n = n + 1

      Next

      ptsList.Add(ptsRow)

    Next

 

I wonder if:

A.  Anyone could help me understand this specific issue in the script

B.  Provide some advice as to how to maybe more efficiently do such conversions as many tutorials that I have for VB were not written in Rhinocommon.  (I tend to find specific tips for correcting syntax in specific places like blogs.  Is this the most efficient way to become more familiar with such differences?  The SDK help file provides the new commands but I seem to be having little problems with less obvious syntax holdups.)

 

Thanks for any kind of help! 

 

 

 

 

Views: 650

Replies to This Discussion

Hi Aaron,

there is no 1:1 mapping from old SDK to RhinoCommon I'm afraid, some things haven't changed at all, some have changed marginally, and some have changed beyond recognition. One of the bigger changes is the usage of Structures ("ValueTypes" as they are known in generic .NET parlance) over Classes. In the old SDK On3dPoint was a class. That means that if you declare an On3dPoint, it would be Nothing:


  Dim pt As On3dPoint   'This results in a pt which doesn't exist, i.e. Nothing


Attempting to call any functions on this would result in a NullReferenceException. Point3d in RhinoCommon is a structure, very much like the integer and boolean types. It is impossible for a structure to not exist. Therefore, when you declare a Point3d, you actually get a valid point at {0,0,0}:


Dim pt As Point3d      'This results in a point at {0,0,0}


The difference between structures and classes ("value types" and "reference types" respectively) is very important and you'll need to understand it otherwise you'll keep running into weird bugs you can't explain. One of the biggest differences to keep in mind is that assigning a Value Type to another Value Type automatically creates a copy of it. Observe these two pieces of code:


  Dim pt0 As New On3dPoint(1,4,0)

  Dim pt1 As On3dPoint = pt0

  pt1.Z = 10


vs.


  Dim pt2 As New Point3d(1,4,0)

  Dim pt3 As Point3d = pt2

  pt3.Z = 10


The first example uses On3dPoint, which is a reference type. "Reference" means that the actual data stored inside the pt0 and pt1 variables is in fact a memory address. When you then try to access the Z component of the point, the computer will read that memory address stored inside the variable, go to that location in memory and extract the number at that location that represents the Z component of the point. So On3dPoints exist somewhere in memory (you don't know where and you shouldn't have to care) and you can have any number of On3dPoint references pointing to that address, i.e. all sharing the same point data.


Not so with value types. Value types don't store their data elsewhere and then remember where elsewhere is supposed to be, they store their data directly inside the variable. So when you assign pt0 to pt1, what's actually happening is that the reference to the On3dPoint instance is copied, and now pt0 and pt1 both point at the same location in memory. Therefore changing the Z of pt1 will also change the Z of pt0, since they're the same Z. However when you assign pt2 to pt3, it also assigns the data inside pt2 to pt3, but in this case the data is in fact the point coordinate itself. So changing the Z of pt3 does not affect the Z of pt2.



Because of this fundamental difference between the old and the new points* you cannot simply translate code, you need to really understand what the code is doing in order to rewrite it. My guess in this case is that you're trying to call a constructor that doesn't exist:


  Dim pt As New Point3d(pts(n))


In the old SDK the best way to create a copy of a point was to use the copy-constructor. However this constructor is missing in RhinoCommon because simply assigning the old point has the same effect:


  Dim pt As Point3d = pts(n)




* Vectors, Planes, Transform matrices, Circles, Arcs, Lines etc. are also all Value Types in RhinoCommon

--

David Rutten

david@mcneel.com

Poprad, Slovakia

 

Thankyou for such a detailed and thorough response.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service