Community
AutoCAD Forum
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VBA to select block by name filter

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
Anonymous
6717 Views, 10 Replies

VBA to select block by name filter

Hi All,

 

I am looking for a VBA code to select the blocks in drawing based on Block name (Arrow).

Similar to autolisp command "(sssetfirst nil (ssget "_X" '((0 . "INSERT") (2 . "Arrow"))))"

 

Please share me if any code available.

 

Thank you..

 

Regards,

Harish Kumar M

10 REPLIES 10
Message 2 of 11
cadffm
in reply to: Anonymous

Use the lisp expression as stupid commandline-input (AutoCAD understand lisp expressions directly from the commandline),

use SENDCOMMAND to fire the input to the commandline // quick and dirty solution, because in VBA is nothing similar to ssget available.

 

Or you have to programm what you need, iterate thru the space(s) you want, check every item

for type, if blockreference check it for effectivename and if the effectivename match what you are

searching for, add the item to a selectionset.

 

Sorry.

Sebastian

Message 3 of 11
ed57gmc
in reply to: Anonymous

You can get a SelectionSet of block references using the code below. Call using GetSS_BlockName("Arrow")

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

Public Function GetSS_BlockName(BlockName As String) As AcadSelectionSet
    'creates a ss of blocks with the name supplied in the argument
    Dim s2 As AcadSelectionSet
    
    Set s2 = AddSelectionSet("ssBlocks")                ' create ss with a name
    s2.Clear                                        ' clear the set
    Dim intFtyp(3) As Integer                       ' setup for the filter
    Dim varFval(3) As Variant
    Dim varFilter1, varFilter2 As Variant
    intFtyp(0) = -4: varFval(0) = "<AND"
    intFtyp(1) = 0: varFval(1) = "INSERT"           ' get only blocks
    intFtyp(2) = 2: varFval(2) = BlockName          ' whose name is specified in argument
    intFtyp(3) = -4: varFval(3) = "AND>"
    varFilter1 = intFtyp: varFilter2 = varFval
    s2.Select acSelectionSetAll, , , varFilter1, varFilter2        ' do it
    Set GetSS_BlockName = s2

End Function

 

BTW, The AutoCAD VBA forum is over here.

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

Message 4 of 11
Anonymous
in reply to: cadffm

Hi,

 

Thank you for your reply..

I tried sending ssget by command line, but i am getting the error shown below.  Please suggest

Snap 2019-04-16 at 20.56.52.png

 

 

 

 

 

 

 

 

 

Thank you..

 

Harish

Message 5 of 11
ed57gmc
in reply to: Anonymous

The SendCommmand method takes an argument that evaluates to a single String argument. You've got multiple double quotes. You need to use the backslash to tell vba that they're literal characters. However, you won't be able to use SendCommand that way because it is asyncronous. The SendCommand method waits until all the vba is processed and then it sends the string to the command line AFTER the vba is through. Use the vba functions I gave you.

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

Message 6 of 11
Anonymous
in reply to: ed57gmc

Hi,

I need to send around series of 63 AutoCAD command via VBA to execute a single task (like AutoCAD script), in which there are requirement to select blocks by its name in between. So i am looking for VBA SEND COMMAND code to simulate "ssget".

Please suggest how to incorporate "(sssetfirst nil (ssget "_X" '((0 . "INSERT") (2 . "Arrow"))))" in VBA.

 

Thank you..

 

Regards,

Harish

Message 7 of 11
ed57gmc
in reply to: Anonymous

Why do you need to use SendCommand? Its not a very good way to do things. The code I sent you previously creates a selection set just like ssget does.

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

Message 8 of 11
Anonymous
in reply to: ed57gmc

Hi 

Please bare with me as I am new to Autolisp & VBA, just started developing myself on VBA..Smiley Frustrated

Thank you..

Message 9 of 11
ed57gmc
in reply to: Anonymous

Your script has some logic problems. The ssget will find all blocks called "arrow". Your script will only process the first one. Is that what you want?

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

Message 10 of 11
Anonymous
in reply to: ed57gmc

Exactly.. In Layout PSPACE I have only one block called "arrow", which needs to be selected and rotate at its position by give angle value. For which I am using "ssget" to select that block. Also I am not find any other way to select the block by name in AutoCAD command lines.

Please share me if you have any other idea to execute this..

 

Thank you..

Harish

Message 11 of 11
ed57gmc
in reply to: Anonymous

Using the functions I gave you earlier...

Dim ss As AcadSelectionSet
Set ss = GetSS_BlockName("Arrow")
If ss.Count > 0 Then
   Dim oBlock As AcadBlockReference
   Set oBlock = ss.Item(0)
   With oBlock
      .InsertionPoint = (23.0, 2.3, 0)
      .Rotation = 340
.Update End With End If

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report