.NET

Reply
Active Contributor
xdbk07
Posts: 42
Registered: ‎11-06-2012
Message 1 of 15 (472 Views)
Accepted Solution

Get block reference information error

472 Views, 14 Replies
03-21-2013 04:13 AM

Hi All,

 

I get a block reference (has attribute) from other drawing as code follow:

 

Public Sub GetBlock_Info()
        Dim blockName As String = "test"
        Try
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim doclock As DocumentLock = doc.LockDocument()
            Dim ed As Editor = doc.Editor

            Dim ofd As New OpenFileDialog("Select Drawing", Nothing, "dwg; dwt", "ATS detai drawing", Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles)
            Dim dr As System.Windows.Forms.DialogResult = ofd.ShowDialog()
            If dr <> System.Windows.Forms.DialogResult.OK Then
                Return
            End If
            Dim filePath As String = ofd.Filename
            'Using doclock
            Using OpenDb As New Database
                OpenDb.ReadDwgFile(filePath, System.IO.FileShare.ReadWrite, True, "")

                Using opentrans As Transaction = OpenDb.TransactionManager.StartTransaction()
                    Dim openBT As BlockTable = DirectCast(opentrans.GetObject(OpenDb.BlockTableId, OpenMode.ForRead), BlockTable)
                    If openBT.Has(blockName) Then

                        Dim openbtrec As BlockTableRecord = openBT(blockName).GetObject(OpenMode.ForRead)

                        Dim openbref As New BlockReference(New Point3d(0, 0, 0), openbtrec.ObjectId)

                        If openbtrec.HasAttributeDefinitions Then

                            Dim attCol As AttributeCollection = openbref.AttributeCollection
                     
                            For Each attId As ObjectId In attCol
                                Dim attRef As AttributeReference = DirectCast(opentrans.GetObject(attId, OpenMode.ForRead), AttributeReference)

                                MsgBox(attRef.TextString)
                                
                            Next
                        End If
                    End If
                    opentrans.Commit()
                    opentrans.Dispose()
                End Using
            End Using
       Catch ex As Autodesk.AutoCAD.Runtime.Exception
        End Try
    End Sub

Block "test" contains some attributes but the code can not get them.

Hope for help.

 

 

 

I was right and you confuse BlockTableRecord and BlockReference, AttributeDefinition and AttributeReference.

 

Try this code (no error checking done):

 

<CommandMethod("test")> _
Public Sub test()
Dim blockName As String = "blank_sheet"
Dim ofd As New Autodesk.AutoCAD.Windows.OpenFileDialog( _
"CAD FILE", Nothing, "dwg; dwt", "CAD FILE", _
Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles)
Dim dr As System.Windows.Forms.DialogResult = ofd.ShowDialog()
If dr <> System.Windows.Forms.DialogResult.OK Then
Return
End If
Dim sourcefile As String = ofd.Filename
Dim doc As Document = _
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument()
Dim doclock As DocumentLock = doc.LockDocument()
Dim ed As Editor = doc.Editor
Dim acCurDb As Database = doc.Database
Using doclock
Using OpenDb As New Database(False, True)
OpenDb.ReadDwgFile(sourcefile, System.IO.FileShare.ReadWrite, True, "")
Using opentrans As Transaction = OpenDb.TransactionManager.StartTransaction()
Dim openBT As BlockTable = _
DirectCast(opentrans.GetObject(OpenDb.BlockTableId, OpenMode.ForRead), BlockTable)
If openBT.Has(blockName) Then
Dim id As ObjectId = openBT(blockName)
Dim btAttRec As BlockTableRecord = opentrans.GetObject(id, OpenMode.ForRead)
 For Each idBref As ObjectId In btAttRec.GetBlockReferenceIds(True, True)
