Author Topic: Create and Populate Autocad Table  (Read 14594 times)

0 Members and 1 Guest are viewing this topic.

jnieman

  • Guest
Create and Populate Autocad Table
« on: July 16, 2008, 06:18:55 PM »
Does anyone know where I can find some good information about various controls and commands for creating/editting a table object in Autocad?

I'm creating a routine that will populate a table with X,Y coordinates for each instance of a certain block that's inserted into a drawing numerous times.

I have the coordinate extraction part down, I just need to know how to pass that information to a table that I create.

I searched through the Autodesk VBA book but it excludes tables altogether regarding object creation, and did some googling for a while, as well as searching these forums... I found some that almost help, but fell decidedly short.

Does anyone have a snippet I could examine or some functions I can look for?

The visual basic reference from the help menu is not being too helpful either (or I don't know the right place to look)

Spike Wilbury

  • Guest
Re: Create and Populate Autocad Table
« Reply #1 on: July 16, 2008, 06:35:45 PM »
Here is one:

http://www.theswamp.org/index.php?topic=9730.0

It is ARX but might be easy to port (don't know) - but I guess you will end up moving or trying to use C# instead.

There are some .NET samples in the ARX SDK.... over here: www.objectarx.com

jnieman

  • Guest
Re: Create and Populate Autocad Table
« Reply #2 on: July 16, 2008, 06:51:14 PM »
Thanks for the help, Luis, I got some good information that's already helped me.

Also, embarrassingly enough, I... was looking in the help accessed via the visual basic editor, and wow... big difference if you use the developer's help accessed from the main Autocad window!


*slams forehead on keyboard*

The entry for "Autocad table object" is quite robust.

fixo

  • Guest
Re: Create and Populate Autocad Table
« Reply #3 on: July 17, 2008, 09:16:54 AM »
Here is Q&D example but this will get
you started

Code: [Select]
Option Explicit

Sub Blocks_Table()

    Dim oSset As AcadSelectionSet
    Dim oEnt As AcadEntity
    Dim oBlk As AcadBlockReference
    Dim varPt As Variant
    Dim ftype(0) As Integer
    Dim fdata(0) As Variant
    Dim bName As String
    Dim xStr As String
    Dim yStr As String
Dim i As Long, j As Long

    ftype(0) = 0: fdata(0) = "INSERT"
    Dim dxfCode, dxfValue
    dxfCode = ftype: dxfValue = fdata

    With ThisDrawing.SelectionSets
        While .Count > 0
            .Item(0).Delete
        Wend
        Set oSset = .Add("$Blocks$")
    End With

    oSset.SelectOnScreen dxfCode, dxfValue

    Dim oSpace As AcadBlock
    If ThisDrawing.ActiveSpace = acModelSpace Then
        Set oSpace = ThisDrawing.ModelSpace
    Else
        Set oSpace = ThisDrawing.PaperSpace
    End If
   
    varPt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Specify insertion point: ")
    Dim oTable As AcadTable
    Set oTable = oSpace.AddTable(varPt, oSset.Count + 2, 3, 10, 30)
    ZoomExtents
    With oTable
    .RegenerateTableSuppressed = True

        .SetCellTextHeight i, j, 5
        .SetCellAlignment i, j, acMiddleCenter
        .SetCellType i, j, acTextCell
        .SetText 0, 0, "Blocks Position"

        .SetCellType i, j, acTextCell
       
        .SetText 1, j, "Block Name"
        .SetCellTextHeight 1, j, 4.5
        .SetText 1, j + 1, "X"
        .SetCellTextHeight 1, j + 1, 4.5
        .SetText 1, j + 2, "Y"
        .SetCellTextHeight 1, j + 2, 4.5

        For i = 0 To oSset.Count - 1

            Set oEnt = oSset.Item(i)
            Set oBlk = oEnt
            If oBlk.IsDynamicBlock Then
                bName = oBlk.EffectiveName
            Else
                bName = oBlk.Name
            End If
            xStr = Format(CStr(Round(oBlk.InsertionPoint(0), 3)), "#0.000")
            yStr = Format(CStr(Round(oBlk.InsertionPoint(1), 3)), "#0.000")
            .SetCellTextHeight i, j, 4
            .SetCellAlignment i, j, acMiddleCenter
            .SetText i + 2, j, bName
            .SetCellTextHeight i + 2, j, 4#
            .SetText i + 2, j + 1, xStr
            .SetCellTextHeight i + 2, j + 1, 4#
            .SetText i + 2, j + 2, yStr
            .SetCellTextHeight i + 2, j + 2, 4#
        Next i
    .RegenerateTableSuppressed = False
    .Update
    End With

    MsgBox "done"

End Sub

~'J'~

jnieman

  • Guest
Re: Create and Populate Autocad Table
« Reply #4 on: July 17, 2008, 09:35:28 AM »
Here is Q&D example but this will get
you started

~'J'~

I just stepped through that and it has all the function that I require... I have the most trouble using selection sets but I think once I go through this and figure out what's doing what... I should be in good shape.

I appreciate this very much, you've helped me a great deal!

fixo

  • Guest
Re: Create and Populate Autocad Table
« Reply #5 on: July 17, 2008, 10:42:25 AM »
You're welcome
Cheers :)

~'J'~

jnieman

  • Guest
Re: Create and Populate Autocad Table
« Reply #6 on: July 17, 2008, 10:43:16 AM »
I must thank you again fixo, you've been a godsend for this.  The last hour I've spent on this was more productive than the 8 hours on and off I spent from the beginning!!  (although I'm sure those beginning hours were needed to understand what was going on, this much)

I am having some trouble with my table creation.  I'm trying to not have a single-column top row.  I wish for it to be un-merged as the rest of the table is.  I cannot see or find what it is in the definitions that make the top row into a single cell, rather than a cell for each column.  

I can't figure it out.  I've only learned how to merge cells using the "object.MergeCells(minRow, maxRow, minCol, maxCol) " method.

Code: [Select]
Option Explicit
Sub Blocks_Table()

    Dim oSset As AcadSelectionSet
    Dim oEnt As AcadEntity
    Dim oBlk As AcadBlockReference
    Dim varPt As Variant
    Dim ftype(0) As Integer
    Dim fdata(0) As Variant
    Dim bName As String
    Dim xStr As String
    Dim yStr As String
Dim i As Long, j As Long

    ftype(0) = 0: fdata(0) = "INSERT"
    Dim dxfCode, dxfValue
    dxfCode = ftype: dxfValue = fdata

    With ThisDrawing.SelectionSets
        While .Count > 0
            .Item(0).Delete
        Wend
        Set oSset = .Add("$Blocks$")
    End With
    
    ThisDrawing.ActivePViewport.Display True
    ThisDrawing.ActiveSpace = acModelSpace

    oSset.SelectOnScreen dxfCode, dxfValue
    
    ThisDrawing.ActiveSpace = acPaperSpace
    
    Dim paSpace As AcadPaperSpace
    Set paSpace = ThisDrawing.PaperSpace
    
    varPt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Specify insertion point: ")
    Dim oTable As AcadTable
    Set oTable = paSpace.AddTable(varPt, oSset.Count + 2, 5, 0.3, 1.5)
    ZoomExtents
    With oTable
    .RegenerateTableSuppressed = True
    
        .SetCellTextHeight i, j, 0.09375
        .SetCellAlignment i, j, acMiddleCenter
        .SetCellType i, j, acTextCell
        .SetText 0, j, "ITEM"
        .SetCellTextHeight i, j + 2, 0.09375
        .SetText 0, j + 2, "EQUIPMENT DATUM"
        .SetCellTextHeight i, j + 4, 0.09375
        .SetText 0, j + 4, "DATUM LOCATION"
        
        .SetText 1, j + 1, "N"
        .SetCellTextHeight 1, j + 1, 0.09375
        .SetText 1, j + 2, "E"
        .SetCellTextHeight 1, j + 2, 0.09375
        .SetText 1, j + 3, "EL"
        .SetCellTextHeight 1, j + 3, 0.09375
        
        For i = 0 To oSset.Count - 1

            Set oEnt = oSset.Item(i)
            Set oBlk = oEnt
            If oBlk.IsDynamicBlock Then
                bName = oBlk.EffectiveName
            Else
                bName = oBlk.Name
            End If
            xStr = Format(CStr(Round(oBlk.InsertionPoint(1), 3)), "#0.000")
            yStr = Format(CStr(Round(oBlk.InsertionPoint(0), 3)), "#0.000")
            .SetCellTextHeight i, j, 0.09375
            .SetCellAlignment i, j, acMiddleCenter
            .SetText i + 2, j, "-ID #-"
            .SetCellTextHeight i + 2, j, 0.09375
            .SetText i + 2, j + 1, xStr
            .SetCellTextHeight i + 2, j + 1, 0.09375
            .SetText i + 2, j + 2, yStr
            .SetCellTextHeight i + 2, j + 2, 0.09375
        Next i
    .RegenerateTableSuppressed = False
    .Update
    End With

    MsgBox "done"

End Sub

(also, interesting side note... FF3 crashed consistently when trying to paste the code directly from the VBA Manager in acad... I pasted it to notepad, and copied it over here successfully however.)

jnieman

  • Guest
Re: Create and Populate Autocad Table
« Reply #7 on: July 17, 2008, 03:08:32 PM »

I am having some trouble with my table creation.  I'm trying to not have a single-column top row.  I wish for it to be un-merged as the rest of the table is.  I cannot see or find what it is in the definitions that make the top row into a single cell, rather than a cell for each column.  

I can't figure it out.  I've only learned how to merge cells using the "object.MergeCells(minRow, maxRow, minCol, maxCol) " method.

Oh man, I'm not thinking 'outside the box'

It's set that way because the first row is of "Title" type in the table style.  I assumed it defaulted to a "data" type, but since it's a title, it spans the whole table by default... it's not in the code, but the active table style  *smacks forehead*

Now I gotta figure out how to set the first row as 'data' type, as opposed to title or header, now...

fixo

  • Guest
Re: Create and Populate Autocad Table
« Reply #8 on: July 17, 2008, 04:16:42 PM »
Glad you soved it by yourself
so I can rest :)

~'J'~

jnieman

  • Guest
Re: Create and Populate Autocad Table
« Reply #9 on: July 17, 2008, 04:25:19 PM »
Fixo, I don't think you realize how much you're helping me today, beyond the post above!

Many times I have searched for how to get a certain thing to work (such as merging cells, amongst others) and doing a search on the Swamp has revealed that you already explained it to someone :-D

Your vba guidance on this forum is excellent!

I'm currently having trouble extracting the value of a defined attribute within that block, for placing into this same table... but I'm not given up quite yet.

jnieman

  • Guest
Re: Create and Populate Autocad Table
« Reply #10 on: July 17, 2008, 05:03:37 PM »
ok I ended up giving up :(

I've looked over so many examples of people using GetAttributes that I cannot figure out what is going on... the help file does not seem to be very helpful, for me, either.

Below is the code I have so far.

The block I am going to be using for this table has a 3 or 4 attributes in it that hold information that will be passed to this table.

Tags:
ID
ELEVATION
DATUMLOC
DESC

I need to get the value for each individual attribute/tag and output it to a table cell exactly how I am doing with xStr and yStr as you have showed me.

Code: [Select]
Option Explicit
Sub Blocks_Table()

    Dim oSset As AcadSelectionSet
    Dim oEnt As AcadEntity
    Dim oBlk As AcadBlockReference
    Dim varPt As Variant
    Dim ftype(0) As Integer
    Dim fdata(0) As Variant
    Dim bName As String
    Dim Atts As Variant
    Dim xStr As String
    Dim yStr As String
    Dim ID As String
    Dim Desc As String
Dim i As Long, j As Long

    ftype(0) = 0: fdata(0) = "INSERT"
    Dim dxfCode, dxfValue
    dxfCode = ftype: dxfValue = fdata

    With ThisDrawing.SelectionSets
        While .Count > 0
            .Item(0).Delete
        Wend
        Set oSset = .Add("$Blocks$")
    End With
   
    ThisDrawing.ActivePViewport.Display True
    ThisDrawing.ActiveSpace = acModelSpace

    oSset.SelectOnScreen dxfCode, dxfValue
   
    ThisDrawing.ActiveSpace = acPaperSpace
   
    Dim paSpace As AcadPaperSpace
    Set paSpace = ThisDrawing.PaperSpace
   
    varPt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Specify insertion point: ")
    Dim oTable As AcadTable
    Set oTable = paSpace.AddTable(varPt, oSset.Count + 3, 6, 0.3, 1.5)
    oTable.TitleSuppressed = False
    oTable.HeaderSuppressed = True
    ZoomExtents
    With oTable
    .RegenerateTableSuppressed = True
   
        .SetCellTextHeight i, j, 0.15625
        .SetCellAlignment i, j, acMiddleCenter
        .SetCellType i, j, acTextCell
        .SetText 0, 0, "EQUIPMENT LAYOUT SCHEDULE"
   
        .SetText 1, 0, "ITEM"
        .SetCellTextHeight 1, 0, 0.09375
        .SetText 1, 1, "EQUIPMENT DATUM"
        .SetCellTextHeight 1, 1, 0.09375
        .SetText 1, 4, "DATUM LOCATION"
        .SetCellTextHeight 1, 4, 0.09375
        .SetText 1, 5, "DESCRIPTION"
        .SetCellTextHeight 1, 5, 0.09375
       
        .SetText 2, 1, "N"
        .SetCellTextHeight 2, 1, 0.09375
        .SetText 2, 2, "E"
        .SetCellTextHeight 2, 2, 0.09375
        .SetText 2, 3, "EL"
        .SetCellTextHeight 2, 3, 0.09375
       
        For i = 0 To oSset.Count - 1

            Set oEnt = oSset.Item(i)
            Set oBlk = oEnt
            If oBlk.IsDynamicBlock Then
                bName = oBlk.EffectiveName
            Else
                bName = oBlk.Name
            End If
            xStr = Format(CStr(Round(oBlk.InsertionPoint(1), 3)), "#0.000")
            yStr = Format(CStr(Round(oBlk.InsertionPoint(0), 3)), "#0.000")
           
            .SetCellTextHeight i + 3, j, 0.09375
            .SetCellAlignment i + 3, j, acMiddleCenter

            .SetText i + 3, j, ID
            .SetCellTextHeight i + 3, j, 0.09375
            .SetText i + 3, j + 1, xStr
            .SetCellTextHeight i + 3, j + 1, 0.09375
            .SetText i + 3, j + 2, yStr
            .SetCellTextHeight i + 3, j + 2, 0.09375
            .SetText i + 3, j + 3, Desc
            .SetCellTextHeight i + 3, j + 3, 0.09375

        Next i
       
    .RegenerateTableSuppressed = False
    .Update
    End With

        oTable.MergeCells 1, 2, 0, 0
        oTable.MergeCells 1, 2, 4, 4
        oTable.MergeCells 1, 2, 5, 5
        oTable.MergeCells 1, 1, 1, 3
       
    MsgBox "Yahoooooo!"

End Sub

jnieman

  • Guest
Re: Create and Populate Autocad Table
« Reply #11 on: July 17, 2008, 05:04:14 PM »
Right now I have not set a value to ID or DESC, but I'm not sure how to even do so.

fixo

  • Guest
Re: Create and Populate Autocad Table
« Reply #12 on: July 17, 2008, 05:21:17 PM »
Upload your sample drawing with blocks
(delete unused information and other things before)
I'll look at it tomorrow

~'J'~

jnieman

  • Guest
Re: Create and Populate Autocad Table
« Reply #13 on: July 17, 2008, 05:31:07 PM »
Here is everything necessary.

From what it seems... I think my problem is that I don't know how to handle arrays to get the information I need :(

test.dwg

Contains the block "DATUM" in model space in a variety of instances.

I select all those blocks during the routine, it switches to paperspace, asks for the insertion point of the table, and then creates it.

I just don't know how to get the value of the attributes and then put them into the table.

Thank you very much for your time!

jnieman

  • Guest
Re: Create and Populate Autocad Table
« Reply #14 on: July 17, 2008, 06:43:08 PM »
fixo!

Please do not spend any more of your time on me!

JeffM gave me the final push in understanding what's going on with GetAttributes that I wasn't able to understand.

http://www.theswamp.org/index.php?topic=24004.0 is where I have the final entire code.

if you have comments, lessons, advice, I welcome it, but I won't ask any more of your time for this... you've done quite a lot already and I owe the success to you (and Jeff M) for sure!