<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Script to Draw lines between points in VBA Forum</title>
    <link>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12796174#M645</link>
    <description>&lt;P&gt;Disregard, I just needed to change the first for loop starting at 2.&lt;/P&gt;&lt;LI-CODE lang="visual-basic"&gt;  For i = 2 To ws.UsedRange.Rows.Count ' Start from the second row to ignore headers
        Set row = ws.Rows(i)
        grnum = row.Cells(1, 4).Value '4th row is group/ row #
        If groups.Exists(grnum) Then
            groups(grnum).Add row.Value
        Else
            groups.Add grnum, New Collection
            groups(grnum).Add row.Value
        End If
    Next i&lt;/LI-CODE&gt;</description>
    <pubDate>Fri, 24 May 2024 17:38:23 GMT</pubDate>
    <dc:creator>carl3ZPF6</dc:creator>
    <dc:date>2024-05-24T17:38:23Z</dc:date>
    <item>
      <title>Script to Draw lines between points</title>
      <link>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12790533#M635</link>
      <description>&lt;P&gt;I have an input file that looks like the following:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="carl3ZPF6_0-1716392652517.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1365929i1424DDC80A56F464/image-size/medium?v=v2&amp;amp;px=400" role="button" title="carl3ZPF6_0-1716392652517.png" alt="carl3ZPF6_0-1716392652517.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I need to draw polylines between the points of the same group, the color of these lines is specified in the Line color column:&lt;/P&gt;&lt;P&gt;1- Blue&lt;/P&gt;&lt;P&gt;2-Red&lt;/P&gt;&lt;P&gt;3-Green&lt;/P&gt;&lt;P&gt;4-Yellow&lt;/P&gt;&lt;P&gt;So the output should look something like this (the numbers correspond to the # column, I have just shown them for reference they don't need to be part of the output):&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="carl3ZPF6_1-1716393008322.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1365932i6A5DC6B8AA42BE1C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="carl3ZPF6_1-1716393008322.png" alt="carl3ZPF6_1-1716393008322.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;This is obviously just an example the actual files I will use this script on are like 9,000 pts total. My start so far (not much just determining how many rows and how many groups, my plan was to do something in the second for loop but I was not sure what, perhaps an index match?):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;Sub EqptLayout()
Dim acadApp As Object
Dim acadDoc As Object
Dim acadModelSpace As Object
Dim cl As Object
Dim i As Integer
Dim d As Integer
Dim excelApp As Object
Dim excelWorkbook As Object
Dim excelSheet As Object
Dim polyline As Object
Dim bearing As String
Dim foundation As String
Dim splice As String
Dim numRows As Integer
Dim startRow As Integer



Dim dblX As Double
Dim dblY As Double
Dim dblZ As Double
Dim dblRotation As Double


Dim utilObj As Object   ' Late bound object
Set utilObj = ThisDrawing.Utility


' Start AutoCAD (if not already running)
On Error Resume Next
Set acadApp = GetObject(, "AutoCAD.Application")
On Error GoTo 0

If acadApp Is Nothing Then
    Set acadApp = CreateObject("AutoCAD.Application")
    acadApp.Visible = True
End If

Set acadDoc = acadApp.ActiveDocument
Set acadModelSpace = acadDoc.ModelSpace

Dim msg As String
     i = 0
     d = 0
Dim excelFilePath As String
excelFilePath = "C:\Users...\Test4.xlsx"
Set excelApp = CreateObject("Excel.Application")
Set excelWorkbook = excelApp.Workbooks.Open(excelFilePath)
Set excelSheet = excelWorkbook.Sheets(1)


With excelSheet

    For Each cl In .Range(.[A1].CurrentRegion.Columns(1).Address) 'for each excel row
    i = i + 1
    Next cl
    
   numRows = excelApp.worksheetfunction.Max(.Range("D1:D" &amp;amp; i))
   
   startRow = excelApp.worksheetfunction.Min(.Range("D1:D" &amp;amp; i))
   
   For d = startRow To numRows 'for each group
   
   'perform line drawing here?
   
   Next d
   
End With





