Select all block follow insert field text

Select all block follow insert field text

magicdl96
Advocate Advocate
9,075 Views
23 Replies
Message 1 of 24

Select all block follow insert field text

magicdl96
Advocate
Advocate

hi all,

I have a block and I use insert field in it. I want to select all blocks with insert field as B1C3. 

I have the following code but I don't know how to choose them.

Sub SelectBlocksField()
Dim atts As Variant
Dim AcadObj As AcadObject, BlockRef As AcadBlockReference
Dim att As AcadAttributeReference
Dim ssetObj As AcadSelectionSet

For Each AcadObj In ThisDrawing.ModelSpace
    If TypeOf AcadObj Is AcadBlockReference Then
   atts = AcadObj.GetAttributes()
   For i = 0 To UBound(atts)
        Set att = atts(i)
        If InStr(1, att.TextString, "B1C3") Then
                'Select what ??
        End If
    Next
    End If
Next AcadObj
End Sub

magicdl96_0-1657190951402.png

 

0 Likes
9,076 Views
23 Replies
Replies (23)
Message 2 of 24

Ed__Jobe
Mentor
Mentor

I changed your code to do what you want.

Sub SelectBlocksField()
Dim atts As Variant
Dim oEnt As AcadEntity, BlockRef As AcadBlockReference
Dim att As AcadAttributeReference
Dim ssetObj As AcadSelectionSet

For Each oEnt In ThisDrawing.ModelSpace
    If TypeOf oEnt Is AcadBlockReference Then
        Set BlockRef = oEnt
   atts = BlockRef.GetAttributes()
   For i = 0 To UBound(atts)
        Set att = atts(i)
        If InStr(1, att.TextString, "B1C3") Then
                'Select what ??
                'BlockRef is already selected, use it
                BlockRef.Explode 'bang
        End If
    Next
    End If
Next AcadObj
End Sub

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 24

magicdl96
Advocate
Advocate
hi,
I want to select it. Not Explode it.
0 Likes
Message 4 of 24

Ed__Jobe
Mentor
Mentor

That was a silly example. You missed the point. I don't have to select it. It represents the current block reference being worked on as you iterate through model space. The idea is that after I made the changes to your code, I could just use the BlockRef object.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 5 of 24

magicdl96
Advocate
Advocate
I don't know what I'm missing.. I'm pretty bad at that. Is there any VBA code to help me select those objects.
0 Likes
Message 6 of 24

Ed__Jobe
Mentor
Mentor

The code I gave you does select them. You just have to replace block.explode with the code you want. What do you want to do with it after it's selected?

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 7 of 24

norman.yuan
Mentor
Mentor

@magicdl96 

When you said "...I want to select all blocks..." and later said "...any VBA code to help me select those objects...", the meaning of the word "select" could really be multiple. In AutoCAD programming, it usually means to identify one or more entities by code, as @Ed__Jobe 's code show. That is, as long as the code finds the target entity and is about to something with it, that means the entity is selected. But for the CAD user, it usually means entity or entities are visually "selected" (highlighted with grips available). This kind of selection is meant for user to be aware some entities are selected and are waiting some action(s) upon them (that is, if some is to happen, the code behind the actions knows which entities are "selected", regardless user sees it or not).

 

As you can see, if you write code to do something to targeted entities, ones the code identifies the target(s), in most cases, your code will continue to work on the "selected" target, there is no need to "visually show the selection".

 

So, you may want to explain in detail what exactly "select" means in your context (probably, also why).

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 8 of 24

magicdl96
Advocate
Advocate
Thank you for telling me I understand so I can better express what I want.

what i want here is to select all block whose insert field is B1C3 (highlight it so it can be moved, copied or checked position in drawing)

similar to how we use the left mouse button to select objects.
0 Likes
Message 9 of 24

Ed__Jobe
Mentor
Mentor

Below is some updated code that is heavily modified from your original code. Note, You had several errors, so you should always have Option Explicit at the top of your module to prevent misnamed and undimensioned variables. You can set this in your VBA Options>Editor tab>Require Variable Declaration.

 

 

 


