.NET

Reply
Mentor
Amremad
Posts: 212
Registered: ‎08-24-2010
Message 1 of 10 (513 Views)
Accepted Solution

How Can I Get All Block Attributes and change it

513 Views, 9 Replies
05-22-2013 03:46 AM

hi every one

 

How Can I Get All Block Attributes and change it

Distinguished Contributor
HJohn1
Posts: 169
Registered: ‎09-26-2008
Message 2 of 10 (498 Views)

Re: How Can I Get All Block Attributes and change it

05-22-2013 05:46 AM 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

 

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 3 of 10 (473 Views)

Re: How Can I Get All Block Attributes and change it

05-22-2013 04:20 PM 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.

 

Distinguished Contributor
HJohn1
Posts: 169
Registered: ‎09-26-2008
Message 4 of 10 (449 Views)

Re: How Can I Get All Block Attributes and change it

05-23-2013 07:02 AM 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. 

Mentor
Amremad
Posts: 212
Registered: ‎08-24-2010
Message 5 of 10 (391 Views)

Re: How Can I Get All Block Attributes and change it

06-02-2013 02:13 AM 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

Mentor
Amremad
Posts: 212
Registered: ‎08-24-2010
Message 6 of 10 (389 Views)

Re: How Can I Get All Block Attributes and change it

06-02-2013 02:14 AM in reply to: DiningPhilosopher

thanks MR. DiningPhilosopher for you advice

i promise you i will do than

Valued Contributor
jaboone
Posts: 91
Registered: ‎12-05-2011
Message 7 of 10 (351 Views)

Re: How Can I Get All Block Attributes and change it

06-04-2013 05:51 AM 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
Mentor
Amremad
Posts: 212
Registered: ‎08-24-2010
Message 8 of 10 (328 Views)

Re: How Can I Get All Block Attributes and change it

06-05-2013 01:39 AM 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 ?

Distinguished Contributor
jboone
Posts: 167
Registered: ‎08-31-2006
Message 9 of 10 (315 Views)

Re: How Can I Get All Block Attributes and change it

06-05-2013 07:00 AM 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.

Mentor
Amremad
Posts: 212
Registered: ‎08-24-2010
Message 10 of 10 (309 Views)

Re: How Can I Get All Block Attributes and change it

06-05-2013 07:13 AM in reply to: jboone

 i think you , you must choose the correct framework version

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community