XY Axis – Selection Sets – Part 2

As we draw the lines for the XY Axis we are going to add them to a selection set, then use the selection set to make a block.

The programmer can make any number of Selection Sets and name them anything he likes. They are actual objects in the drawing but they are transient and not saved anywhere when the drawing is closed. The collection of selection sets only has two methods, Add and Item, and one property Count. Add and Item both return a selection set that is then set to an object variable. The Item method can use the name or the index, which is zero based.

dim s_set as AcadSelectionSet
Set s_set= acadDoc.SelectionSets.Add("newname")
or
Set s_set= acadDoc.SelectionSets.Item("existingname")
or
Set s_set = s_sets.Item(i) 'where i is a valid index integer

Sub sets_count_and_name()
Call connect_acad
Dim s_set As AcadSelectionSet
Dim s_sets As AcadSelectionSets
Set s_sets = acadDoc.SelectionSets

On Error Resume Next
Set s_set = s_sets.Add("yip")
Set s_set = s_sets.Add("yap")
On Error GoTo 0

Debug.Print s_sets.Count

For Each s_set In s_sets
Debug.Print s_set.Name
Next
End Sub

s_set.clear empties the ss but does not affect the entities.
s_set.delete deletes the selection set itself in the collection of sets.
s_set.erase erases the entities in the set from the drawing.

A For Each loop is not a good place to delete all sets. Deleting the loop control will give an error. A Do loop with the counter counting down works.


Sub sets_delete()
Call connect_acad
Dim s_sets As AcadSelectionSets
Set s_sets = acadDoc.SelectionSets

Dim i As Integer
i = s_sets.Count

Do While i > 0
i = i - 1 'because ss index is zero based
s_sets.Item(i).Delete 'delete is a method of the Set, Item(i) returns a set
Loop
End Sub

The selection set object has a large number of methods to select objects, erase them from the drawing, and delete itself. Here is a drill into the autocad help for this topic. Hopefully these links wont be made obsolete overnight. Good as of 9-27-2015

Autodesk Developer network for Autocad

Selection Sets Collection – Active X 2016

Selection Set Object – Active X 2016

make note – The ActiveX API documentation also be found locally at:

  • %ProgramFiles%\Common Files\Autodesk Shared\acad_aag.chm
  • %ProgramFiles%\Common Files\Autodesk Shared\acadauto.chm

I find the local files much easier to navigate. I copy them where i can get to them quickly. The online version you have to remember to change “User” to “Developer” before you Search or you get nothing.

If you think help is only helpful when you don’t need it, here is a good general autocad vba reference. The hard cover 2006 version is the latest and a real bargain if you can get it, although all of them are still good.

http://www.abebooks.com/book-search/author/joe-sutphin/

Selection sets are not saved when the drawing is closed. If a selection set does not exist and you try to reference it you get an error. If a selection set does exist and you try to add it you get an error. We handle that by expecting errors and turning off error reporting temporarily during the creation of the set.

‘pseudo_code
‘on error resume next
‘set s_set = s_sets.item(“name”)
‘generate a quiet error if does not exist
‘s_set.delete
‘eliminate if does exist
‘generate a quiet error if does not exist
‘we are now sure there is no s_sets.item(“name”)
‘set s_set= s_sets.add(“name”)
‘just about any name is valid, even normally taboo / \ “ : ; ? * | = ‘

Sub add_ss(strname As String)
'adds a new empty named selection set
Dim s_set As AcadSelectionSet

On Error Resume Next
Set s_set = acadDoc.SelectionSets.Item(strname)
s_set.Clear
s_set.Delete
On Error GoTo 0

Set s_set = acadDoc.SelectionSets.Add(strname)
End Sub
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s