Language="VBSCRIPT"

Sub CATMain()
CATIA.DisplayFileAlerts = FALSE
Dim productDocument1 As Document
Set productDocument1 = CATIA.ActiveDocument

Dim product1 As Product
Set product1 = productDocument1.Product

'******** Behavior == VBScriptImplementation
' NO error 
'On Error Resume Next

Dim CurSelection As Selection
set CurSelection = productDocument1.Selection

Dim VBScriptBeh As Behavior
set VBScriptBeh = CurSelection.FindObject("CATIABehaviorVBScript")

'msgbox " OK pour recuperer le behavior" 
'******** Start 

if VBScriptBeh Is Nothing then
'        MsgBox "Not Started From A Behavior"
else
	'vbscript is started 
	VBScriptBeh.Start 

	' -- Behavior -- 
	' how to access an input and use it for valuating the offset parameter of constraint 
	Dim IN_PartDocName
	IN_PartDocName = VBScriptBeh.TestInput("PartDocName")
	if( IN_PartDocName ) then
'		msgbox " PartDocName exists"
		Set PartDocName = VBScriptBeh.GetInput("PartDocName")

     
        Set oFileSys = CATIA.FileSystem
		FilePath = CATIA.SystemService.Environ("CATTemp") &  oFileSys.FileSeparator &  PartDocName.value & ".CATPart"
		
		Set LastDoc = CATIA.Documents.Item(CATIA.Documents.Count)
'		msgbox "Last document's current name = " & LastDoc.FullName

' --------
' Implement this to specify file name and location each run time
'		FilePath = CATIA.FileSelectionBox("Select a file", "*.CATPart", CatFileSelectionModeOpen)
' --------

' --------
' Implement this to delete the part file if it already exists.  This eliminates a confirmation dialog each time
		Set FS = CATIA.FileSystem
		If FS.FileExists(FilePath) Then
			FS.DeleteFile(FilePath)
		End if
' --------

		LastDoc.SaveAs(FilePath)
'		msgbox "Last document's new name = " & LastDoc.FullName
	else
'		msgbox " PartDocName does not exist ", 64
	end if

	VBScriptBeh.Done 
end if 

End Sub
