In this blog post I will introduce some very common AutoCAD operations and how we can implement them in a Python script using pyautocad. We will discuss how to copy, move/paste, delete, rotate, offset, scale and highlight objects.
Setting up Python script and AutoCAD model
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)
Adding AutoCAD objects to drawing template using pyautocad in Python
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.
Using copy, move and delete methods for basic AutoCAD operations
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.
As you can see from Figures 1 and 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.
Rotate AutoCAD objects with pyautocad in Python
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)
Offsets objects in AutoCAD using pyautocad
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)
Scaling AutoCAD objects in Python
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)
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”.
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.