+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Full Member CaveMan's Avatar
    Computer Details
    CaveMan's Computer Details
    Operating System:
    XP - Win7 32 & 64
    Computer:
    64Bit Hybrid
    CPU:
    Intel i7
    RAM:
    12 Gig
    Graphics:
    GeForce GT 220
    Monitor:
    Dual Dell 22"
    Using
    AutoCAD 2011
    Join Date
    Jul 2011
    Location
    South Africa - JHB
    Posts
    40

    Default AutoCAD Tables Data Extraction

    Registered forum members do not see this ad.

    Good Day

    looking for assistance with extracting data from an AutoCAD table.
    Hav found a fex examples on how to create a table in AutoCAD with VB.Net but no luck on how to loop through the rows and columns in an AutoCAD table and see the items within. Want to be able to loop through a list of drawings, open drawings find the table in Paper or Model space, loop through the records and export them to excel - SQL or XML, or even a simple method of creating a collection if records in VB.Net, where one colud then export the list

    Any assistnace most welcome
    Regards

  2. #2
    Super Member fixo's Avatar
    Computer Details
    fixo's Computer Details
    Operating System:
    Windows 7
    Motherboard:
    E7500
    CPU:
    Intel(R)Core(TM)2 DUO CPU 2.93HGz
    RAM:
    4098 Gb
    Graphics:
    1024 Gb
    Using
    AutoCAD 2009
    Join Date
    Jul 2005
    Location
    Pietari, Venäjä
    Posts
    1,598

    Default

    Give this a try, change full path of folder and CSV file name
    at the start of command WTB

    Code:
     
    Imports System
    Imports System.Text
    Imports System.IO
    Imports System.Data
    Imports System.Linq
    Imports Autodesk.AutoCAD.Runtime
    Imports Autodesk.AutoCAD.DatabaseServices
    Imports Autodesk.AutoCAD.EditorInput
    Imports Autodesk.AutoCAD.Geometry
    Imports Autodesk.AutoCAD.ApplicationServices
     
    ''-----------------------------------------''
     
            Public Function GetDirectoryDrawings(directoryFullName As String, subDirsBrowse As Boolean) As List(Of FileInfo)
                Dim dir As New DirectoryInfo(directoryFullName)
                If Not dir.Exists Then
                    Throw New DirectoryNotFoundException()
                    Return Nothing
                End If
                Dim opt As SearchOption = SearchOption.AllDirectories
                If subDirsBrowse = False Then opt = SearchOption.TopDirectoryOnly
                Return dir.GetFiles("*dwg", opt).AsEnumerable().OrderBy(Function(x) x.FullName).ToList()
            End Function
            'change command name to suit
            <CommandMethod("wtb", CommandFlags.Session)> _
            Public Sub testDirFiles()
                Dim folder As String = "C:\Test\"
                Dim csvfile As String = "C:\Test\atable.csv"
                Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
                Dim db As Database = doc.Database
                Dim ed As Editor = doc.Editor
                Dim fd As List(Of FileInfo) = GetDirectoryDrawings(folder, False) ''     True for processing the subdirectories
                Dim collect As New StringBuilder
                If fd.Count = 0 Then Return
     
                Try
                    Using doclock As DocumentLock = doc.LockDocument()
                        For n As Integer = 0 To fd.Count - 1
                            Dim dwgname As String = fd(n).FullName
                            Using newdb As Database = New Database(False, True)
                                ed.WriteMessage(vbLf + dwgname + vbLf)
                                Try
                                    newdb.ReadDwgFile(dwgname, FileOpenMode.OpenForReadAndAllShare, False, Nothing)
                                Catch ex As Autodesk.AutoCAD.Runtime.Exception
                                    If ex.ErrorStatus = ErrorStatus.DwgNeedsRecovery Then
                                        Continue For
                                    End If
                                End Try
     
                                Using newtr As Transaction = newdb.TransactionManager.StartTransaction
                                    ' Open dictionary for reading 
                                    Dim layoutDict As DBDictionary = DirectCast(newtr.GetObject(newdb.LayoutDictionaryId, OpenMode.ForRead), DBDictionary)
                                    ' Loop through dictionary entries
                                    For Each entry As DictionaryEntry In layoutDict
                                        Dim ltr As Layout = DirectCast(newtr.GetObject(DirectCast(entry.Value, ObjectId), OpenMode.ForRead), Layout)
                                        Dim btr As BlockTableRecord = DirectCast(newtr.GetObject(ltr.BlockTableRecordId, OpenMode.ForRead), BlockTableRecord)
                                        For Each id As ObjectId In btr
                                            Dim obj As DBObject = DirectCast(newtr.GetObject(id, OpenMode.ForRead), DBObject)
                                            If TypeOf obj Is Table Then
                                                Dim tbl As Table = TryCast(obj, Table)
                                                If tbl IsNot Nothing Then
                                                    collect.AppendLine(String.Format("---------------------------------------"))
                                                    collect.AppendLine(String.Format("Drawing name: {0}", dwgname))
                                                    collect.AppendLine(String.Format("Tab: {0}", ltr.LayoutName))
                                                    Dim numrows As Integer = tbl.Rows.Count
                                                    Dim numcols As Integer = tbl.Columns.Count
                                                    For i As Integer = 0 To numrows - 1
                                                        Dim rowline As New List(Of String)
                                                        Dim line As String = String.Empty
                                                        For j = 0 To numcols - 1
                                                            Dim cel As Cell = tbl.Cells(i, j)
                                                            rowline.Add(cel.TextString)
                                                            line = line + cel.TextString + vbTab
                                                        Next
                                                        collect.AppendLine(line.TrimEnd(vbTab))
                                                    Next
                                                End If
                                            End If
                                        Next
                                    Next
                                    newtr.Commit()
                                End Using
                            End Using
                        Next
                    End Using
                    WriteTableToFile(csvfile, collect)
                Catch ex As System.Exception
                    ed.WriteMessage(vbLf + ex.ToString + vbLf)
                Finally
                    ed.WriteMessage(vbLf + vbTab + "--->   File saved as: {0}   <---" + vbLf, csvfile)
                End Try
            End Sub
     
            Public Sub WriteTableToFile(fpath As String, data As StringBuilder)
     
                Using fs As New FileStream(fpath, FileMode.Append, FileAccess.Write)
                    Using sw As New StreamWriter(fs, Encoding.ASCII)
                        sw.WriteLine(data.ToString)
                    End Using
                End Using
            End Sub
    ~'J'~

    The soul is healed by being with children. - Fyodor Dostoyevsky, novelist (1821-1881)

  3. #3
    Full Member CaveMan's Avatar
    Computer Details
    CaveMan's Computer Details
    Operating System:
    XP - Win7 32 & 64
    Computer:
    64Bit Hybrid
    CPU:
    Intel i7
    RAM:
    12 Gig
    Graphics:
    GeForce GT 220
    Monitor:
    Dual Dell 22"
    Using
    AutoCAD 2011
    Join Date
    Jul 2011
    Location
    South Africa - JHB
    Posts
    40

    Default

    Good Day

    Been a while since i have done some development, as always much appreciate your assistance and example code . . .
    Will work through your example code, Will let you know if success.

    Best Regards Richard
    Keep Well

  4. #4
    Super Member fixo's Avatar
    Computer Details
    fixo's Computer Details
    Operating System:
    Windows 7
    Motherboard:
    E7500
    CPU:
    Intel(R)Core(TM)2 DUO CPU 2.93HGz
    RAM:
    4098 Gb
    Graphics:
    1024 Gb
    Using
    AutoCAD 2009
    Join Date
    Jul 2005
    Location
    Pietari, Venäjä
    Posts
    1,598

    Default

    Registered forum members do not see this ad.

    Thanks bro,
    Let me know if some issues is appears,

    ~'J'~
    The soul is healed by being with children. - Fyodor Dostoyevsky, novelist (1821-1881)

Similar Threads

  1. Data extraction tables: Extremely tantalizing but entirely useless?
    By WINTERMUTE in forum AutoCAD Drawing Management & Output
    Replies: 15
    Last Post: 1st Jun 2012, 09:10 pm
  2. Help with data extraction tables and fields
    By Catgirl in forum AutoCAD Beginners' Area
    Replies: 1
    Last Post: 14th Jun 2011, 03:01 am
  3. Data Extraction Tables
    By leemckenzie in forum AutoCAD General
    Replies: 0
    Last Post: 19th Jul 2010, 12:42 am
  4. Autocad 2010 Data Extraction
    By WCCSAM in forum AutoCAD Drawing Management & Output
    Replies: 0
    Last Post: 20th May 2010, 08:31 pm
  5. Tables from Datalinking and Data Extraction in AutoCAD 2008
    By AutoCAD Insider in forum AutoCAD RSS Feeds
    Replies: 0
    Last Post: 22nd Feb 2007, 04:50 pm

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts