Tree structure for AutoCAD and pyautocad

In one of my previous blog posts, I discussed how we can use lists and dictionaries for storing AutoCAD objects created. In this post I want to introduce some more efficient data structures that we can use for organizing AutoCAD objects in accordane with their properties. One of them is a tree data structure.

Tree data structure for organizing AutoCAD objects

A tree data structure is a non-linear data structure because it does not store data in a sequential manner. It is a hierarchical structure as elements in a tree are arranged on multiple levels and take the form of nodes. Nodes can contain any data type. In the case of pyautocad, I will need to fetch strings, integers, floats, etc.

Each node contains some data and the link or reference of other nodes. These associated nodes are parent or child nodes, depending on their position in the tree-shaped data hierarchy.

Figure 1: Tree data structure

For instance, as illustrated in above figure, node A is the root node. The root node is the upper node in the tree, and represents the first node in the tree. In this case the root node A has three (3) child nodes: B, F, and J. In other words, node A is a parent node to these nodes.

Organizing AutoCAD objects in such a tree-shaped hierarchical datastructure aids storage, maintenance, tracking, and manipulation of data.

Applying tree data structures when working with pyautocad in Python

Sometimes we need to interlink data while creating or editing drawings. For example, let us consider an example in which I am designing a small concrete floor framework containing beams and columns.

I could store each object as a separate node. In the coding example displayed below I create a node named Column, having 4 children. These children contain the data of a structural AutoCAD element as their own respective children.

    column = TreeNode("Column")

    c1 = TreeNode("Column1")
    c2 = TreeNode("Column2")
    c3 = TreeNode("Column3")
    c4 = TreeNode("Column4")


    c1.add_child(TreeNode(acad.model.AddPolyline(aDouble(0, 0, 0, 100, 0, 0, 100, 100, 0, 0, 100, 0, 0, 0, 0))))
    c2.add_child(TreeNode(acad.model.AddPolyline(aDouble(500, 0, 0, 600, 0, 0, 600, 100, 0, 500, 100, 0, 500, 0, 0))))
    c3.add_child(TreeNode(acad.model.AddPolyline(aDouble(500, 500, 0, 600, 500, 0, 600, 600, 0, 500, 600, 0, 500, 500, 0))))
    c4.add_child(TreeNode(acad.model.AddPolyline(aDouble(0, 500, 0, 100, 500, 0, 100, 600, 0, 0, 600, 0, 0, 500, 0))))

The above data tree structures the column elements that were created and added to the AutoCAD drawing. The elements are created and added with pyautocad, a module for scripting AutoCAD workflows in Python. I can structure beam elements in the same way. In that way I combine both into an overall tree-shaped data structure that organizes and stores the data for the entire building.

For this example, I proceed by implementing a node method for printing the content of a node and its children. The method can be called for the root node. The method will furthermore also print the surface area associated with each child node content, beam, or column. Here is the code for this:

# function for printing the structure and content of a data tree
def print_tree(self):
        space = "   " * self.get_level()
        prefix = space + "|--" if self.parent else ""
        print(prefix, end="")
            print("Area = " +  str(round(,2)))
        if self.children:
            for child in self.children:

In this example, I work with the structural AutoCAD framework displayed in the figure below.

Figure 2.1: Structural framework using pyautocad

For this example, the “print_tree” method created the following output:

Figure 2.2: Output for the data tree created in Python using pyautocad

Advantages of using tree data structure while working with pyautocad

Due to some technical problems or due to structural design necessity one may need to change the dimensions of a structure. In such an event some interlinked members will be affected. Hence, dimensions for these members have to be adjusted accordingly. To avoid extensive manual effort I can simply adjust the dimensions of one member and other members will get changed accordingly.

This can be achieved by using a combination of a data tree and a linked list. For instance, if I change the dimension of an object I can search for the dimension property of that particular object in the linked list. This object must be linked with the connecting objects as well, which can be further modified automatically on the basis of changes made with the dimensions of the connecting element. In this way, I can maintain the hierarchy of objects as well as link them together too.

Another use case is in terms of blockchain, i.e. we can track down the place where the changes have been made if we hash the data stored in each node. Or we can hash the entire document and track down the changes made to the same.

There can be multiple such use cases depending on the industry, domain, or work performed in AutoCAD. For more on AutoCAD, you can also check the official documentation for AutoCAD.

You May Also Like

Leave a Reply

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.