[VB.NET] Can't find blocks in drawing
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm trying to migrate some old macros we have developed to VB.NET and I'm having some difficulties with what (I think) should be a fairly simple thing to do. I'm trying to open a specific block (I know the block name) and read the attributes, but I'm having trouble even getting the block. I've spent about a day now reading up on BlockTableRecords, searching the forums and following different examples, but I can't seem to load any objects in the drawing.
main.vb
Imports Autodesk.AutoCAD.Runtime Imports Autodesk.AutoCAD.ApplicationServices Namespace CADPLUGIN Public Class Commands <CommandMethod("Welcon", CommandFlags.Session)> Public Shared Sub ShowForm() Dim frm As New Form1 'Launch user form Application.ShowModelessDialog(frm) End Sub End Class End Namespace
Form1.vb
Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Public Class Form1 Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click Dim Path As String = "C:\Test\CADPLUGIN\dwgs\11-E-03436_SHT2_rB.dwg" Dim i As Integer 'Set up AutoCAD and load drawing Dim acDocMgr As DocumentCollection = Application.DocumentManager Dim acDoc As Document = DocumentCollectionExtension.Open(acDocMgr, Path, False) Dim acDb As Database = acDocMgr.MdiActiveDocument.Database 'Init transaction manager and get block table Using acTrans As Transaction = acDb.TransactionManager.StartTransaction() Dim acBlkTbl As BlockTable = acTrans.GetObject(acDb.BlockTableId, OpenMode.ForRead) Dim acBlkTblRec As BlockTableRecord = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForRead) For Each acBlkId As ObjectId In acBlkTblRec 'DO SOMETHING i = i + 1 Next
End Using End Sub End Class
The idea is that I'd loop through all the objects in the Model Space until the block I want is found, and then get it's attributes. I've just set up a simple counter to start off with, which should increment for each object. But when I debug the script it just skips the for loop completely and ends the sub.
What am I doing wrong? How do I access blocks using the ACAD .NET API?