dxfedit/03_Python_OpenSource_DXF/diagnose_blocks.py
puzzlesion 5e87bd7c33 refactor(dxf): optimize table drawing and add diagnostic scripts
This commit includes several updates to the Python_OpenSource_DXF project:

- The core script draw_table_from_template.py has been refactored for better logic and clarity.

- New utility scripts such as diagnose_blocks.py, export_dxf.py, and plot_by_block_name.py have been added to enhance diagnostic and plotting capabilities.

- The convert_dxf_to_pdf.py script was removed as its functionality is now covered by other modules.

- README.md and .specstory documentation have been updated to reflect these changes.
2025-09-12 17:21:08 +08:00

50 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import ezdxf
import argparse
from pathlib import Path
def diagnose_blocks(dxf_file: Path):
"""
加载一个DXF文件并打印出其模型空间中所有顶层块参照的名称。
"""
if not dxf_file.is_file():
print(f"错误: 文件不存在 -> {dxf_file}")
return
try:
doc = ezdxf.readfile(dxf_file)
except IOError:
print(f"错误: 无法读取文件 -> {dxf_file}")
return
except ezdxf.DXFStructureError as e:
print(f"DXF 文件结构错误: {e}")
return
msp = doc.modelspace()
top_level_blocks = msp.query('INSERT')
print(f"\n--- 在 '{dxf_file.name}' 的模型空间中找到的顶层块参照 ---")
if not top_level_blocks:
print("未找到任何顶层的块参照 (INSERT entities)。")
return
# 为了更好的可读性,我们统计一下每个块名出现的次数
from collections import Counter
block_names = [block.dxf.name for block in top_level_blocks]
block_counts = Counter(block_names)
print(f"共找到 {len(top_level_blocks)} 个块参照,涉及 {len(block_counts)} 个不同的块定义。")
print("-" * 50)
for name, count in block_counts.items():
print(f" 块名: '{name}' \t (在模型空间中出现了 {count} 次)")
print("-" * 50)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="诊断DXF文件列出其模型空间中所有顶层的块参照名称。"
)
parser.add_argument("input_dxf", type=Path, help="要诊断的源DXF文件的路径。")
args = parser.parse_args()
diagnose_blocks(args.input_dxf)