Grasshopper

algorithmic modeling for Rhino

Hi all, i'm starting to write code, and i'm a newbie 


i wrote a function in vb where i apply in input a vector of 3d points and a number that identify the number 
of points that i use as control in loop to connect with line command this point i with the point i+1, but 
something doesn't work! i've only the last line draw in my viewport.

that's the code: (don't worry about  variable like pt3,pt4, or other...unused...i use all this in second time to connect 
in different way this points between us)


  Private Sub RunScript(ByVal ListPoints As List(Of On3dPoint), ByVal NColonne As Integer, ByRef A As Object, ByRef B As Object) 
    Dim pt1,pt2,pt3,pt4 As On3dPoint
    Dim count As Integer
    Dim arr_line As New List(Of OnLine)

    Dim line As New OnLine

    Dim N_matrix As Integer

    N_matrix = (ncolonne * ncolonne) - Ncolonne - 1

    For count = 0 To N_Matrix

      pt1 = ListPoints(count)
      print(pt1.x)

      pt2 = ListPoints(count + 1)
      print(pt2.x)

      line.Create(pt1, pt2)

      arr_line.Add(line)







    Next
    B = arr_line

  End Sub 


thanks all.

Views: 489

Replies to This Discussion

Hi Daniel,

the problem is that you are adding the same line over and over again to the list. OnLine is a class, meaning that when you do this:

arr_line.Add(line)

you end up with a list filled with references to the same line. And

line.Create(pt1, pt2)

thus changes all the elements in arr_line.

You should make sure you create a new line for every solution. So instead of:

line.Create(pt1, pt2)

You should use:

arr_line.Add(New OnLine(pt1, pt2))

--
David Rutten
david@mcneel.com
Poprad, Slovakia
thanks a lot! it works! :) but now i need to understand the concept of this (new online...)
Let me try. Hope I don't go overboard on this...

There are two types of data you can have in .NET (and many other languages). Reference Types and Value Types.

A Value Type is the easiest to understand. It's basically just the data directly in memory. For example if you declare a new integer, that integer value is stored 'inside' the variable:

Dim val As Integer

val contains zero by default, and no matter what you do, val always contains a number. You can of course assign a value directly to it at declaration like so:

Dim val As Integer = 55

Whenever you pass this integer to a function, val is copied (unless you pass it by reference, but let's assume now we use the default mechanism). This means that a function cannot screw with 'our' val:

Dim val As Integer = 55
SquareInteger(val)
...
...

Public Function SquareInteger(ByVal int As Integer) As Integer
   int *= int
   Return int
End Function


val goes into SquareInteger(), it is copied into the int parameter, so when the function multiplies int by itself, it does not affect val.


to be continued >>>
OnLine on the other hand is a Reference Type. The difference is that the actual data that represents the line (the x, y, z coordinates of the start and end points, 6 numbers in total) is not stored 'inside' the OnLine variable. It is instead stored somewhere else in memory, and the OnLine variable only contains the address of this chunk of far-away memory. In other words, it contains not the value, but a reference to the value.

So when I say:

Dim ln As New OnLine()

then the computer creates a new OnLine instance (the 6 numbers) somewhere deep down in the RAM, and ln merely holds the address of this instance.

When I call a function that takes an OnLine as an input, it is in fact given an address, meaning that any changes to the OnLine from within the function can have far reaching effects:

Dim ln As New OnLine()
ln.From = New On3dPoint(10, 10, 0)
ln.To = New On3dPoint(15, 20, 3)
...
...

Public Sub FlattenLine(ByVal line As OnLine)
   Dim project As New OnXForm()
   project.PlanarProjection(OnPlane.World_xy)
   line.Transform(project)
End Sub


This function changes the line parameter, and since OnLine is a Reference Type, it also affects every other variable that points to the same memory.


to be continued >>>
So to backtrack to your original problem of filling a list with OnLines. Let's say we create a list and fill it with integers, like so:

Dim val As Integer = 1
Dim lst As New List(Of Integer)
For i As Integer = 0 To 9
   val *= 2
   lst.Add(val)
Next


Since val is a ValueType, when we assign it to the list we actually put a copy of val into the list. Thus, the list contains the following memory layout:

[0] = 2
[1] = 4
[2] = 8
[3] = 16
[4] = 32
[5] = 64
[6] = 128
[7] = 256
[8] = 512
[9] = 1024

Now let's assume we do the same, but with OnLines:

Dim ln As New OnLine(A, B)
Dim lst As New List(Of OnLine)
For i As Integer = 0 To 9
   ln.Transform(xform)
   lst.Add(ln)
Next


When we declare ln on line 1, it is assigned an address in memory, say "24 Bell Ave." Then we modify that one line over and over, and keep on adding the same address to lst. Thus, the memory layout of lst is now:

[0] = "24 Bell Ave."
[1] = "24 Bell Ave."
[2] = "24 Bell Ave."
[3] = "24 Bell Ave."
[4] = "24 Bell Ave."
[5] = "24 Bell Ave."
[6] = "24 Bell Ave."
[7] = "24 Bell Ave."
[8] = "24 Bell Ave."
[9] = "24 Bell Ave."

To do this properly, we need to create a unique line for every element in lst:

Dim lst As New List(Of OnLine)
For i As Integer = 0 To 9
   Dim ln As New OnLine(A, B)
   ln.Transform(xform)
   lst.Add(ln)
Next


Now, ln is constructed not just once, but whenever the loop runs. And every time it is constructed, a new piece of memory is reserved for it and a new address is created. So now the list memory layout is:

[0] = "24 Bell Ave."
[1] = "12 Pike St."
[2] = "377 The Pines"
[3] = "3670 Woodland Park Ave."
[4] = "99 Zoo Ln."
[5] = "13a District Rd."
[6] = "2 Penny Lane"
[7] = "10 Broadway"
[8] = "225 Franklin Ave."
[9] = "420 Paper St."

--
David Rutten
david@mcneel.com
Poprad, Slovakia
ps. In the new Rhino SDK (RhinoCommon), a lot of geometry types are in fact Value Types. Point3d, Vector3d, Interval, Line, Arc, Circle, Plane, Transform etc. etc. These now all behave like Integers.

This can be quite confusing and it often requires very different kinds of methods to deal with Lines and Curves (Curves are still Reference Types). However, Value Types allow us to work with these objects much faster than before.

--
David Rutten
david@mcneel.com
Poprad, Slovakia
perfect :) now i think to understand all :D thanks a lot :)

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service