<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: [VB.NET] Can't find blocks in drawing in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/vb-net-can-t-find-blocks-in-drawing/m-p/4877192#M45086</link>
    <description>&lt;P&gt;Hi ,&lt;/P&gt;&lt;P&gt;Try to lock the document before starting&amp;nbsp; transaction. As you are using modeless dialog so to access a document you have to lock it first. you can use something like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;Using&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;&lt;FONT face="Consolas" size="2"&gt; acLckDoc &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;As&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2" color="#2b91af"&gt;&lt;FONT face="Consolas" size="2" color="#2b91af"&gt;&lt;FONT face="Consolas" size="2" color="#2b91af"&gt;DocumentLock&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;&lt;FONT face="Consolas" size="2"&gt; = acDoc.LockDocument()&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;Using&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;&lt;FONT face="Consolas" size="2"&gt; acTrans &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;As&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2" color="#2b91af"&gt;&lt;FONT face="Consolas" size="2" color="#2b91af"&gt;&lt;FONT face="Consolas" size="2" color="#2b91af"&gt;Transaction&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;&lt;FONT face="Consolas" size="2"&gt; = acCurDb.TransactionManager.StartTransaction()&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;'do as you like&lt;/P&gt;&lt;P&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;End&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;Using&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;End&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;Using&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Mar 2014 08:10:48 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2014-03-11T08:10:48Z</dc:date>
    <item>
      <title>[VB.NET] Can't find blocks in drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-can-t-find-blocks-in-drawing/m-p/4876608#M45085</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;main.vb&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices

Namespace CADPLUGIN

    Public Class Commands
        &amp;lt;CommandMethod("Welcon", CommandFlags.Session)&amp;gt;
        Public Shared Sub ShowForm()
            Dim frm As New Form1

            'Launch user form
            Application.ShowModelessDialog(frm)
        End Sub
    End Class

End Namespace&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;STRONG&gt;Form1.vb&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;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
&lt;BR /&gt;        End Using
    End Sub
End Class&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;But when I debug the script it just skips the for loop completely and ends the sub.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What am I doing wrong? How do I access blocks using the ACAD .NET API?&lt;/P&gt;</description>
      <pubDate>Mon, 10 Mar 2014 23:34:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-can-t-find-blocks-in-drawing/m-p/4876608#M45085</guid>
      <dc:creator>subscriptions</dc:creator>
      <dc:date>2014-03-10T23:34:02Z</dc:date>
    </item>
    <item>
      <title>Re: [VB.NET] Can't find blocks in drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-can-t-find-blocks-in-drawing/m-p/4877192#M45086</link>
      <description>&lt;P&gt;Hi ,&lt;/P&gt;&lt;P&gt;Try to lock the document before starting&amp;nbsp; transaction. As you are using modeless dialog so to access a document you have to lock it first. you can use something like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;Using&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;&lt;FONT face="Consolas" size="2"&gt; acLckDoc &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;As&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2" color="#2b91af"&gt;&lt;FONT face="Consolas" size="2" color="#2b91af"&gt;&lt;FONT face="Consolas" size="2" color="#2b91af"&gt;DocumentLock&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;&lt;FONT face="Consolas" size="2"&gt; = acDoc.LockDocument()&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;Using&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;&lt;FONT face="Consolas" size="2"&gt; acTrans &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;As&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2" color="#2b91af"&gt;&lt;FONT face="Consolas" size="2" color="#2b91af"&gt;&lt;FONT face="Consolas" size="2" color="#2b91af"&gt;Transaction&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;&lt;FONT face="Consolas" size="2"&gt; = acCurDb.TransactionManager.StartTransaction()&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;'do as you like&lt;/P&gt;&lt;P&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;End&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;Using&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;End&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;&lt;FONT face="Consolas" size="2" color="#0000ff"&gt;Using&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Mar 2014 08:10:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-can-t-find-blocks-in-drawing/m-p/4877192#M45086</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2014-03-11T08:10:48Z</dc:date>
    </item>
    <item>
      <title>Re: [VB.NET] Can't find blocks in drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-can-t-find-blocks-in-drawing/m-p/4877202#M45087</link>
      <description>&lt;P&gt;See codes gathered from my old project, change to your suit&lt;/P&gt;</description>
      <pubDate>Tue, 11 Mar 2014 08:20:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-can-t-find-blocks-in-drawing/m-p/4877202#M45087</guid>
      <dc:creator>Hallex</dc:creator>
      <dc:date>2014-03-11T08:20:33Z</dc:date>
    </item>
    <item>
      <title>Re: [VB.NET] Can't find blocks in drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-can-t-find-blocks-in-drawing/m-p/4879570#M45088</link>
      <description>&lt;P&gt;Thanks for the replies, but I think I found the problem. I was pointing the database to &lt;FONT face="courier new,courier"&gt;DocumentManager.MdiActiveDocument.Database&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Dim acDocMgr As DocumentCollection = Application.DocumentManager
Dim acDoc As Document = DocumentCollectionExtension.Open(acDocMgr, Path, False)
Dim acDb As Database = acDocMgr.MdiActiveDocument.Database&lt;/PRE&gt;&lt;P&gt;Changing it fo &lt;FONT face="courier new,courier"&gt;acDoc.Database&lt;/FONT&gt; seems to have solved the problem. Here's the final code I came up with. Instead of looping through the entire BlockTableRecord, I'm using a selection filter to create an array of all the blocks in the Model Space and then loop through them. If the block isn't found, it tries again in the Paper Space.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Private Function SelFilter(SearchElem As String, Optional ByVal Space As Integer = 0)
    'Create a selection filter to limit the number of iterations required when looping through drawing objects
    'Pass the element name as a string and a Model/Paper Space selection
    '(0=Model Space, 1=Paper Space)
    Dim acTypValAr(1) As TypedValue
    acTypValAr.SetValue(New TypedValue(67, Space), 0)
    acTypValAr.SetValue(New TypedValue(DxfCode.Start, SearchElem), 1)
    Dim acSelFtr As SelectionFilter = New SelectionFilter(acTypValAr)
    Return acSelFtr
