Operations with AutoCAD objects using pyautocad (Python)

In this blog post, we will learn some of the common operations that we tend to perform on AutoCAD objects most often. We will discuss how to copy, move/paste, delete, rotate, offset, scale & highlight objects.

1. Setting up environment

First of all, we will set up our work environment for performing actions on the AutoCAD template using python’s pyautocad module.

from pyautocad import Autocad, APoint, aDouble
from math import *

acad = Autocad(create_if_not_exists=True)

2. Drawing objects

As we have already discussed from our previous blogs in this pyautocad series about creating objects, we will draw some objects to perform the operations on them as mentioned above.

c1 = acad.model.AddCircle(APoint(100, 100, 0), 100)
l1 = acad.model.AddLine(APoint(100,100), APoint(300, 350))
el1 = acad.model.AddEllipse(APoint(250, 300), APoint(700, 450), 0.5)

So, we have created a circle, a line, and an ellipse.

We will zoom into the created objects using the “ZoomExtents” method. This command works same as the default “zoom all” command in AutoCAD.

acad.app.ZoomExtents()
Figure 1: Practice objects

3. Using copy, move & delete methods

As we have sketched the basic AutoCAD objects, we will start with three of the most used commands i.e. copy, move, and delete.

The way copy work while using pyautocad is, we can create a copy of an existing object, store that in a variable and use the “Move” method to paste it at the desired location.

c2 = c1.Copy()


#obj.Move(previous location, new location)
c2.Move(APoint(100, 100), APoint(300, 300))

Here, we are copying the “c1″ circle and storing the replica of that in the variable”c2”. Then, we are copying it from its existing center point i.e. (100, 100, 0) to (300, 300, 0) by using the “Move” method.

Figure 2.1: Copied circle c1 to c2

As you can see from figures 1 & 2, we have copied circle 1 to a new location. Now, if we do not want our previous circle “c1”, we can Delete it; else we could have moved the circle “c1” using the Move method instead of copying it.

Let’s delete the circle “c1” now.

Figure 2.2: Deleted circle c1

4. Rotate objects

Now, we will discuss how to rotate objects. Here, we will copy line l1 and will rotate the same from its existing base point (100, 100, 0) to 90 Degrees.

l2 = l1.Copy()


# obj.Rotate(Base point, Angle of rotation)
l2.Rotate(APoint(100,100), pi*90/180)
Figure 3: Rotated line l1 to l2

5. Offsets of objects

Now, lets discuss how to take offsets of any object. We will need the Offset method to use against the object we want that offset for.

Here, we will take offset for the existing ellipse i.e. “el1”.

el2 = el1.Offset(10)
Figure 4: Offset of ellipse el1

6. Scaling objects

To scale an object up or down we will need the “Offset” method to be used against the object we want to scale.

Let’s scale down the existing circle “c2”.

# obj.ScaleEntity(Base point, Scaling factor)
c3 = c2.ScaleEntity(APoint(300, 300), 0.5)
Figure 5.1: Scaled down circle c2

In case, we have many objects to deal with, we can highlight the specific object using the “Highlight” method and pass the parameter as “True”.

l2.Highlight(True)  
Figure 5.2: Highlighted line l2

For more information on AutoCAD itself, you can also review the Autodesk documentation.

Leave a Reply

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