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)
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)
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)
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)
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)
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.