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.
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.
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.
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)
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)
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)
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”.
For more information on AutoCAD itself, you can also review the Autodesk 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.