Deleting objects in a AutoCAD template with pyautocad & pywin32 (Python)

eraser

As we mentioned in our previous blog post, we will be discussing more on the pywin32 module, here is our new blog post. In this blog post, we will be discussing a very small topic i.e. how to delete objects from an AutoCAD template with pyautocad as well as with pywin32.

1. Deleting objects using pyautocad:

1.1 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)

1.2 Using delete method with pyautocad

To delete objects, I have created some objects on our AutoCAD template as shown in figure 1 below:

Figure 1.1: Created objects on AutoCAD template.

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.

Figure 1.2: Deleted all objects from AutoCAD template using pyautocad

1.3 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.1, then we can use the following methodology.

for object in acad.iter_objects(object_name_or_list="AcDbCircle"):
    object.Delete()
Figure 2: Filtered out other objects to delete the circle only

2. Deleting objects using pywin32 (win32com.client)

2.1 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.1.

2.2 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.

2.3 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.

Leave a Reply

2 thoughts on “Deleting objects in a AutoCAD template with pyautocad & pywin32 (Python)

  1. 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
    acad.prompt(atts)
    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!

    1. 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!

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.

Close

Meta