End Function

Private Function ReadTitleblock(ByVal dwgPath As String, ByVal Index As Integer)
    Dim DwgAtts As New TransmittalData
    Dim bBlkFnd As Boolean = False

    'Set up AutoCAD and load drawing
    Dim acDocMgr As DocumentCollection = Application.DocumentManager
    Dim acDoc As Document = DocumentCollectionExtension.Open(acDocMgr, dwgPath, True)
    Dim acDb As Database = acDoc.Database
    Dim acEd As Editor = acDocMgr.MdiActiveDocument.Editor

    'Initialise transaction manager
    Using acTrans As Transaction = acDb.TransactionManager.StartTransaction()

        'Select all blocks in drawing and search for titleblock
        Dim acObjCol() As ObjectId
        For i As Integer = 0 To 1
            acObjCol = acEd.SelectAll(SelFilter("INSERT", i)).Value.GetObjectIds()
            For Each acObj As ObjectId In acObjCol
                Dim acBlkRef As BlockReference = acTrans.GetObject(acObj, OpenMode.ForRead)
                If acBlkRef.Name.ToUpper() Like gbl.Client(Index).titleblock Then
                    'Titleblock found, get attributes
                    Dim AcAttCol As AttributeCollection = acBlkRef.AttributeCollection
                    For Each acAtt As ObjectId In AcAttCol
                        Dim acAttRef As AttributeReference = acTrans.GetObject(acAtt, OpenMode.ForRead)
						
			    'EVALUATE ATTRIBUTES HERE

                    Next
                    bBlkFnd = True
                    Exit For
                End If
            Next

            'Titleblock attributes found
            'Do not attempt Paper Space search
            If bBlkFnd = True Then
                Exit For
            End If
        Next
    End Using

    'Close drawing
    DocumentExtension.CloseAndDiscard(acDoc)

    'Return drawing attributes
    Return DwgAtts
End Function&lt;/PRE&gt;&lt;P&gt;This seems to work fine. The function is returning a custom structure I've made that contains all the attribute values that I need. However now I'm having problems closing the documents. AutoCAD crashes after calling the function. If I remove the &lt;FONT face="courier new,courier"&gt;DocumentExtension.CloseAndDiscard(acDoc)&lt;/FONT&gt; line it is fine, except the documents obviously stay opened.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Mar 2014 21:57:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-can-t-find-blocks-in-drawing/m-p/4879570#M45088</guid>
      <dc:creator>subscriptions</dc:creator>
      <dc:date>2014-03-11T21:57:59Z</dc:date>
    </item>
    <item>
      <title>Re: [VB.NET] Can't find blocks in drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-can-t-find-blocks-in-drawing/m-p/4879666#M45089</link>
      <description>&lt;P&gt;After some more debugging and investigation I have worked out the cause of the original problem. It seems that &lt;FONT face="courier new,courier"&gt;MdiActiveDocument&lt;/FONT&gt; is not updating when I load a new document.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Dim acDocMgr As DocumentCollection = Application.DocumentManager
Dim acDoc As Document = DocumentCollectionExtension.Open(acDocMgr, dwgPath, True)&lt;/PRE&gt;&lt;P&gt;When I load the new drawing using DocumentcollectionExtension.Open, acDogMgr.MdiActiveDocument is not updating to the newly opened document. That is why when I set the Database to &lt;FONT face="courier new,courier"&gt;acDocMgr.MdiActiveDocument.Database&lt;/FONT&gt; it wasn't finding any elements in the BlockTableRecord (it was loading the BlockTableRecord for the blank document that AutoCAD loads upon opening).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm now trying to close the document with:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;DocumentExtension.CloseAndDiscard(acDocMgr.MdiActiveDocument)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;But obviously this isn't working because MdiActiveDocument isn't actually pointing to the open document. What is going on?&lt;/P&gt;</description>
      <pubDate>Tue, 11 Mar 2014 23:44:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-can-t-find-blocks-in-drawing/m-p/4879666#M45089</guid>
      <dc:creator>subscriptions</dc:creator>
      <dc:date>2014-03-11T23:44:18Z</dc:date>
    </item>
    <item>
      <title>Re: [VB.NET] Can't find blocks in drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-can-t-find-blocks-in-drawing/m-p/4880256#M45090</link>
      <description>&lt;P&gt;Can you add below line after opening your drawing? As you did not make it active so you should not expect that it become active design automatically. Please add the below line and try once again&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;  acDocMgr.MdiActiveDocument = acDoc&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Mar 2014 08:08:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-can-t-find-blocks-in-drawing/m-p/4880256#M45090</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2014-03-12T08:08:09Z</dc:date>
    </item>
    <item>
      <title>Re: [VB.NET] Can't find blocks in drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-can-t-find-blocks-in-drawing/m-p/4880882#M45091</link>
      <description>Sorry, my code is tested in A2014, all is working good,&lt;BR /&gt;so I couldn't add any more</description>
      <pubDate>Wed, 12 Mar 2014 12:34:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-can-t-find-blocks-in-drawing/m-p/4880882#M45091</guid>
      <dc:creator>Hallex</dc:creator>
      <dc:date>2014-03-12T12:34:38Z</dc:date>
    </item>
  </channel>
</rss>

