dxfedit/03_Python_OpenSource_DXF/list_layouts.py
puzzlesion 31e03fd2d9 feat(dxf): enhance block diagnostics and layout listing
This commit introduces several new scripts to the DXF project to improve diagnostics and data extraction capabilities:

- find_block_coordinates.py: A new utility to accurately find the coordinates of rectangular blocks, which is essential for automated positioning and plotting.

- diagnose_block_geometry.py: A script to analyze and report the geometric properties of blocks, aiding in debugging and validation.

- list_layouts.py: A simple tool to list all available layouts within a DXF file.

Associated documentation has been added to .specstory to cover these new features.
2025-09-15 10:09:10 +08:00

35 lines
1.1 KiB
Python

import ezdxf
import sys
import os
def list_layouts(dxf_file_path: str):
if not os.path.exists(dxf_file_path):
print(f"错误: 文件不存在 '{dxf_file_path}'")
return
try:
doc = ezdxf.readfile(dxf_file_path)
except IOError:
print(f"错误: 无法打开文件 '{dxf_file_path}'")
return
except ezdxf.DXFStructureError as e:
print(f"错误: 无效或损坏的 DXF 文件. {e}")
return
print(f"'{os.path.basename(dxf_file_path)}' 文件中的布局:")
# The 'Model' layout is the model space.
print(f"- Model (模型空间)")
# Iterate over paper space layouts
for layout in doc.layouts:
if layout.name.lower() != 'model':
print(f"- {layout.name} (图纸空间)")
if __name__ == "__main__":
if len(sys.argv) > 1:
file_path = sys.argv[1]
list_layouts(file_path)
else:
default_path = r"C:\Users\83500\久翌\CAD编辑同步excel\测试文件区\04_Test_Files\塔器.dxf"
print(f"未提供文件路径。使用默认路径: {default_path}")
list_layouts(default_path)