End Sub&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Any help is appreciated for how to approach this, thanks. Input file is attached.&lt;/P&gt;</description>
      <pubDate>Wed, 22 May 2024 16:00:28 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12790533#M635</guid>
      <dc:creator>carl3ZPF6</dc:creator>
      <dc:date>2024-05-22T16:00:28Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Draw lines between points</title>
      <link>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12791624#M636</link>
      <description>&lt;P&gt;Is there a HashMap or Dictionary in VBA? &amp;nbsp;Seems like it would be easier to iterate all the rows and map them by Group#&lt;/P&gt;&lt;P&gt;Then sort the rows in each key by order in group. If needed. This is Python, but I hope it will illustrate my point&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import traceback
from pyrx_imp import Rx, Ge, Gi, Db, Ap, Ed
import openpyxl as xl

def PyRxCmd_doit() -&amp;gt; None:
    try:
        # maps
        groups = {}
        clr = {1: 5, 2: 10, 3: 90, 4: 50}

        wb = xl.open("e:/Test4.xlsx")
        ws = wb["Sheet1"]
        
        #assumes sorted order in group
        for row in ws.iter_rows(values_only=True):
            grnum = row[3]
            if grnum in groups:
                groups[grnum].append(row)
            else:
                groups[grnum] = [row]

        lines = []
        for group in groups.values():
            for idx in range(1, len(group)):
                sl = group[idx - 1]
                el = group[idx]
                line = Db.Line(Ge.Point3d(sl[1], sl[2], 0.0), 
                               Ge.Point3d(el[1], el[2], 0.0))
                line.setColorIndex(clr[sl[4]])
                lines.append(line)
        Db.curDb().addToModelspace(lines)

    except Exception as err:
        traceback.print_exception(err)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="lines.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1366173i27218307CF2D1D9D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="lines.png" alt="lines.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2024 01:57:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12791624#M636</guid>
      <dc:creator>daniel_cadext</dc:creator>
      <dc:date>2024-05-23T01:57:22Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Draw lines between points</title>
      <link>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12793233#M637</link>
      <description>&lt;P&gt;Hi Daniel, VBA just has the Dictionary object, Add:key,value.&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2024 15:00:03 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12793233#M637</guid>
      <dc:creator>Ed__Jobe</dc:creator>
      <dc:date>2024-05-23T15:00:03Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Draw lines between points</title>
      <link>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12793338#M638</link>
      <description>&lt;P&gt;OK, I think I can use a dictionary where the key is the # and the items are an array consisting of group #, color, and order in group&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2024 15:29:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12793338#M638</guid>
      <dc:creator>carl3ZPF6</dc:creator>
      <dc:date>2024-05-23T15:29:34Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Draw lines between points</title>
      <link>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12793361#M639</link>
      <description>&lt;P&gt;On second thought, I am not sure having 9000+ dictionaries is the best solution&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2024 15:38:44 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12793361#M639</guid>
      <dc:creator>carl3ZPF6</dc:creator>
      <dc:date>2024-05-23T15:38:44Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Draw lines between points</title>
      <link>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12793838#M640</link>
      <description>&lt;P&gt;The task you are facing could/should be divided into 2 subtasks: retrieving data from Excel sheet (or whatever data source, for that matter) into a data model; draw the lines using the data model. The "draw" part is really simple, if the data model is correctly create, for example, it is a collection of a sub data model which is a group of points used to create lines.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, your code should look like&lt;/P&gt;
&lt;P&gt;Public Sub DrawLinesFromExternalData(xlsFileName as String)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Dim groups As LineGroupCollection&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Set groups = ReadDataFromSheet(xlsFileName)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; If groups Is Nothing Then&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; MsgBox "Reading data file failed!"&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Else&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; DrawGroupLines groups&lt;/P&gt;
&lt;P&gt;&amp;nbsp; End If&lt;/P&gt;
&lt;P&gt;End Sub&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see, you need to have a data model (class LineGroupCollection), which is a collection of another class LineGroup, which would hold all the points in a group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hopefully, you do know how to create class in VBA. To simplify things a bit, you can use an array of LineGroup class, instead of LineGroupCollection class.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately, VBA does not provide many built-in data structure. If you do it with .NET API, retrieve the group points and hold them in sortable/groupable data structures are quite easy, but with VBA, you may have to sort/group the data with your own code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, you really should do it from AutoCAD VBA side, rather than from Excel VBA side. Otherwise drawing a few thousands of lines from Excel side would take much longer time than drawing them within AutoCAD VBA.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2024 19:25:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12793838#M640</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2024-05-23T19:25:33Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Draw lines between points</title>
      <link>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12793952#M641</link>
      <description>&lt;P&gt;Thanks to &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/8575899"&gt;@daniel_cadext&lt;/a&gt; I was able to get the data reading into a dictionary based on his data structures:&lt;/P&gt;&lt;LI-CODE lang="general"&gt;Sub PyRxCmd_doit()
    Dim acadApp As Object
    Dim acadDoc As Object
    Dim acadModelSpace As Object
    Dim groups As Object
    Dim clr As Object
    Dim wb As Object
    Dim ws As Object
    Dim row As Variant
    Dim grnum As Variant
    Dim idx As Integer
    Dim sl As Variant
    Dim el As Variant
    Dim line As AcadLWPolyline
    Dim lines As Object
    
    Dim utilObj As Object   ' Late bound object
