Option Explicit
'Script written by Will Sixel
'Script copyrighted by Intact Solutions LLC
'Script version Friday, June 12, 2015 12:23:24 PM

Call Main()
Sub Main()

	'Declare:'
	'Objects'
	Dim outerCyl, innerCyl, tube, tubeCP, objSnSPlugIn
	'Values'
	Dim innerRad, outerRad, Length, basePt, endPt
	'SnS variables'
	Dim SamplePt, StressVM, loading, resolution, targetVMStress, radStep, stepCount, yPos
	
	'Set the initial cyllinder geometry'
	innerRad = 3
	outerRad = 5
	length = 25
	basePt = Array(0, 0, 0)
	endPt = Array(length, 0, 0)
	
	'Set SnS Conditions'
	SamplePt = Array(1, 0, 5)
	loading = Array(0, 0, -100)
	resolution = 10000
	targetVMStress = 50	'in MPa'
	radStep = 0.25				'desired step size for radius'
	stepCount = 0

	

	'Set up SnSScript plugin'
	Set objSnSPlugIn = Rhino.GetPluginObject("SnSScript")
	
	
	'Optimization loop'
	Do 
		stepCount = stepCount + 1
		'Create outer cyllinder, based at origin'
		outerCyl = Rhino.AddCylinder(basePt, endPt, outerRad)
		innerCyl = Rhino.AddCylinder(basePt, endPt, innerRad)
		
		'Create the hollow cyllinder through boolean difference'
		tube = Rhino.BooleanDifference(Array(outerCyl), Array(innerCyl), True)
		
		yPos = -stepCount * 2.5 * outerRad
		SamplePt = Array(1, yPos, outerRad)
		
		Call Rhino.MoveObject(tube(0), Array(0, 0, 0), Array(0, yPos, 0))

		
		'Set the required SnS values for material, restraint, load, and then solve'
		Call objSnSPlugIn.SetMaterial(tube(0), "Aluminum 6061")
		Call objSnSPlugIn.AddFaceRestraint(tube(0), "resBot", 1, "xyz")
		Call objSnSPlugIn.AddFaceVectorLoad(tube(0), "endLoad", 2, loading, False)
		Call objSnSPlugIn.Solve(tube(0), resolution)
	
		'Query the von Mises stress value at desired point.'
		StressVM = objSnSPlugIn.QuerySolutionValue(tube(0), "VONM", SamplePt)
		
		'Increment the inner radius value'
		innerRad = innerRad + radStep


		'Enable display of the solution'
		Call objSnSPlugIn.SolutionDisplayEnable(tube(0), "DGRVM", 10)
				

		'Start again with the increaed inner radius'
	Loop While StressVM < targetVMStress
	
	
	'Report the values that caused failure'
	Call Rhino.MessageBox("Stress value: " & StressVM & ", Inner Radius: " & innerRad)

	
End Sub