Romoving duplicate polyline in VB Common

Hey,

I am trying to write a simple script to remove duplicated polylines.

the polylines are closed edges of trangular surfaces. each surface has more than 2 duplicates. what i am trying to do is to find the contre of each polyline. if the centres are the same, polyline will be removed from the list.

Here is my script:

    Dim i As Integer
    Dim j As Integer

    Dim threshold As Integer:threshold = 1

    Dim cen As New Point3d
    Dim pLine As New Polyline


    Dim pList As New list(Of Polyline):pList = x
 
    
    Dim cenT As New Point3d
    Dim pLineT As New polyline



    For i = 0 To x.Count - 1
      pLine = x(i)
      cen = pLine.CenterPoint
 
      For j = 0 To x.Count - 1

        If Not j = i Then
          pLineT = x(j)
          cenT = pLineT.CenterPoint

          If cen.DistanceTo(cenT) <= threshold Then

            pList.removeat(j)

          End If

        End If

      Next

    Next

    A = indexList

I think I have found the problem which is: duplicated polylines are being removed more than one time. but I can't find the right syntax to correct it....

Help!!!!

many thanks in advance!!

Kwo

  • up

    the status Kuo

    hey guys,

    i think i have solved the problem by myself. In stead of removing objects from a list, now i just create a new empty list to add non-repeated objects in.

    cheers

    • up

      David Rutten

      Hi Kwo,

      if you're removing items from a collection, you should iterate over it backwards. If you iterate forwards you have to adjust your iteration variable on every remove. There were other weird things in your code, a lot of pointless New keywords. Below is how I would write it.

      Dim tolerance As Double = 1.0

      'First collect all center points.

      Dim c As New List(Of Point3d)

      For Each polyline As Polyline in x

        c.Add(polyline.CenterPoint())

      Next

      For i As Int32 = x.Count - 1 To 1 Step -1

        For j As Int32 = 0 To i-1

          If (c(i).DistanceTo(c(j)) < tolerance) Then

            x.RemoveAt(i)

            c.RemoveAt(i)

            Exit For

          End If

        Next

      Next

      A = x

       

      --

      David Rutten

      david@mcneel.com

      Poprad, Slovakia

      6