In this post I will demonstrate how to draw a polygon by creating polylines in AutoCAD with Python, using pyautocad. I will make use of the aDouble constructor and the AddPolyline method. In previous posts we already demonstrated a simple use case implementing pyautocad for AutoCAD in Python. We also wrote posts documenting the implementation of e.g. a pyautocad helice, pyautocad spline, and pyautocad solid object.
Motivation for drawing polylines in AutoCAD with pyautocad
In pyautocad we do not have any method assigned to drawing a polygon, rectangle, or triangle. Thus, we have to draw such objects using a polyline, i.e. by using the AddPolyline method in pyautocad.
Importing relevant libraries: pyautocad, os, and math
# Importing necessary methods from pyautocad library to draw a polygon: from pyautocad import Autocad, APoint, aDouble # os.X_OK: Checks if path can be executed: from os import X_OK # Importing math library to compute coordinates for a polygon: 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” so that the code will automatically generate an autocad template which can be saved later:
acad = Autocad(create_if_not_exists=True)
Request and embed user input into workflow and code
With this code we can create an inscribed polygon.
To draw this polygon we need some user inputs to generate the drawing. This includes input concerning center point coordinate for circle, radius of circle.
We can code this as per our preferred coding language. In this case Python, using pyautocad:
# 1. Number of vertices for polygon na = int(input("Enter the number of vertices for the polygone: ")) # 2. Center & Radius of Circle # Center cc = input("Enter x, y coordinates for center of circle with comma separators e.g. x, y: ") ccc = (map(float, cc.split(", "))) # Converting list to tuple as pyautocad accepts tuple as input ccct = tuple(ccc) print('The coordinates for the center of circle : ',ccct) # Radius rc = float(input("Enter the radius of Circle: "))
Calculate coordinates for vertices of AutoCAD polygon
We can calculate coordinates for vertices of the polygon using the formula below:
x + r * cos( 2 * pi * i / n ), y + r * sin(2 * pi * i / n)
The components of this formula are list below:
x, y = Coordinates for the center of the circle
r = Radius of circle
i = Iteration
n = Number of vertices
I have encoded this as mentioned below:
# 3. Calculate coordinates i=0 # Creating an empty list for coordinates pgonc= for i in range(na): x=round(ccct+rc*cos(2*pi*i/na),2) y=round(ccct+rc*sin(2*pi*i/na),2) z=0 crd = [x, y, z] pgonc.extend(crd) i += 1 # Addind first point again to complete the loop of polygon fp = [pgonc, pgonc, pgonc] pgonc.extend(fp) # Converting list to tuple as pyautocad accepts tuple as input pgont=tuple(pgonc) print("Coordinates of polygon: ") print(pgont)
Draw the AutoCAD polygon using polylines in pyautocad
To draw the polygon, we need to pass the coordinates to aDouble method in a tuple format:
polygon = aDouble(pgont)
Now with the AddPolyline method we can draw the polygon according to the coordinates we passed to aDouble:
polygond = acad.model.AddPolyline(polygon)
Cross verification of polylines in pyautocad
We can check whether the center point and radius of the circle are correct in AutoCAD, as depicted on Figures 1, 2 & 3:
We can also check whether the coordinates calculated as shown in Figure 1 (175.0, 25.0, 0, 123.18, 96.33, 0, 39.32, 69.08, 0, 39.32, -19.08, 0, 123.18, -46.33, 0, 175.0, 25.0, 0) are matching with the vertices drawn on the template. I have used the ID command from autocad for each endpoint of the vertices. Refer to Figures 4.1 to 4.5.
Rotate the AutoCAD polygon with pyautocad
We can rotate the polygon using rotate method on the variable assigned to polyline:
Here we will try rotating the polygon around its center i.e. (100, 25, 0), at 185 degrees.
Syntax for rotation:
object.Rotate(Coordinates for Axis, Angle)
#Rotate the polygon polygond = polygond.Rotate(APoint(100, 25, 0), 3.14*185/180)
Note: We shall convert the angle in radians if we want to enter the same in degrees.
As per Figure 5, we can see that the new polygon has been created after rotation.
Other properties of polylines in pyautocad
We can find other properties of the created objects, i.e. here polygon too; using some of the methods mentioned below:
#Area = Specifies the area of closed entities: pa = polygond.Area print("Area of polygon: " + str(pa)) #Length = Specifies the length of any object: pl = polygond.Length print("Perimeter of polygon: " + str(pl)) #Closed = Specified whether the created object is open or closed: is_closed = polygond.Closed print("Is the Polyline Object a Closed space: " + str(is_closed)) #Coordinate: Specify coordinates of a single vertex by passing vertex index no as a parameter: print("Elevation of Vertex: ") print(polygond.Coordinate(1)) #Coordinates: We can get coordinates of all the vertices of polygon: print(polygond.Coordinates) #Layer = Specifies the layer used to draw the object: print("Perimeter of polygon: " + polygond.Layer) O/p: Area of Polygon: 13374.603200000005 Perimeter of Polygon: 440.84505325868145 Is the Polyline Object a Closed space: True Elevation of Vertex: (83.00542638047868, -48.05111749652207, 0.0) Elevation of Polyline: (25.27479789063051, 18.585627878437045, 0.0, 83.00542638047868, -48.05111749652207, 0.0, 164.22761049486715, -13.72883744859422, 0.0, 156.68772987837394, 74.1081467908993, 0.0, 70.80443535564973, 94.08618027577995, 0.0, 25.27479789063051, 18.585627878437045, 0.0) Perimeter of Polygon: 0
Finally, here is a link to the official 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.