Help debug this IF statement Please :)

Help debug this IF statement Please :)

frahaman7SKBG
Advocate Advocate
545 Views
8 Replies
Message 1 of 9

Help debug this IF statement Please :)

frahaman7SKBG
Advocate
Advocate

Can someone help me debug this code? I could not for the life of me figure out why this 'if statement' won't work

I have checked the output value of 'Ang2'  through the 'Z2' output in the parameter and have confirmed that it is in fact equal to 90. So I can't figure out why the code thinks the "sharedVariable("Ang2")" does not equal 90 when in fact it does. 

 

I added the ipt part model to this code that is being tested. 

 

Dim oParams As Parameters
oParams=ThisDoc.Document.ComponentDefinition.Parameters
Dim oUserParams As UserParameters
oUserParams=oParams.UserParameters 


	
sharedVariable("A") = Measure.ExtentsLength 
sharedVariable("B") = Measure.ExtentsWidth
sharedVariable("C") = Measure.ExtentsHeight
sharedVariable("Ang1") = Measure.Angle("Large Face", "XZ Plane")
sharedVariable("Ang2") = Measure.Angle("Large Face", "XY Plane")
sharedVariable("Ang3") = Measure.Angle("Large Face", "YZ Plane")

X1 = sharedVariable("A")
X2 = sharedVariable("B")
X3 = sharedVariable("C")
Z1 = sharedVariable("Ang1") 
Z2 = sharedVariable("Ang2") 
Z3 = sharedVariable("Ang3") 



If sharedVariable("Ang1") >=0  And sharedVariable("Ang2") = 90  Then
	
	Try
	oUserParams("G_L").Value = (sharedVariable("A")/Cos(sharedVariable("Ang1")*PI/180)*2.54)
	Catch
	' assume error means not found and create it
	oUserParams.AddByValue("G_L", (sharedVariable("A")/Cos(sharedVariable("Ang1")*PI/180)*2.54), kInchLengthUnits) 
	End Try

Else	
	
End If


iLogicVb.UpdateWhenDone = True

 

0 Likes
Accepted solutions (1)
546 Views
8 Replies
Replies (8)
Message 2 of 9

A.Acheson
Mentor
Mentor

Just an initial guess is that measure.area would be system units of radians and your looking to check that value against a degree value. Haven't tested this but perhaps you can check. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 9

frahaman7SKBG
Advocate
Advocate

That was my fist assumption too, but it doesn't look like it. Check it against both radian and degree. 

0 Likes
Message 4 of 9

frahaman7SKBG
Advocate
Advocate

BTW if I change the 'Ang2' with 'Z2' in the if statement, the code works as expected. Can't figure out why it won't work with 'Ang2'

0 Likes
Message 5 of 9

S.Hørup
Contributor
Contributor

I am not able to use the part you have attached, so can't test it properly.

 

My initial thought is that the meassurement is not exactly 90°- but perhaps 90.00001° - then the if statement is not true.

 

As a test, you can try set Ang2 to this:

sharedVariable("Ang2") = Measure.Angle("YZ Plane", "XY Plane")

then you are absolutely sure it is 90°

 

 

Next,I would first try changing the IF-statement to 

If sharedVariable("Ang1") >=0  And Round(sharedVariable("Ang2")) = 90  Then

 

0 Likes
Message 6 of 9

frahaman7SKBG
Advocate
Advocate

Hi @S.Hørup ,

 

Thanks for your suggestion. In both cases, the code seems to work as expected. 

 

I have checked the value up to 8 decimal places for the Ang2, and it gives 90.00000000. So I still don't understand why the code won't work as is without the rounding function. 

 

If sharedVariable("Ang1") >=0  And sharedVariable("Ang2") = 90  Then
0 Likes
Message 7 of 9

S.Hørup
Contributor
Contributor

Hi again, now I got to test it with your part as well.

 

This code works:

If sharedVariable("Ang1") >= 0 And Round(sharedVariable("Ang2"),13) = 90 Then
	MsgBox("Both statements are true")
	Try
	oUserParams("G_L").Value = (sharedVariable("A")/Cos(sharedVariable("Ang1")*PI/180)*2.54)
	Catch
	' assume error means not found and create it
	oUserParams.AddByValue("G_L", (sharedVariable("A")/Cos(sharedVariable("Ang1")*PI/180)*2.54), kInchLengthUnits) 
	End Try
Else		
End If

 As you see, I round with 13 digits - Then the code works fine. If I change the round-function to 14, the code fails.

 

But every time I want to see the value of Ang2 I also only see trailing zeros.

Edit: Could the issue be in the precision of the data types? 

 

Message 8 of 9

JelteDeJong
Mentor
Mentor
Accepted solution

Precision problems with floating point variables are common. At least so common that Microsoft wrote a page about it. And Autodesk iLogic made a special function for comparing floating point variables. "DoublesAreEqual(... ...)"

 

try it like this:

If sharedVariable("Ang1") >= 0 And DoubleUtil.DoublesAreEqual(sharedVariable("Ang2"),90) Then
	MsgBox("Both statements are true")
	Dim val = sharedVariable("A")/Cos(sharedVariable("Ang1")*PI/180)*2.54
	Try
		oUserParams("G_L").Value = val
	Catch
		' assume error means not found and create it
		oUserParams.AddByValue("G_L", val, kInchLengthUnits) 
	End Try	
End If

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

Message 9 of 9

frahaman7SKBG
Advocate
Advocate

Thanks for the insight on this issue. 

0 Likes