-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcad_data.py
More file actions
86 lines (70 loc) · 3.12 KB
/
cad_data.py
File metadata and controls
86 lines (70 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import streamlit as st
import ezdxf
def extract_metadata_from_dxf(file):
"""Extract metadata from a DXF file using ezdxf."""
try:
# Save the file temporarily for processing
with open("temp.dxf", "wb") as f:
f.write(file.getbuffer())
# Load DXF file using ezdxf
dxf_doc = ezdxf.readfile("temp.dxf")
# Extract various entity counts
block_count = len(dxf_doc.blocks)
line_count = len(dxf_doc.modelspace().query("LINE"))
arc_count = len(dxf_doc.modelspace().query("ARC"))
polyline_count = len(dxf_doc.modelspace().query("LWPOLYLINE POLYLINE"))
viewport_count = len(dxf_doc.viewports)
dimension_count = len(dxf_doc.modelspace().query("DIMENSION"))
# Extract units (if available)
units = dxf_doc.header.get('$INSUNITS', 'Unknown')
# Extract author (if available)
author = dxf_doc.header.get('$LASTSAVEDBY', 'Unknown')
# Initialize a list to store extracted text
extracted_text = []
# Query for TEXT and MTEXT entities in the modelspace
for entity in dxf_doc.modelspace().query("TEXT MTEXT"):
if entity.dxftype() == "TEXT":
extracted_text.append(f"{entity.dxf.text.strip()}")
elif entity.dxftype() == "MTEXT":
extracted_text.append(f"{entity.text.strip()}")
# Format the extracted text into a structured format
formatted_text = "\n".join(extracted_text)
# Extract metadata
metadata = {
"File Format": "DXF",
"Entities Count": len(dxf_doc.entities),
"Layers Count": len(dxf_doc.layers),
"Block Definitions Count": block_count,
"Line Entities Count": line_count,
"Arc Entities Count": arc_count,
"Polyline Entities Count": polyline_count,
"Dimensions Count": dimension_count,
"Viewport Count": viewport_count,
"Units": units,
"File Version": dxf_doc.header["$ACADVER"],
"Author": author,
"Creation Date": dxf_doc.header.get('$TDCREATE', 'Unknown'),
"Modification Date": dxf_doc.header.get('$TDUPDATE', 'Unknown'),
"Texts detected": formatted_text
}
return metadata
except Exception as e:
return {"Error": str(e)}
def process_dxf(file):
"""Process DXF file."""
file_name = file.name.lower()
if file_name.endswith(".dxf"):
return extract_metadata_from_dxf(file)
else:
return {"Error": "Unsupported file format. Please upload a DWG or DXF file."}
# Streamlit app
st.title("DXF File Metadata Extractor")
# Allow user to upload a single DWG or DXF file
uploaded_file = st.file_uploader("Upload a DXF file", type=["dxf"], accept_multiple_files=False)
if uploaded_file:
st.write(f"**Processing file:** {uploaded_file.name}")
# Process the file (DWG or DXF)
metadata = process_dxf(uploaded_file)
# Display metadata
st.subheader(f"Metadata for {uploaded_file.name}")
st.json(metadata)