' =========================================================================
'  CATIA STP -> WRL Converter Macro
'  FikatiaV2024 - Automatic Batch Conversion System
' =========================================================================
'
'  Bu makro CATIA tarafından otomatik olarak çalıştırılır
'  Komut satırından parametreler alır ve STP dosyasını WRL'e çevirir
'
'  Kullanım:
'    CATSTART.exe -run "CNEXT.exe" -macro "ConvertMacro.catvbs" "input.stp" "output.wrl"
'
' =========================================================================

Option Explicit

' Log dosyası için değişkenler
Dim logPath, fso, logFile

' Script parametrelerini al
Dim args
Set args = WScript.Arguments

' Log sistemi başlat
Set fso = CreateObject("Scripting.FileSystemObject")
logPath = fso.GetParentFolderName(WScript.ScriptFullName) & "\conversion.log"

' Log dosyasını aç (append mode)
On Error Resume Next
Set logFile = fso.OpenTextFile(logPath, 8, True)
If Err.Number <> 0 Then
    Set logFile = fso.CreateTextFile(logPath, True)
End If
On Error GoTo 0

' Log fonksiyonu
Sub WriteLog(message)
    If Not logFile Is Nothing Then
        logFile.WriteLine Now & " | " & message
    End If
End Sub

' =========================================================================
' ANA İŞLEM BAŞLANGICI
' =========================================================================

WriteLog "=========================================="
WriteLog "CATIA STP->WRL Conversion Started"
WriteLog "=========================================="

' Parametre kontrolü
If args.Count < 2 Then
    WriteLog "ERROR: Insufficient parameters"
    WriteLog "Usage: ConvertMacro.catvbs <input.stp> <output.wrl>"
    WScript.Echo "HATA: STP ve WRL dosya yolu verilmedi!"
    WScript.Echo "Kullanim: ConvertMacro.catvbs input.stp output.wrl"
    WScript.Quit 1
End If

Dim stepPath, wrlPath
stepPath = args.Item(0)
wrlPath = args.Item(1)

WriteLog "Input STP : " & stepPath
WriteLog "Output WRL: " & wrlPath

' Dosya varlık kontrolü
If Not fso.FileExists(stepPath) Then
    WriteLog "ERROR: Input file not found: " & stepPath
    WScript.Echo "HATA: Kaynak dosya bulunamadi: " & stepPath
    WScript.Quit 1
End If

WriteLog "Input file verified: " & fso.GetFile(stepPath).Size & " bytes"

' =========================================================================
' CATIA BAĞLANTISI
' =========================================================================

Dim CATIA
On Error Resume Next

WriteLog "Attempting to connect to existing CATIA instance..."
Set CATIA = GetObject("", "CATIA.Application")

If Err.Number <> 0 Then
    WriteLog "No existing CATIA found, creating new instance..."
    Err.Clear
    Set CATIA = CreateObject("CATIA.Application")

    If Err.Number <> 0 Then
        WriteLog "ERROR: Failed to create CATIA instance: " & Err.Description
        WScript.Echo "HATA: CATIA baslatilamadi!"
        WScript.Quit 1
    End If

    CATIA.Visible = False
    WriteLog "New CATIA instance created (invisible mode)"
Else
    WriteLog "Connected to existing CATIA instance"
End If

On Error GoTo 0

' CATIA ayarları
On Error Resume Next
CATIA.DisplayFileAlerts = False
CATIA.Interactive = False
WriteLog "CATIA settings configured (no alerts, non-interactive)"
On Error GoTo 0

' =========================================================================
' DOSYA AÇMA
' =========================================================================

WriteLog "Opening STP file..."
Dim doc
On Error Resume Next
Set doc = CATIA.Documents.Open(stepPath)

If Err.Number <> 0 Or doc Is Nothing Then
    WriteLog "ERROR: Failed to open STP file: " & Err.Description
    WScript.Echo "HATA: STP dosyasi acilamadi!"
    On Error GoTo 0
    CATIA.Quit
    WScript.Quit 1
End If
On Error GoTo 0

WriteLog "STP file opened successfully: " & doc.Name

' =========================================================================
' WRL EXPORT
' =========================================================================

WriteLog "Starting WRL export..."

' Eğer çıktı dosyası varsa sil
If fso.FileExists(wrlPath) Then
    WriteLog "Deleting existing WRL file..."
    fso.DeleteFile wrlPath, True
End If

On Error Resume Next
doc.ExportData wrlPath, "wrl"

If Err.Number <> 0 Then
    WriteLog "ERROR: WRL export failed: " & Err.Description
    WScript.Echo "HATA: WRL export basarisiz!"
    doc.Close
    CATIA.Quit
    WScript.Quit 1
End If
On Error GoTo 0

WriteLog "WRL export completed"

' Çıktı dosyası kontrolü
If fso.FileExists(wrlPath) Then
    Dim wrlSize
    wrlSize = fso.GetFile(wrlPath).Size
    WriteLog "WRL file created successfully: " & wrlSize & " bytes"
    WScript.Echo "BASARILI: WRL dosyasi olusturuldu (" & FormatNumber(wrlSize, 0) & " bytes)"
Else
    WriteLog "WARNING: WRL file not found after export"
    WScript.Echo "UYARI: WRL dosyasi bulunamadi!"
End If

' =========================================================================
' TEMİZLİK
' =========================================================================

WriteLog "Closing document..."
doc.Close

WriteLog "Shutting down CATIA..."
CATIA.Quit

WriteLog "=========================================="
WriteLog "Conversion completed successfully"
WriteLog "=========================================="
WriteLog ""

' Log dosyasını kapat
logFile.Close

WScript.Quit 0
