-- * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-- *  Set length of spline sub-objects to the specified  *
-- *  value, rescaling from their first vertex.          *
-- *  GARP - 2009                                        *
-- * * * * * * * * * * * * * * * * * * * * * * * * * * * *

(
global rltSetSplinesLength
try destroyDialog rltSetSplinesLength catch()

rollout rltSetSplinesLength "Set Splines Length" width:150
(
	spinner spnLength "Length: " range:[0, 10000, 10] align:#center
	fn filterShape obj = superClassOf obj == Shape
	pickButton btnShape "Pick Shape" width:104 align:#center filter:filterShape
	checkBox chkSel "Use Spline Selection" checked:true
	
	on btnShape picked obj do undo "Set Splines Length" on
	(
		local len = spnLength.value
		convertToSplineShape obj
		local aSS = getSplineSelection obj
		
		fn resizeSpline obj si len =
		(
			m = len / (curveLength obj si)
			for vi = 1 to numKnots obj si do
			(
				try 
				(
					setKnotPoint obj si vi (m * getKnotPoint obj si vi + (1 - m) * getKnotPoint obj si 1)
					if getKnotType obj si vi == #bezier or getKnotType obj si vi == #bezierCorner do
					(
						setInVec obj si vi (m * getInVec obj si vi + (1 - m) * getKnotPoint obj si 1)
						setOutVec obj si vi (m * getOutVec obj si vi + (1 - m) * getKnotPoint obj si 1)
					)
				)
				catch messageBox "Current Length is null.\nCan't perform."
			)

		)
		
		if chkSel.checked and aSS.count != 0 then for si in aSS do resizeSpline obj si len
		else for si = 1 to numSplines obj do resizeSpline obj si len
		
		updateShape obj
		completeRedraw()
	)
)

createDialog rltSetSplinesLength
)