pyautocad polylines for AutoCAD polygons

high angle view of a man

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[0]+rc*cos(2*pi*i/na),2)
    y=round(ccct[1]+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[0], pgonc[1], pgonc[2]]
pgonc.extend(fp)
# Converting list to tuple as pyautocad accepts tuple as input
pgont=tuple(pgonc)
print("Coordinates of polygon: ")
print(pgont)
Figure 1: User Input

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:

Polygon drawn with pyautocad
Figure 2: Center of Circle
Circle drawn with pyautocad
Figure 3: Circle Properties

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.

Figure 4.1: Point 1
Figure 4.2: Point 2
Figure 4.3: Point 3
Figure 4.4: Point 4
Figure 4.5: Point 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.

Figure 5: Rotated polygon

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.

Leave a Reply

2 thoughts on “pyautocad polylines for AutoCAD polygons

  1. Hi Mr. Tanmay Sawant

    Thank you for your introduction about the polyline. However I find out one problem:

    The value in z-axis doesn’t work and will be set to z=0 as always, from example:
    polygond = acad.model.AddPolyline(aDouble(0,0,-1,1,0,-2))
    the effect is actually
    polygond = acad.model.AddPolyline(aDouble(0,0, 0,1,0, 0))

    I am wondering if there is a way to make polyline truely for three-dimensional space.

    Thank you.

    Best regards

    1. Hi Mr. Lin,

      Unfortunately, creating 3D polyline is not possible using this way, but I’ll make sure that I can come up with some solution soon for this.

      But there is a facility to rotate the polyline in 3 Dimensions.
      You can use: object.Rotate3D(Point1, Point2, RotationAngle)
      where,
      Point1: First Point of the rotation axis
      Point2: Second Point of the rotation axis
      RotationAngle:Radians can be an integer

      e.g.
      polygond = acad.model.AddPolyline(aDouble(1,1,0,10,10,0))
      polygond.Rotate3D(aDouble(1, 1, 0), aDouble(10, 10, 10), 200)

      Kind Regards

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close

Meta