Option Explicit

Dim INPUTFile, WRLFile
Dim CATIA, doc, fso, extension
Dim isPartOrProduct

' -------------------------
' Dosya parametresi (sürükle-bırak)
' -------------------------
If WScript.Arguments.Count = 0 Then
    WScript.Echo "Lutfen dosyayi surukleyip bu script'in uzerine birakiniz."
    WScript.Quit
End If

INPUTFile = WScript.Arguments(0)

' -------------------------
' FSO ve dosya kontrolü
' -------------------------
Set fso = CreateObject("Scripting.FileSystemObject")

If Not fso.FileExists(INPUTFile) Then
    WScript.Echo "Dosya bulunamadi: " & INPUTFile
    WScript.Quit
End If

WRLFile = fso.GetParentFolderName(INPUTFile) & "\" & fso.GetBaseName(INPUTFile) & ".wrl"
extension = LCase(fso.GetExtensionName(INPUTFile))

' -------------------------
' CATIA COM
' -------------------------
On Error Resume Next
Set CATIA = GetObject(, "CATIA.Application")
If Err.Number <> 0 Then
    Err.Clear
    Set CATIA = CreateObject("CATIA.Application")
    CATIA.Visible = True
End If
On Error GoTo 0

' -------------------------
' Dosya aç
' -------------------------
Set doc = CATIA.Documents.Open(INPUTFile)

' -------------------------
' Part/Product kontrolü
' -------------------------
isPartOrProduct = False
On Error Resume Next

' PartDocument kontrolü
Dim dummy
Set dummy = doc.Part
If Err.Number = 0 Then isPartOrProduct = True
Err.Clear

' ProductDocument kontrolü
Set dummy = doc.Product
If Err.Number = 0 Then isPartOrProduct = True
Err.Clear

On Error GoTo 0

If Not isPartOrProduct Then
    WScript.Echo "Dosya ExportData icin uygun degil (Part veya Product olmasi lazim): " & INPUTFile
    doc.Close
    WScript.Quit
End If

' -------------------------
' WRL export
' -------------------------
On Error Resume Next
doc.ExportData WRLFile, "VRML"
If Err.Number <> 0 Then
    WScript.Echo "WRL export hatasi: " & Err.Description
    doc.Close
    WScript.Quit
End If
On Error GoTo 0

doc.Close
WScript.Echo "Islem tamamlandi: " & WRLFile
