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.
82 lines
2.9 KiB
Python
82 lines
2.9 KiB
Python
import ezdxf
|
|
import sys
|
|
import os
|
|
|
|
def diagnose_block_geometry(dxf_file_path: str, block_name: str):
|
|
"""
|
|
Loads a DXF file, finds a specific block definition, and prints a
|
|
report of the geometric entities it contains.
|
|
"""
|
|
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
|
|
|
|
# Get the block definition
|
|
block_def = doc.blocks.get(block_name)
|
|
if not block_def:
|
|
print(f"错误: 在文件中找不到名为 '{block_name}' 的图块定义。")
|
|
return
|
|
|
|
print(f"--- 图块 '{block_name}' 的几何结构诊断报告 ---")
|
|
|
|
entity_count = 0
|
|
|
|
if not block_def:
|
|
print("图块定义为空。")
|
|
return
|
|
|
|
for entity in block_def:
|
|
entity_count += 1
|
|
print(f"\n实体 #{entity_count}:")
|
|
print(f" - 类型: {entity.dxftype()}")
|
|
|
|
if entity.dxftype() == 'LWPOLYLINE':
|
|
print(f" - 是否闭合: {entity.closed}")
|
|
with entity.points() as points:
|
|
point_list = list(points)
|
|
print(f" - 顶点数量: {len(point_list)}")
|
|
for i, p in enumerate(point_list):
|
|
print(f" - P{i+1}: ({p[0]:.3f}, {p[1]:.3f})")
|
|
|
|
elif entity.dxftype() == 'LINE':
|
|
print(f" - 起点: ({entity.dxf.start.x:.3f}, {entity.dxf.start.y:.3f})")
|
|
print(f" - 终点: ({entity.dxf.end.x:.3f}, {entity.dxf.end.y:.3f})")
|
|
|
|
elif entity.dxftype() == 'ARC':
|
|
print(f" - 圆心: ({entity.dxf.center.x:.3f}, {entity.dxf.center.y:.3f})")
|
|
print(f" - 半径: {entity.dxf.radius:.3f}")
|
|
print(f" - 起始角: {entity.dxf.start_angle:.3f}")
|
|
print(f" - 终止角: {entity.dxf.end_angle:.3f}")
|
|
|
|
else:
|
|
print(f" - (未详细解析的实体)")
|
|
|
|
if entity_count == 0:
|
|
print("图块定义中不包含任何几何实体。")
|
|
|
|
print(f"\n--- 报告结束: 共找到 {entity_count} 个实体 ---")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
DXF_FILE = r"C:\Users\83500\久翌\CAD编辑同步excel\测试文件区\04_Test_Files\塔器.dxf"
|
|
BLOCK_TO_DIAGNOSE = "A$C1CC9093B" # The block that previously failed
|
|
|
|
if len(sys.argv) > 2:
|
|
dxf_path = sys.argv[1]
|
|
block_name = sys.argv[2]
|
|
diagnose_block_geometry(dxf_path, block_name)
|
|
else:
|
|
print(f"未提供参数。将使用默认文件和图块进行诊断:")
|
|
print(f" - 文件: {DXF_FILE}")
|
|
print(f" - 图块: {BLOCK_TO_DIAGNOSE}")
|
|
diagnose_block_geometry(DXF_FILE, BLOCK_TO_DIAGNOSE)
|