dxfedit/test_aspose_cad.py
puzzlesion 91d0262300 Initial commit: Aspose.CAD for Python 问题分析与解决方案
- 完成多版本测试(22.2, 23.6, 24.12, 25.3)
- 发现API设计问题:无法访问CAD特定功能
- 提供5个解决方案,推荐使用ezdxf替代方案
- 创建完整的测试脚本和分析报告
- 包含详细的README文档
2025-09-02 15:08:09 +08:00

211 lines
6.8 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.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Aspose.CAD for Python 测试脚本
功能测试DWG文件加载、信息读取和格式转换
作者AI Assistant
日期2024
"""
import os
import sys
from pathlib import Path
import aspose.cad as cad
from aspose.cad.imageoptions import CadRasterizationOptions, PngOptions, PdfOptions
from aspose.cad import Color
def print_separator(title=""):
"""打印分隔线"""
print("=" * 60)
if title:
print(f" {title} ")
print("=" * 60)
def test_aspose_cad_installation():
"""测试Aspose.CAD安装是否正常"""
print_separator("测试Aspose.CAD安装")
try:
# 测试基本导入
print("✓ 成功导入aspose.cad模块")
# 测试版本信息
print(f"✓ Aspose.CAD版本信息: {cad.__version__ if hasattr(cad, '__version__') else '版本信息不可用'}")
return True
except ImportError as e:
print(f"✗ 导入失败: {e}")
print("请确保已正确安装Aspose.CAD for Python")
return False
except Exception as e:
print(f"✗ 测试失败: {e}")
return False
def get_dwg_file_info(dwg_path):
"""获取DWG文件基本信息"""
print_separator("DWG文件信息")
if not os.path.exists(dwg_path):
print(f"✗ 文件不存在: {dwg_path}")
return None
try:
# 获取文件基本信息
file_size = os.path.getsize(dwg_path)
print(f"文件路径: {dwg_path}")
print(f"文件大小: {file_size:,} 字节 ({file_size/1024/1024:.2f} MB)")
# 加载DWG文件
print("正在加载DWG文件...")
with cad.Image.load(dwg_path) as image:
print(f"✓ 成功加载DWG文件")
# 获取图像信息
print(f"图像宽度: {image.width} 像素")
print(f"图像高度: {image.height} 像素")
print(f"图像格式: {type(image).__name__}")
# 如果是CAD图像尝试获取更多信息
if hasattr(image, 'layers'):
print(f"图层数量: {len(image.layers) if image.layers else '未知'}")
return image
except Exception as e:
print(f"✗ 加载DWG文件失败: {e}")
return None
def convert_dwg_to_png(dwg_path, output_path=None):
"""将DWG文件转换为PNG格式"""
print_separator("DWG转PNG转换")
if output_path is None:
output_path = dwg_path.replace('.dwg', '_converted.png')
try:
# 加载DWG文件
with cad.Image.load(dwg_path) as image:
# 设置光栅化选项
rasterization_options = CadRasterizationOptions()
rasterization_options.page_width = 1600
rasterization_options.page_height = 1600
rasterization_options.background_color = Color.white
# 设置PNG选项
png_options = PngOptions()
png_options.vector_rasterization_options = rasterization_options
# 保存为PNG
print(f"正在转换为PNG格式...")
image.save(output_path, png_options)
print(f"✓ 转换成功: {output_path}")
# 检查输出文件
if os.path.exists(output_path):
output_size = os.path.getsize(output_path)
print(f"输出文件大小: {output_size:,} 字节 ({output_size/1024/1024:.2f} MB)")
return True
except Exception as e:
print(f"✗ PNG转换失败: {e}")
return False
def convert_dwg_to_pdf(dwg_path, output_path=None):
"""将DWG文件转换为PDF格式"""
print_separator("DWG转PDF转换")
if output_path is None:
output_path = dwg_path.replace('.dwg', '_converted.pdf')
try:
# 加载DWG文件
with cad.Image.load(dwg_path) as image:
# 设置光栅化选项
rasterization_options = CadRasterizationOptions()
rasterization_options.page_width = 1600
rasterization_options.page_height = 1600
rasterization_options.background_color = Color.white
# 设置PDF选项
pdf_options = PdfOptions()
pdf_options.vector_rasterization_options = rasterization_options
# 保存为PDF
print(f"正在转换为PDF格式...")
image.save(output_path, pdf_options)
print(f"✓ 转换成功: {output_path}")
# 检查输出文件
if os.path.exists(output_path):
output_size = os.path.getsize(output_path)
print(f"输出文件大小: {output_size:,} 字节 ({output_size/1024/1024:.2f} MB)")
return True
except Exception as e:
print(f"✗ PDF转换失败: {e}")
return False
def main():
"""主函数"""
print_separator("Aspose.CAD for Python 测试脚本")
print("此脚本将测试Aspose.CAD的基本功能")
print()
# 测试安装
if not test_aspose_cad_installation():
print("安装测试失败请检查Aspose.CAD安装")
return
# 查找DWG文件
dwg_files = list(Path('.').glob('*.dwg'))
if not dwg_files:
print("✗ 当前目录下未找到DWG文件")
print("请确保DWG文件在当前目录中")
return
print(f"找到 {len(dwg_files)} 个DWG文件:")
for i, dwg_file in enumerate(dwg_files, 1):
print(f" {i}. {dwg_file}")
# 使用第一个DWG文件进行测试
test_file = str(dwg_files[0])
print(f"\n使用文件进行测试: {test_file}")
# 获取文件信息
image = get_dwg_file_info(test_file)
if image is None:
print("无法加载DWG文件测试终止")
return
# 测试格式转换
print("\n开始格式转换测试...")
# 转换为PNG
png_success = convert_dwg_to_png(test_file)
# 转换为PDF
pdf_success = convert_dwg_to_pdf(test_file)
# 总结
print_separator("测试总结")
print(f"DWG文件加载: {'✓ 成功' if image else '✗ 失败'}")
print(f"PNG转换: {'✓ 成功' if png_success else '✗ 失败'}")
print(f"PDF转换: {'✓ 成功' if pdf_success else '✗ 失败'}")
if png_success or pdf_success:
print("\n✓ Aspose.CAD测试完成转换后的文件已保存到当前目录")
else:
print("\n✗ 所有转换测试都失败了")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\n用户中断了程序执行")
except Exception as e:
print(f"\n\n程序执行出错: {e}")
import traceback
traceback.print_exc()