Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

iLogic Sort Values

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
ddavis
4156 Views, 5 Replies

iLogic Sort Values

I have an iLogic rule that populates the fields shown below in our title block. The DIM. Values are strings set with H,W,L values. This works well, but I have an additional need to sort these values in ascending order, so the smallest value is first followed by the next largest and finally the largest value. (4.000x5.000x14.000). I haven't had much luck finding any information about how this can be done. I did however come across some code here that someone else was using for a similar feature.

 

Man_Prop.JPG

 

Dim sizes As New List (Of Decimal)

sizes.Add(bH)
sizes.Add(bW)
sizes.Add(bL)

sizes.Sort()

D1 = sizes(1)
D2 = sizes(2)
D3 = sizes(3)

This is what I have been attempting, though it does not work. I get the following error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index". I'm not sure if my list type is correct (Decimal), but no matter what I choose it fails. The (bH,bW,bL) are the actual block dimensions pulled from the .ipt file. Once sorted I want the values of each position (1),(2),(3) to be assigned to (D1),(D2),(D3) so I can place them in my title block in the correct order.

 

I'm no expert with this and really appreciate any help anyone can provide. If there is a need to see the complete code and/or example files, please let me know.

 

Thank you,

Darek

 

5 REPLIES 5
Message 2 of 6
Spidermeld
in reply to: ddavis

First time posting here.  I have been working on a similar rule.  Here's how I did the sort.

 

' Get overall Sizes
Dim x = Measure.ExtentsLength
Dim y = Measure.ExtentsWidth
Dim z = Measure.ExtentsHeight

' DEBUG - Displays sizes
'MessageBox.Show("Material Size = " & x & ", " & y & ", " & z, "Overall Sizes")

' Sorts sizes from lowest to highest
Dim Height = MinOfMany (x, y, z)
Dim Length = MaxOfMany (x, y, z)
Dim Width As Double

If (Height = x And Length = y) Or (Height = y And Length = x)Then
	Width = z
Else If (Height = x And Length = z) Or (Height = z And Length = x)Then
	Width = y
Else If (Height = y And Length = z) Or (Height = z And Length = y)Then
	Width = x
End If

 Hope it helps

Message 3 of 6
MegaJerk
in reply to: ddavis

To sort through things, you can use the below method to get a good understanding of how it works : 

 


Dim ourArray As New ArrayList
Dim i As Integer

Dim Num1 As Double = 5.015
Dim Num2 As Double = 6.501
Dim Num3 As Double = 1.055

Dim Sum1 As Double
Dim Sum2 As Double
Dim Sum3 As Double

Dim holderList As String

ourArray.Add(Num1)
ourArray.Add(Num2)
ourArray.Add(Num3)

ourArray.Sort()

Sum1 = ourArray(0)
Sum2 = ourArray(1)
Sum3 = ourArray(2)

For i = 1 To ourArray.Count Step 1

'''///////////////////////////////////////////////////
'''
''' This is cycling through each element of this array
''' and adding it to a String called 'holderList'.
''' This would be a great place to do something like
''' populating your fields, or assigning values
''' to your parameters.
'''
'''///////////////////////////////////////////////////

holderList = holderList & CStr(ourArray(i-1)) & ", "

Next

MessageBox.Show("Array has been sorted down to: " & holderList,"")

MessageBox.Show("Our Sums equal : " & Sum1 & " - " & Sum2 & " - " & Sum3)

 

 

You can see where I show you both ways of getting values from this sort of approach. Because I'm almost sure that you will be working with a fixed array size, you can simply hardcode it as is shown using the Sum1 / Sum2 / Sum3 method, however, if you're ever looking to get values from a dynamic array, you can use that For / Next section to grab the value with each pass of the loop! 

So really, if you're sure that you are using a fixed array amount that will never change (and assuming that your initial post contained the correct Parameter names), the following code will probably work best for you : 

Dim ourArray As New ArrayList 

ourArray.Add(bH)
ourArray.Add(bW)
ourArray.Add(bL)

ourArray.Sort()

D1 = ourArray(0)
D2 = ourArray(1)
D3 = ourArray(2)

 

Remember that Arrays typically start at '0' where as the number that is returned when using the .Count method is the total of all elements inside of the Array. That's why D1 = ourArray(0)  instead of ourArray(1), and also why (if you were to use the example 1 code) - 

MessageBox.Show(ourArray.Count)

 

Would return the value of 3 

Further reading about ArrayLists can be found here : 

 

I hope that this helps! 



 

 

 



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 4 of 6
ddavis
in reply to: ddavis

Thank you both for the suggestions. I ended up using MegaJerks method as it was a few less lines of code.

 

Thanks again fellas, I really appreciate it.

 

Darek

Message 5 of 6
Daan_M
in reply to: Spidermeld

wrong post...

 

Dim W As Double = 1
Dim H As Double = 2
Dim T As Double = 3

Dim firstplace = MinOfMany(W, H, T)
Dim lastplace = MaxOfMany(W, H, T)
Dim middleplace As Double

If 	firstplace <> W And lastplace<>  W Then
	middleplace = W
Else If firstplace  <>  H And lastplace <>  H Then
	middleplace = H 
Else 
	middleplace = T
End If

MessageBox.Show(firstplace & middleplace & lastplace)

 

Message 6 of 6
Daan_M
in reply to: ddavis

.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report