Autocad VBA Tables

Autocad has over 200 table and table style methods and properties. Many of these are overlapping uncertain as to application. The ActiveX help is not much. I imagine whoever turned it over to his/her boss said this is just a framework, i need to put as many hours in to finish it. and there it stands. however, the multitude of options show how fully it is implemented. the autocad interfaces are very good. in fact that is the only way to learn how to use the methods, study the user interfaces and find their vba counterparts. The realization comes that there is not much to be gained by duplicating the user interface in vba. the work flow is to assemble data in excel, format it in excel, then use the pastespecial command in autocad to paste the data to a table complete with formatting.

you can start to sort out the activex help by copy pasting the methods page to an excel sheet. the links transfer and you can begin to assemble the most required methods and add your own notes.

the > 200 table methods. these encompass the table and the tabledit command. tabledit is accessed by rightclicking the table, and by the properties window, as well as the ribbon panel for tables.
Autocad 2016 Table Methods and Properties ActiveX VBA

there are fewer tablestyle methods.
Autocad 2016 TableStyle Methods and Properties ActiveX VBA

2016-07-28_1

here is some barebones beginning code to create a multiplication table, any size, with an array. Arrays are natural tools to use with tables, whether in excel or autocad. Feed the array to a make table subprocedure which makes the autocad table and sizes it the same as the array. There is no attempt to create a tablestyle, which makes it necessary to run the unmerge method. Even though no tablestyle is selected, the table has a default, whatever is current, which is likely the “Standard” that automatically merges the first row and makes it a “Title” row. There is also some commented out code that writes the data to a new sheet. And some commented out code which demonstrates how to obtain the data for the autocad table from a spreadsheet.


Sub test()
DelSht ("new_sheet1")
CreateSheet ("new_sheet1")

Dim rows As Integer, i As Integer
Dim columns As Integer, j As Integer

'change these to anything you like
rows = 12
columns = 12

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

'this comment block demonstrates taking spreadsheet data to autocad table
'Dim rng As Range
'Worksheets("new_sheet1").Activate
'Set rng = Range("B2", Cells(rows + 1, columns + 1))
'rng = ar_mult

'Dim ar2 As Variant
'ar2 = rng
'Call makethetable1(ar2)

Call makethetable1(ar_mult)

End Sub


Sub makethetable1(ar As Variant)
'first attempt assume table is two-dimensional and one-base
'no attempt to set up a tablestyle
'which makes the unmerge method necessary

    Call connect_acad
    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 = 1
    'create the table sized for the array
    Set tbl = acadDoc.ModelSpace.AddTable(pt0, rowcount, colcount, drowh, dcolw)

      'object.UnmergeCells minRow, maxRow, minCol, maxCol
    tbl.UnmergeCells 0, 0, 0, 0

With tbl

For i = 1 To rowcount
 For j = 1 To colcount
  If Not IsEmpty(ar(i, j)) Then
   .SetText i - 1, j - 1, ar(i, j)
  End If
 Next j
Next i

End With

tbl.Update
ZoomExtents

End Sub

2016-07-29_1

to conquer the tablestyle creation, you need to remember the enumeration constants, such as acdatarow and acHorzBottom can be added up. their values can be obtained for reference by typing in the immediate window. if you want your entire grid to be blue, you can do it with one line of code.


Sub table_style_full()
  'sets a full table style with title and header
    Call connect_acad
    
    Dim dictionaries As AcadDictionaries
    Dim dictObj As AcadDictionary
    Set dictionaries = acadDoc.Database.dictionaries
    Set dictObj = dictionaries.Item("acad_tablestyle")
     
    Dim keyName As String, className As String
    Dim TS As AcadTableStyle
    keyName = "Tb_full"
    className = "AcDbTableStyle"
    Set TS = dictObj.AddObject(keyName, className)
    TS.Name = "Tb_full"
    TS.Description = "Style data"
  
    TS.TitleSuppressed = False
    TS.HeaderSuppressed = False
    TS.BitFlags = 1
    TS.FlowDirection = acTableTopToBottom
        
    TS.EnableMergeAll "Title", True
          
    TS.HorzCellMargin = 0.06
    TS.VertCellMargin = 0.03 ' affects the height of the cell
    
    TS.SetTextHeight acTitleRow, 0.25
    TS.SetTextHeight acHeaderRow, 0.1875
    TS.SetTextHeight acDataRow, 0.125
        
   'doesnt like it if these are not already created
    TS.SetTextStyle acTitleRow, "Arial"
    TS.SetTextStyle acHeaderRow, "Arial"
    TS.SetTextStyle acDataRow, "ArialN"

    TS.SetAlignment acTitleRow, acMiddleCenter
    TS.SetAlignment acHeaderRow, acMiddleCenter
    TS.SetAlignment acDataRow, acMiddleRight
    
  Dim col As AcadAcCmColor
  Set col = AcadApplication.GetInterfaceObject("AutoCAD.AcCmColor.20")
    'Blue
  Call col.SetRGB(0, 0, 255)

