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

Problem with editing attributes in a block

2 REPLIES 2
Reply
Message 1 of 3
btmcad
534 Views, 2 Replies

Problem with editing attributes in a block

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

 

 

2 REPLIES 2
Message 2 of 3
adadnet
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

 

Message 3 of 3
Hallex
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

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