Option Explicit
'Script by Mitch Heynick
'Version 8 December 2013
'added a user-defined start point
'added possibility to unroll curves

Private oldAddNumbers,oldSpaceVal
If IsEmpty(oldAddNumbers) Then oldAddNumbers = True
If IsEmpty(oldSpaceVal) Then oldSpaceVal = 1.0

Call MultiUnroll()
Sub MultiUnroll()
	
	Dim arrobjs,arrBool,dblSpace,arrNewStPt,arrCrvs
		
	arrObjs = Rhino.GetObjects("Select surfaces/polysurfaces to unroll", 8 + 16,, True)
	If Not Isarray(arrObjs) Then Exit Sub
			
	arrCrvs = Rhino.GetObjects("Select curves to unroll with surfaces", 4)
	
	arrBool = Rhino.GetBoolean("Number objects?", Array("Number", "No", "Yes"), Array(oldAddNumbers))
	If Not IsArray(arrBool) Then Exit Sub
		
	arrNewStPt = Rhino.GetPoint("Start point for unrolls - press Enter for world 0")	
	If IsNull(arrNewStPt) Then arrNewStPt = Array(0, 0, 0)

	dblSpace = Rhino.GetReal("Spacing between unrolls?", oldSpaceVal, 0)
	If Not IsNumeric(dblSpace) Then Exit Sub
	
	oldAddNumbers = arrBool(0)
	oldSpaceVal = dblSpace

	Dim arrCtr,strComm,arrURObjs,tempGroup,i
	Dim dot,group,arrBB,noUnroll,count
	noUnroll = 0 : count = 0
	
	strComm = "-_UnrollSrf _Explode=_No Labels=_No"
	If IsArray(arrCrvs) Then
		tempGroup = Rhino.AddGroup()
		Call Rhino.AddObjectsToGroup(arrCrvs, tempGroup)
		strComm = strComm & " _-SelGroup " & tempGroup & " _Enter"
	End If	
	
	Call Rhino.EnableRedraw(False)	
	For i=0 To UBound(arrObjs)
		Call Rhino.UnselectAllObjects		
		arrCtr = BBCP(arrObjs(i))
		If IsArray(arrCtr) Then			
			Call Rhino.SelectObject(arrObjs(i))
			Call Rhino.Command(strComm & " _Enter", False)		
			arrURObjs = Rhino.LastCreatedObjects
			
			If IsArray(arrURObjs) Then
				If arrBool(0) Then
					'group orig obj and number dot
					dot = Rhino.AddTextDot(Cstr(i + 1), arrCtr)
					Call Rhino.ObjectLayer(dot, Rhino.ObjectLayer(arrObjs(i)))
					group = Rhino.AddGroup()
					Call Rhino.AddObjectsToGroup(Array(arrObjs(i), dot), group)					
				End If
				
				arrBB = Rhino.BoundingBox(arrURObjs)
				Call Rhino.MoveObjects(arrURObjs, arrBB(0), arrNewStPt)			
				arrNewStPt(0) = arrNewStPt(0) + arrBB(1)(0) - arrBB(0)(0) + dblSpace
				arrCtr = BBCP(arrURObjs)
				
				If arrBool(0) Then
					'add number dot to unrolled objects and group
					dot = Rhino.AddTextDot(Cstr(i + 1), arrCtr)
					group = Rhino.AddGroup()
					Call Rhino.AddObjectsToGroup(arrURObjs, group)
					Call Rhino.AddObjectToGroup(dot, group)	
				End If
				count = count + 1
			Else
				noUnroll = noUnroll + 1
			End If			
		End If		
	Next
	If Not IsEmpty(tempGroup) Then Call Rhino.DeleteGroup(tempGroup)
	Call Rhino.UnselectAllObjects
	Call Rhino.EnableRedraw(True)
	Call Rhino.Print("Sucessfully unrolled " & count & " objects")
	If noUnroll > 0 Then
		Call Rhino.Print("Unable to unroll " & noUnroll & " objects")
	End If
End Sub

Private Function BBCP(ByVal strObj)
	BBCP = Null
	Dim arrBB
	arrBB = Rhino.BoundingBox(strObj)
	If IsArray(arrBB) Then
		BBCP = Rhino.PointDivide(Rhino.PointAdd(arrBB(0), arrBB(6)), 2)
	End If	
End Function