Index out of Range Error

Index out of Range Error

erobinsonMRQE2
Contributor Contributor
1,945 Views
1 Reply
Message 1 of 2

Index out of Range Error

erobinsonMRQE2
Contributor
Contributor

I am working on rule to insert virtual parts into an assembly based on known user parameters and a list of possible virtual parts. 

 

I am encountering the an issue at the end of the rule where I get the error

Error in rule: Erase, in document: VirtualPartsModelAssembly.iam

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

 

The rule runs and successfully creates the parts but shows the error at the end.

'Sets up Document
Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = oAssyDoc.ComponentDefinition
Dim oOccs As ComponentOccurrences
oOccs = oAssyDoc.ComponentDefinition.Occurrences


'Deletes any existing virtual parts
For Each oOcc In oOccs
	If Not oOcc.suppressed Then 
	If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
		oOcc.Delete
	End If
End If	
Next





'Defines Parts to Look at
'Dim PartList As New ArrayList
'PartList.Add("Lid") '0
'PartList.Add("Module") '1
'PartList.Add("ModuleFrame") '2
'Logger.Trace("PartList")



'Virtual parts
Dim VirtList As New ArrayList
VirtList.Add("Bolt") '0
VirtList.Add("Nut") '1
VirtList.Add("Washer") '2
VirtList.Add("Gasket") '3
Logger.Trace("Virt List")



Dim m As Integer
If OutletModuleQty = 1 Then
	'Defines the number of modules

	m = Outlet1LHQty + Outlet1RHQty 'User specified number of parts
	Logger.Trace("OutletQTY")
End If




'Defines number of virtual parts
Dim iQty As Integer
iQty = m
Logger.Trace("iQty")
'Defines Virtual Part to create 
Dim oString As String

For Each oString In VirtList
	Vstart :
	vPart = VirtList.Item(i)
	i += 1
	Logger.Trace(i & "VirtList")

	'Defines location for Virt Part
	Dim identity As Matrix
	identity = ThisApplication.TransientGeometry.CreateMatrix()
	Dim index As Integer
	index = 2
	Logger.Trace(index)
	If vPart = "Washer" Then
		'Loops back to bypass creating a virtual part
		GoTo VStart
	Else
		iQty = m 'defines number of virtual parts
	End If
	'create first instance of the virtual part
	Dim virtOcc As ComponentOccurrence
	virtOcc = oOccs.AddVirtual(vPart, identity)

	'add next instance starting at instance2 (if applicable)



	Do While index <= iQty
		oOccs.AddByComponentDefinition(virtOcc.Definition, identity)
	index += 1
	Logger.Trace(index & "Index")
	Logger.Trace(iQty & "iQTY")
	Loop
	
	
Next	

 

0 Likes
Accepted solutions (1)
1,946 Views
1 Reply
Reply (1)
Message 2 of 2

Justin.B.
Enthusiast
Enthusiast
Accepted solution

You're accessing each string in VirtList with a For Each loop, and also using i to access each VirtList.Item. But then:

If vPart = "Washer" Then
	GoTo VStart

at this point you're still in the same "Washer" step of your For Each, and i += 1 gets run a second time. vPart is no longer "Washer", and you move on to the next item. But now i is out of sync with the For Each loop, which moves on from "Washer", and increases i again. Since i is one ahead, VirList.Item(i) is trying to access an item past the end of the list, and giving an index out of range error.

 

If your intention is to bypass "Washer", then instead of going back to VStart try wrapping everything in:

If vPart = "Washer" Then
	'Do nothing
Else
'Run your code