Imports System Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.Runtime Imports Autodesk.AutoCAD.EditorInput Imports Autodesk.AutoCAD.ApplicationServices Public Module AttributeHelper ' Set the value of all occurences of an attribute with the given tag, ' in every insertion of the given block, to the specified string. Public Function SetAttributeValue(ByVal btr As BlockTableRecord, ByVal tag As String, ByVal newValue As String) As Integer Dim cnt As Integer = 0 Using tr As Transaction = btr.Database.TransactionManager.StartTransaction Dim ids As ObjectIdCollection = btr.GetBlockReferenceIds(True, True) If (Not ids Is Nothing) Then Dim btrId As ObjectId For Each btrId In ids Dim blockref As BlockReference = TryCast(btrId.GetObject(OpenMode.ForRead, False), BlockReference) If (Not blockref.AttributeCollection Is Nothing) Then Dim attId As ObjectId For Each attId In blockref.AttributeCollection Dim att As AttributeReference = TryCast(attId.GetObject(OpenMode.ForRead, False), AttributeReference) If String.Equals(att.Tag, tag, StringComparison.OrdinalIgnoreCase) Then Try att.UpgradeOpen() Catch ex As Global.Autodesk.AutoCAD.Runtime.Exception If (ex.ErrorStatus <> ErrorStatus.OnLockedLayer) Then Throw End If Exit For End Try att.TextString = newValue att.DowngradeOpen() cnt += 1 Exit For End If Next End If Next End If tr.Commit() End Using Return cnt End Function End Module Public Class SetAttributeValueExample ' This helper method gets the ObjectId of the non-erased block of the given name Private Shared Function GetBlockTableRecordId(ByVal db As Database, ByVal name As String) As ObjectId If (db Is Nothing) Then Throw New ArgumentNullException("db") End If If String.IsNullOrEmpty(name) Then Throw New ArgumentException("name") End If Dim result As ObjectId = ObjectId.Null Using tr As Transaction = db.TransactionManager.StartTransaction Dim bt As BlockTable = TryCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable) Try result = bt.Item(name) If (Not result.IsNull AndAlso result.IsErased) Then Dim id As ObjectId For Each id In bt Dim btr As BlockTableRecord = TryCast(id.GetObject(OpenMode.ForRead), BlockTableRecord) If btr.Name.Equals(name, StringComparison.OrdinalIgnoreCase) Then Return id End If Next End If GetBlockTableRecordId = result Catch GetBlockTableRecordId = result Finally tr.Commit() End Try End Using End Function ' Command showing usage example: ' ' Prompts user for the name of a block, and the name ' of an attribute tag, and the string to assign to ' attributes with the given tag, in every insertion ' of the named block. _ Public Shared Sub SetAttributeValueDemoCommand() Dim pso As New PromptStringOptions(vbNewLine & "Block name: ") pso.AllowSpaces = True Dim doc As Document = Application.DocumentManager.MdiActiveDocument Dim res As PromptResult = doc.Editor.GetString(pso) If ((res.Status = PromptStatus.OK) AndAlso (res.StringResult.Trim <> String.Empty)) Then Dim blockName As String = res.StringResult Dim id As ObjectId = SetAttributeValueExample.GetBlockTableRecordId(doc.Database, blockName) If id.IsNull Then doc.Editor.WriteMessage(vbNewLine & "Block {0} not found.", New Object() {blockName}) Else pso.AllowSpaces = False pso.Message = vbNewLine & "Attribute tag: " res = doc.Editor.GetString(pso) If ((res.Status = PromptStatus.OK) AndAlso (res.StringResult.Trim <> String.Empty)) Then Dim tag As String = res.StringResult.ToUpper pso.AllowSpaces = True pso.Message = vbNewLine & "New value for all occurences of specfied attribute: " res = doc.Editor.GetString(pso) If ((res.Status = PromptStatus.OK) AndAlso (res.StringResult.Trim <> String.Empty)) Then Dim newValue As String = res.StringResult Using tr As Transaction = doc.TransactionManager.StartTransaction Dim btr As BlockTableRecord = TryCast(id.GetObject(OpenMode.ForRead), BlockTableRecord) Dim cnt As Integer = AttributeHelper.SetAttributeValue(btr, tag, newValue) doc.Editor.WriteMessage(vbNewLine & "Updated {0} attributes", New Object() {cnt}) tr.Commit() End Using End If End If End If End If End Sub End Class