Hs anyone come across code to export a table?

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

  1. Dan

    Dan Guest

    I am new to VB, and cannot seem to find a reference in AutoCAD to extract a
    table's data to a file beyond tableexport. I would like to make this a
    seemless process to the user, which gets its name(.csv), and drawing path
    from the current drawing. Tableexport works well, but it is all manual, and
    I cannot turn off the dialog boxes for it.

    Thanks to everyone in sthis group, I have learned alot, and I have a long
    way to go.
     
    Dan, Aug 3, 2004
    #1
  2. Dan,
    I don't have 2k5 so I can't see the table objects, but I could still help
    you write a simple exporter, I think. Here's the only code I found online
    regarding tables:

    http://www.imaginit-tech.com/services/articles/2004/jun04.htm

    If you want help, you'll need to copy a list of the AcadTable's properties
    and methods from the help file into a post so I can see what they are.


    James
     
    James Belshan, Aug 3, 2004
    #2
  3. Thanks, James =)

    In my article, we're taking info from Excel into a 2005 table object. Iif
    you download the source code it should be fairly simple to work backwards.
    Here is the meat of a sub for you - you just need to add the selectiion of
    the table object:

    '========BEGIN CODE BLOCK===========
    Public Sub ExportTable()
    Dim oTable As AcadTable
    Dim lCols As Long
    Dim lRows As Long
    Dim cCntr As Long
    Dim rCntr As Long
    Dim sRow As String
    Dim cValues As Collection
    Set cValues = New Collection
    'write portion to retrieve the tabe reference
    'possible solutions are:
    Set oTable = ?????
    '1. use Utility.Select
    '2. iterate the block table looking
    ' for any unnamed blocks starting
    ' with a T -> *T1, *T2, etc.
    '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 = "" & 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
    End Sub
    '=========END CODE BLOCK============

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

    wmcgrego Guest

    I've been working on a similar problem taking old, raw text and lines bills of materials from drawings and making Excel spreadsheets from them. There are still bugs in my code but I'll share it if you're interested.
    Email me at
     
    wmcgrego, Aug 4, 2004
    #4
  5. Dan

    Dan Guest

    Thank you all for the leads. I have come up with nothing on the tableexport
    documentation. I appreciate any leads. I just want to take a table, with
    fields, and export it into a .csv file. I didnt think it was so unusual,
    but I havent seen any code on it, not anyone talking about it.



    Thanks again all, I will work on it. If I get it dialed in, I will be sure
    to post the code for ALL to use if they need it.



    Dan





    bills of materials from drawings and making Excel spreadsheets from them.
    There are still bugs in my code but I'll share it if you're interested.
     
    Dan, Aug 4, 2004
    #5
  6. What are you talking about Dan??? I posted the code FOR YOU! All you have
    to do is add the selecting of the table entity and the vb code to create
    the file!

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

    Dan Guest

    I apologize for my ingnorance. I am very new to VBA. I have written the VB
    to create the variables I wish for the file name, and one for the file path,
    but am not sure how to pass it on to the tableexport dialog box. I
    appreciate your efforts, and your code. I am working on it now. Do I need to
    define the selection set and make it Set oTable = (set)? My tables are all
    at -100,516.

    I has this so far:
    --------
    For Each Table in TablesCollection
    If Abs(Table.InsertionPoint(0) - 100) < 0.1 and
    Abs(Table.InsertionPoint(1) - 516) < 0.1 then export
    end if
    next
    --------
    I know "export" curently does not get me there, but its a place holder.

    Thanks Mike.

    Dan
     
    Dan, Aug 4, 2004
    #7
  8. Wait a minute and back up Dan, there is no way to pass info to the
    tableexport command. Its your task to re-recreate it. How automated do want
    this to work? If the user picking a button and selecting the table is good
    enough, just use a menu macro instead of vba.

    If you want it entirely automated then you need to:

    1. Select the table by parsing the blocks table looking for any blocks
    where the name starts with *T.
    2. Use my code to build the Collection object which stores each table row
    as an Item of the collection.
    3. Create a csv file with the Write Option set
    4. Iterate the collection and write each item to the file
    5. Close the csv file

    Look in the AutoCAD and VBA help files for sample code to do each of the
    missing pieces.

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Aug 4, 2004
    #8
  9. Dan,
    You can use Mike's code as a framework for a replacement for the TABLEEXPORT
    command. Here's a quick, incomplete stab at this.

    To your code add the subroutine call:
    sFileName = "c:\temp\temp.csv" 'put your path and filename
    together here
    call ExportTable (Table, sFileName) ' call Mike's sub


    To Mike's code, add the parameters and file-writing:

    Public Sub ExportTable(oTable As AcadTable, sFileName as string)
    'Dim oTable As AcadTable
    Dim lCols As Long
    ....
    ....
    cValues.Add sRow
    'reset the var for next pass
    sRow = vbNullString
    Next

    ' now, write out lines of collection into file (old-school way, I know
    there's newer ways)
    open sFileName for output as #1 'improvement: variable lFileNum based on
    FreeFile()
    dim cValue as Variant
    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 'cValue
    close #1

    End Sub
     
    James Belshan, Aug 4, 2004
    #9
  10. Dan

    Dan Guest

    Thanks Guys, I will work on it..afterall the fun is getting there.. I
    appreciate the help, and will post the final code after I get it worked out.
    I wish it to be completely seemless to the user. Since all tables are in the
    exact same spot (-100,516) I just want them to click a button....it selects
    tha table/names it/saves file in same directory/DONE. I have much more code
    that utilizes these .csv files, but this is the last step for me to figure
    out. I am just learning how to write AutoCAD VBA code.

    Thanks again!

    Dan
     
    Dan, Aug 4, 2004
    #10
  11. Dan

    Dan Guest

    I am not sure what to do with: Set oTable = ?????; End If error? I have an
    IF previous....
    Is there an easier way to extract the file path as well. This was what I
    have come up with so far, but it works....
    Anyway, flame on, newbie here.

    Thanks,
    Dan


    This is what I have so far:

    Private Sub CommandButton10_Click()
    Dim sFileName As String
    Dim thisdwgpath As String
    thisdwgpath = ThisDrawing.GetVariable("dwgprefix")
    Dim thisdwgname As String
    thisdwgname = ThisDrawing.GetVariable("dwgname")
    Dim thisnewdwgpath As String
    'begin file new path loop
    i = 1
    For j = 1 To 3
    Do
    strA = Mid(thisdwgpath, i, 1)
    i = i + 1
    Loop Until strA = "\"
    Next j
    thisnewdwgpath = Left(thisdwgpath, i - 1)
    'end new file path loop
    Dim thisnewdwgname As String
    'begin new file name loop
    i = 1
    Do
    strA = Mid(thisdwgname, i, 1)
    i = i + 1
    Loop Until strA = "."
    thisnewdwgname = Left(thisdwgname, i - 2)
    'end new file name loop
    'Begin Mike's Sub
    Dim oTable As AcadTable
    Dim lCols As Long
    Dim lRows As Long
    Dim cCntr As Long
    Dim rCntr As Long
    Dim sRow As String
    Dim cValues As Collection
    Set cValues = New Collection
    'Pause Mike's Sub
    For Each Table In TablesCollection
    If Abs(Table.InsertionPoint(0) - 100) < 0.1 And
    Abs(Table.InsertionPoint(1) - 516) < 0.1 Then _
    sFileName = thisnewdwgpath & "Data\Rebar Data\" & thisnewdwgname &
    ".csv"
    'Begin James's Sub
    Call ExportTable(Table, sFileName) ' call Mike's sub
    'Pause James's Sub
    End If '<---------------------------------------------------Error here?
    not sure why I have an IF.
    Next
    'Continue Mike's Sub
    'write portion to retrieve the tabe reference
    'possible solutions are:
    Set oTable =
    ?????'<---------------------------------------------------What do I do here?
    '1. use Utility.Select
    '2. iterate the block table looking
    ' for any unnamed blocks starting
    ' with a T -> *T1, *T2, etc.
    '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 = "" & 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

    ' now, write out lines of collection into file (old-school way, I know
    there 's newer ways)
    Open sFileName For Output As #1 'improvement: variable lFileNum based on
    FreeFile()
    Dim cValue As Variant
    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 'cValue
    Close #1

    End Sub
     
    Dan, Aug 4, 2004
    #11
  12. Lets go back to James' version! You'll need to check the selectionset for
    objects before setting the table - which by the way, there is no such thing
    as a Tables Collection. Laurie was thinking logically but way off. So you
    can't use For statement you had setup - besides you know the point so use
    it. If the table can be anywhere, you'll have to iterate the blocks table
    looking for a name begining with "*T". Would be prettier calling subs/funcs
    instead of all in one, but I'm swamped at the moment so this is it FAST!
    Just add your file naming back in

    Public Sub ExportTable(oTable As AcadTable, sFileName as string)
    Dim oTable As AcadTable
    Dim lCols As Long
    Dim lRows As Long
    Dim cCntr As Long
    Dim rCntr As Long
    Dim sRow As String
    Dim cValues As Collection
    Set cValues = New Collection
    Dim oSet As AcadSelectionSet
    Dim lCols As Long
    Dim dPt(2)
    dPt(0) = -100 : dPt(1) = 516 :dPt(2) = 0
    oSet = ThisDrawing.SelectionSets.Add("DAN")
    oSet.SelectAtPoint(dPt)
    oTable = oSet(1)
    '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 = "" & 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

    ' now, write out lines of collection into file (old-school way, I know
    there's newer ways)
    open sFileName for output as #1 'improvement: variable lFileNum based
    on
    FreeFile()
    dim cValue as Variant
    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 'cValue
    close #1

    End Sub


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

    Dan Guest

    I will work on it, you all are very helpful, I have been learning alot.

    Thanks,
    Dan
     
    Dan, Aug 4, 2004
    #13
  14. Dan

    Dan Guest

    Thanks again for the help. I have worked/researched/on the code. I got an
    error at "oSet = ThisDrawing.SelectionSets.Add("DAN")". Does it need to be
    "Set oSet = ThisDrawing.SelectionSets.Add("DAN")" I get by that, then error
    at "lRows = oTable.Rows" and get an Object variable erro. Not sure where to
    go from here.
     
    Dan, Aug 5, 2004
    #14
  15. Thanks again for the help.
    No problem. I normally would do more but I have a Friday deadline and
    problems of my own
    Yes, my bad - I've been programming in .NET too long. .NET assumes the SET
    command
    Before you start pulling out table info, test to make sure you got a table.
    Try something like:

    If Not oTable = Nothing Then
    <do all the stuff we already wrote>
    Else
    Msgbox "No table found"
    End If
    End Sub

    I've got a feeling you/your program is not selecting anything. If true, you
    need to iterate all the blocks as I've mentioned before. If this can wait
    til early next week, I can whip something up for you unless someone else
    has the time to do it. Also, learn to use the Locals Window and step thru
    your program line-by-line as it runs. This will help you see where your
    problems are.

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

    Dan Guest

    Thanks again All,
    I will keep working on it. For just beginning, I guess writing my own
    export VB isnt a good start, but that how we learn. I wil post my findings.
    Thanks to everyone again.

    Dan
     
    Dan, Aug 5, 2004
    #16
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.