Rectangular arrays in AutoCAD using pyautocad (Python)

white and blue solar panels

While working with autocad, we cannot ignore arrays as it is the most integral part of AutoCAD utilities. There are two types of arrays in pyautocad, one being rectangular and the other is polar. In this blog post, we are going to discuss the rectangular array.

1. Setting up the environment

Before moving forward, we will set up our work environment by importing the pyautocad library. This integrates Python with AutoCAD.

from pyautocad import Autocad, APoint, aDouble
from math import *

acad = Autocad(create_if_not_exists=True)

2. Creating an object

To create an array, we will draw a rectangle using the AddPolyline method to use it as a base object.

sqp = aDouble(50, 150, 0, 50, 550, 0, 850, 550, 0, 850, 150, 0, 50, 150, 0)
sq1 = acad.model.AddPolyline(sqp)
Figure 1: Rectangle

3. Working with the ArrayRectangular method

Now, we will create a rectangular array. But, before creating the same, we will understand how does pyautocad creates the rectangular array using the parameters we pass.

Let’s check out the syntax of the ArrayRectangular method:

# Syntax:

object.ArrayRectangular(NumberOfRows, NumberOfColumns, NumberOfLevels, DistBetweenRows, DistBetweenColumns, DistBetweenLevels)

Levels are 3d parameters. We use that to create layers of array along z axis. We set NumberOfLevels parameter to 1 always as we need to create the array on x, y plane.

The object in the selection set is assumed to be in the lower left-hand corner, and the array is generated up and to the right when the object is created.

For instance, let’s create an array from the rectangle we have already created.

arr1 = sq1.ArrayRectangular(5, 5, 1, 100, 100, 0)
Figure 2: Array of the rectangle

As we can see from Figure 2, the command parameters row distance (100 units) and column distance (100 units) were used by pyautocad by considering the lower-left corner of the rectangle as base points to create the array.

But, we are not expecting this. We need to create an array having 100 as the distance between the boundaries of two objects.

4. Creating array using boundaries as base

Now, we will write a simple code to use the boundaries of objects as a base for creating array offsets for the rectangle.

def array_rectangle(x, y, z, r, c, lr, dr, dc, dl):
    l=int(input("Enter length of rectangle:"))
    w=int(input("Enter width of rectangle:"))
    rect = (x, y, z, x, y+w, z, x+l, y+w, z, x+l, y, z, x, y, z)
    rec = acad.model.AddPolyline(aDouble(rect))
    arr = rec.ArrayRectangular(r, c, lr, dr+w, dc+l, dl)

In this code, we have to pass x, y, z coordinates as the base point for creating the rectangle. After that we will add the same set of parameters as mentioned in the command syntax.

Now, let us see how the array gets created after passing parameters in this code.

array_rectangle(50, 150, 0, 3, 3, 1, 150, 250, 0)
Figure 3.1: Taking length & breadth as user input
Figure 3.2: Array of the rectangle (boundary as the base)

5. Array of circle

Let us try drawing an array for a circle using the existing command.

c = acad.model.AddCircle(APoint(100, 100), 100)
arr = c.ArrayRectangular(3, 3, 1, 50, 50, 0)
Figure 4.1: Array of circle

Now, we will write a simple code to create an array of circles to consider the boundaries of the circle as a base.

def array_circle(x, y, rad,  r, c, lr, dr, dc, dl):
    c1 = acad.model.AddCircle(APoint(x, y), rad)
    arr2 = c1.ArrayRectangular(r, c, lr, dr+rad*2, dc+rad*2, dl)    
array_circle(100, 100, 100, 3, 3, 1, 50, 100, 0)
Figure 4.2: Array of the circle (boundary as the base)

For more information on AutoCAD itself, you can also review the Autodesk documentation.

Leave a Reply

6 thoughts on “Rectangular arrays in AutoCAD using pyautocad (Python)

  1. Dear Tanmay,

    I don’t understand why is returned this error when I use the method ArrayRectangle.
    Thank you
    Regards,
    Edoardo

    start=APoint(1000.00, 1036.64, 0.00)
    Ri=100
    Nx=40
    Ny=23
    DistBetweenRows=450
    DistBetweenColumns=779.42
    CircleObj1 = acad.model.AddCircle(start, Ri)
    arr = CircleObj1.ArrayRectangular(Nx, Ny, 1, DistBetweenRows, DistBetweenColumns, 0)
    Traceback (most recent call last):

    File “”, line 2, in
    arr = CircleObj1.ArrayRectangular(Nx, Ny, 1, DistBetweenRows, DistBetweenColumns, 0)

    File “C:\Python37\lib\site-packages\comtypes\client\lazybind.py”, line 182, in caller
    return self._comobj._invoke(descr.memid, descr.invkind, 0, *args)

    File “C:\Python37\lib\site-packages\comtypes\automation.py”, line 730, in _invoke
    return var._get_value(dynamic=True)

    File “C:\Python37\lib\site-packages\comtypes\automation.py”, line 457, in _get_value
    typ = _vartype_to_ctype[self.vt & ~VT_ARRAY]

    KeyError: 9

    1. Dear Edoardo,

      Thanks for your question. I would like to know, which python version is being used over here. Can you please update the same if it is 3.7 or anything older than that? This sometimes happens with older versions of python while working with some random commands.

      Else please check if the version of package comtypes is a latest one, otherwise use:
      pip install comtypes

      I would like to know if this helps you.

      Have a nice day!

  2. Hi Tanmay,

    thank you for the quick reply.
    The python version is Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18)
    Comtypes is c:\python37\lib\site-packages (1.1.10)

    So, do you suggest to install a more recent version of python?
    Thank you
    Cheers
    Edoardo

    1. Hi,

      Yes, if possible, please try using the latest version of Python. Also, check if the comtypes package installed is updated too, as it is a dependency for pyautocad.

      Thank you!

      1. Hi,

        sorry to bother you again.
        I’ve just checked with
        Python 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03)
        comtypes-1.1.10 pyautocad-0.2.0
        but it returns the same error.
        And unfortunately I can’t install newer ver of python because they need Win 10, I don’t have it.
        I will try an alternative way.

        Thank you
        Have a nice day
        Edo

        1. Hi Edoardo,

          No issues at all, please find the script below, this has to work.:

          import win32com.client
          import pythoncom
          from math import pi

          acad = win32com.client.Dispatch(“AutoCAD.Application”)
          acadModel = acad.ActiveDocument.ModelSpace

          def APoint(x, y, z = 0):
          return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z))

          def ADouble(xyz):
          return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (xyz))

          def aVariant(vObject):
          return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, (vObject))

          c1 = acadModel.AddCircle(APoint(1000.00, 1036.64, 0.00), 100)

          c1.ArrayRectangular(Nx, Ny, 1, DistBetweenRows, DistBetweenColumns, 0)

          —————————————————————————–

          Always happy to help 🙂

          Have a great day.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Close

Meta