Dears, wanted to create python code to read from dxf file and extract all information show on the autocad map such as :
Layers
Dimensions , the text shows on the dimension ( 32)and the unit
checked the below code shows the unit only
enter image description here
tried this code :
import ezdxf
import pandas as pd
def extract_dimension_data(doc):
modelspace = doc.modelspace()
dimensions = []
for entity in modelspace.query('DIMENSION'):
# Retrieve the dimension text
dim_text = entity.dxf.text
# Check if the dimension text is a placeholder or missing
if not dim_text or dim_text == "<>":
try:
# Get the actual measurement as fallback
dim_measurement = entity.dxf.measurement
dim_text = f"{dim_measurement:.4f}"
except AttributeError:
dim_text = "N/A" # Fallback if the measurement can't be retrieved
dimension = {
'Dimension Text': dim_text,
}
dimensions.append(dimension)
return dimensions
def save_to_excel(dimensions, units, output_file):
df = pd.DataFrame(dimensions)
df['Units'] = units # Add the units column
df.to_excel(output_file, index=False)
# Example usage
dwg_file = 'your_file.dxf'
output_file = 'dimensions.xlsx'
doc = ezdxf.readfile(dwg_file)
units = get_drawing_units(doc)
dimensions = extract_dimension_data(doc)
save_to_excel(dimensions, units, output_file)
print(f"Dimensions saved to {output_file} with units: {units}")
```
``
New contributor
Udemy Instructor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1