Grasshopper

algorithmic modeling for Rhino

Hello,

I m having problems with the following code:

  Private Sub RunScript(ByVal ptList1 As List(Of On3dPoint), ByVal ptList2 As List(Of On3dPoint), ByRef lineNet As Object)

    Call Network(ptList1, ptList2)

  End Sub

  '<Custom additional code>

  Function Network(ptList1 As list(Of On3dPoint), ptList2 As List(Of On3dPoint)) As OnLine

    Dim line2 As New OnLine

    Dim dist As Double 'variable to store distance between points

    Dim maxDist As Double
    Dim minDist As Double

    maxDist = 20
    minDist = 10

    Dim count1 As Integer
    count1 = ptList1.Count()'set length of list for loop
    Dim count2 As Integer
    count2 = ptList2.Count()


    Dim newPtList1 As List(Of On3dPoint)
    Dim newPtList2 As List(Of On3dPoint)

    newPtList1 = ptList1
    newPtList2 = ptList2

    ptList1.RemoveAll
    ptList2.RemoveAll

    For i As Integer =0 To count2 - 1
      For j As Integer = 0 To count1 - 1
        Dim pt2 As On3dPoint
        pt2 = newPtList2(i)

        Dim pt1 As On3dPoint
        pt1 = newPtList1(i)

        dist = pt2.DistanceTo(pt1)

        If (dist < maxDist & dist > minDist) Then

          line2 = New OnLine(pt2, pt1)
          ptList2.Add(pt1)

        Else
          ptList1.Add(pt1)

        End If
      Next
    Next

    Dim count As Integer
    count = ptList1.Count()

    If (count > 0) Then
      Call Network(ptList1, ptList2, lineNet)
    End If

  End Function


What i have as input are two lists of points (the first one having n-items inside while the other one has only one starting point). It s supposed to work as a recursive function always returning one or more lines in the end.

The error i m getting is because of the ptList1/ptList2.RemoveAll command.

Does anyone have a clue why i can t remove the items from the lists? What i'm basically trying to do is just clean them of all content.

I've attached the def with the code as well.

Thank you in advance,
Tudor

Views: 353

Attachments:

Replies to This Discussion

01 Dim newPtList1 As List(Of On3dPoint)
02 Dim newPtList2 As List(Of On3dPoint)
03
04 newPtList1 = ptList1
05 newPtList2 = ptList2
06
07 ptList1.RemoveAll
08 ptList2.RemoveAll


line 1, declare a variable, but don't instantiate it.

line 4, copy the reference from ptList1 into newPtList1. Now, ptList1 and newPtList1 are both pointing to the same List in memory.

line 7, Call the RemoveAll method on ptList1. Since ptList1 and newPtList1 both 'share' the same List, they are both affected.


I think what you want to do is:

01 Dim newPtList1 As New List(Of On3dPoint)
02 Dim newPtList2 As New List(Of On3dPoint)
03
04 newPtList1.AddRange(ptList1)
05 newPtList2.AddRange(ptList2)
06
07 ptList1.RemoveAll
08 ptList2.RemoveAll

Now, ptList1 and newPtList1 are two different lists and if you modify ptList1 it will no longer affect newPtList1. Note however that that points inside ptList1 and newPtList1 are still shared, so doing this:

ptList1(0).x = 18.4

will change the x coordinate of the point in both lists.

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Oh, and don't use RemoveAll(). RemoveAll() requires a predicate, and it will remove all objects from the list that adhere to the predicate rules. For example you could write a predicate that removes all numbers smaller than 6.23 from a List(Of Double). If you only want to erase a list use Clear().

Also, your loop seems to run to count1 and count2. I think you probably need count1-1 and count2-1 otherwise you'll overstep the list bounds.

--
David Rutten
david@mcneel.com
Poprad, Slovakia
If (dist < 20 & dist > 10) Then

Nope, that works in C# but not VB. You need to use And or AndAlso:

If (dist < 20) AndAlso (dist > 10) Then

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Dim line As New OnLine
line = New OnLine(pt2, pt1)

I don't know why you're declaring this line and not using it, but you're instantiating it twice. Either do this:

Dim line As New OnLine(pt2, pt1)

or this:

Dim line As OnLine
line = New OnLine(pt2, pt1)

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Not to mention your recursive function doesn't return any values...

:)

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Thanks a lot david!!

i 'll go read through the code again, "repairing" it where you pointed out the mistakes.

Just to make it clear for myself:

newPtList.AddRange(ptList) -> will store the items from ptList in newPtList

ptList.RemoveAll -> will delete all items in ptList and in the same time from newPtList?

In the end, the function should return the lines, which i hope i ll manage to create within the function. That's where another question comes up: how to send the lines from the function to the output of the vb component?

Thanks a lot,
Tudor
Not quite, List(Of T) is a class (also called a "Reference type"). So when you say:

Dim A As SomeClass
A = B
B.PerformSomeActionOnThisObject()

Then both A and B will be pointing to the same instance of that class in memory. When you call a method on B that changes the state of B, it will also change the state of A and vice versa.


If A and B were primitives or structures instead (also called "Value types"), then any contents will be copied during an assignment. Thus:

Dim A As Drawing.Rectangle
A = B
B.Inflate(4, 4)

In this case the data inside B will be copied to A, because Drawing.Rectangle is a structure, not a class. Therefore changing B will not affect A.


I know this is somewhat unpredictable and inconsistent, but we'll have to live with it.

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Hello David,

Thanks again for your replies, they helped a lot!!!

This is a temporary result(about what i was trying to do), just managed to get it going, even if at this state it tends to CRASH a lot. (i ll have to change the logic a bit)

Anyway i'll work on it and post the final result once i have it.

Cheers!
Attachments:

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service