.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How Can I Get All Block Attributes and change it

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
Amremad
961 Views, 9 Replies

How Can I Get All Block Attributes and change it

hi every one

 

How Can I Get All Block Attributes and change it

9 REPLIES 9
Message 2 of 10
HJohn1
in reply to: Amremad

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.DocumentManager.MdiActiveDocument.Editor
            Dim Doc As Autodesk.AutoCAD.ApplicationServices.Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.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.DocumentManager.MdiActiveDocument.TransactionManager.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

 

Message 3 of 10
DiningPhilosopher
in reply to: HJohn1

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.

 

Message 4 of 10
HJohn1
in reply to: DiningPhilosopher

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. 

Message 5 of 10
Amremad
in reply to: HJohn1

sorry for late


i was very sick , but now iam good , thanks god
thank you very much MR. HJohn1 for helping me

Message 6 of 10
Amremad
in reply to: DiningPhilosopher

thanks MR. DiningPhilosopher for you advice

i promise you i will do than

Message 7 of 10
jaboone
in reply to: Amremad

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.?

Thanks.

 

Learning as I go
Message 8 of 10
Amremad
in reply to: jaboone

Mr.  Jaboone

 

can you send me your source code to see what's a problem !!!

and tell me what's the version of cad  and framework ?

Message 9 of 10
jboone
in reply to: Amremad

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.

Message 10 of 10
Amremad
in reply to: jboone

 i think you , you must choose the correct framework version

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost