'/// ScaleRef 1.0 for XSI 3.5+
'/// By Homam Bahnassi - In|Framez 2004
'--------------------------------------

set oSelection = GetValue("SelectionList")

disMode = msgbox("specify distance numericlly?", vbYesNo, "ScaleRef")
if disMode = 6 then
	ScaleRef oSelection,summed_distance,NewLength
else
	ScaleRef oSelection,summed_distance,summed_distance
end if

'-------------------------------------------------
' ScaleRef
'-------------------------------------------------

function ScaleRef(oInObjs,fDist,fLength)
	ScaleRef = false

	dim fScale,oObj
		
	if typename(fLength) = "Boolean" then
		logmessage "ScaleRef: Invaled input..."
		exit function
	end if
		
	fScale = fLength/fDist

	for each oObj in oInObjs
		oObj.sclx.value = oObj.sclx.value * fScale
		oObj.scly.value = oObj.scly.value * fScale
		oObj.sclz.value = oObj.sclz.value * fScale
	next
end function

'-------------------------------------------------
' summed distance by Michael Isner modified by Homam Bahnassi
'-------------------------------------------------

function summed_distance

	Dim l_obj1, l_obj2, x1,y1,z1, x2,y2,z2, dx,dy,dz, l_button

	count = 0	

	last_x = 0 
	last_y = 0
	last_z = 0
	
	dx = 0
	dy = 0
	dz = 0
	
	sum = 0 
	
	' Continue looping until until a right click
	
	do	
	
		PickPosition "Pick position (Ctrl: Snapping)", "Pick position (Ctrl: Snapping)", x1, y1, z1, l_button
		if l_button = 0 then
			exit do 
		end if
	
		if count > 0 then	
			
			dx = abs(last_x - x1)
			dy = abs(last_y - y1)			
			dz = abs(last_z - z1)	
			
			dist = sqr(dx*dx + dy*dy + dz*dz)
	
		end if
		
		last_x = x1
		last_y = y1
		last_z = z1
		
		sum = sum + dist
	
		count = count + 1
		
	loop	
	
	summed_distance = sum
	
end function

function NewLength()

	Dim oDial, OptionSliders
	NewLength = False
	
	set oRoot = application.activeproject.activescene.root
	set oDial = AddProp("Custom_parameter_list", oRoot, , "New Length").Value("Value")
	OptionSliders = oRoot & "." & oDial
	
	SIAddCustomParameter OptionSliders, "ln", siFloat, 10, 0.000, 1000.000,, siPersistable+siSilent  , 0.000, 360.000,, "Abs Length"

	On Error Resume Next
	InspectObj OptionSliders ,,"Scale Ref",SIModal

	If Err.Number <> 0 Then
		DeleteObj OptionSliders
		Exit Function
	End If

	NewLength = GetValue(OptionSliders & ".ln")

	DeleteObj OptionSliders

end function
