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: 

setting precision via ilogic

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
cadmanagershat
978 Views, 4 Replies

setting precision via ilogic

I would very much appreciate if anyone could have a look at the following code and...........

A. tell me why it won't (when it used to) change the precision of mass to 2 decimal places when mass < 1.

B. help me to include the leading zero when mass < 1.

Ever since I inserted "0" before the Str on this line, which added the leading zero but removed the second decimal place I have had the issue with missing decimal.

 

This is all done in order to have the mass precision change automatically in my drawing template.

 

mass = iProperties.Mass
iProperties.Value("Custom", "Mymass") = iProperties.Mass
If mass  < 1 Then iProperties.Value("Custom", "Mymass")= Str ( Round(iProperties.Mass ,2))+ " kg "
If mass  <= 10 Then iProperties.Value("Custom", "Mymass")= Str( Round(iProperties.Mass ,1))+ " kg "
If mass  > 10 Then iProperties.Value("Custom", "Mymass")= Str ( Round(iProperties.Mass ,0))+ " kg "

 Any help will be most welcome.

4 REPLIES 4
Message 2 of 5
MegaJerk
in reply to: cadmanagershat

try this. 

Dim mass As Decimal 
mass = iProperties.Mass

Dim massString As String


Select Case mass
	Case < 1 
	massString = CStr(Round(mass, 2, MidpointRounding.AwayFromZero))& " kg"
	Case 1 To 10 
	massString = CStr(Round(mass, 1, MidpointRounding.AwayFromZero))& " kg"
	Case > 10 
	massString = CStr(Round(mass, 0, MidpointRounding.AwayFromZero))& " kg"
End Select 

iProperties.Value("Custom", "Mymass")= massString

 



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

GitHub
Message 3 of 5
cadmanagershat
in reply to: MegaJerk

Great stuff, that's excellent.

Thanks a lot.

Any ideas why my original stopped working?

Message 4 of 5
MegaJerk
in reply to: cadmanagershat

It actually took me a moment to realize why your original code was failing, and it is the reason that I re-wrote your code into a Select Case statement. The reason you kept getting the wrong answer is due to your code ‘falling through’. Essentially, if you meet the first condition you present X < 1 Then by default you ALSO meet the condition for the next statement X <= 10. 

Because the code is not nested, it easily deceives you into thinking that there is no problem with the conditional flow that is presented. Due to how us humans tend to read things, we get to that first line and think, “Oh… well I am most certainly not over 1, so nothing else should happen after this is done!” However, the compiler, realizing the next line is also true updates our value accordingly and moves on to provide you with an unexpected result!

I hope that this clears up the mystery!



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

GitHub
Message 5 of 5
cadmanagershat
in reply to: MegaJerk

That makes sense thanks.

I think possibly when it worked I had the first condition (<1) as the second condition which would allow this condition to be met. I re-ordered to tidy up a bit and obviously didn't realize why I had placed it second in the first place!

I hope that makes sense.

 

Thanks again though much appreciated.

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

Post to forums  

Autodesk Design & Make Report