dxfedit/03_Python_OpenSource_DXF/run_workflow.py
2025-09-09 18:42:30 +08:00

135 lines
5.0 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 os
import subprocess
import ezdxf
# --- 配置 ---
# ODA 文件转换器的路径 (请根据你的安装位置进行修改)
ODA_CONVERTER_PATH = r"C:\Program Files\ODA\ODAFileConverter 26.7.0\OdaFileConverter.exe"
# 输入和输出目录
INPUT_DIR = os.path.join('..', '04_Test_Files')
OUTPUT_DIR = '.' # 当前目录 (03_Python_OpenSource_DXF)
# 文件名
INPUT_DWG_FILENAME = '图签测试.dwg'
CONVERTED_DXF_FILENAME = '图签测试_converted.dxf'
FINAL_DXF_FILENAME = '图签测试_color_modified.dxf'
# 完整路径
input_dwg_path = os.path.join(INPUT_DIR, INPUT_DWG_FILENAME)
converted_dxf_path = os.path.join(OUTPUT_DIR, CONVERTED_DXF_FILENAME)
final_dxf_path = os.path.join(OUTPUT_DIR, FINAL_DXF_FILENAME)
# 确保输出目录存在
os.makedirs(OUTPUT_DIR, exist_ok=True)
def convert_dwg_to_dxf(converter_path, input_path, output_dir):
"""
使用 ODA File Converter 将 DWG 转换为 DXF。
"""
print(f"--- 步骤 1: 开始转换 DWG -> DXF ---")
if not os.path.exists(converter_path):
print(f"错误: ODA 转换器未找到: '{converter_path}'")
print("请检查 ODA_CONVERTER_PATH 变量是否正确。")
return False
if not os.path.exists(input_path):
print(f"错误: 输入的 DWG 文件未找到: '{input_path}'")
return False
# 构建命令行参数
# OdaFileConverter <input_folder> <output_folder> <output_version> <output_type> <recurse> <audit>
# 我们将输入和输出文件夹都设置为包含我们文件的目录
# DXF 版本: ACAD2018, DXF 格式: 0 (ASCII), 不递归, 执行修复
command = [
converter_path,
os.path.dirname(input_path), # 输入文件夹
output_dir, # 输出文件夹
"ACAD2018", # 输出版本
"DXF", # 输出类型 (ASCII DXF)
"0", # 不递归子目录
"1", # 执行修复
input_path # 指定具体输入文件
]
print(f"正在执行命令: {' '.join(command)}")
try:
# 使用 subprocess.run 来执行命令并等待其完成
result = subprocess.run(command, check=True, capture_output=True, text=True, encoding='gbk')
print("ODA 转换器输出:\n" + result.stdout)
print("DWG 到 DXF 转换成功!")
return True
except FileNotFoundError:
print(f"错误: 无法执行命令。请确保 '{converter_path}' 是一个有效的可执行文件。")
return False
except subprocess.CalledProcessError as e:
print(f"错误: ODA 转换器执行失败。返回码: {e.returncode}")
print("错误输出:\n" + e.stderr)
return False
except Exception as e:
print(f"执行 ODA 转换器时发生未知错误: {e}")
return False
def process_dxf_file(input_path, output_path):
"""
使用 ezdxf 读取、分析和修改 DXF 文件。
"""
print(f"\n--- 步骤 2: 开始处理 DXF 文件 ---")
try:
# 1. 加载转换后的 DXF 文件
print(f"正在加载 DXF 文件: '{input_path}'")
doc = ezdxf.readfile(input_path)
msp = doc.modelspace()
print("DXF 文件加载成功。")
# 2. 提取数据 (示例:统计 LINE 实体)
lines = msp.query('LINE')
print(f"文件中包含 {len(lines)} 条 LINE 实体。")
# 3. 修改颜色
print("正在修改所有 LINE 实体的颜色为蓝色 (color index: 5)...")
modified_count = 0
for line in lines:
line.dxf.color = 5 # 5 = Blue
modified_count += 1
print(f"成功修改了 {modified_count} 条 LINE 实体的颜色。")
# 4. 保存修改后的文件
print(f"正在保存修改后的 DXF 文件到: '{output_path}'")
doc.saveas(output_path)
print("文件保存成功!")
return True
except IOError:
print(f"错误: 无法读取或写入 DXF 文件。请检查路径和权限: '{input_path}'")
return False
except ezdxf.DXFStructureError as e:
print(f"错误: DXF 文件结构损坏或格式不支持: {e}")
return False
def main():
# 步骤 1: 转换 DWG 到 DXF
if not convert_dwg_to_dxf(ODA_CONVERTER_PATH, input_dwg_path, OUTPUT_DIR):
print("\n流程因转换失败而中止。")
return
# 步骤 2: 处理转换后的 DXF 文件
# OdaFileConverter 会自动命名输出文件,我们需要找到它
# OdaFileConverter may create the DXF in the output directory with the same name.
if os.path.exists(converted_dxf_path):
if process_dxf_file(converted_dxf_path, final_dxf_path):
print(f"\n流程成功完成!请在 CAD 查看器中打开 '{final_dxf_path}' 查看结果。")
else:
print("\n流程因 DXF 处理失败而中止。")
else:
print(f"\n错误未找到转换后的DXF文件 '{converted_dxf_path}'。ODA转换可能未按预期生成文件。")
if __name__ == '__main__':
main()