Creating / Switching Arrays in a For Loop

Creating / Switching Arrays in a For Loop

dutt.thakar
Collaborator Collaborator
1,885 Views
5 Replies
Message 1 of 6

Creating / Switching Arrays in a For Loop

dutt.thakar
Collaborator
Collaborator

This is more sort of a Programming question, but I was testing one thing in iLogic and I was having hard time to figure out how to accomplish this. See below code and explanation on what I am trying to achieve.

 

I have created two Arrays named as Occ1() and Occ2() as Doubles, and What I want to do it as I am iterating to two occurrences in my assembly, I would like to switch the arrays from Occ1() to Occ2() to save values of Points in two arrays. Lines which I think needs modification are added as Bold.

 

So the case is basically I have only two occurrences in my assembly and I am trying to get some geometrical data from both of them, and my intention is when my for loop iterates, first it will check my first occurrence and it saves the point data in Occ1() array and when it starts iterating the second occurrence, it saves that data in Occ2() array. I am not sure how to declare that or how to switch that.

 

Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
Dim oSS As SelectSet = oDoc.SelectSet
Dim oOcc As ComponentOccurrence
Dim Occ1(0) As Double
Dim Occ2(0) As Double

For Each oOcc In oDef.Occurrences
	Dim oPDef As PartComponentDefinition = oOcc.Definition
	Dim oBody As SurfaceBody = oPDef.SurfaceBodies.Item(1)
	i = 0
	For Each oFace As Inventor.Face In oBody.Faces
	'oSS.Clear
		If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface
			Dim oFP As FaceProxy
			oOcc.CreateGeometryProxy(oFace, oFP)
			oSS.Select(oFP)
			Dim oCylinder As Cylinder = oFP.Geometry
			ReDim Preserve Occ1(i+1)
			Occ1(i) = Math.Round(oCylinder.BasePoint.X * 10, 0) 'In the next iteration I would like to make this Occ2(i), to save the values.
			i = i+1
		End If
	Next
Next

 

Any help on this will be highly appreciated. I would also welcome another suggestions apart from using two arrays to accomplish the same. My intention is to compare then these two array values.

If this answer has solved your problem please ACCEPT SOLUTION and hit like if you found it helpful..!


Regards,
Dutt Thakar
LinkedIn
0 Likes
Accepted solutions (2)
1,886 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @dutt.thakar.  Arrays are very useful and can be pretty dynamic, but I often avoid them when it comes to more complicated situations, because they can be harder to manage than some of the alternatives.  I'm not sure why you are using single value sized array's of Double, instead of simple Double variables, or why you may need two variables instead of one list/array of doubles, but I'm sure you have something in mind.  Depending on how you want to be able to use the retrieved data, there are a few alternative variable types I would recommend that may be easier to use for this situation.  If you would like to use a single variable that can hold both values, and want to keep it simple (one input variable per assignment) you could use something like a List(Of Double).  And if order is important you could also use either a Queue(Of Double), or a Stack(Of Double), instead of the List.  But if you want to have more control and be sure of what values belong to which objects, you could use a two factor variable like NameValueMap or a Dictionary(Of String, Double), where the String could be the name of the component.

I know that this is a custom testing situation, and you know exactly how many components there are and how many cylindrical faces they have, but in theory, since it is looping through a faces collection at that point, it could find more than one cylindrical face and add it's coordinate data to the variable, because there is no insurance built in that it will only find one per occurrence.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

J-Camper
Advisor
Advisor
Accepted solution

Hello @dutt.thakar ,

 

Kind of blending what @WCrihfield mentioned, regarding the use of a List, into your current rule setup, you can create a list of double arrays.  Below is a simple sample where I fill a list with 3 Double arrays of varying Lengths:

 

Dim arrayList As New List(Of Double())

For i = 1 To 3
	Dim curDouble(0) As Double
	If i = 1
		For j = 0 To 5
			ReDim Preserve curDouble(j)
			curDouble(j) = j + 2
		Next
	Else If i = 2
		For j = 0 To 3
			ReDim Preserve curDouble(j)
			curDouble(j) = j + 5
		Next
	Else If i = 3
		For j = 0 To 8
			ReDim Preserve curDouble(j)
			curDouble(j) = j + 3
		Next
	End If	
	arrayList.Add(curDouble)
Next

