Wierd Problem with what seems to be simple code

Wierd Problem with what seems to be simple code

Anonymous
Not applicable
317 Views
4 Replies
Message 1 of 5

Wierd Problem with what seems to be simple code

Anonymous
Not applicable

Hello,  I am working with Inventor 2014 and am very new to programing,  I have been making some code to auto output some information so we do not need to enter it manually.  One part of the program is to detect if the length of the part is in inches or mm and to record the length appropriately.   

 

How I do this is I measure its length to 5 decimal places in mm ( this is all that is needed, and should prevent any small rounding errors from inventor ) then divide this by 25.4 and times by 16.  Essentially if this is a hole number it is inches and if it is not it is mm.

 

The problem I have is that it seems to work randomly.  For example 3.5", 3", 13.0625", 33" all wont work but 3.75", 3.0625", 40", 2.5", 12.5", 12.0625" Will work.   If someone could please shed some light on what I am doing wrong, or if they have a work around it would be much appreciated.

 

Thank you!

 

Dim xxLength As String

If Round(Measure.ExtentsHeight,5)/25.4*16 = Round(Measure.ExtentsHeight/25.4*16,0) Then

	xxLength = RoundToFraction(Measure.ExtentsHeight/25.4, 1/16, RoundingMethod.Round).Replace(" ", "-") & Chr(34)
	
Else

	xxLength = Round(Measure.ExtentsHeight,1) & " mm"
	
End If

MessageBox.Show(xxLength, "Title")

 

0 Likes
Accepted solutions (1)
318 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable

Just adding to the last post I tried INT() command instead as well as a set mm value of 76.2mm (3") and I have the same problem, put in 101.6mm (4") and it works, really no idea what I am doing worng

 

Dim xxLength As String

'Dim xHeight As Double = Round(Measure.ExtentsHeight,5)/25.4*16 
Dim xHeight As Double = 76.2/25.4*16 

If xHeight= Int(xHeight) Then

	xxLength = RoundToFraction(xHeight/16, 1/16, RoundingMethod.Round).Replace(" ", "-") & Chr(34)
	
Else

	xxLength = Round(Measure.ExtentsHeight,1) & " mm"
	
End If

MessageBox.Show(xxLength & "     initial = " & xHeight, "Title")
0 Likes
Message 3 of 5

Anonymous
Not applicable

I found a work around but if anyone can still explain to me what I did wrong it would be appreciated.  I'm assuming it has something to do with how the values are actually rounded. Thank you again for taking the time to look at my problem!

 

Dim xxLength As String

Dim xHeight As Double = Round(Measure.ExtentsHeight,5)/25.4*16 
'Dim xHeight As Double = 76.2/25.4*16 

If xHeight-Int(xHeight)<.000001 Then

	xxLength = RoundToFraction(xHeight/16, 1/16, RoundingMethod.Round).Replace(" ", "-") & Chr(34)
	
Else

	xxLength = Round(Measure.ExtentsHeight,1) & " mm"
	
End If

MessageBox.Show(xxLength & "     initial = " & xHeight, "Title")

 

0 Likes
Message 4 of 5

JelteDeJong
Mentor
Mentor
Accepted solution

Comparing doubles is always tricky. You might want to look at the DoubleForEquals.IsEqual(...) function. Your function would look something like this.

Dim xxLength As String

Dim xHeight As Double = 101.6/25.4*16 

If DoubleForEquals.IsEqual( xHeight, Int(xHeight)) Then
	xxLength = RoundToFraction(xHeight/16, 1/16, RoundingMethod.Round).Replace(" ", "-") & Chr(34)	
Else
	xxLength = Round(Measure.ExtentsHeight,1) & " mm"	
End If

MessageBox.Show(xxLength & "     initial = " & xHeight, "Title")

I wrote a post "Unexpected Results" which might interest you.

Also, there is a post about converting values. There are some built-in functions that can help you. "Parameter, Document and Database Units"

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 5 of 5

Anonymous
Not applicable

Thank you very much for the response and detailed information.  I had a read over the information you supplied, and tried out the code you wrote and it works perfect.  Thank you again!

0 Likes