dim infoText as string
	infoText = "mkArrange version 2.0.0"

dim buttonsColumns as array[string]
	buttonsColumns.Push("Left")
	buttonsColumns.Push("Center")
	buttonsColumns.Push("Right")
dim buttonsRows as array[string]
	buttonsRows.Push("Front")
	buttonsRows.Push("Middle")
	buttonsRows.Push("Back")
dim buttonsLevels as array[string]
	buttonsLevels.Push("Front")
	buttonsLevels.Push("Middle")
	buttonsLevels.Push("Back")

dim buttonsGrid as array[string]
	buttonsGrid.Push("Matrix")
	buttonsGrid.Push("Circle")

sub OnInitParameters()
	RegisterInfoText(infoText)
	RegisterParameterInt("elementsCount", "Elements Count", 51, 0, 500)
	RegisterParameterInt("columns", "Columns", 17, 1, 500)
	RegisterParameterInt("rows", "Rows", 3, 1, 500)
	RegisterParameterInt("offsetColumns", "Offset Columns", 26, -10000, 10000)
	RegisterParameterInt("offsetRows", "Offset Rows", 26, -10000, 10000)
	RegisterParameterInt("offsetLevels", "Offset Levels", 0, -10000, 10000)
	RegisterRadioButton("centerColumns", "Center Columns", 1, buttonsColumns)
	SetAnimateParameter(false)
	RegisterRadioButton("centerRows", "Center Rows", 1, buttonsRows)
	SetAnimateParameter(false)
	RegisterRadioButton("centerLevels", "Center Levels", 1, buttonsLevels)
	SetAnimateParameter(false)
	RegisterParameterDouble("radiusOne", "Radius 1", 150.0, 0.0, 10000.0)
	RegisterParameterDouble("radiusTwo", "Radius 2", 150.0, 0.0, 10000.0)
	RegisterParameterDouble("startAngle", "Start Angle", 0.0, 0.0, 360.0)
	RegisterParameterDouble("endAngle", "End Angle", 180.0, 0.0, 360.0)
	RegisterRadioButton("arrangeGrid", "Arrange Grid", 1, buttonsGrid)
	SetAnimateParameter(false)
	RegisterPushButton("rebuild", "Rebuild", 0)
end sub

sub OnExecAction(buttonId As Integer)
	Open = false
	if ChildContainerCount = 0 and GetParameterInt("elementsCount") > 0 then
		AddContainer(3)
	end if
	if ChildContainerCount > 0 then
		this.CreateElements()
	end if
	if GetParameterInt("elementsCount") < ChildContainerCount then
		do while ChildContainerCount > GetParameterInt("elementsCount")
			GetChildContainerByIndex(GetParameterInt("elementsCount")).Delete()
		loop
	end if
	if GetParameterInt("arrangeGrid") = 0 then
		this.ArrangeMatrix()
	else
		this.ArrangeCircle()
	end if
	Scene.UpdateSceneTree()
end sub

sub OnParameterChanged(parameterName As String)
	Select Case parameterName
		Case "elementsCount"
			for index = 1 to GetParameterInt("elementsCount")
				GetChildContainerByIndex(index - 1).Active = true
			next
			for index = GetParameterInt("elementsCount") to ChildContainerCount - 1
				GetChildContainerByIndex(index).Active = false
			next
		Case "columns", "rows", "offsetColumns", "offsetRows", "offsetLevels", "centerColumns", "centerRows", "centerLevels", "radiusOne", "radiusTwo", "startAngle", "endAngle"
			if GetParameterInt("arrangeGrid") = 0 then
				this.ArrangeMatrix()
			else
				this.ArrangeCircle()
			end if
			for index = 1 to GetParameterInt("elementsCount")
				GetChildContainerByIndex(index - 1).Active = true
			next
			for index = GetParameterInt("columns") * GetParameterInt("rows") to ChildContainerCount
'				GetChildContainerByIndex(index).Position.x = 0
'				GetChildContainerByIndex(index).Position.y = 0
'				GetChildContainerByIndex(index).Position.z = 0
				GetChildContainerByIndex(index).Active = false
			next
		Case "arrangeGrid"
			if GetParameterInt("arrangeGrid") = 0 then
				SendGuiParameterShow("centerColumns", 1)
				SendGuiParameterShow("centerRows", 1)
				SendGuiParameterShow("centerLevels", 1)
				SendGuiParameterShow("radiusOne", 0)
				SendGuiParameterShow("radiusTwo", 0)
				SendGuiParameterShow("startAngle", 0)
				SendGuiParameterShow("endAngle", 0)
				SendGuiRefresh()
				this.ArrangeMatrix()
			else
				SendGuiParameterShow("centerColumns", 0)
				SendGuiParameterShow("centerRows", 0)
				SendGuiParameterShow("centerLevels", 0)
				SendGuiParameterShow("radiusOne", 1)
				SendGuiParameterShow("radiusTwo", 1)
				SendGuiParameterShow("startAngle", 1)
				SendGuiParameterShow("endAngle", 1)
				SendGuiRefresh()
				this.ArrangeCircle()
			end if
	End Select
