VBA Arrays and Autocad Tables

Here is the basic routine for making an autocad table from an array in its simplest form from a one-based array, and a generalized form that creates a table from any two-dimensional array.


Sub test_array_to_acadtable1()
'test to make a one-based two dimensional array of numbers
'and send the array to the maketable routines

Call connect_acad
Dim rows As Integer, i As Integer
Dim columns As Integer, j As Integer
'change these to anything you like
rows = 14
columns = 16

Dim ar_mult() As Integer
ReDim ar_mult(1 To rows, 1 To columns)

For i = 1 To rows
For j = 1 To columns
ar_mult(i, j) = i * j
Next j
Next i

'makes two identical tables
Call makethetable2(ar_mult)
Call makethetable3(ar_mult)
End Sub


Sub test_array_to_acadtable2()
'test to make a random-based two dimensional array of numbers
'and send the array to the maketable routine
'that has been generalized to accept an array of any base

Call connect_acad
Dim rows As Integer, i As Integer
Dim columns As Integer, j As Integer
'change these to anything you like
rows = 14
columns = 16

Dim ar_mult() As Integer
ReDim ar_mult(3 To rows + 2, 3 To columns + 2)

For i = 3 To rows + 2
For j = 3 To columns + 2
ar_mult(i, j) = (i - 2) * (j - 2)
Next j
Next i

Call makethetable3(ar_mult)
End Sub


Sub makethetable3(ar As Variant)
'table is two-dimensional and any-base
    Dim tbl As AcadTable
    Dim i As Integer, j As Integer
    Dim rowcount As Integer, colcount As Integer
    Dim rowLbound As Integer, colLbound As Integer
    Dim rowUbound As Integer, colUbound As Integer
    
    Dim drowh As Double, dcolw As Double
    Dim pt0(0 To 2) As Double
    
    rowLbound = LBound(ar, 1)
    colLbound = LBound(ar, 2)
    rowUbound = UBound(ar, 1)
    colUbound = UBound(ar, 2)
    rowcount = rowUbound - rowLbound + 1
    colcount = colUbound - colLbound + 1
        
        drowh = 0.125
        dcolw = 0.625
    Set tbl = acadDoc.ModelSpace.AddTable(pt0, rowcount, colcount, drowh, dcolw)
        tbl.UnmergeCells 0, 0, 0, 0
        tbl.TitleSuppressed = True
        tbl.HeaderSuppressed = True

    For i = rowLbound To rowUbound
        For j = colLbound To colUbound
        tbl.SetText i - colLbound, j - rowLbound, ar(i, j)
        Next j
    Next i
End Sub


Sub makethetable2(ar As Variant)
'the simpler routine
'assume table is two-dimensional and one-base
'no attempt to set up a tablestyle
'which makes the unmerge method necessary
 
    Dim tbl As AcadTable
    Dim i As Integer, j As Integer
    Dim rowcount As Integer, colcount As Integer
    Dim drowh As Double, dcolw As Double
    Dim pt0(0 To 2) As Double
    
    rowcount = UBound(ar, 1)
    colcount = UBound(ar, 2)
    drowh = 0.125
    dcolw = 0.625
    'create the table sized for the array
   Set tbl = acadDoc.ModelSpace.AddTable(pt0, rowcount, colcount, drowh, dcolw)
    tbl.UnmergeCells 0, 0, 0, 0
    tbl.TitleSuppressed = True
    tbl.HeaderSuppressed = True

    For i = 1 To rowcount
     For j = 1 To colcount
     tbl.SetText i - 1, j - 1, ar(i, j)
     Next j
    Next i

End Sub

any selection of data on a spreadsheet can be saved to an array with a single line of code, and the array fed to the makethetable routine. of course the formatting is terrible but we have tools for that.


Sub make_table_from_selection()
Dim ar_tbl As Variant
ar_tbl = Selection.Value
 'a selection assigned to a variant
 'creates a one-based two-dimension array
 'the first dim is the row, the second is the column
  
'MsgBox LBound(ar_tbl, 1)  returns 1
'MsgBox LBound(ar_tbl, 2)  returns 1

Call connect_acad
Call makethetable3(ar_tbl)

End Sub

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s