"Hs anyone come across code to export a table...Take 2"

Discussion in 'AutoCAD' started by Dan, Aug 10, 2004.

  1. Dan

    Dan Guest

    I am at a loss now, I appreciate the help, but I guess this part is a bit
    beyond my newbie abilities. I am learning quite a lot in Excel VB, but
    AutoCAD seems very difficult. Documentation must be out there, or I am just
    looking in the wrong places, because I cannot find much on tables, and
    exporting. I have posted my drawing in the other thread 8/3, which includes
    the table I am trying to export. I have fields in it, so the cvsout.lsp does
    not help either, and I am trying to incorporate it into the excel VB code I
    have written. There is a lot done with the data in each table.

    Thanks again everyone. I really appreciate this group.
     
    Dan, Aug 10, 2004
    #1
  2. ..but AutoCAD seems very difficult.
    AutoCAD is not difficult, its just a different object model
    All the documentation needed is in the help file that ships with AutoCAD.
    Every object inherits the properties of the generic AcadObject. What you've
    been missing all along is that there is no such thing as an export. If the
    method does not exist for the object, then its up to the programmer to
    create.

    Below is your program, almost. It kicks out an ASCII text file that is
    pipe-delimited. James had placed comments in there explaining how to add
    the commas and quotations marks - which I would not do the qmarks because
    they are most likely not req'd.

    I scratched your filenaming code for the 3 lines shown as well as changed
    the selection of the table to work from a selection set of all entities.
    From this, only tables are selected because they are filtered. This code
    will not run on your sample drawing because it has 2 tables in it. You can
    figure out what you want to do in a case like that.

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...

    '==== CODE BEGIN ====
    Option Explicit

    Public Sub ExportTable()
    Dim oTable As AcadTable
    Dim oSet As AcadSelectionSet
    Dim FilterType(0) As Integer
    Dim FilterData(0) As Variant
    Dim cValue As Variant
    Dim lCols As Long
    Dim lRows As Long
    Dim cCntr As Long
    Dim rCntr As Long
    Dim sCSVFile As String
    Dim sRow As String
    Dim cValues As Collection
    Set cValues = New Collection
    FilterType(0) = 0
    FilterData(0) = "ACAD_TABLE"
    Set oSet = ThisDrawing.SelectionSets.Add("TableExporter")
    oSet.Select acSelectionSetAll, , , FilterType, FilterData
    Select Case oSet.Count
    Case Is = 0
    MsgBox "No TABLES found!"
    Case Is = 1
    Set oTable = oSet(0)
    'get table's row & column counts
    lRows = oTable.Rows
    lCols = oTable.Columns
    'iterate the rows
    For rCntr = 0 To lRows - 1
    'iterate each column in row
    For cCntr = 0 To lCols - 1
    'read the row and pad it incase of a null value
    sRow = sRow & "" & oTable.GetText(rCntr, cCntr) & "|"
    Next
    'strip last pipe from string
    sRow = Mid(sRow, 1, Len(sRow) - 2)
    'add "row" to our collection
    cValues.Add sRow
    'reset the var for next pass
    sRow = vbNullString
    Next
    With ThisDrawing
    sCSVFile = .GetVariable("DWGNAME")
    sCSVFile = Replace(sCSVFile, ".dwg", ".csv")
    sCSVFile = .GetVariable("DWGPREFIX") & sCSVFile
    End With
    'write out lines of collection into file
    Open sCSVFile For Output As #1
    For Each cValue In cValues
    'need to substitute commas for pipe symbols here or earlier
    'need to wrap strings in quotes???
    Print #1, cValue
    'or could use write #1 to have quotes added automatically
    Next
    Close #1
    'clean up
    If Not oTable Is Nothing Then Set oTable = Nothing
    If Not oSet Is Nothing Then
    oSet.Delete
    Set oSet = Nothing
    End If
    Case Is > 1
    MsgBox "Too many TABLES found!"
    End Select
    End Sub
    '==== CODE END ====
     
    Mike Tuersley, Aug 10, 2004
    #2
  3. Dan

    Dan Guest

    Thank you Mike!
    I will work on it. I really aprreciate your support and helpfulness. I am
    really green at VBA, and do not fully understand all the resources
    available, or how to get the info I need, but I am learning.

    I do have multiple tables in the drawings (2) currently, but their locations
    do not change. I will try to work with what you have provided as a
    foundation. Its an AWESOME start. Thank you. The routine can only be ran
    once, and I think I need to figure out how to clear the
    ThisDrawing.SelectionSets.Add("TableExporter") first, because the name has
    already been in use. To simplify things, I thought I could just delete a
    table, run the routine, the sendcommand, undo to bring the table back. Your
    file path is a new approach to me as well. Thanks for teaching me a few
    things. I hope oneday to repay the favor to the group.

    Thankd again,
    Dan
     
    Dan, Aug 11, 2004
    #3
  4. No problem, Dan =)

    You should be able to run the program as many times as you'd like as long
    as you let the program finish. If you stop it in the IDE, then you will get
    the selection set error. Easiest method is to trap the error and delete the
    selectionset if it exists.

    For the multiple tables, location has nothing to do with it as the program
    scans the entire drawing. You just need to determine a unique property that
    distinguishes the two. Then in the select case = 2, add a loop to do both
    and an if to check which one is which.

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Aug 11, 2004
    #4
  5. Dan

    Dan Guest

    Hmmm, I modified:
    'sRow = Mid(sRow, 1, Len(sRow) - 2)
    sRow = Mid(sRow, 1, Len(sRow) - 1) '<-------------------Modified

    And that stopped the truncation, hope it does not effect anything else, I
    could not see.
    Now on to figureing out the added loop to do both.
    I know this should help somebody else someday! Thanks again.
     
    Dan, Aug 11, 2004
    #5
  6. Dan

    Dan Guest

    You are AWESOME Mike, Thanks!
    I took the easy rout for now, and simply combined my tables, and chose a
    different selection set range in Excel. I will continue to persure you
    suggestion for multiple tables. Thanks again! That gets me started, and
    way beyond what I was doing when I started this project.
     
    Dan, Aug 11, 2004
    #6
  7. Thanks Dan - I appreciate it. Sorry it took so long to get you to where you wanted to be.

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Aug 12, 2004
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.
Loading...