Dim bRef As BlockReference = opentrans.GetObject(idBref, OpenMode.ForRead)
For Each idAttRef In bRef.AttributeCollection
Dim attRef As AttributeReference = opentrans.GetObject(idAttRef, OpenMode.ForRead)
ed.WriteMessage(vbCrLf + "attRef.Tag = {0}", attRef.Tag)
ed.WriteMessage(vbCrLf + "attRef.TextString = {0}", attRef.TextString)
Next
Next
End If
opentrans.Commit()
End Using
End Using
End Using
End Sub
Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 2 of 15 (455 Views)

Re: Get block reference information error

03-21-2013 04:57 AM in reply to: xdbk07

xdbk07 wrote:

...Block "test" contains some attributes but the code can not get them...


That is why you do not add attributes after inserting of block: Inserting a block with attributes to modelspace

You have to iterate block definition to find all non-constant AttributeDefinitions and adding corresponding AttributeReferences to openbref.AttributeCollection


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

Active Contributor
xdbk07
Posts: 42
Registered: ‎11-06-2012
Message 3 of 15 (443 Views)

Re: Get block reference information error

03-21-2013 05:56 AM in reply to: Alexander.Rivilis

I have just get information from attribute of that block. I do not want to add it into model space. How to?

Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 4 of 15 (437 Views)

Re: Get block reference information error

03-21-2013 06:26 AM in reply to: xdbk07

xdbk07 wrote:

I have just get information from attribute of that block. I do not want to add it into model space. How to?


I think you do not understand me. Reread my post and my link again.


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

Active Contributor
xdbk07
Posts: 42
Registered: ‎11-06-2012
Message 5 of 15 (418 Views)

Re: Get block reference information error

03-21-2013 10:03 AM in reply to: xdbk07

Was it an impossibility?

Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 6 of 15 (415 Views)

Re: Get block reference information error

03-21-2013 10:10 AM in reply to: xdbk07

xdbk07 wrote:

Was it an impossibility?


If you create BlockReference with code and do not add AttributeReferences to BlockReference.AttributeCollection - then there are no AttributeReference within BlockReference. So in this case you can only get AttributeDefinitions from BlockTableRecord.


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

Active Contributor
xdbk07
Posts: 42
Registered: ‎11-06-2012
Message 7 of 15 (403 Views)

Re: Get block reference information error

03-21-2013 05:51 PM in reply to: Alexander.Rivilis

But I want to read the block only. I don't creat or insert it. 

Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 8 of 15 (395 Views)

Re: Get block reference information error

03-21-2013 11:29 PM in reply to: xdbk07

xdbk07 wrote:

But I want to read the block only. I don't creat or insert it. 


Repeat:

 

So in this case you can only get AttributeDefinitions from BlockTableRecord.

 


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

Active Contributor
ditran
Posts: 27
Registered: ‎01-28-2013
Message 9 of 15 (392 Views)

Re: Get block reference information error

03-21-2013 11:47 PM in reply to: Alexander.Rivilis

Are there any way to get value of AttributeDefinitions. Thanks.

Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 10 of 15 (389 Views)

Re: Get block reference information error

03-22-2013 12:08 AM in reply to: ditran

ditran wrote:

Are there any way to get value of AttributeDefinitions. Thanks.


Of course!

 

      Dim DB As Database = HostApplicationServices.WorkingDatabase
      Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
      Dim trans As Transaction = DB.TransactionManager.StartTransaction
      Using trans
        Dim id As ObjectId ' id of our BlockTableRecord - in your's case is openBT(blockName)
        Dim btAttRec As BlockTableRecord = trans.GetObject(id, OpenMode.ForRead)
        Dim idAtt As ObjectId
        For Each idAtt In btAttRec
          Dim ent As Entity = trans.GetObject(idAtt, OpenMode.ForRead)
          If TypeOf ent Is AttributeDefinition Then
            Dim attDef As AttributeDefinition = CType(ent, AttributeDefinition)
            ed.WriteMessage(vbCrLf + "attdef.TextString = {0}", attDef.TextString)
            ed.WriteMessage(vbCrLf + "attdef.Tag = {0}", attDef.Tag)
          End If
        Next
        trans.Commit()
      End Using

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

Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.