Using the objects in SelectionSet

Using the objects in SelectionSet

sunchuanpu
Enthusiast Enthusiast
2,152 Views
3 Replies
Message 1 of 4

Using the objects in SelectionSet

sunchuanpu
Enthusiast
Enthusiast

Hello,

I apologize if this is a noob question. I have been looking for this on the internet for a while but what I found was all about creating, adding items, etc., none of them gave any examples over my problem, so I decided to ask here.

 

When I have a SelectionSet containing Objects, how do I use those Objects? For instance, if I have a SelectionSet which already contains multiple lines, is it possible to use things like "line.Endpoint" to get information of specific line, or pass a specific line as "AcadLine" into a Function?

 

Thank you very much.

0 Likes
Accepted solutions (1)
2,153 Views
3 Replies
Replies (3)
Message 2 of 4

grobnik
Collaborator
Collaborator
Accepted solution

@sunchuanpu 

Hi, I'm not a professional programmer of VBA, all what I know I learned myself looking and participating to forums like this.

I'll try to explain how selectionset works with some code, and side explanation.

First of all you have to keep in mind how the selection set is searching the objects, it's based upon the same code used when you export a dwg in dxf format, there are two arrays which are determining the type of object or sometime logical function, or directly a string.

Here a link to help web page where you can find the code http://help.autodesk.com/view/OARX/2018/ENU/?guid=GUID-2553CF98-44F6-4828-82DD-FE3BC7448113

Here an example of code:

 

 

 

Sub selectLine()
   Dim sset As AcadSelectionSet
   Set sset = ThisDrawing.SelectionSets.Add("TestSet2")
   Dim filterType As Variant
   Dim filterData As Variant
   Dim p1(0 To 2) As Double
   Dim p2(0 To 2) As Double
  
   Dim grpCode(0) As Integer
   grpCode(0) = 2
   filterType = grpCode
   Dim grpValue(0) As Variant
   grpValue(0) = "Line"
   filterData = grpValue
   sset.Select acSelectionSetAll, p1, p2, filterType, filterData
   Debug.Print "Entities: " & str(sset.count)
   
For Each ObjectLine in sset
    If type of ObjectLine is AcadLine then
       Debug.print ObjectLine.Endpoint(0), ObjectLine.Endpoint(1)
    End if
Next
sset.Delete
End Sub

 

 

 

All Objects found by Selection Set will be stored in sset variable, and inside Item(x) you can find each object with own properties. Use debug to see all properties, count, and so on.

Above I have inserted P1, and P2 that could be used if a selection window shall be used, on the opposite you can use ", ," instead P1 and P2.

Above code shall be tested, I can't, because actually I'm working with another machine without Autocad installed.

I'll check within today, however it should work fine.

Regards

0 Likes
Message 3 of 4

sunchuanpu
Enthusiast
Enthusiast

Thank you so much for your code and explanation! I didn't actually test your code, but by looking at it, now I understand how to use the SelectionSet.

0 Likes
Message 4 of 4

grobnik
Collaborator
Collaborator

Hi @sunchuanpu Thank you,

just a note about the two arrays filter variables:

into above code I have indicated just 1 row in the arrays, but you can insert logical function like "AND", "OR",  or for example block names "Block1,Block2", increasing both the array size, each group code shall be followed by a group value. The only issue concern the error, because, based upon my experience, if you made a mistake selecting the proper group code and/or value the selection set return you no errors but only count 0 (zero). So I suggest to check every time the selection set count that shall be " SelectionSet Name.count >0 ".

Regards.

0 Likes