#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Debug script to investigate weight calculation issues for IPart
"""
import sys
import os
from win32com.client import Dispatch

# Proje yolunu ekle
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from IFikatia import IFikatia
from IPartDokuman import IPartDokuman

def debug_part_properties():
    """
    Açık olan CATIA dökümanının Part nesnesi özelliklerini incele
    """
    try:
        # CATIA COM aplikasyonuna bağlan
        catia_app = Dispatch("Catia.Application")
        print(f"✓ CATIA bağlandı: {catia_app}")

        # IFikatia wrapper'ı oluştur
        fikatia = IFikatia(obj=catia_app)
        print(f"✓ IFikatia oluşturuldu")

        # Aktif dökümanı al
        dokuman = fikatia.aktifDokuman
        if not dokuman:
            print("✗ Aktif döküman bulunamadı!")
            return

        print(f"✓ Döküman: {dokuman.ismi}")
        print(f"  - Tür: {dokuman.turu}")
        print(f"  - Tam yol: {dokuman.tamIsmi}")

        # COM nesnesini al
        doc_com = dokuman.catNesne
        print(f"\n[DOC_COM ÖZELLİKLERİ]")
        print(f"  - Tür: {type(doc_com)}")

        # GetTotalMass() metodunu dene
        if hasattr(doc_com, 'GetTotalMass'):
            try:
                mass = doc_com.GetTotalMass()
                print(f"  ✓ doc_com.GetTotalMass() = {mass}")
            except Exception as e:
                print(f"  ✗ doc_com.GetTotalMass() hatası: {e}")
        else:
            print(f"  ✗ doc_com.GetTotalMass() methodu yok")

        # Mass özelliğini dene
        if hasattr(doc_com, 'Mass'):
            try:
                mass = doc_com.Mass
                print(f"  ✓ doc_com.Mass = {mass}")
            except Exception as e:
                print(f"  ✗ doc_com.Mass hatası: {e}")
        else:
            print(f"  ✗ doc_com.Mass özelliği yok")

        # Part özelliğini kontrol et
        if hasattr(doc_com, 'Part'):
            part_com = doc_com.Part
            print(f"\n[PART_COM ÖZELLİKLERİ]")
            print(f"  - Tür: {type(part_com)}")

            # Part özelliklerini listele
            properties = [prop for prop in dir(part_com) if not prop.startswith('_')]

            # İlgili özellikler
            for prop in ['Volume', 'Density', 'Mass', 'GetTotalMass']:
                if hasattr(part_com, prop):
                    try:
                        if callable(getattr(part_com, prop)):
                            value = getattr(part_com, prop)()
                            print(f"  ✓ part_com.{prop}() = {value}")
                        else:
                            value = getattr(part_com, prop)
                            print(f"  ✓ part_com.{prop} = {value}")
                    except Exception as e:
                        print(f"  ✗ part_com.{prop} hatası: {e}")
                else:
                    print(f"  ✗ part_com.{prop} yok")

            # PartDocument'in Part özelliğini kontrol et
            print(f"\n[PART SPESIFIK KOMUTLARI]")

            # UpdateInformation() methodunu dene
            if hasattr(part_com, 'UpdateInformation'):
                try:
                    part_com.UpdateInformation()
                    print(f"  ✓ part_com.UpdateInformation() çağrıldı")
                except Exception as e:
                    print(f"  ✗ part_com.UpdateInformation() hatası: {e}")

        # IPartDokuman wrapper'ını dene
        print(f"\n[IPARTDOKUMAN WRAPPER]")
        try:
            part_dokuman = dokuman.asPartDokuman
            print(f"  ✓ asPartDokuman dönüştürüldü")

            # IPart wrapper'ını al
            part_wrapper = part_dokuman.part
            print(f"  ✓ part wrapper oluşturuldu: {type(part_wrapper)}")

            # Yogunluk özelliğini dene
            try:
                yogunluk = part_wrapper.yogunluk
                print(f"  ✓ part_wrapper.yogunluk = {yogunluk}")
            except Exception as e:
                print(f"  ✗ part_wrapper.yogunluk hatası: {e}")

        except Exception as e:
            print(f"  ✗ IPartDokuman dönüştürme hatası: {e}")

    except Exception as e:
        print(f"✗ Hata: {e}")
        import traceback
        traceback.print_exc()

if __name__ == '__main__':
    debug_part_properties()
