Get Block references by Name

Get Block references by Name

Anonymous
Not applicable
9,493 Views
14 Replies
Message 1 of 15

Get Block references by Name

Anonymous
Not applicable

This may seem like a duplicate post, but I have read the forums and run across a few posts - one of which being http://forums.autodesk.com/t5/NET/C-find-Block-References-by-Name/m-p/1672857/highlight/true#M3723 where Tony mentions using GetBlockReferenceIds.

 

I also did a few google searches and found some vague references as well. My goal is to find all instances of an inserted block with a certain name. I would prefer to not iterate through the entire block table as this seems expensive as far as resources. However, in some of the google hits I found people mentioned that there are some pitfalls to using GetBlockReferenceIds because it can return erased blocks.

 

Is there a best practice on this?

 

Thanks in advance.

0 Likes
9,494 Views
14 Replies
Replies (14)
Message 2 of 15

Anonymous
Not applicable

Just set up a selectionset with the blockname then loop true the Sset, you can now do whatever you want, change layer, color,...

 

If you want i can provide you the code in VB.NET

0 Likes
Message 3 of 15

Anonymous
Not applicable

I've seen some partial examples of using selection sets, so yes I would like to see your solution for this in vb.net. Are there any known issues with using this method?

0 Likes
Message 4 of 15

Anonymous
Not applicable

I think it would be six of one, half dozen of another.  SSget will step through all entities in the drawing, and compare it to the filter list.  So it might not matter which way you go, as you will have to step through something.  I usually go through the block table, and use GetBlockReferenceIds on the record I want.

0 Likes
Message 5 of 15

Anonymous
Not applicable

Have you had any issues with entities that were "erased" as some of the google hits warned me of, or are those just edge cases that probably wont concern the common coder?

0 Likes
Message 6 of 15

Anonymous
Not applicable

They are there, the erased ones that is, but it is an easy check.  As you step through the objects, just check the IsErased property, and skip the ones that are true.

0 Likes
Message 7 of 15

chiefbraincloud
Collaborator
Collaborator

Since you have the name, you don't have to step through the block table at all.

 

The Item Property (default) of the Block Table will return the ObjectID of the BlockTableRecord, and you can use the Has method to make sure that it exists.

 

Dim BT as BlockTable = trans.getobject(db.BlockTableID, openmode.forread)

 

if BT.Has (Blockname) then

       dim btr as BlockTableRecord = trans.getobject(BT(Blockname), openmode.forread)

       dim btrIDs as objectidcollection = btr.getblockreferenceids(True, False) 

       for each id as objectid in btrids

              if not id.iseffectivelyerased then

                      'Do whatever

              end if

       next

End If

Dave O.                                                                  Sig-Logos32.png
Message 8 of 15

Anonymous
Not applicable

I'm pretty sure the Has method steps through the block table, and I remember that Has might return erased records, so you might want to make sure that the record isn't an erased one, and if it is, then step through the table with your own function to retrieve the non-erased record, if there is one.

 

I know Tony T. has posted a routine like this before.

0 Likes
Message 9 of 15

Anonymous
Not applicable

Hey T.willey,

 

Is this what you were refering to?

 

http://www.theswamp.org/index.php?topic=12123.msg150999#msg150999

0 Likes
Message 10 of 15

Anonymous
Not applicable

Yup.  I would have thought he would have posted it on this site also, but maybe not.

Message 11 of 15

chiefbraincloud
Collaborator
Collaborator

Just to be clear, Yes I am aware that the Has method will ultimately loop through the block table in underlying IL code, I just meant that it saves me from having to do it in my own code.

 

For what it is worth, I have a bunch of places in my code that use GetBlockReferenceIds, and even more places that use BlkTbl.Has, but I have only had erased objects present a problem in one function I can remember.

 

After the discussion here, I will likely be going back through my code and determining if I have just been lucky, or if I have just enough defensive code to prevent the problem.

Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 12 of 15

BKSpurgeon
Collaborator
Collaborator

So as I understand it btr.GetReferenceIDs(bool, bool) can return block reference object Ids which are deleted from the model/paper space?

 

if so that is very disturbing, because I didn't know about it, nor did i know one has to check for it.

 

thanks all for the clarification.

 

 

rgds

 

Bk

0 Likes
Message 13 of 15

kerry_w_brown
Advisor
Advisor

 

For those interested, we've stated a list of items in the AutoCAD API documentation needing addressing.

 

These items ( if deemed worthy) will be rectified/added in the documentation.

 

Please feel free to participate, we won't get this chance often.

 

http://www.theswamp.org/index.php?topic=50501.0

 

Reply #15 addressed the IsErased issue, and it's lack of prominent documentation.

 

Regards,

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 14 of 15

Anonymous
Not applicable

Greetings! I faced with same issue but ur code helped a lot. But I have one more question. I have needed block with needed blockname which contains number of points. Is  there any way to get coordinates of every point in my block?

0 Likes
Message 15 of 15

ActivistInvestor
Mentor
Mentor

Deleted reply to old post (no link to last post in thread is to blame for this).

0 Likes