Set utilObj = ThisDrawing.Utility
    Dim polyPoints As Variant

' Start AutoCAD (if not already running)
On Error Resume Next
Set acadApp = GetObject(, "AutoCAD.Application")
On Error GoTo 0

If acadApp Is Nothing Then
    Set acadApp = CreateObject("AutoCAD.Application")
    acadApp.Visible = True
End If
    
    Set groups = CreateObject("Scripting.Dictionary")
    Set clr = CreateObject("Scripting.Dictionary")
    
    clr.Add 1, 5
    clr.Add 2, 10
    clr.Add 3, 90
    clr.Add 4, 50
    
Dim excelFilePath As String
excelFilePath = "C:Test4.xlsx"
Set excelApp = CreateObject("Excel.Application")
Set wb = excelApp.Workbooks.Open(excelFilePath)
Set ws = wb.Sheets(1)
    
    For Each row In ws.UsedRange.Rows
        grnum = row.Cells(1, 4).Value
        If groups.Exists(grnum) Then
            groups(grnum).Add row.Value
        Else
            groups.Add grnum, New Collection
            groups(grnum).Add row.Value
        End If
    Next row
    
    Set lines = CreateObject("Scripting.Dictionary")
    
    For Each group In groups
        For idx = 2 To groups(group).Count
            sl = groups(group)(idx - 1)
            el = groups(group)(idx)
            
            utilObj.CreateTypedArray polyPoints, vbDouble, sl(1, 2), sl(1, 3), el(1, 2), el(1, 3)
            Set line = ThisDrawing.ModelSpace.AddLightWeightPolyline(polyPoints)
            'line.TrueColor = clr(sl(4))
            lines.Add lines.Count + 1, line
        Next idx
    Next group
    
    wb.Close False
End Sub&lt;/LI-CODE&gt;&lt;P&gt;I am now able to get all lines to plot,&amp;nbsp; but I had to comment out the color line if not commented out I get "Subscript out of range", any thoughts?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2024 20:17:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12793952#M641</guid>
      <dc:creator>carl3ZPF6</dc:creator>
      <dc:date>2024-05-23T20:17:25Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Draw lines between points</title>
      <link>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12794001#M642</link>
      <description>&lt;P&gt;Update, got it to work with a case statement:&lt;/P&gt;&lt;LI-CODE lang="general"&gt;Sub PyRxCmd_doit()
    Dim acadApp As Object
    Dim acadDoc As Object
    Dim acadModelSpace As Object
    Dim groups As Object
    Dim clr As Object
    Dim wb As Object
    Dim ws As Object
    Dim row As Variant
    Dim grnum As Variant
    Dim idx As Integer
    Dim sl As Variant
    Dim el As Variant
    Dim line As AcadLWPolyline
    Dim lines As Object
    Dim color As AcadAcCmColor
    
    Dim utilObj As Object   ' Late bound object
Set utilObj = ThisDrawing.Utility
    Dim polyPoints As Variant

' Start AutoCAD (if not already running)
On Error Resume Next
Set acadApp = GetObject(, "AutoCAD.Application")
On Error GoTo 0

If acadApp Is Nothing Then
    Set acadApp = CreateObject("AutoCAD.Application")
    acadApp.Visible = True
End If
    
    Set groups = CreateObject("Scripting.Dictionary")
    Set clr = CreateObject("Scripting.Dictionary")
    
    clr.Add 1, 5
    clr.Add 2, 25
    clr.Add 3, 90
    clr.Add 4, 50
    
