SelectionSets.add("xx") not working for me

SelectionSets.add("xx") not working for me

Timothy_BrooksU4G6R
Participant Participant
1,700 Views
16 Replies
Message 1 of 17

SelectionSets.add("xx") not working for me

Timothy_BrooksU4G6R
Participant
Participant

I need a little help, I have used this code in Acad 2024 and it worked fine, now I am at a company that is still running 2020. I am trying to use same snippet of code and it is erroring out on me.

I can’t figure out what I am doing wrong. Hopefully someone here can point in the right direction.

 

Sub testing()
‘set vars
    Dim acadApp As Object
    Dim sset As AcadSelectionSet   

 ‘get Autocad  
   Set acadApp = GetObject(, "AutoCAD.Application")
‘get active drawing
   Set acadDoc = acadApp.ActiveDocument
‘just checking to see if I got drawing
               Debug.Print acadDoc.Name
‘delete any selection set              
If acadDoc.SelectionSets.Count > 0 Then
    Debug.Print Str(acadDoc.SelectionSets.Count)
        acadDoc.SelectionSets(0).Delete       
    End If
    ‘get new selection set
    Set sset = acadDoc.SelectionSets.Add("xx")
    ‘get  an error "run-time error ’13:’ Type mismatch"
   End Sub

 

Moderator edit: Please post your code in a code window.

0 Likes
Accepted solutions (1)
1,701 Views
16 Replies
Replies (16)
Message 2 of 17

Ed__Jobe
Mentor
Mentor

Use these functions instead. If you're using these functions outside of acad, then in AddSelectionSet method, change ThisDrawing to an acad Document object or set a new var called ThisDrawing.

Public Function GetAcad(Optional ver As String) As AcadApplication
    ' support multiple acad versions.
    'Sample ver for AutoCAD 2023 ' ".24.2"
    On Error Resume Next
    Dim acApp As AcadApplication
    Dim clsid As String
    clsid = "AutoCAD.Application"
    If Not ver = "" Then
        clsid = clsid & ver
    End If
    Set acApp = GetObject(, clsid)
    If acApp Is Nothing Then
        Set acApp = CreateObject(clsid)
    End If
    Set GetAcad = acApp
End Function


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 3 of 17

Timothy_BrooksU4G6R
Participant
Participant

thanks, the if statement did the trick and I got selection...

 If Err.Number <> 0 Then
        Set AddSelectionSet = ThisDrawing.SelectionSets.item(SetName)
        AddSelectionSet.Clear
    End If

 

...and of now that have selection, I can't select anything...this line of code broke for me too

<code> sset.Select acSelectionSetAll, 0, 0, 2, "Arion_Arch_B_L" </code>

 

sset.Select acSelectionSetAll, 0, 0, 2, "Arion_Arch_B_L"

 

...fun fun fun (sigh)

 

0 Likes
Message 4 of 17

Ed__Jobe
Mentor
Mentor

Change line 18 to

Set sset = AddSelectionSet("test")

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 17

Timothy_BrooksU4G6R
Participant
Participant

here is the current code...

Sub testing()

Dim acadApp As Object
Dim sset As AcadSelectionSet

Set acadApp = GetObject(, "AutoCAD.Application")
Set acadDoc = acadApp.ActiveDocument
If acadDoc Is Nothing Then
Set acadDoc = acadApp.Documents.Add
End If
Debug.Print acadDoc.Name

If acadDoc.SelectionSets.Count > 0 Then
Debug.Print Str(acadDoc.SelectionSets.Count)
acadDoc.SelectionSets(0).Delete

End If

On Error GoTo here
Set sset = acadDoc.SelectionSets.Add("test")

If Err.Number <> 0 Then
Set sset = acadDoc.SelectionSets.Item("test")
sset.Clear
End If
here:
Debug.Print acadDoc.SelectionSets.Count
Debug.Print acadDoc.SelectionSets(0).Name
'yeap the word "test" printed

Dim filterType As Variant
Dim filterData As Variant
Dim p1 As Double
Dim p2 As Double

p1 = 0
p2 = 0
'search by block name
filterType = 2
'block name to search for
filterData = "Arion_ARCH_B_L"


'try this 1st didn't work so try hard coding the info
'sset.Select acSelectionSetAll, 0, 0, filterType, filterData

sset.Select acSelectionSetAll, 0, 0, 2, "Arion_Arch_B_L"
'Run-time error '91:' object bariable or with block variable not set
Debug.Print sset.Item(0).GetAttributes.TagString.TextString


End Sub
0 Likes
Message 6 of 17

Timothy_BrooksU4G6R
Participant
Participant

and just curious, new here, how do make code look like code with line numbers?

