.NET

.NET

Reply
Active Contributor
btmcad
Posts: 31
Registered: ‎05-09-2007
Message 1 of 3 (241 Views)

Problem with editing attributes in a block

241 Views, 2 Replies
03-15-2012 02:02 PM

I'm trying to edit the attributes in a titleblock using VB.net. The block name is "Titleblock". It is already inserted into a drawing. My program does not need to insert it. It only needs to find the reference, for all layouts, and update a couple attributes for the titleblock on everylayout. I know some of the basics, but get confused with blocktables, and blocktablerecords.

Here is a piece of code I was experimenting with. With this code, I'm trying to get to the attributes, and read an attribute value for a drawing number.  It fails when I try to assign a value to the variable blkreftitle. Any help and suggestions would be greatly appreciated.

 

Dim

doc AsDocument = Application.DocumentManager.MdiActiveDocument

   

Using docLoc AsDocumentLock = doc.LockDocument

   

Dim db AsDatabase = HostApplicationServices.WorkingDatabase

           

Using tr AsTransaction = db.TransactionManager.StartTransaction

               

Dim bt AsBlockTable = db.BlockTableId.GetObject(OpenMode.ForRead)

               

Dim blID AsObjectId = bt("TITLEBLOCK")

               

Dim blkRefTitle AsBlockReference = blID.GetObject(OpenMode.ForRead)

               

Dim myAttColl AsAttributeCollection = blkRefTitle.AttributeCollection

               

ForEach myAttRefId AsObjectIdIn myAttColl

                   

Dim myAttRef AsAttributeReference = myAttRefId.GetObject(OpenMode.ForWrite)

                   

If myAttRef.Tag = "DWGNUM"Then

                        strDwgNum = myAttRef.TextString

                        MsgBox(strDwgNum)

                   

EndIf

               

Next

                tr.Commit()

           

EndUsing

   

EndUsing

 

 

Valued Contributor
FFlix
Posts: 95
Registered: ‎11-15-2011
Message 2 of 3 (221 Views)

Re: Problem with editing attributes in a block

03-16-2012 07:22 AM in reply to: btmcad

hi

 

firstly, i think that although the block table does contain blocktablerecords and block definitions (and block attribute definitions), the starting point should be the former so that: 

"Dim blkRefTitle AsBlockReference = blID.GetObject(OpenMode.ForRead)" should read

Dim blkRefTitle AsBlockTableRecord = blID.GetObject(OpenMode.ForRead), i.e. access any specificly named blocktablerecord via its 'parent' blocktable.



secondly, a blocktablerecord may or may not then contain blockreferences (derived from the blockdefinition in the blocktable), so only when you find the respective blockreference in its 'parent' blocktablerecord you can access it as such. (i would think the same applies to block attributes: attdefs are kept in the bt and attrefs are appended to the blockref's attcol.)

 

this is new to me too, but hope this helps.

 

felix

 













*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 3 (209 Views)

Re: Problem with editing attributes in a block

03-16-2012 09:52 AM in reply to: btmcad

Your main mistake is you don't understand difference between BlockReference

and BlockTableRecord

See nice explanation about this in the post#2 by Norman Yuan from this thread:

http://forums.autodesk.com/t5/NET/Check-is-AttributeReference-instance-of-concrete/m-p/2709253/highl...

secondly, this line of your code:

 

strDwgNum = myAttRef.TextString



just read attribute value but do not change it

and also if you want to change just one item within the

For Each....Next statement, you have to use Exit For right after

you reach at desired item to avoid infinite or extafluous  loop, something like

 

For Each obj as AnyType in SomeCollection

If obj.SomeProperty=ExactValue Then

obj.OtherPrperty=OtherExactValue

Exit For

Next

 

hope that helps

 

~'J'~

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.