Sub SelectBlocksField()
    Dim oEnt As AcadEntity
    Dim BlockRef As AcadBlockReference
    Dim varAtts As Variant
    Dim varSelection() As AcadEntity
    Dim oAtt As AcadAttributeReference
    Dim ss2 As AcadSelectionSet
    Dim i As Integer
    Dim j As Integer
    Dim bln As Boolean
    Dim str1 As String
    
    bln = False
    
    Set ss2 = AddSelectionSet("Atts")
    str1 = ThisDrawing.Utility.GetString(0, "Enter attribute text to search for: ")
    For Each oEnt In ThisDrawing.ModelSpace
        If TypeOf oEnt Is AcadBlockReference Then
            Set BlockRef = oEnt
            varAtts = BlockRef.GetAttributes()
            For i = 0 To UBound(varAtts)
                 Set oAtt = varAtts(i)
                 If InStr(1, oAtt.TextString, str1) Then
                    'Select what ??
                    'BlockRef is already selected, use it
                    Debug.Print oAtt.TextString
                    bln = True
                 End If
             Next
             If bln Then
                ReDim Preserve varSelection(j)
                Set varSelection(j) = BlockRef
                ss2.AddItems varSelection
                j = j + 1
             End If
        End If
        bln = False
        j = 0
    Next
    ss2.Highlight True
    ss2.Update
    
End Sub


Public Function AddSelectionSet(SetName As String) As AcadSelectionSet
' This routine does the error trapping neccessary for when you want to create a
' selectin set. It takes the proposed name and either adds it to the selectionsets
' collection or sets it.
    On Error Resume Next
    Set AddSelectionSet = ThisDrawing.SelectionSets.Add(SetName)
    If Err.Number <> 0 Then
        Set AddSelectionSet = ThisDrawing.SelectionSets.Item(SetName)
        AddSelectionSet.Clear
    End If
End Function

 

 

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 10 of 24

Ed__Jobe
Mentor
Mentor

@magicdl96 Did my code answer your question? If so, please mark it as the solution.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 11 of 24

magicdl96
Advocate
Advocate
Cannot work.
pls help me check this file.

https://drive.google.com/file/d/1bbg5Zb90idgAuJUevVX5fnmMDclN-GxX/view?usp=sharing

Thank you very much.
0 Likes
Message 12 of 24

Ed__Jobe
Mentor
Mentor

Sorry, but can you post your code into a code window using the </> button or post the dvb? Also, you have to do better than say it can't work. Describe what is happening when you run the code. My code was tested. If you changed it, show what you changed and describe any errors.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 13 of 24

Ed__Jobe
Mentor
Mentor

@magicdl96 I made some changes to the code in message 9. I added a prompt for the text to search and removed the SELECT command. The code finds the text just fine. Now you have to add code to do something with the found text/attribute. You never really said what you want to do with it.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 14 of 24

magicdl96
Advocate
Advocate

sorry I reply long time.

as i said: what i want here is to select all block whose insert field is B1C3 (highlight it so it can be moved, copied or checked position in drawing)

My purpose is to select those blocks to check their location in drawing is correct or not.

Autodesk.png

0 Likes
Message 15 of 24

magicdl96
Advocate
Advocate

Autodesk.png

0 Likes
Message 16 of 24

Ed__Jobe
Mentor
Mentor

VBA doesn't have a way to pass the selection set to the command line, but you could add

ThisDrawing.SendCommand "SELECT P"

to the end of the code I provided.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 17 of 24

magicdl96
Advocate
Advocate

 Can you help me convert this VBA code to lisp?

 

ThisDrawing.SendCommand "SELECT P" not work.

0 Likes
Message 18 of 24

Ed__Jobe
Mentor
Mentor

I'm afraid I don't have time. Got a lot of work to do before AU. But I edited the code above. If I had more time, I could probably improve on the logic.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 19 of 24

seabrahenrique
Advocate
Advocate

Is there a way to do selection with VBA code using selection by a "FENCE" mode..

 

Try to do this: (only for test)

 

1) Draw a circle in a 0,0,0 coord with 1 fo radius;

2) Run this code: ThisDrawing.SendCommand "select" & vbCr & "F" & vbCr & "0,0" & vbCr & "2,0" & vbCr & vbCr

 

As u can see, the code can select objects with "FENCE" (select f) inside the points "0,0" and "2,0"....

 

If u have blocks in your drawing, and u have the BlockRef.InsertionPoint for each one... U can made something like the code above to select the blocks by fence using coords inside then...

 

I don't know.. it's only one idea...

 

 

0 Likes
Message 20 of 24

magicdl96
Advocate
Advocate

magicdl96_0-1663896749598.png

I try but not work. 

And ThisDrawing.SendCommand "select" & vbCr & "F" & vbCr & "0,0" & vbCr & "2,0" & vbCr & vbCr still not work.

 

and it occurred to me that if you transfer this code to another machine you have to install VBA. 😑

 

0 Likes