#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简化的Aspose.CAD调试脚本
"""

import os
import sys
from pathlib import Path

def main():
    print("=" * 60)
    print(" Aspose.CAD 问题调试 ")
    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}")
        
        # 方法1：使用CadImage.load
        print("\n方法1：使用CadImage.load")
        try:
            cad_image = CadImage.load(test_file)
            print(f"成功加载，类型: {type(cad_image).__name__}")
            
            # 检查属性
            attrs = [attr for attr in dir(cad_image) if not attr.startswith('_')]
            print(f"可用属性数量: {len(attrs)}")
            
            # 查找可能包含实体的属性
            entity_attrs = [attr for attr in attrs if 'entit' in attr.lower() or 'block' in attr.lower() or 'layer' in attr.lower()]
            print(f"可能的实体属性: {entity_attrs}")
            
            for attr in entity_attrs:
                try:
                    value = getattr(cad_image, attr)
                    print(f"  {attr}: {type(value).__name__}")
                    if hasattr(value, '__len__'):
                        print(f"    长度: {len(value)}")
                except Exception as e:
                    print(f"  {attr}: 访问失败 - {e}")
            
        except Exception as e:
            print(f"使用CadImage.load失败: {e}")
        
        # 方法2：使用Image.load
        print("\n方法2：使用Image.load")
        try:
            with cad.Image.load(test_file) as image:
                print(f"成功加载，类型: {type(image).__name__}")
                print(f"图像尺寸: {image.width} x {image.height}")
                
                # 检查属性
                attrs = [attr for attr in dir(image) if not attr.startswith('_')]
                print(f"可用属性数量: {len(attrs)}")
                
                # 查找可能包含实体的属性
                entity_attrs = [attr for attr in attrs if 'entit' in attr.lower() or 'block' in attr.lower() or 'layer' in attr.lower()]
                print(f"可能的实体属性: {entity_attrs}")
                
                for attr in entity_attrs:
                    try:
                        value = getattr(image, attr)
                        print(f"  {attr}: {type(value).__name__}")
                        if hasattr(value, '__len__'):
                            print(f"    长度: {len(value)}")
                    except Exception as e:
                        print(f"  {attr}: 访问失败 - {e}")
        
        except Exception as e:
            print(f"使用Image.load失败: {e}")
        
        # 方法3：检查文件格式
        print("\n方法3：检查文件格式")
        try:
            file_format = cad.Image.get_file_format(test_file)
            print(f"文件格式: {file_format}")
            
            can_load = cad.Image.can_load(test_file)
            print(f"可以加载: {can_load}")
            
        except Exception as e:
            print(f"检查文件格式失败: {e}")
        
        # 方法4：尝试导入CAD实体类
        print("\n方法4：尝试导入CAD实体类")
        try:
            from aspose.cad.fileformats.cad.cadobjects import CadLine
            print("成功导入CadLine")
            
            # 尝试创建CadLine实例
            line = CadLine()
            print(f"成功创建CadLine实例: {type(line).__name__}")
            
            # 检查CadLine的属性
            line_attrs = [attr for attr in dir(line) if not attr.startswith('_')]
            print(f"CadLine可用属性: {line_attrs}")
            
        except Exception as e:
            print(f"导入CadLine失败: {e}")
        
    except Exception as e:
        print(f"调试失败: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    main()
