CaveMan Posted July 27, 2012 Posted July 27, 2012 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 Quote
fixo Posted July 29, 2012 Posted July 29, 2012 Give this a try, change full path of folder and CSV file name at the start of command WTB 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'~ Quote
CaveMan Posted July 31, 2012 Author Posted July 31, 2012 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 Quote
fixo Posted July 31, 2012 Posted July 31, 2012 Thanks bro, Let me know if some issues is appears, ~'J'~ Quote
greekcypriot Posted August 28, 2013 Posted August 28, 2013 Hello "fixo", Would you please answer this question: Would DATAEXTRACT work with old Drawings prior to design of 2002 AutoCAD as well as the 2012 and later on? Would the data extract on drawing be consistent in Column count for every drawing? or does it very from drawing to drawing? I also try to add the VB code to a project and it can not find the names spaces mentioned in the import. Am I doing something wrong? I have AutoCAD 2012 installed. Thank you. Quote
CaveMan Posted August 29, 2013 Author Posted August 29, 2013 Hi There The sample code that was given was VB.NET version 3.5 AutoCAD 2012 was upgraded to .Net 4 The Name Spaces were changed between .Net 3.5 and .Net 4 Regrets i have never upgraded the code to work on .Net 4 To be able to compile and edit the code, you can use VS 2010 Link Provided Below http://www.microsoft.com/visualstudio/eng/downloads#d-2010-express The setup and environment would also require a bit of understanding in Developing .Net add ons for AutoCAD. Link provided below http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=1911627 The Data below will search for all Tables then loop through all the columns then rows for the particular table, if you had 10 various table in a drawing, it will extract all the data specific to each table, no matter how different they are. I say this under correction, but if you open a previous version of AutoCAD with tables in it and AutoDesk have not changed their table class then the data should be able to be extracted. If you look at the Properties of the object in AutoCAD as the attached image this must be Table. I have however seen other table types in AutoCAD, Like AutoCAD Structural Detailing, but have never tried to check or extract the data from them. Hope that the above information provides some help, If i ever get some time i will update the Data Extract function and attach a zip file with a .Net Compiled Project. Sadly i am renovating my house this side and not much time available. Best Regards Richard Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.