Select case = or less than in ilogic

Select case = or less than in ilogic

Anonymous
Not applicable
3,519 Views
4 Replies
Message 1 of 5

Select case = or less than in ilogic

Anonymous
Not applicable

Hi guys,

 

With the help here, I had a great result with my plates format yesterday and now I am trying to do the same with round bars but alittle bit trickier because I need to add less or equal to or greater than. 

 

so I want it to say that if my roundbar is less than or equal to .25 then use 446-1004 but I cant get it to work.

then for the second line is >= .3125 and so on

 

Thanks again in advance!

 

 

iProperties.Value("Custom", "Thickness") = B_DIA
oThickness = iProperties.Value("Custom", "Thickness")

odrawingname = ThisDoc.FileName(False)
namedxf = Left(odrawingname,3) & "\" & Mid(odrawingname, 5,8) 
ofilename = namedxf & ".DXF"

iProperties.Value("Project", "Description") = "=RNDBAR,4140 HTSR, <B_DIA> DIA"
iProperties.Value("Project", "Cost Center") = "=<B_L>"
iProperties.Value("Summary", "Keywords") = "BP#" & ofilename  

'select the case for B_DIA
Select Case B_DIA 
    Case >= 0.25 
        oSN = "446-1004"
    Case 0.3125
        oSN = "446-1005"
    Case 0.375 
        oSN = "446-1006"
   Case Else
        oSN = "446-????"
End Select

iProperties.Value("Project", "Stock Number") = oSN

 

I've got it to work when I did Case is < 1... hmmmm..but I want something if its less than..

 

0 Likes
3,520 Views
4 Replies
Replies (4)
Message 2 of 5

rikard.nilsson
Collaborator
Collaborator

Hi,

 

You write "less than or equal to .25" but the Case says "Case >= 0.25"..

But I try to give you some example

 

If you move it to last. Then your example works..

Dim B_DIA = 0.3
Select Case B_DIA 
    Case 0.3
        MessageBox.Show("0,3", "Title")
    Case 0.4
        MessageBox.Show("0,4", "Title")
    Case >= 0.25 
        MessageBox.Show("0,25", "Title")
   Case Else
       MessageBox.Show("< 0,25", "Title")
End Select

 

 

If you want to set limitation to the Case, then this works..

 

Dim B_DIA = 0.4
Select Case B_DIA 
    Case <= 0.25
        MessageBox.Show("0,25", "Title")
    Case 0.25 To 0.399999999
        MessageBox.Show("0,25 to max 0,39999", "Title")
    Case >= 0.4
        MessageBox.Show(" >= 0,4", "Title")
    
   Case Else
       MessageBox.Show("< 0,25", "Title")
End Select

 

 

Maybe this will help you..

 

/Rikard

Message 3 of 5

ekinsb
Alumni
Alumni

Using a "Select Case" won't work as expected in this case because of an issue when comparing floating point numbers.  You can read more about it here.  The code below however will do the comparisons correctly but still won't do what you want because if the value is greater than 0.25 the first If statement will be run and the others will never be evaluated.  If it's less than 0.25 then the last Else If will be executed so you need to make sure the evaluations happen in the correct order.  I'm not sure what the rules are in your case so I didn't change anything but a check for a range of values (anything greater than 0.25 in this case) should take place after the checks for specific values.

 

iProperties.Value("Custom", "Thickness") = B_DIA
oThickness = iProperties.Value("Custom", "Thickness")

odrawingname = ThisDoc.FileName(False)
namedxf = Left(odrawingname,3) & "\" & Mid(odrawingname, 5,8) 
ofilename = namedxf & ".DXF"

iProperties.Value("Project", "Description") = "=RNDBAR,4140 HTSR, <B_DIA> DIA"
iProperties.Value("Project", "Cost Center") = "=<B_L>"
iProperties.Value("Summary", "Keywords") = "BP#" & ofilename  

If B_DIA >= 0.25 Then
    oSN = "446-1004"
Else If Math.Abs(B_Dia - 0.3125) < 0.00000001 Then
    oSN = "446-1005"
Else If Math.Abs(B_Dia - 0.375) < 0.00000001 Then
    oSN = "446-1006"
Else
    oSN = "446-????"
End If

iProperties.Value("Project", "Stock Number") = oSN

 

It would be simpler to use the tolerance function that's described in the link above but I wrote this code so it's not dependent on any other functions.  The main thing to remember is that you shouldn't ever directly compare two floating numbers for equivalency but instead need to check that they're the same within some tolerance.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 4 of 5

Anonymous
Not applicable

Thanks for clearing that up.. Will test this out in a bit!!

0 Likes
Message 5 of 5

Owner2229
Advisor
Advisor

This should work for the floating point number issue as well:

 

'select the case for B_DIA
Select Case Math.Round(B_DIA, 0.000001)
    Case <= 0.25: oSN = "446-1004"
    Case 0.3125:  oSN = "446-1005"
    Case 0.375:   oSN = "446-1006"
    Case Else:    oSN = "446-????"
End Select
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes