Your request is open to many possibilities. You could ask the users to select a particular BlockReference or perhaps you already know which one you need to change and so on. Here I am attaching a sample to change the AttributeReference TextString of all the BlockReferences on the drawing which have attributes. Hope it helps or gives you some ideas.
Public Sub GetAllAttributes() Dim Ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.D
ocumentManager.MdiActiveDocument.Editor Dim Doc As Autodesk.AutoCAD.ApplicationServices.Document = Autodesk.AutoCAD.ApplicationServices.Application.D ocumentManager.MdiActiveDocument Dim DocLock As Autodesk.AutoCAD.ApplicationServices.DocumentLock Dim Db As Database = HostApplicationServices.WorkingDatabase Dim Ids As New ObjectIdCollection Dim BlkTblRec As BlockTableRecord Dim AttRef As AttributeReference Dim BlkRef As BlockReference Dim BlkTbl As BlockTable Dim AttRefId As ObjectId Dim RefId As ObjectId Dim Id As ObjectId Dim Mtxt As MText DocLock = Doc.LockDocument Try Using Trans As Transaction = Autodesk.AutoCAD.ApplicationServices.Application.D ocumentManager.MdiActiveDocument.TransactionManage r.StartTransaction BlkTbl = CType(Trans.GetObject(Db.BlockTableId, OpenMode.ForRead), BlockTable) For Each Id In BlkTbl BlkTblRec = CType(Trans.GetObject(Id, OpenMode.ForRead), BlockTableRecord) If BlkTblRec.HasAttributeDefinitions = True Then Ids = BlkTblRec.GetBlockReferenceIds(False, False) If Ids.Count > 0 Then For Each RefId In Ids BlkRef = CType(Trans.GetObject(RefId, OpenMode.ForWrite), BlockReference) For Each AttRefId In BlkRef.AttributeCollection AttRef = CType(Trans.GetObject(AttRefId, OpenMode.ForWrite), AttributeReference) If AttRef.IsMTextAttribute Then Mtxt = AttRef.MTextAttribute Mtxt.Contents = "New TextString" AttRef.MTextAttribute = Mtxt AttRef.UpdateMTextAttribute() Else AttRef.TextString = "New TextString" End If Next Next End If End If Next Trans.Commit() End Using Catch ex As Exception MessageBox.Show(ex.Message, "Changing Attributes", MessageBoxButtons.OK, MessageBoxIcon.Error) Finally DocLock.Dispose() End Try End Sub
You should probably take some time to read the docs for the APIs you're attempting to use in that code, to understand them better.
GetBlockReferenceIds( false, false ) will return the ids of any block that is directly or indirectly inserted into the block whose BlockTableRecord you invoke the method on.
Since your code is iterating over the entire contents of the BlockTable, the 'false' argument to GetBlockReferenceIds() is redundant.
DP you advice has been well taken, thank you for pointing it out and sharing your knowledge. I really wish I could be able to understand everything I read on the docs. I guess that perhaps the documentation has been wrintten by people who know for people who know, not for the layperson like me.
I need this type of thing too. Thanks for posting. I am getting some errors when I just plugged it into my code. Is there an Autodesk dll I forgot to include causing the error past Autodesk.AutoCAD.ApplicationServices.?
It seems that when I put the code in a tutorial there were no errors, but then putting it in another, it had fewer errors. Let me investigate why and I will get back.
Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register