Dim excelFilePath As String
excelFilePath = "C:\Users\CarlJackson\Downloads\Test4.xlsx"
Set excelApp = CreateObject("Excel.Application")
Set wb = excelApp.Workbooks.Open(excelFilePath)
Set ws = wb.Sheets(1)
    
    For Each row In ws.UsedRange.Rows
        grnum = row.Cells(1, 4).Value
        If groups.Exists(grnum) Then
            groups(grnum).Add row.Value
        Else
            groups.Add grnum, New Collection
            groups(grnum).Add row.Value
        End If
    Next row
    
    Set lines = CreateObject("Scripting.Dictionary")
    
    For Each group In groups
        For idx = 2 To groups(group).Count
            sl = groups(group)(idx - 1)
            el = groups(group)(idx)
            
            utilObj.CreateTypedArray polyPoints, vbDouble, sl(1, 2), sl(1, 3), el(1, 2), el(1, 3)
            Set line = ThisDrawing.ModelSpace.AddLightWeightPolyline(polyPoints)
            'Set color = AcadApplication.GetInterfaceObject("AutoCAD.AcCmColor." &amp;amp; acVer)
            'color.SetRGB 255, 255, 0
            Set color = line.TrueColor
            Select Case sl(1, 5)
            
            Case 1
            color.SetRGB 0, 0, 255
            Case 2
            color.SetRGB 255, 0, 0
            Case 3
            color.SetRGB 0, 255, 0
            Case 4
            color.SetRGB 0, 255, 255
            
            
            End Select
            
            With line
                      .Closed = False
                        .ConstantWidth = 10
                         .Layer = "0"
                          .TrueColor = color
            End With
            lines.Add lines.Count + 1, line
        Next idx
    Next group
    
    wb.Close False
End Sub&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 23 May 2024 20:51:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12794001#M642</guid>
      <dc:creator>carl3ZPF6</dc:creator>
      <dc:date>2024-05-23T20:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Draw lines between points</title>
      <link>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12794168#M643</link>
      <description>&lt;P&gt;Awesome! BTY, 9000 entries is nothing on modern computers, even millions is no problem&lt;/P&gt;&lt;P&gt;The “Subscript out of range” could have been you hitting the ‘NaN’, I did that while penning the python sample.&lt;/P&gt;&lt;P&gt;Since you are using RGB in a case, you can nix the clr dictionary, you probably don’t need the ‘lines’ dictionary either, at least I don’t see a need for it. If you need access to the lines later, just store the objectids or handles in an array&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2024 23:07:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12794168#M643</guid>
      <dc:creator>daniel_cadext</dc:creator>
      <dc:date>2024-05-23T23:07:39Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Draw lines between points</title>
      <link>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12796157#M644</link>
      <description>&lt;P&gt;Trying to add column headers into the input document, I changed this line&lt;/P&gt;&lt;LI-CODE lang="general"&gt; grnum = row.Cells(1, 4).Value&lt;/LI-CODE&gt;&lt;P&gt;to&lt;/P&gt;&lt;LI-CODE lang="general"&gt;grnum = row.Cells(2, 4).Value&lt;/LI-CODE&gt;&lt;P&gt;buy still getting error on&amp;nbsp; :&lt;/P&gt;&lt;P&gt;utilObj.CreateTypedArray polyPoints, vbDouble, sl(1, 2), sl(1, 3), el(1, 2), el(1, 3)&lt;/P&gt;&lt;P&gt;looks like s1 is picking up the header names, how can I prevent this?&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2024 17:25:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12796157#M644</guid>
      <dc:creator>carl3ZPF6</dc:creator>
      <dc:date>2024-05-24T17:25:29Z</dc:date>
    </item>
    <item>
      <title>Re: Script to Draw lines between points</title>
      <link>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12796174#M645</link>
      <description>&lt;P&gt;Disregard, I just needed to change the first for loop starting at 2.&lt;/P&gt;&lt;LI-CODE lang="visual-basic"&gt;  For i = 2 To ws.UsedRange.Rows.Count ' Start from the second row to ignore headers
        Set row = ws.Rows(i)
        grnum = row.Cells(1, 4).Value '4th row is group/ row #
        If groups.Exists(grnum) Then
            groups(grnum).Add row.Value
        Else
            groups.Add grnum, New Collection
            groups(grnum).Add row.Value
        End If
    Next i&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 24 May 2024 17:38:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/vba-forum/script-to-draw-lines-between-points/m-p/12796174#M645</guid>
      <dc:creator>carl3ZPF6</dc:creator>
      <dc:date>2024-05-24T17:38:23Z</dc:date>
    </item>
  </channel>
</rss>