0 Likes
Message 7 of 17

Ed__Jobe
Mentor
Mentor

@Timothy_BrooksU4G6R wrote:

and just curious, new here, how do make code look like code with line numbers?


See the comment that was made to your first post.

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 8 of 17

Ed__Jobe
Mentor
Mentor

@Timothy_BrooksU4G6R wrote:

here is the current code...


 You didn't use my functions I gave you.

 


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 GetAcad(Optional ver As String) As AcadApplication
    ' support multiple acad versions.
    'Sample ver for AutoCAD 2023 ' ".24.2"
    On Error Resume Next
    Dim acApp As AcadApplication
    Dim clsid As String
    clsid = "AutoCAD.Application"
    If Not ver = "" Then
        clsid = clsid & ver
    End If
    Set acApp = GetObject(, clsid)
    If acApp Is Nothing Then
        Set acApp = CreateObject(clsid)
    End If
    Set GetAcad = acApp
End Function


Sub testing()

    Dim acadApp As AcadApplication
    Dim acadDoc As AcadDocument
    Dim sset As AcadSelectionSet
    
    Set acadApp = GetAcad()
    Set acadDoc = acadApp.ActiveDocument
    
    Set sset = AddSelectionSet("test")
    'Dim filterType As Variant - must be an array
    'Dim filterData As Variant - must be an array
    Dim filterType(0) As Variant 
    Dim filterData(0) As Variant
    Dim p1(0 To 2) As Double
    Dim p2(0 To 2) As Double
    
    'p1 = 0 
    'p2 = 0
    'Proper setting of array, however this is unnecessary because
    'the default for a double is 0.
    p1(0) = 0; p1(1) = 0; p1(2) = 0
    'search by block name
    filterType(0) = 2
    'block name to search for
    filterData(0) = "Arion_ARCH_B_L"
    
    'sset.Select acSelectionSetAll, 0, 0, 2, "Arion_Arch_B_L"
    sset.Select acSelectionSetAll, pt1, pt2, FilterType, FilterData
    If TypeOf sset.item(0) Is AcadAttributeReference Then
    
      Debug.Print sset.item(0).GetAttributes.TagString.TextString
      
    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

0 Likes
Message 9 of 17

Timothy_BrooksU4G6R
Participant
Participant

 

Well basically, I use your code for the selectionset just moved to be within the sub instead of a function, and didn't need the GetAcad, my original code already had gotten cad.

But I tried it as function to and still got errors, my comment are the two ‘’ 

 


  Sub testing5()
    Dim sset As AcadSelectionSet
    Dim acad As AcadApplication
    
    Set acad = GetAcad
    Set sset = AddSelectionSet("test")
    
    ''thought the sset returned a selectionset array
    ''tried printing the count is giving me an error
''Run-time error ‘91’: Object variable or With Block variable not set
    Debug.Print Str(sset.Count)
    End Sub
    
    
    Public Function GetAcad(Optional ver As String) As AcadApplication
    ' support multiple acad versions.
    'Sample ver for AutoCAD 2023 ' ".24.2"
    On Error Resume Next
    Dim acApp As AcadApplication
    Dim clsid As String
    clsid = "AutoCAD.Application"
    If Not ver = "" Then
        clsid = clsid & ver
    End If
    Set acApp = GetObject(, clsid)
    If acApp Is Nothing Then
        Set acApp = CreateObject(clsid)
    End If
    Set GetAcad = acApp
    'had to add this a global var
    Set ThisDrawing = GetAcad.ActiveDocument
End Function
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
    ''added ThisDrawing as global var and set in GetAcad
    Set AddSelectionSet = ThisDrawing.SelectionSets.Add(SetName)
    If Err.Number <> 0 Then
        Set AddSelectionSet = ThisDrawing.SelectionSets.Item(SetName)
        AddSelectionSet.Clear
    End If
End Function

 oh and thanks for the heads up on how to post code. 

0 Likes
Message 10 of 17

Ed__Jobe
Mentor
Mentor

@Timothy_BrooksU4G6R wrote:

 

Well basically, I use your code for the selectionset just moved to be within the sub instead of a function, and didn't need the GetAcad, my original code already had gotten cad.

My functions use OnError to trap the errors. You don't want to do that inside your main function. It's best to encapsulate specific tasks, especially if you're doing error trapping. You want that error to stay in the function where the error is in order to make debugging easier. Otherwise and error could "bubble up" to a higher level function.

 

But I tried it as function to and still got errors, my comment are the two ‘’ 

 

AddSelectionSet only creates an AcadSelectionSet object. You still have to execute a SelectXXX method. On line 6, you need parenthesis after GetAcad() because it is on the right side of a set statement. This tells the compiler that it is a function and not a variable.

 

 

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 17

