import tkinter as tk
from tkinter import ttk, messagebox, simpledialog,filedialog
class demo(tk.Tk):
def __init__(self):
super().__init__()
self.title("DXF信息核对")
self.geometry("200x200")
self.resizable(True, True)
self.btn = tk.Button(text = '选择文件', command=self.count_elements_per_layer)
self.text1 = tk.Text(height=20, width=60)
self.label = tk.Label(text = 'DXF图层信息:')
self.btn.grid(row=0, column=0)
self.label.grid(row=1, column=0)
self.text1.grid(row=2, column=0, columnspan=3)
def count_elements_per_layer(self):
dwg_file_path = filedialog.askopenfilename(title="选择DXF文件", filetypes=[("DXF文件", "*.dxf")])
# 打开DWG文件
doc = ezdxf.readfile(dwg_file_path)
# 确保文件已正确打开
if not doc:
raise ValueError(f"无法打开文件: {dwg_file_path}")
msp = doc.modelspace()
layer_counts = {}
for entity in msp.query(): # query() 方法可以返回所有实体
# 获取实体的图层名
layer_name = entity.dxf.layer
# 如果图层名不在字典中,则初始化为0
if layer_name not in layer_counts:
layer_counts[layer_name] = 0
# 增加该图层名的元素个数
layer_counts[layer_name] += 1
for layer_name, count in layer_counts.items():
#print(f"Layer: {layer_name}, Elements: {count}")
result_text = layer_name + ":" + str(count) + "\n"
self.text1.insert('insert', result_text)
print(result_text)
if __name__ == '__main__':
app = demo()
app.mainloop()
效果图:
1729477282745.png (19.12 KB, 下载次数: 0)
下载附件
2024-10-21 10:21 上传