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.
print(s1.Name)
print(s1.Count)
O/p:
SS1
2
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.
print(ss1.Item(0).ObjectName)
O/p:
AcDbLine
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.
ss1.RemoveItems(aVariant([l2]))
print(ss1.Count)
O/p:
1
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.
ss1.SelectOnScreen()
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:
- Link: Extending the objects in AutoCAD using pyautocad in Python
- Link: Using Python lists and dictionaries to work with AutoCAD objects with pyautocad
- Link: Hatching objects on AutoCAD template using pywin32 in Python
- Link: Raster image object in AutoCAD with pyautocad in Python
- Link: Working with 3D mesh object in AutoCAD using pyautocad in Python
- Link: Creating adouble constructor using pywin32 in Python
- Link: Creating apoint method using pywin32 in Python
- Link: Python integration with AutoCAD using pywin32 and win32com
- Link: Deleting objects in a AutoCAD template with pyautocad and pywin32 in Python
- Link: Mirror object on a 2D plane with pyautocad in Python
- Link: Working with texts in Autocad using pyautocad in Python
- Link: Polar arrays in AutoCAD using pyautocad in Python
- Link: Rectangular arrays in AutoCAD using pyautocad in Python
- Link: Operations with AutoCAD objects using pyautocad in Python
- Link: Solid objects in AutoCAD using pyautocad in Python
- Link: Working with helices in AutoCAD using pyautocad in Python
- Link: Drawing splines in AutoCAD with pyautocad in Python
- Link: Polylines in pyautocad for drawing AutoCAD polygons in Python
- Link: Drawing ellipse arcs in AutoCAD using pyautocad in Python
- Link: Drawing arcs in AutoCAD using pyautocad in Python
- Link: Near simultaneous factory design and process optimization with Promodel AutoCAD edition
- Link: Python for AutoCAD pyautocad module
- Link: Region object in AutoCAD with Python
- Link: AutoCAD Application object class in Python
- Link: AutoCAD Document object in Python
- Link: AutoCAD Block object in Python
- Link: AutoCAD Attribute object in Python
- Link: DimAligned object in AutoCAD using Python

Civil engineer interested in automation in core subjects such as civil, mechanical and electrical, using IT skills comprising cloud computing, devops, programming languages and databases along with the technical skills gained while working as a civil engineer since past 3 years.
3 comments
Hello,
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?
Email:
nguyentaduong1997@gmail.com
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)
ss1.SelectOnScreen()
#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
#print(ss1.Name)
print(ss1)
for i in range(0,ss1.Count):
if ss1.Item(i).ObjectName==”AcDbCircle”:
ss1.RemoveItems(aVariant([i]))
print(ss1.Item(i).ObjectName)
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!