
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
How to iterate through a particular named block, type and then update only that have a certain value?
I had a request to make a plugin that found every blocked called "TY-I-Generic" that has Type: "GENERIC" with Value: DC and change it to Value: MC-1.
So I adjusted some code I found here:
http://forums.autodesk.com/autodesk/attachments/autodesk/152/16859/1/AttributesVb.txt
as this...
Public Function SetAttributeValue(ByVal btr As BlockTableRecord, ByVal tag As String, ByVal oldValue 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 If att.TextString = oldValue 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 End If Next End If Next End If tr.Commit() End Using Return cnt End Function
then a button to trigger it
Private Sub btnChange_Click(sender As System.Object, e As System.EventArgs) Handles btnChange.Click Dim sOldValue As String sOldValue = txtValueFrom.Text Dim sNewValue As String sNewValue = txtValueTo.Text Dim sBlockName As String sBlockName = txtBlock.Text Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Dim id As ObjectId = GetBlockTableRecordId(doc.Database, sBlockName) If id.IsNull Then doc.Editor.WriteMessage(vbNewLine & "Block {0} not found.", New Object() {sBlockName}) Else Using tr As Transaction = doc.TransactionManager.StartTransaction Dim btr As BlockTableRecord = TryCast(id.GetObject(OpenMode.ForRead), BlockTableRecord) Dim cnt As Integer = SetAttributeValue(btr, Tag, sOldValue, sNewValue) doc.Editor.WriteMessage(vbNewLine & "Updated {0} attributes", New Object() {cnt}) tr.Commit() End Using End If MsgBox("Finished changing.") End Sub
sOldValue = "DC"
sNewValue = "MC-1"
sBlock = "TY-I-GENERIC"
are all pulled from text boxes...
And if I do SetAttributeValue(btr, Tag, sOldValue, sNewValue) that should do it but no... Currently with the above I do not get an error but no change either.
Also does this need to take into account the Type: DEVICE? Do I need to change this code so that it only finds block: TY-I-GENERIC and its Type: DEVICE and changes the value MC-1 to TY-I-GENERIC?
Is there anything else I can do to change the syntax to make sure this is correct?
Thank you in advance.
Solved! Go to Solution.