Timothy_BrooksU4G6R
Participant
Participant

Yes but your function return an AcadSelectionSet which should be an array of SelectionSet with the first indexed item named 'test'.

 

I posted this a few post back..

 

Debug.Print acadDoc.SelectionSets(0).Name
'yeap the word "test" printed

 on line 28 29

 

On the code where I posted that, I got pass creating the Selection, my code was then erroring on this line item, try to load the Selection by search for block name 'Arion_ARCH_B_L'

 

like I said most all of the was working in 2024 so don't why didn't in 2020 version

sset.Select acSelectionSetAll, 0, 0, 2, "Arion_Arch_B_L"

 

0 Likes
Message 12 of 17

Ed__Jobe
Mentor
Mentor

@Timothy_BrooksU4G6R wrote:

Yes but your function return an AcadSelectionSet which should be an array of SelectionSet with the first indexed item named 'test'.

No, The SelectionSets collection contains SelectionSet objects. When you create a new SelectionSet, it will be empty until you execute a Select method. Then it will contain objects that derive from AcadEntity, not SelectionSet objects.

 

I posted this a few post back..

 

Debug.Print acadDoc.SelectionSets(0).Name
'yeap the word "test" printed

 on line 28 29

In that line, you are getting the name of the SelectionSet at index 0 of the SelectionSets collection. If you ask for the Count property, it will be 0 because you haven't selected any objects yet.

 

On the code where I posted that, I got pass creating the Selection, my code was then erroring on this line item, try to load the Selection by search for block name 'Arion_ARCH_B_L'

 

like I said most all of the was working in 2024 so don't why didn't in 2020 version

sset.Select acSelectionSetAll, 0, 0, 2, "Arion_Arch_B_L"

That is the wrong syntax for passing a selection set filter. I didn't pay attention to it sooner. I was focused on your other problems. See the documentation for the Select method. There is a sample code. Points are 3D arrays. Dim pt(0 to 2) As Double. This will create an array with the defaults at each index, which is 0 for doubles. I edited the code in post 8.


 

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 17

Timothy_BrooksU4G6R
Participant
Participant

SOLVED!!!!

 

Man after days of messing with it, I found the issue. My orignal code worked just fine, I found a bug in the 2020 version of Autocad. 

 

Dim sset As AcadSelectionSet

 

...is not work right, don't ask me why, but all I had to do was change it to a Variant and everything worked.

0 Likes
Message 14 of 17

norman.yuan
Mentor
Mentor

Glad that you "solved" your code issue. However, if you want to keep working with AutoCAD VBA, you may need to know:

 

1. Regarding to

 

Dim sset As AutocadSelectionSet

 

It is not a bug with AutoCAD 2020, nor with ANY AutoCAD version. There is no such thing "AutoCADSelectionSet". It should be AcadSelectionSet.

 

2. While you can do

 

Dim sset As Variant

 

and this code works, but it is not NECESSARY (it is called late-binding: the code does not specify wat the data type for variable "sset" is, and relies on AutoCAD to figure out it is the type of AcadSelectionSet at runtime). Al least, doing this, you lose VBA editor's intellisense (i.e. while you type "sset.", VBA editor does not pops up dropdown tips to show available options for properties/methods, such as "Count"). And, if you click menu "Debug=> Compile XXXXX" to compile your code, VBA would not fond possible error of late-bound variables. for example, if you accidently do

 

Dim sset as Variant

...

sset.SetName "xxx"

 

where you accidently use a non-exist method name "SetName", the code would still compile, but would error out at runtime.

 

So, you DO NOT HAVE TO declare the variable for selection set as "Variant". Event making it Variable works, you should declare it as AcadSelectionSet, as best practice.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 15 of 17

Timothy_BrooksU4G6R
Participant
Participant
yeah, I meant to type 'AcadSelectionSet' not 'AutocadSelectionSet' ...but it was AcadSelectionSet that wasn't working till I changed it to a Variant
0 Likes
Message 16 of 17

Ed__Jobe
Mentor
Mentor
Accepted solution

@Timothy_BrooksU4G6R That tells me that there is a problem with your project references, since variant is a vba type and not an acad type. You must have a reference set to the wrong acad tlb. A dvb can only bind to one acad version at a time. For example, if you have a dvb for 2020 and then use that in 2025, then acad will remove the reference to the 2020 tlb and add a reference to the 2025 tlb and recompile the dvb. You will no longer be able to use the dvb in 2020.

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 17

Timothy_BrooksU4G6R
Participant
Participant

Thanks Ed, that was the right answer for some reason VBA default to 2001 libraries!

0 Likes