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.
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.
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:
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.
Leave a Reply