In this post, we will learn how to draw arcs using pyautocad in Python (AddArc method). In one of our previous posts, we already demonstrated how we can draw circles using the AddCircle method, implementing pyautocad for AutoCAD in Python.
1. Importing necessary libraries: pyautocad, math
#Importing necessary methods from pyautocad library to draw a polygon:
from pyautocad import Autocad, APoint, aDouble
#Importing math library:
from math import *
2. Open or create a template from AutoCAD in pyautocad
We can open any pre-created AutoCAD template in pyautocad.
Alternatively, we can set the “create_if_not_exists” parameter to “True”. As a result, the code will generate an AutoCAD template which can then be stored locally on our machine.
acad = Autocad(create_if_not_exists=True)
3. Parameters for the command
With the specified pyautocad method AddArc, we need to pass certain parameters to draw an arc object.
Therefore, we need have to pass coordinates for the center point of the arc object, its radius, start angle, and end angle.
Syntax:
acad.model.AddArc(<Center Point of Circle>, <Radius>, <Start Angle>, <End Angle>)
#Center Point
p1 = APoint(30,30)
arc1 = acad.model.AddArc(p1, 7.5, 0, 4)
The pyautocad command accepts angle in radians and not in degrees.
The arc will be drawn counter-clockwise by autocad, starting from the x-axis of the 1st quadrant.
However, we can directly pass the center point coordinates to the command itself, with angles converting to radians from degrees, for instance.
arc2 = acad.model.AddArc(APoint(45, 45), 10, 15*pi/180 , 105*pi/180)
Now we are all set for creating an arc on the AutoCAD template. Refer to Figures 1.1 & 1.2.
4. Area of an arc object
In the case of open objects such as arcs, spline curves, and open polylines the area is computed as if a straight line connects the start point and endpoint of the arc.
To get the area of the arc object we will use the object.Area() method.
a1a = print("Area of Arc1 = " + str(round(arc1.Area,2)))
a2a = print("Area of Arc2 = " + str(round(arc2.Area,2)))
O/p:
Area of Arc1 = 133.79
Area of Arc2 = 28.54
Refer to Figures 2.1 & 2.2.
5. Other properties of an arc object
We can find more such properties related to an arc object. For example, center point, radius, start & endpoints, start and end angles, etc. as described below:
#Center of Arc
a1c = print("Center of Arc1 = " + str(arc1.Center))
a2c = print("Center of Arc2 = " + str(arc2.Center))
#Radius of Arc
a1c = print("Radius of Arc1 = " + str(arc1.Radius))
a2c = print("Radius of Arc2 = " + str(arc2.Radius))
#Start & End points of Arc
a1sp = print("Start point of Arc1 = " + str(arc1.StartPoint))
a1ep = print("End point of Arc1 = " + str(arc1.EndPoint))
a2sp = print("Start point of Arc2 = " + str(arc2.StartPoint))
a2ep = print("End point of Arc2 = " + str(arc2.EndPoint))
#Start & End angles of Arc
a1sa = print("Start angle of Arc1 = " + str(arc1.StartAngle))
a1ea = print("End angle of Arc1 = " + str(arc1.EndAngle))
a2sa = print("Start angle of Arc2 = " + str(arc2.StartAngle))
a2ea = print("End angle of Arc2 = " + str(arc2.EndAngle))
O/p:
Center of Arc1 = (30.0, 30.0, 0.0)
Center of Arc2 = (45.0, 45.0, 0.0)
Radius of Arc1 = 7.5
Radius of Arc2 = 10.0
Start point of Arc1 = (37.5, 30.0, 0.0)
End point of Arc1 = (25.09767284352291, 24.323981285190538, 0.0)
Start point of Arc2 = (54.65925826289068, 47.588190451025206, 0.0)
End point of Arc2 = (42.411809548974794, 54.65925826289068, 0.0)
Start angle of Arc1 = 0.0
End angle of Arc1 = 4.0
Start angle of Arc2 = 0.2617993877991494
End angle of Arc2 = 1.8325957145940461
Refer to Figures 1.1 & 1.2 for verifying the output.
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.
6 comments
Thank you so much!
Thanks Tanmay for this contribution, I am Fernando from Mexico, and I will use it within my CNC project. Congratulations for your technical and programiing skills.
Thanks for your feedback Fernand.
I want to create a polyline that contains an arc. Is this possible?
Yes, see how to draw polyline in pyautocad
I see instructions on how to create a polyline from a list line endpoints:
points = [(0, 0), (10, 0), (10, 10), (0, 10)]
polyline = acad.model.AddPolyline(points)
I can not find anything about adding an arc into a polyline.