#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 分析真正的问题 """ import os import sys from pathlib import Path def main(): print("=" * 60) print(" 分析真正的问题 ") print("=" * 60) try: import aspose.cad as cad from aspose.cad.fileformats.cad import CadImage # 查找DWG文件 dwg_files = list(Path('.').glob('*.dwg')) if not dwg_files: print("未找到DWG文件") return test_file = str(dwg_files[0]) print(f"使用文件: {test_file}") # 关键发现:CadImage.load返回的是Image对象,不是CadImage对象 print("\n关键发现分析:") print("1. CadImage.load() 返回的是 Image 对象,不是 CadImage 对象") print("2. 这意味着Aspose.CAD的Python API与文档描述不一致") print("3. 需要找到正确的方法来访问CAD特定的功能") # 尝试不同的方法 print("\n尝试不同的方法:") # 方法1:检查Image对象是否有CAD特定的方法 with cad.Image.load(test_file) as image: print(f"Image对象类型: {type(image).__name__}") # 检查所有方法 methods = [attr for attr in dir(image) if not attr.startswith('_') and callable(getattr(image, attr))] print(f"Image对象方法: {methods}") # 检查是否有CAD特定的方法 cad_methods = [method for method in methods if 'cad' in method.lower() or 'entity' in method.lower() or 'block' in method.lower()] print(f"可能的CAD方法: {cad_methods}") # 方法2:尝试直接创建CadImage实例 print("\n方法2:尝试直接创建CadImage实例") try: cad_image = CadImage() print(f"成功创建CadImage实例: {type(cad_image).__name__}") # 检查CadImage的方法 cad_methods = [attr for attr in dir(cad_image) if not attr.startswith('_') and callable(getattr(cad_image, attr))] print(f"CadImage方法: {cad_methods}") # 尝试使用load方法 if 'load' in cad_methods: print("CadImage有load方法,尝试使用...") try: loaded_cad = cad_image.load(test_file) print(f"使用CadImage.load成功: {type(loaded_cad).__name__}") except Exception as e: print(f"使用CadImage.load失败: {e}") except Exception as e: print(f"创建CadImage实例失败: {e}") # 方法3:检查是否有其他加载方式 print("\n方法3:检查其他加载方式") try: # 检查CadImage的静态方法 static_methods = [attr for attr in dir(CadImage) if not attr.startswith('_') and callable(getattr(CadImage, attr))] print(f"CadImage静态方法: {static_methods}") # 尝试使用静态方法 if 'load' in static_methods: print("尝试使用CadImage.load静态方法...") try: static_loaded = CadImage.load(test_file) print(f"使用静态方法成功: {type(static_loaded).__name__}") # 检查这个对象是否有CAD特定属性 attrs = [attr for attr in dir(static_loaded) if not attr.startswith('_')] print(f"静态加载对象的属性: {attrs}") except Exception as e: print(f"使用静态方法失败: {e}") except Exception as e: print(f"检查静态方法失败: {e}") # 方法4:检查文件内容 print("\n方法4:检查文件内容") try: # 读取文件头部 with open(test_file, 'rb') as f: header = f.read(32) print(f"文件头部: {header.hex()}") # 检查DWG版本 if header.startswith(b'AC10'): print("DWG版本: AC1012 (AutoCAD 2000)") elif header.startswith(b'AC10'): print("DWG版本: AC1014 (AutoCAD 2004)") elif header.startswith(b'AC10'): print("DWG版本: AC1015 (AutoCAD 2007)") elif header.startswith(b'AC10'): print("DWG版本: AC1018 (AutoCAD 2010)") elif header.startswith(b'AC10'): print("DWG版本: AC1021 (AutoCAD 2013)") elif header.startswith(b'AC10'): print("DWG版本: AC1024 (AutoCAD 2016)") elif header.startswith(b'AC10'): print("DWG版本: AC1027 (AutoCAD 2018)") else: print("未知DWG版本") except Exception as e: print(f"检查文件内容失败: {e}") # 总结 print("\n" + "=" * 60) print(" 问题分析总结 ") print("=" * 60) print("真正的问题可能是:") print("1. Aspose.CAD Python API与文档不一致") print("2. CadImage.load()返回的是通用Image对象,不是CAD特定对象") print("3. 需要找到正确的方法来访问CAD实体") print("4. 可能需要使用不同的API或方法") print("\n建议:") print("1. 联系Aspose技术支持获取正确的API使用方法") print("2. 尝试使用其他CAD处理库") print("3. 考虑将DWG转换为DXF格式后处理") print("4. 检查Aspose.CAD的版本和文档") except Exception as e: print(f"分析失败: {e}") import traceback traceback.print_exc() if __name__ == "__main__": main()