.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get specified attribute from external block

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
ditran
577 Views, 2 Replies

Get specified attribute from external block

Hi All,

 

How to get attributes from block located at external drawing?

 

Thanks for any helps.

 

 

 

2 REPLIES 2
Message 2 of 3
Alexander.Rivilis
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"

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 3 of 3
Hallex
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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost