SelectionSet object in AutoCAD with Python

In this article I will discuss the SelectionSet object in AutoCAD and methods to automatize operations for the same using Python. Along with that I will also add a small introduction coverign the Group object.

Preparing the code for this AutoCAD example

For this coding example I am using the pythoncom and win32com modules. These modules initiate my application and perform various different tasks.

import win32comu.client
import pythoncom

acad = win32com.client.Dispatch("AutoCAD.Application")
doc = acad.ActiveDocument
acadModel = doc.ModelSpace

def APoint(x, y, z = 0):
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z))

def aDouble(xyz):
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (xyz))

def aVariant(vObject):
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, (vObject))

The SelectionSet AutoCAD object

First, let me explain the SelectionSet object in AutoCAD. A SelectionSet object simply refers to the set of items that I select in AutoCAD. While using AutoCAD I usually select items by creating a window around objects that I want to select from the drawing. In that case I usually do so by using the mouse. Different objects require SelectionSet as a reference object to perform different activities. Lets understand how to create SelectionSet object in Python.

Creating a SelectionSet AutoCAD object in Python

In this coding example, I will try to create two lines using the AddLine method. I add these two lines to a SelectionSet object.

l1 = acadModel.AddLine(APoint(0, 0, 0),APoint(1000, 1000, 0))
l2 = acadModel.AddLine(APoint(1000, 1000, 0),APoint(2000, 0, 0))

Now, I can create a SelectionSet object with the name “SS1” using the Add method. I use that method on the SelectionSets object. This SelectionSets object is a collection of all SelectionSet objects present inside the Document object.

To know more about the Document object and other AutoCAD objects, do check out my other Blog posts.

ss1 = doc.SelectionSets.Add("SS1")

The SelectionSet object takes an array of objects as input parameter. Those are the objects that are to be added to the SelectionSet. To do so, I use the AddItems method on the SelectionSet object to pass the created lines as input values. See below code.

ss1.AddItems(aVariant([l1, l2]))

To confirm whether the SelectionSet is created and whether the two objects are present in the SelectionSet or not I can use two methods: Name and Count.




Methods of the SelectionSet AutoCAD object

There are also a few other methods that can be used to perform various different tasks on a SelectionSet object. For instance, Clear, Delete, Erase, Highlight, Item, RemoveItems, SelectOnScreen.

From the method mentioned above, the Item method can be used to fetch the Item present at a given index in a Collection, Group or SelectionSet.




To delete SelectionSet or items from SelectionSet I can use the Clear, Delete, Erase or Remove Items method. The functionality of these methods is as mentioned below:

  • Clear: This method empties the SelectionSet. The SelectionSet object will still exist, but will not contain any items. The items that previously resided in the selection still exist, but they no longer reside in the SelectionSet.
  • RemoveItems: This method helps to remove one or more items from a SelectionSet. The removed items still exists, but they no longer reside in the SelectionSet.
  • Erase: This method deletes all items in a SelectionSet. The SelectionSet object still exists, but will not contain any items. The items that previously resided in the SelectionSet no longer exist.
  • Delete: Deletes a SelectionSet object, but not the objects in the SelectionSet. While the SelectionSet itself will not exist after the call to the Delete method, the items previously contained by the SelectionSet will still exist.

From the methods listed above only RemoveItems method requires parameters to be passed. For example, from the lines that I have added to SelectionSet I want to remove Line2 (l2). I must pass l2 as an element of the array of the Items list that I want to remove from the SelectionSet.




Sometimes I want to select objects using the conventional method only. I.e. dragging/drawing a window around the objects that I want to select using my mouse. Python also allows me to do so using the SelectOnScreen method.


Once I apply the method as shown above, the code waits for AutoCAD user to select objects. Upon selecting the objects the AutoCAD user needs to press Enter on the keyboard. This will finalize the selection. The code then eventually moves ahead.

Group object in AutoCAD using Python

The Group object is a named SelectionSet object. Since is does not contain any special methods I have added this small introductory section for the purpose of discussing the Group object.

I can add the Group object similarly to how I added the SelectionSet object. I.e. by using the Add method. Obviously, here I need to use the Groups collection object to Add a Group.

Also, to add items to the created Group object I can use the AppendItems method. I can perform the task similarly to how I did with the AddItems method for the SelectionSet object.

In addition to that here follows a list of various different methods and properties that can be used to perform activities on AutoCAD Group objects:

  • Delete
  • GetExtensionDictionary
  • Highlight
  • Item
  • RemoveItems
  • Update
  • Count
  • Document
  • Handle
  • HasExtensionDictionary
  • Layer
  • Linetype
  • LinetypeScale
  • Lineweight
  • Material
  • Name
  • ObjectID
  • ObjectName
  • OwnerID
  • PlotStyleName
  • TrueColor
  • Visible

Concluding remarks

Using the SelectionSet object I can perform different activities on other AutoCAD objects. To learn more about automatizing different tasks using Python in AutoCAD, do check out my other blog posts. In case of any doubts or inqueries please always feel free to use the comment section below to ask any questions that you might have. Also, for any kind of technical consultation, please contact me through the SCDA contact form on this blog.

References to related content

I have already established a rather comprehensive documentation on pyautocad, AutoCAD, pythoncom, and pywin32. So please see a list of some related content below:

You May Also Like

Leave a Reply


M Ahmad says:

I am looking to duplicate a set of shapes 100 pixels to the right. Can I do that using a selectionset object? I cannot find the Move method.
Using move method on individual shapes is prolly not the best way, because Move takes 2 arguments( exisiting center, New center),
So for circle shape i can do this .Move(circle.Center, APoint(newCenterX, newCenterY))
But I cannot do the same for PolyLine as it dont have a center, also other different kinds of shapes. for example, Center for circle, InsertionPoint for Text, StartPoint for Line

Kindly suggest what method to use to duplicate and move a set of shapes, if possible using a displacement metric?

Duong Nguyen Ta says:

Can you fix this code for me?
# importing pyautocad
import pyautocad
import pythoncom
import win32com.client
# creating AutoCAD instance
acad = pyautocad.Autocad()
doc = acad.ActiveDocument
# def APoint(x, y, z = 0):
# return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z))

# def aDouble(xyz):
# return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (xyz))

def aVariant(vObject):
return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, (vObject))
# Kiểm tra xem có bản vẽ nào đang mở hay không
# Kiểu đối tượng bạn muốn chọn
object_type = “AcDbLine”

# Tạo tên cho SelectionSet
ss_name = “SS” + str(doc.SelectionSets.Count + 1)
ss1 = doc.SelectionSets.Add(ss_name)
#Lặp qua tất cả đối tượng trên bản vẽ và thêm những đối tượng kiểu LINE vào SelectionSet
for i in range(0,ss1.Count):
if ss1.Item(i).ObjectName==”AcDbCircle”:

YI WEI says:

Hello, respected Tanmay Sawant
Your article has been very helpful to me, but I still have some questions to ask you.
Please allow me to introduce myself first. I am Chinese and not good at English. This is a comment I wrote using translation software, so if there is any offense, it is not my intention. I am very grateful to you and your article.
The problem is as follows:
I have now built the selection set and added the content I want to it, but how can I make the objects in the selection set selected in AutoCAD? Just like how we click or box select in AutoCAD, we first select one or more objects and then perform operations such as moving, copying, and rotating.
For some reasons, it is very difficult for me to come to your website.
But whether you answer me or not, thank you very much!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.