'gridlinetypes
'acHorzBottom 4
'acHorzInside 2
'acHorzTop 1
'acVertInside 16
'acVertLeft 8
'acVertRight 32
'border sum is 63

'rowtypes
'acdatarow 1
'acTitleRow 2
'acHeaderRow 4
'row sum is 7

    'setgridcolor gridlinetypes, rowtypes, color
    TS.SetGridColor 63, 7, col
    TS.SetGridVisibility 63, 7, True
    
     Call col.SetRGB(0, 0, 0)
     'setcolor rowtypes, color
     TS.SetColor 7, col
    'Set col = TS.GetColor(acDataRow)
    'MsgBox col.Red & col.Green & col.Blue
      
    'Set col = TS.GetBackgroundColor(acDataRow)
    'MsgBox col.Red & col.Green & col.Blue
    TS.SetBackgroundColor 7, col
    TS.SetBackgroundColorNone 7, True
     
    acadDoc.SetVariable ("ctablestyle"), "Tb_full"
    acadDoc.SetVariable ("clayer"), "0"
    
End Sub

well i thought that i could create a tablestyle with a comprehensive sub procedure, setting nearly every variable, set it to current, then create a new tablestyle and it would inherit the current values, but it does not. the user interface works that way, it starts with the the current style, but vba creation does not. i wanted to have a tablestyle library that did not duplicate code. i tried changing the “Standard” style, as it seems that is the template, setting that current, but still the inherited values in the new unset tablestyle seem to come from an intrinsic standard, not the “Standard.” thats ok, we will refine our basic tablestyle routine and copy / modify it as required.

i need to learn how to format, for example displaying a calculated double like 1/3 with a desired number of decimal places, and investigate the difference between SetTextString and SetText and SetValue and SetCellValue, and the difference between SetFormat and SetCellFormat and SetDataFormat, some jobs like that, other than that, we have many if not most of the tools we need demonstrated.

2016-07-31_1

Sub test()
Call connect_acad
acadApp.Preferences.Display.GraphicsWinModelBackgrndColor = 16250871
acadDoc.SetVariable "LWDISPLAY", 1
Call set_text_style

Dim rows As Integer, i As Integer
Dim columns As Integer, j As Integer

'change these to anything you like
rows = 14
columns = 14

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

'this comment block demonstrates taking spreadsheet data to autocad table
'DelSht ("new_sheet1")
'CreateSheet ("new_sheet1")
'Dim rng As Range
'Worksheets("new_sheet1").Activate
'Set rng = Range("B2", Cells(rows + 1, columns + 1))
'rng = ar_mult

'Dim ar2 As Variant
'ar2 = rng
'Call makethetable1(ar2)

Call table_style_data
Call makethetable1(ar_mult)
End Sub
Sub table_style_data()
'doesnt influence
'Call table_style_full

    Dim dictionaries As AcadDictionaries
    Dim dictObj As AcadDictionary
    Set dictionaries = acadDoc.Database.dictionaries
    Set dictObj = dictionaries.Item("acad_tablestyle")
     
    Dim keyName As String, className As String
    Dim TS As AcadTableStyle
    keyName = "Tb_data"
    className = "AcDbTableStyle"
    Set TS = dictObj.AddObject(keyName, className)
    TS.Name = "Tb_data"
    TS.Description = "Style data"
  
    TS.TitleSuppressed = True
    TS.HeaderSuppressed = True
    
    TS.HorzCellMargin = 0.03
    TS.VertCellMargin = 0.03 ' affects the height of the cell

    TS.SetTextHeight acTitleRow, 0.125
    TS.SetTextHeight acHeaderRow, 0.125
    TS.SetTextHeight acDataRow, 0.125


    Dim col As AcadAcCmColor
  Set col = AcadApplication.GetInterfaceObject("AutoCAD.AcCmColor.20")
'    'Blue
  Call col.SetRGB(0, 0, 255)
   TS.SetGridColor 63, 7, col
   
    TS.SetGridLineWeight 63, 7, acLnWt030
     TS.SetTextStyle acDataRow, "Tempus Sans ITC"
      
    acadDoc.SetVariable ("ctablestyle"), "Tb_data"
    acadDoc.SetVariable ("clayer"), "0"
End Sub
Sub makethetable1(ar As Variant)
'first attempt assume table is two-dimensional and one-base
'no attempt to set up a tablestyle
'which makes the unmerge method necessary

    Call connect_acad
    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.5
    'create the table sized for the array
    Set tbl = acadDoc.ModelSpace.AddTable(pt0, rowcount, colcount, drowh, dcolw)

      'object.UnmergeCells minRow, maxRow, minCol, maxCol
    tbl.UnmergeCells 0, 0, 0, 0
    
    tbl.TitleSuppressed = True
    tbl.HeaderSuppressed = True

With tbl
    For i = 1 To rowcount
     For j = 1 To colcount
      If Not IsEmpty(ar(i, j)) Then
       .SetText i - 1, j - 1, ar(i, j)
      End If
     Next j
    Next i
End With

ZoomExtents
 'tbl.GenerateLayout
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