AutoCAD geometry— Find number of nodes using Python.

Sivadharan K
2 min readNov 26, 2021

--

In complex construction projects, finding number of nodes would be useful for better cost estimation during tendering stage of the project when the 3D model is not ready.

Steps:

  1. Set up Python and Jupiter lab if not done already. Reference: https://www.youtube.com/watch?v=zd6EXwDJGMU
  2. Install ezdxf python library using the command ‘pip install ezdxf’. It is a Python package to create new DXF files and read/modify/write existing DXF files.
  3. Change the AutoCAD DXF file path in the below python script and run it. Make sure that you don’t have any extra objects in the dxf file other than the line geometry.
# Import the required python libraries
import sys
import pandas as pd
import numpy as np
import ezdxf
# Set the AutoCAD input file path
InputFilePath = r'E:\AutoCAD-analytical-geometry\AutoCAD_input_file.dxf'
# Load the AutoCAD file
AutocadDxfDoc = ezdxf.readfile(InputFilePath)

# Load the Model Space
ModelSpace = AutocadDxfDoc.modelspace()

# Load the LINE entity
Lines = ModelSpace.query('LINE')

# Get all the start and end points of the lines
P1List, P2List = [], []
for Line in Lines:
P1List.append(Line.dxf.start)
P2List.append(Line.dxf.end)

# Load the all the start and end points into Pandas data frame
df = pd.concat(([pd.DataFrame(P1List), pd.DataFrame(P2List)]))

# Round the x1, y1, z1, x2, y2, z2 with 2 digits.
df = np.round(df, 2)

# Remove the duplicate points
df = df.drop_duplicates()

# Print the result: df.shape[0]
print (f'Number of nodes in the input AutoCAD dxf file: {df.shape[0]}')

Also, the source code and the sample input file are available at github

Thanks for reading!

--

--