Select Blocks by its name

Select Blocks by its name

tramyer7mactavish
Contributor Contributor
3,784 Views
1 Reply
Message 1 of 2

Select Blocks by its name

tramyer7mactavish
Contributor
Contributor

Hi Gents,

 

I would like to know how do i code like.

 

How do i select a cad by its name (code that finds a block name then select it)

 

I tried to code but it won't work.

 

Please see below my code kindly correct it as per your liking 🙂 because i know my code lacks of many things XD

 

 

Sub Test()


Dim block As AcadBlockReference


For Each block In ThisDrawing.ModelSpace

If block.Name = "justblock" Then
block.Select

End If

 

than you and kind regards,

 

Tramyer

0 Likes
3,785 Views
1 Reply
Reply (1)
Message 2 of 2

norman.yuan
Mentor
Mentor

1. About looping through ModelSpace.

Not everything in ModelSpace is AcadBlockReference. You need to only test block name if the entity is a block reference

 

2. About block name.

Unless you are absolutely sure you only are interested in static block (i.e. the block reference could be dynamic block), you better to use "EffectiveName" property, instead of "Name" property. Also when comparing name with literal string, you want to make the comparison not case-sensitive.

 

3. About "Select"

 

What do you mean "Select". In AutoCAD standard operation, it means certain entities are added into a memory group for particular operation the follows. AutoCAD highlight them in certain way in order for user to visibaly know which entity is included in the momery group (SelectionSet).

 

Now that you are writing code to let AutoCAD do something, depending what you want to do, you can gather certain entities into a group (such as AcadSelectionSet), and do things with the selection set with or without highlighting these entities.

 

Let's assume here you only want to HIGHLIGHT block refernece with given name, so, you can simply call Highlight() method, pretending the block reference is "selected" visibly. If the code of finding blcok reference with given name is to do something right away, such as move the found blocks, re-scale them..., there is probably no need to highlight them. So you really need to know what "select" really means to your process.

 

Here is the code:

 

Dim ent as AcadEntity

Dim block As AcadBlockReference

For Each ent in ThisDrawing.ModelSpace

    If TypeOf ent Is AcadBlockReference Then

        Set block=ent

        If UCase(block.EffectiveName)="THE_BLOCK" Then

            block.Highlight true

        End If

    End If

Next

 

Well, if you highlighted some entities, you may want to unhighlight them at certain point of time (user can also execute "REGEN" command to get rid of the highlight).

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes