I' m struggling with setting attribute values on an external block. I've posted some code below that I've been experimenting with.
Most of this code came from either this site of the Autodesk site.
The block is an external drawing with an attribute. The code below inserts the drawing, but doesn't set the attribute. If I explode the block, the attribute tag is there. Any help would be greatly appreciated.
Dim hingeBlockFilePath As String = "C:\cad\support\ads\ss edge\DOORNUM.dwg"
Dim inspt As New Point3d(7.53, 10.35, 0)
Dim newBlockName As String = "DOORNUMBER"
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Using docloc As DocumentLock = doc.LockDocument
Using trans As Transaction = doc.TransactionManager.StartTransaction
'Read Block Drawing's Database
Dim db As New Database(False, False)
db.ReadDwgFile(hingeBlockFilePath, FileOpenMode.OpenForReadAndAllShare, True, vbNullString)
'insert it as a new block
Dim idBTR As ObjectId = doc.Database.Insert(newBlockName, db, True)
Using bt As BlockTable = trans.GetObject(doc.Database.BlockTableId, OpenMode.ForRead), _
btr As BlockTableRecord = trans.GetObject(bt(BlockTableRecord.PaperSpace), OpenMode.ForWrite), _
bref As BlockReference = New BlockReference(inspt, idBTR)
btr.AppendEntity(bref)
trans.TransactionManager.AddNewlyCreatedDBObject(bref, True)
Dim blkTblR As BlockTableRecord = btr.Id.GetObject(OpenMode.ForRead)
For Each objId As ObjectId In blkTblR
Dim obj As DBObject = objId.GetObject(OpenMode.ForRead)
If TypeOf obj Is AttributeDefinition Then
Dim ad As AttributeDefinition = objId.GetObject(OpenMode.ForRead)
Dim ar As AttributeReference = New AttributeReference()
ar.SetAttributeFromBlock(ad, bref.BlockTransform)
ar.Position = ad.Position.TransformBy(bref.BlockTransform)
bref.AttributeCollection.AppendAttribute(ar)
trans.AddNewlyCreatedDBObject(ar, True)
End If
Next
Dim myAttColl As AttributeCollection = bref.AttributeCollection
For Each myAttRefId As ObjectId In myAttColl
Dim myAttRef As AttributeReference = myAttRefId.GetObject(OpenMode.ForWrite)
If myAttRef.Tag = "DRNUM" Then myAttRef.TextString = "TEST"
Next
trans.Commit()
End Using
End Using
End Using