end sub

sub CreateElements()
	for index = 1 to GetParameterInt("elementsCount")
		GetChildContainerByIndex(index - 1).AddContainer(1)
		GetChildContainerByIndex(index - 1).Name = cStr(index - 1)
		if not Geometry = null then
			Geometry.Active = false
			if Geometry.Uuid.FullString = "<00000000-0000-0000-0000000000000000>" then
				GetChildContainerByIndex(index - 1).CreateGeometry("BUILT_IN*GEOM*"&Geometry.Name)
			else
				dim geomPath as string
				geomPath = System.SendCommand("#"&Geometry.VizId&"*LOCATION_PATH GET")
				GetChildContainerByIndex(index - 1).CreateGeometry(geomPath)
			end if
		end if
		GetChildContainerByIndex(index - 1).CreateMaterial()
		GetChildContainerByIndex(index - 1).Scaling.xyz = 0.25
	next
end sub

sub ArrangeMatrix()
	if ChildContainerCount > 0 then
		dim index as integer
		for i = 0 to GetParameterInt("columns") - 1
			for j = 0 to GetParameterInt("rows") - 1
				GetChildContainerByIndex(index).Position.x = cDbl(GetParameterInt("offsetColumns") * i)
				GetChildContainerByIndex(index).Position.y = cDbl(GetParameterInt("offsetRows") * j)
				GetChildContainerByIndex(index).Position.z = cDbl(GetParameterInt("offsetLevels") * j)
				Select Case GetParameterInt("centerColumns")
					Case 0
						Center.x = 0
					Case 1
						Center.x = -cDbl(GetParameterInt("offsetColumns") * i) / 2
					Case 2
						Center.x = -cDbl(GetParameterInt("offsetColumns") * i)
				End Select
				Select Case GetParameterInt("centerRows")
					Case 0
						Center.y = 0
					Case 1
						Center.y = -cDbl(GetParameterInt("offsetRows") * j) / 2
					Case 2
						Center.y = -cDbl(GetParameterInt("offsetRows") * j)
				End Select
				Select Case GetParameterInt("centerLevels")
					Case 0
						Center.z = 0
					Case 1
						Center.z = -cDbl(GetParameterInt("offsetLevels") * j) / 2
					Case 2
						Center.z = -cDbl(GetParameterInt("offsetLevels") * j)
				End Select
				GetChildContainerByIndex(index).Rotation.z = 0.0
				index = index + 1
			next
		next
	end if
end sub

dim pi as double = 3.1415926

sub ArrangeCircle()
	if ChildContainerCount > 0 then
		dim index as integer
		for i = 0 to GetParameterInt("columns") - 1
			dim angle as double
			angle = -(GetParameterDouble("endAngle") - GetParameterDouble("startAngle")) * cDbl(i) * pi / cDbl(GetParameterInt("columns") - 1) / 180.0 - (GetParameterDouble("startAngle") + cdbl(i) * cDbl(1 / GetParameterInt("columns"))) * pi / 180.0
			for j = 0 to GetParameterInt("rows") - 1
				GetChildContainerByIndex(index).Position.x = cDbl(GetParameterInt("offsetColumns") * j) * -cos(angle) + (GetParameterDouble("radiusOne") * -cos(angle)) + cDbl(GetParameterInt("offsetColumns") * i) * 0.0
				GetChildContainerByIndex(index).Position.y = cDbl(GetParameterInt("offsetRows") * j) * -sin(angle) + (GetParameterDouble("radiusTwo") * -sin(angle)) + cDbl(GetParameterInt("offsetRows") * j) * 0.0
				GetChildContainerByIndex(index).Position.z = cDbl(GetParameterInt("offsetLevels") * j)
				Center.xyz = 0.0
				GetChildContainerByIndex(index).Rotation.z = angle * 180.0 / pi + cDbl(i) * 0.0 / cDbl(GetParameterInt("columns") - 1)
				index = index + 1
			next
		next
	end if
end sub