In continuation of our previous blog posts related to pyautocad, we will discuss how we can draw an ellipse and elliptical arcs using the AddEllipse method from the pyautocad module on the AutoCAD template.
Importing relevant Python modules
As usual, we will need to import the modules to create an ellipse and to perform some mathematical calculations.
from pyautocad import Autocad, APoint
from math import *
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”. Which will then generate an AutoCAD template that can be stored locally on our machine.
acad = Autocad(create_if_not_exists=True)
Command parameters for ellipse
While working with the AddEllipse method, we need to pass certain parameters to the command itself, and sometimes separately in case of drawing an elliptical arc.
Syntax:
acad.model.AddEllipse(<Centre of ellipse>, <Greater radius end point>, <Ratio of greater to smaller radius>)
We will pass the points for the center of the ellipse object and the position of the endpoint of the greater radius.
#Ellipse 1
p1 = APoint(35, 35)
p2 = APoint(40, 35)
el1 = acad.model.AddEllipse(p1, p2, 0.5)
Let us draw the Ellipse.
Explanation of ellipse 1
Let’s take a look at Figure 1. The ellipse is tilted. Now let us discuss how does this works.
Here the ellipse is formed on basis of the second argument we provide, that is point p2. The greater radius is created in the direction of the vector that has formed due to the points we passed as p2.
Also, talking about the radius size of such a tilted ellipse, it is the hypotenuse of the triangle that has been formed when we pass points.
For example, if p1 = (0, 0) & p2 = (1, 1) ; hypotenuse of x = 1, y = 1 is 1.41.
Hence greater radius = 1.41
Elliptical arcs in pyautocad
Now that we have seen how to draw an ellipse, we need to learn how we can create elliptical arcs.
We need 2 extra arguments having the below-mentioned syntax:
object.StartAngle = Angle in radians
object.EndAngle = Angle in radians
#Ellipse 2
el2 = acad.model.AddEllipse(APoint(100, 50), APoint(150, 50), 0.5)
#Creating arc out of ellipse 2
el2.StartAngle = round(45 * (3.14 / 180),2)
el2.EndAngle = round(270 * (3.14 / 180),2)
As we can see from Figure 2 an elliptical arc has formed and, on clicking on that arc, the AutoCAD interface shows us that the arc is a part of the ellipse 2.
Also, we can check the properties to confirm whether the start and end angles of the arc are correct.
Properties of an AutoCAD ellipse object in pyautocad
We can generate the properties of the ellipse using the below-mentioned commands for detailed information
print("Ellipse 1 properties: " + "\n" +
"Area of Ellipse 1 = " + str(round(el1.Area,2)) + "\n" +
"Center of Ellipse 1 = " + str(el1.Center) + "\n" +
"Start point of Ellipse 1 = " + str(el1.StartPoint) + "\n" +
"End point of Ellipse 1 = " + str(el1.EndPoint) + "\n" +
"Start angle of Ellipse 1 = " + str(round(el1.StartAngle,2)) + "\n" +
"End angle of Ellipse 1 = " + str(round(el1.EndAngle,2)) + "\n" +
"Start parameter of Ellipse 1 = " + str(el1.StartParameter) + "\n" +
"End parameter of Ellipse 1 = " + str(el1.EndParameter) + "\n" +
"Major axis of Ellipse 1 = " + str(el1.MajorAxis) + "\n" +
"Minor axis of Ellipse 1 = " + str(el1.MinorAxis) + "\n" +
"Major radius of Ellipse 1 = " + str(round(el1.MajorRadius,2)) + "\n" +
"Minor radius of Ellipse 1 = " + str(round(el1.MinorRadius,2)) + "\n" +
"Radius ratio of Ellipse 1 = " + str(el1.RadiusRatio)
)
O/p:
Ellipse 1 properties:
Area of Ellipse 1 = 4437.5
Center of Ellipse 1 = (35.0, 35.0, 0.0)
Start point of Ellipse 1 = (75.0, 70.0, 0.0)
End point of Ellipse 1 = (75.00000000000001, 69.99999999999999, 0.0)
Start angle of Ellipse 1 = 0.0
End angle of Ellipse 1 = 6.28
Start parameter of Ellipse 1 = 0.0
End parameter of Ellipse 1 = 6.283185307179585
Major axis of Ellipse 1 = (40.0, 35.0, 0.0)
Minor axis of Ellipse 1 = (-17.5, 20.0, 0.0)
Major radius of Ellipse 1 = 53.15
Minor radius of Ellipse 1 = 26.58
Radius ratio of Ellipse 1 = 0.5
Properties of elliptical arcs in AutoCAD
The reason behind creating a separate point for properties of elliptical arcs is the area of arc.
As we have already discussed in one of our previous blogs regarding arcs; in the case of open objects such as arcs, spline curves, and open polylines:
The area is computed as though a straight line connects the start point and endpoint.
For example:
Let’s generate the properties of the elliptical arc that we created from ellipse 2.
print("Ellipse 2 properties: " + "\n" +
"Area of Ellipse 2 = " + str(round(el2.Area,2)) + "\n" +
"Center of Ellipse 2 = " + str(el2.Center) + "\n" +
"Start point of Ellipse 2 = " + str(el2.StartPoint) + "\n" +
"End point of Ellipse 2 = " + str(el2.EndPoint) + "\n" +
"Start angle of Ellipse 2 = " + str(round(el2.StartAngle,2)) + "\n" +
"End angle of Ellipse 2 = " + str(round(el2.EndAngle,2)) + "\n" +
"Start parameter of Ellipse 2 = " + str(el2.StartParameter) + "\n" +
"End parameter of Ellipse 2 = " + str(el2.EndParameter) + "\n" +
"Major axis of Ellipse 2 = " + str(el2.MajorAxis) + "\n" +
"Minor axis of Ellipse 2 = " + str(el2.MinorAxis) + "\n" +
"Major radius of Ellipse 2 = " + str(round(el2.MajorRadius,2)) + "\n" +
"Minor radius of Ellipse 2 = " + str(round(el2.MinorRadius,2)) + "\n" +
"Radius ratio of Ellipse 2 = " + str(el2.RadiusRatio)
)
O/p:
Ellipse 2 properties:
Area of Ellipse 2 = 25270.19
Center of Ellipse 2 = (100.0, 50.0, 0.0)
Start point of Ellipse 2 = (144.18745032916178, 139.40106955671075, 0.0)
End point of Ellipse 2 = (124.8208084229757, -25.05967107522803, 0.0)
Start angle of Ellipse 2 = 0.79
End angle of Ellipse 2 = 4.71
Start parameter of Ellipse 2 = 1.1108200595027047
End parameter of Ellipse 2 = 4.711194488488036
Major axis of Ellipse 2 = (150.0, 50.0, 0.0)
Minor axis of Ellipse 2 = (-25.0, 75.0, 0.0)
Major radius of Ellipse 2 = 158.11
Minor radius of Ellipse 2 = 79.06
Radius ratio of Ellipse 2 = 0.5
As per the output we have received and Figures 2 & 3, we can see that the area generated from the elliptical arc that we created is correct.
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.
Leave a Reply