.NET

Reply
Active Contributor
ditran
Posts: 27
Registered: ‎01-28-2013
Message 1 of 3 (203 Views)
Accepted Solution

Get specified attribute from external block

203 Views, 2 Replies
03-21-2013 07:26 PM

Hi All,

 

How to get attributes from block located at external drawing?

 

Thanks for any helps.

 

 

 

Try this code tested on A2010,

change block name to your suit

 <CommandMethod("rad")> _
Public Sub ReadAttributesToDataBase()
Dim blockName As String = "TITLEBLOCK"
Dim ofd As New Autodesk.AutoCAD.Windows.OpenFileDialog( _
"Open source DWG for read", Nothing, "dwg", "", Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles)
Dim dr As System.Windows.Forms.DialogResult = ofd.ShowDialog()
If dr <> System.Windows.Forms.DialogResult.OK Then Return
Dim sourcedwg As String = ofd.Filename
Dim dict As New Dictionary(Of String, String)
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument()
Dim doclock As DocumentLock = doc.LockDocument()
Dim ed As Editor = doc.Editor
Dim db As Database = doc.Database
Using doclock
Using exdb As New Database(False, True)
exdb.ReadDwgFile(sourcedwg, System.IO.FileShare.ReadWrite, True, "")
Using extr As Transaction = exdb.TransactionManager.StartTransaction()
Dim exbt As BlockTable = DirectCast(extr.GetObject(exdb.BlockTableId, OpenMode.ForRead), BlockTable)
If exbt.Has(blockName) Then
Dim id As ObjectId = exbt(blockName)
Dim exbtr As BlockTableRecord = extr.GetObject(id, OpenMode.ForRead)
For Each brid As ObjectId In exbtr.GetBlockReferenceIds(True, True)
Dim exbref As BlockReference = extr.GetObject(brid, OpenMode.ForRead)
For Each attid In exbref.AttributeCollection
Dim attref As AttributeReference = extr.GetObject(attid, OpenMode.ForRead)
If Not dict.ContainsKey(attref.Tag) Then
dict.Add(attref.Tag, attref.TextString)
End If
Next
Next
End If
extr.Commit()
End Using
End Using
End Using
'use returned values here:
For Each Kvp As KeyValuePair(Of String, String) In dict
ed.WriteMessage(vbLf + "Tag: {0}" + vbTab + "Value: {1}" + vbLf, Kvp.Key, Kvp.Value)
Next
End Sub

 

Moderator
Alexander.Rivilis
Posts: 1,449
Registered: ‎04-09-2008
Message 2 of 3 (174 Views)

Re: Get specified attribute from external block

03-21-2013 11:48 PM in reply to: ditran

ditran wrote:

Hi All,

 

How to get attributes from block located at external drawing?

 

Thanks for any helps.

 

 

 


Block - is a BlockTableRecord or a BlockReference?

Attribute - is a AttributeDefinition or a AttributeReference or some others?

Please use a "AutoCAD .NET API terminology"


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 3 (141 Views)

Re: Get specified attribute from external block

03-24-2013 10:40 AM in reply to: ditran

Try this code tested on A2010,

change block name to your suit

        <CommandMethod("rad")> _
        Public Sub ReadAttributesToDataBase()
            Dim blockName As String = "TITLEBLOCK"
            Dim ofd As New Autodesk.AutoCAD.Windows.OpenFileDialog( _
             "Open source DWG for read", Nothing, "dwg", "", Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles)
            Dim dr As System.Windows.Forms.DialogResult = ofd.ShowDialog()
            If dr <> System.Windows.Forms.DialogResult.OK Then Return

            Dim sourcedwg As String = ofd.Filename

            Dim dict As New Dictionary(Of String, String)

            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument()
            Dim doclock As DocumentLock = doc.LockDocument()
            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database

            Using doclock
                Using exdb As New Database(False, True)
                    exdb.ReadDwgFile(sourcedwg, System.IO.FileShare.ReadWrite, True, "")
                    Using extr As Transaction = exdb.TransactionManager.StartTransaction()
                        Dim exbt As BlockTable = DirectCast(extr.GetObject(exdb.BlockTableId, OpenMode.ForRead), BlockTable)
                        If exbt.Has(blockName) Then
                            Dim id As ObjectId = exbt(blockName)
                            Dim exbtr As BlockTableRecord = extr.GetObject(id, OpenMode.ForRead)
                            For Each brid As ObjectId In exbtr.GetBlockReferenceIds(True, True)
                                Dim exbref As BlockReference = extr.GetObject(brid, OpenMode.ForRead)
                                For Each attid In exbref.AttributeCollection
                                    Dim attref As AttributeReference = extr.GetObject(attid, OpenMode.ForRead)
                                    If Not dict.ContainsKey(attref.Tag) Then
                                        dict.Add(attref.Tag, attref.TextString)
                                    End If
                                Next
                            Next
                        End If
                        extr.Commit()
                    End Using
                End Using
            End Using
            'use returned values here:
            For Each Kvp As KeyValuePair(Of String, String) In dict
                ed.WriteMessage(vbLf + "Tag: {0}" + vbTab + "Value: {1}" + vbLf, Kvp.Key, Kvp.Value)
            Next

        End Sub

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!