Listing all xrefs attached to a group of (unopened) drawings
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Help - I'm using AutoCAD 2013 64 bit and MS Visual Basic 2010 Express (and Excel 2007), and I'm trying to write VB.NET code to list all xrefs attached to a group of drawings. The list of drawings is in column 1 of an Excel spreadsheet, and I want to list all xref filenames in columns 2 onwards - one row per drawing.
The Excel part works fine. I just wish it was as easy to extract information from DWG files.
I created a "Windows Form Application" project in VB 2010 and added a button to Form1. Clicking on the button runs the code.
When adding references I added "Microsoft Excel 12.0 Object Library" COM for Excel (as per Excel website tutorial), and "acdbmgd.dll" and "acmgd.dll" (both with Copy Local=False) as per AutoDesk .NET tutorial. But I got warning messages saying Acdbmgd.dll and Acmgd.dll target a different processor. Is this different to the Excel COM target processor, or do I need to change the target processor for the whole project (and if so, how)?
I tried adding "AutoCAD 2013 Type Library" COM reference instead of "acdbmgd.dll" and "acmgd.dll" which got rid of the target processor warning messages, and I could then open the dwg files successfully, but still the project crashes.
Here's the code so far...
Imports Excel = Microsoft.Office.Interop.Excel
' do I need to include Imports for AutoCAD here? Various websites list AutoDesk.AutoCAD... imports here, but AutoDesk isn't an available option when I try adding this here - maybe because I have the wrong references?
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim rowDrawing As Integer, colmXref As Integer
Dim szFilename As String
Dim cadApp As AutoCAD.AcadApplication
Dim cadDwg As AutoCAD.AcadDocument
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open("c:\test.xlsx", [ReadOnly]:=True) ' ReadOnly is temporary as file is locked due to project crashing
xlWorkSheet = xlWorkBook.Worksheets("Sheet1")
rowDrawing = 1
Do
szFilename = xlWorkSheet.Cells(rowDrawing, 1).Value
If szFilename <> "" Then
cadApp = New AutoCAD.AcadApplication
cadDwg = cadApp.Documents.Open(szFilename, [ReadOnly]:=True)
colmXref = 2
For Each cadBlock As AutoCAD.AcadBlock In cadDwg.Database.Blocks ' crashes here
If cadBlock.IsXRef Then
xlWorkSheet.Cells(rowDrawing, colmXref).Value = cadBlock.Name
colmXref = colmXref + 1
End If
Next
cadDwg.Close()
cadApp.Quit()
releaseObject(cadApp)
releaseObject(cadDwg)
rowDrawing = rowDrawing + 1
End If
Loop Until szFilename = ""
'xlWorkBook.Save() 'temporarily commented out as can only open as ReadOnly as file is locked due to project crashing
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
End Sub
Private Sub releaseObject(ByVal obj As Object) ' this subroutine is copied from Excel tutorial website
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Class
When I run the project I also get a "A first change exception of type 'System.Runtime.InteropServices.COMException' occurred in MyProject.exe" message. How can I rectify this?
Currently it crashes when it gets to the cadDwg.Database.Blocks line saying "Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))".
The xlsx file and the dwg files refered to in the xlsx file all exist. I've tried running it both with AutoCAD 2013 running and with AutoCAD 2013 not running. Still doesn't work
I feel so close, but what am I missing? I haven't downloaded the "ObjectARX for AutoCAD 2013 (32-bit and 64-bit)" from AutoDesk yet because (i) I have to get a licence, and (ii) it's a very large file, and (iii) I seem to be able to open drawing files without it, but just not access the data I want in the files.
Any help would be gratefully appreciated. I've got 1500+ drawings to look at by the end of the week. If this doesn't work it's back to AutoLisp (and maybe script files, but that's another headache).
Thanks