Logger.Trace(arrayList.Count)
Logger.Trace(arrayList.Item(0).Count)
Logger.Trace(arrayList.Item(1).Count)
Logger.Trace(arrayList.Item(2).Count)

'retrieving Double array from list:
Dim getArray As Double() = arrayList.Item(0)
Logger.Trace(getArray.Count)

 

 

The main "For i = 1 To 3" Loop would be the equivalent of your "For Each oOcc In oDef.Occurrences" Loop.  The subsequent loops are simply there to show different length Double arrays. Each time your loop runs through an occurrence, a new empty Double array is filled with information.  Then before you move on to the next occurrence, you add the temporary Double into Your list.

 

I also show how to access those double arrays after the list is complete. 

 

Let me know if you have any questions, or if this is not working as intended.

0 Likes
Message 4 of 6

dutt.thakar
Collaborator
Collaborator

@J-Camper Your code was really helpful, and gave me insights on how I can use list with my code.  and @WCrihfield your explanation was quite helpful to understand different possibilities.

 

Thanks for your valuable answers, The list was quite useful. I have recreated my code with New arrayList (Of String ()) and then retrieving the data in separate arrays from List Items, One more thing, if I would like to create multiple arrays with a for loop as I have asked in the initial question, is it possible?

 

One more thing, I have also tried with 2D Arrays, and I found that it is only possible to use ReDim Preserve on the last index of an array (i.e. for example, if I am creating an array Arr(x,y) and I would like to re dimension it with values, I can only use ReDim Preserve to change y index of Arr(x,y). I can not re dimension x index of the Arr(x,y) any ideas on how to solve this? Any alternatives etc? 

 

If you can share an example code I will be more than happy.

 

Thanks again for your time and answers.

If this answer has solved your problem please ACCEPT SOLUTION and hit like if you found it helpful..!


Regards,
Dutt Thakar
LinkedIn
0 Likes
Message 5 of 6

J-Camper
Advisor
Advisor

The only way I could see using the 2 different arrays in your loop the way you were initially asking would be to set up an Occurrence counting Integer, which increases as the main loop moves to the next occurrence.  Then before you fill data to an array, check the value with If or case statements to decide which array to fill.  That method would be hard coded to a max of how ever many If/Cases you set up.

 

As For ReDiming a 2D array, I don't actually work with array objects very much.  I much prefer Lists because they are so much easier to make as variable length.  I did a quick google search and found a thread on StackOverflow where someone was asking about the same 2D array ReDim issue.  It seems like you can write a custom function that "ReDim Preserves" by straight creating a new array with the old array values filled in and returning this new array back to the original.  Here is the Link and the function I'm referring to is a few posts down named "ReDimPreserve"

0 Likes
Message 6 of 6

WCrihfield
Mentor
Mentor

You could also use a List(Of List(Of Double)) or List(Of List(Of String)) (basically a list of lists), which would be similar to a 2d array, but you wouldn't have to deal with the whole ReDim situation.  Also Lists can very easily be converted to and from Arrays, and its values can easily be set by an array, instead of individually.  There are 4 main ways to add stuff to Lists.  The regular Add() which adds a single value/object to the end, the AddRange() which adds an array of values/objects to the end, Insert() which inserts a single value/object at the specified index, and InsertRange() which adds an array of values/objects at the specified index.  There are also similar methods for removing values.

 

Then there's the conversion methods for Lists, like ToArray, and there are 3 versions of the CopyTo method, which will copy either the entire list to a single dimensional array, or copy a specified range of the List contents to a single dimensional array.

Dim oListOfLists As New List(Of List(Of Double))

Dim oDList1 As New List(Of Double)
oDList1.Add(.123)
oDList1.Add(1.23)
oDList1.Add(12.3)
oMsg1 = InputListBox("List 1", oDList1) 'just to show the list

Dim oDList2 As New List(Of Double)
oDList2.AddRange({.321, 3.21, 32.1 })
oMsg2 = InputListBox("List 2", oDList2) 'just to show the list

oListOfLists.Add(oDList1)
oListOfLists.Add(oDList2)

oDList1.AddRange(oDList2.ToArray)
oMsg3 = InputListBox("List 1 after merge.", oDList1) 'just to show the list

Dim oDArray(2) As Double 'to hold last 3 values of List
oDList1.CopyTo(3, oDArray, 0, 3)
oMsg3 = InputListBox("Array from merged List.", oDArray) 'just to show the list

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes