As mentioned by me in my previous blog post I will keeps discussing workflows and operations that can be implemented with the pywin32 module in Python. In this post I will show how you can delete AutoCAD objects with pywin32 and pyautocad in Python. I have also written various other posts covering both pyautocad and pywin32. E.g. I have covered topics such as: How to handle solid objects with pyautocad, how to implement AutoCAD operations with pyautocad, how to integrate pywin32 with AutoCAD, as well as workflow-related content such as e.g. Python data structures for AutoCAD object organization.
Deleting objects using pyautocad
Setting up environment for pyautocad
As usual, we will set up our environment for integrating python with AutoCAD.
from pyautocad import Autocad acad = Autocad(create_if_not_exists=True)
Using delete method provided by pyautocad
To delete objects, I have created some objects on our AutoCAD template as shown in Figure 1 below:
Now we will use a very simple utility of pyautocad called iter_objects(), to loop through every object from the template.
for object in acad.iter.objects() object.Delete()
On implementing this command, all the objects will be deleted.
Filtering out objects for deletion using pyautocad
Sometimes we need to filter our objects before deleting i.e. suppose if we want to delete only the circle from the template as shown in Figure 1, then we can use the following methodology.
for object in acad.iter_objects(object_name_or_list="AcDbCircle"): object.Delete()
Deleting objects using pywin32 (win32com.client)
Setting up environment for pywin32
As we have discussed initiating an AutoCAD template using pywin32 in our previous blog, we will set up our work environment to integrate python and AutoCAD.
import win32com.client acad = win32com.client.Dispatch("AutoCAD.Application") acad.Visible = True acadModel = acad.ActiveDocument.ModelSpace
We will be using the same figures as given in Figure 1.
Deleting objects from AutoCAD template using pywin32
While using pywin32, we have a very direct approach to code for iterating through objects from the AutoCAD template.
for object in acadModel: object.Delete()
Considering the difference between the usage of pyautocad and pywin32 while deleting objects, we found pywin32 to be more efficient.
Filtering objects for deletion while working with pywin32
Filtering out objects while using pywin32 takes just an “if statement”. Let’s look at the same.
for object in acadModel: if object.ObjectName == "AcDbCircle": object.Delete()
For more information, do visit Autodesk documentation as well as the Windows object model documentation.
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.
11 thoughts on “pyautocad/pywin32 AutoCAD object deletion”
Hi! I have been having trouble with adjusting attributes of a block and I was told that using iter_objects could be useful for this, but I am not sure how to make it work in regards to a block’s text attribute.
My code is as follows:
point = APoint(10, 10, 10)
block1 = acadModel.InsertBlock(point, ‘test1’, 1, 1, 1, 0)
atts = block1.GetAttributes
atts.TextString = “new”
test1 is the pre-defined block in my AutoCAD file.
From what I have found from reading VBA commands, I believe that I am supposed to define the TextString attached to the “atts” variable. However, I have not figured out what to do to make the text attribute update to the new string.
Thank you for the help!
Hello Mr. Oliver,
Thanks for your question. I think, using AddText method for assigning text could be more easier than this. In that case you can just use
block1 = acad.model.AddText(“string”, insertion point, text height)
And just by using
block1.TextString = “new string”
You can change the text to updated one.
Also, if you are not able to access the string directly, you can use iter_objects as:
for object in acad.model.iter_objects(“Text”):
if object.TextString == “old string”:
object.TextString = “new string”
Hope this works for you.
Have a great day!
I want to delete the drawing between two coordinates on a rectangle to introduce spline. I need your help to achieve this, can you please help.
Hi Mr. Madhan,
Thanks for your question. In such a case what I would do is, I would iterate through every object and check if any of those intersects the rectangle object using the “IntersectWith” method against that rectangle. Once the object satisfies the condition, I would delete the object using the “Delete” method against that object.
Have a nice day!
You can also check our latest blog on intersecting objects.
i have a question how to delete a layer list in python like
lay=[‘t1’, ‘t2’, ‘t3]
for i in range(len(lay)):
## after i created how to delete list of layer such as “lay” variable
for l in lay: l.Delete()
In AutoLISP select the object to use (ssget function and use filter function)
i don’t know python. how to use like AutoLISP.kinldy clarify my question with an example.
I want to delete all the object in autcad space. like clear all the area before another run.
from pyautocad import Autocad
acad = Autocad(create_if_not_exists=True)
Im trying to iterate through all Block Definition objects using
for block in acad.ActiveDocument.Blocks:
But im getting an error pywintypes.com_error: (-2147352567, ‘Exception occurred.’, (0, ‘AutoCAD.Application’, ‘Cannot be erased by caller’,
Can you help?