' Toggle Faceted 
' by Michael Donovan June 2001

' Intended as a modeling tool.

' Toggles Discontinuity from the current value to 0 and back again. The script is able to recall the set dicontinuity
' by creating a custom parameter "OldDiscontinuity" and storing the old value there. 
' You can set this custom paramter to alter the smoothing angle further.

Dim objSelected, NewValue, CurrentValue, OldValue,list

dim MyList
set MyList = GetValue( "SelectionList" )

NewValue=0
CurrentValue=0
OldValue=0

Select Case MyList.Count 
	Case  0
		msgbox "You must have at least one object selected.",,"Toggle Faceted"
	Case  1	
		If MyList(0).Type <> "polymsh" Then
			msgbox "Selected object must be polygonal"
		Else	
			SetDiscontinuity
		End If		
	Case Else
		msgbox "This script only works with one object selected at time.",,"Toggle Faceted"
End Select				

Sub SetDiscontinuity
   
	Set objSelected = SIFilter( null, "sceneobject", true, siQuickSearch )
                  
	If typename(objSelected) = "Nothing" Then
		objSelected="Nothing"
		LogMessage "Invalid selection : ", siError
		Exit Sub
	Else
	    CurrentValue = GetValue (objSelected & ".geomapprox.gapproxmoan")
		
		If CurrentValue > 0 Then
			on error resume next
			SetValue objSelected & ".CustomPset.OldDiscontinuityValue", CurrentValue
				
				if err.Number <> 0 then
					MakeLocal objSelected &".geomapprox", siDefaultPropagation
					SIAddCustomParameter objSelected, "OldDiscontinuityValue", siDouble, 0.000, 0.000, 100.000, , 4, 0.000, 100.000	
				End if
				
			SetValue objSelected & ".CustomPSet.OldDiscontinuityValue", CurrentValue
			NewValue = 0
		Else
			NewValue = GetValue (objSelected & ".CustomPSet.OldDiscontinuityValue") 			
			SetValue objSelected & ".CustomPSet.OldDiscontinuityValue", 0

		End If		
	End If
	
	SetValue objSelected & ".geomapprox.gapproxmoan", NewValue
 
End Sub
