Not sure why this works can some one explain?

Not sure why this works can some one explain?

MCADAEPFW
Enthusiast Enthusiast
1,057 Views
5 Replies
Message 1 of 6

Not sure why this works can some one explain?

MCADAEPFW
Enthusiast
Enthusiast

Ok I was trying to set the description of an assembly based on the length and width of a linked parameter.   Here is the the code I used and was able to get the desired result I've also included screen shots.  Problem is I have no idea why it work.  I just started picking at the code that pops up when you press the '.' 

Snag_41d9e13.png

 

Also I understand why I had to multiply the parameter by 2.54 due to that centimeter conversion thing, but would I still have to do that if the parameter was already in 'ft' .   

One more thing is there a better way to do this so that it rounds down to the nearest fraction and have the dkLenft  and dkWidft  variable end up with the ft on it.  The uom variable string value is "15.083 ft" but I end up losing the ft when I have round it to fraction.  I would like it if the uom variable string value is "00 x/xx ft"  format.   

'[extracts the linear value (in)from a parameter converts to ft and assigns variable
'     to write to the description

'Grabs the length parameter and converts to ft * by 2.54 
uom = ThisDoc.Document.UnitsOfMeasure.GetStringFromValue(DP_LEN * 2.54, "ft")           
'grabs the value from the uom variable and converts to fraction rounding down
'  to the nearest 1/4 fractional unit apply it to a string variable
dkLenft = RoundToFraction(Val(uom.ToString()), 1 / 4, RoundingMethod.RoundDown)

'Grabs the Width parameter and converts to ft * by 2.54 
uom = ThisDoc.Document.UnitsOfMeasure.GetStringFromValue(DP_WID*2.54, "ft")
'Grabs the value from the uom variable and converts to fraction rounding down
'  to the nearest 1/4 fractional unit apply it to a string variable
dkWidft  = RoundToFraction(Val(uom.ToString()), 1/4, RoundingMethod.RoundDown)

'MessageBox.Show(dkLenft & "ft x " & dkWidft & "ft", "Title")

	iProperties.Value("Project", "Description")= _
	"PLATE, FLR DECK, DIAMOND, " & dkLenft & "ft x" & dkWidft _
	& "ft x " & "1/4"" Thk"
	
']  ----End description code

 Snag_41dc300.png

0 Likes
Accepted solutions (3)
1,058 Views
5 Replies
Replies (5)
Message 2 of 6

clutsa
Collaborator
Collaborator
Accepted solution

First, great job getting this to work. I'm not sure how in depth you really need this explained, so if I explain something you know, I'm not trying to insult you it's just how I have to think through things. I added logger.debug functions to help break down what's happening (if you don't have 2019.1 or newer it's going to error) 

So "GetStringFromValue" obviously returns a string (it's in the name) in this case you get "15.083 ft"

"RoundToFraction" wants the first argument to be a double... you don't have a double you have a string see above 😋

"Val" converts a string with numbers in it to a double... that fixed the issue above

"RoundToFraction" wants a double for the second argument... 1 / 4 = .25... no problem there

"RoundToFraction" wants a RoundingMethod for the third argument... RoundingMethod.RoundDown is perfect

You successfully  satisfied all three arguments so "RoundToFraction" returns a string... "15" it only returns 15 because "Val" cut the "ft" off so we could get that string to a double. If you like you can add the " ft" on to dkLenft but to concatenate the description in the end you still have to add the & " x " & val & " x " & so you don't really save that much trouble... it's up to you really.

 

'[extracts the linear value (in)from a parameter converts to ft and assigns variable
'     to write to the description

'Grabs the length parameter and converts to ft * by 2.54 
uom = ThisDoc.Document.UnitsOfMeasure.GetStringFromValue(DP_LEN * 2.54, "ft")   
Logger.Debug("uom = {0}", uom)
'grabs the value from the uom variable and converts to fraction rounding down
'  to the nearest 1/4 fractional unit apply it to a string variable
dkLenft = RoundToFraction(Val(uom), 1 / 4, RoundingMethod.RoundDown) & " ft"
Logger.Debug("dkLenft = {0}", dkLenft)

'Grabs the Width parameter and converts to ft * by 2.54 
uom = ThisDoc.Document.UnitsOfMeasure.GetStringFromValue(DP_WID * 2.54, "ft")
Logger.Debug("uom = {0}", uom)
'Grabs the value from the uom variable and converts to fraction rounding down
'  to the nearest 1/4 fractional unit apply it to a string variable
dkWidft = RoundToFraction(Val(uom), 1 / 4, RoundingMethod.RoundDown) & " ft"
Logger.Debug("skWidft = {0}", dkWidft)

'MessageBox.Show(dkLenft & "ft x " & dkWidft & "ft", "Title")

	iProperties.Value("Project", "Description")= _
	"PLATE, FLR DECK, DIAMOND, " & dkLenft & " x " & dkWidft _
	& " x " & "1/4"" Thk"
	
	Logger.Debug(iProperties.Value("Project", "Description"))
']  ----End description code
If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

0 Likes
Message 3 of 6

MCADAEPFW
Enthusiast
Enthusiast

Thank you so much for the explanation I would appreciate all the depth you could give and I won't be insulted. 🙂  

I'm never satisfied getting something to work I want to know WHY it works (or doesn't) 

Also if I missing syntax that should be there fill free to let me know.   I'm still unclear on how all that works when to 'Dim' a variable or when it's just assumed there seems to be no clear rules on this. 

 

The part that I really am unclear as to why it works is this:

uom = ThisDoc.Document.UnitsOfMeasure.GetStringFromValue(DP_LEN * 2.54, "ft") 

 mainly why do I have to multiply a value that is in 'inches' by 2.54 (I know this is the cm equivalent to 'inch' )  and is inventor  first converting the value to cm then returning it in feet because I specified "ft" in the second part? 

Also what if the parameters were in feet would I still need  multiply by 2.54 or would I have to multiply by 30.48; Or if it was in cm and I want to ft do I even need to multiply by the 2.54?  What exactly is iLogic doing to that parameter to get the 'feet' value?   I have been using Autodesk Inventor since Rubicon so I know that the base units is centimeters and when I tried it without multiplying by 2.54 I got 5.938 ft.  

Is there a check I could do to make sure that value is converted to centimeters before being converted to feet?

 

I know this is a lot of question for an 'answered' post but any light you can shed would be greatly appreciated.    I am struggling with the analysis part of getting the values I need for my iLogic code.   Most of the time it's something stupid simple and I'm kicking myself.  

 

Thanks

Erik

-One last thing:

How could I get it to show in Architectural units  like 15' -2 1/2"  if I wanted to? Or even just 15' with the tick instead of the 'ft'?

0 Likes
Message 4 of 6

Anonymous
Not applicable
Accepted solution

Hope this will help, at 26:50 of the video there is an explaination about units.

https://www.autodesk.com/autodesk-university/class/iLogic-and-Inventor-API-2016#video

Message 5 of 6

clutsa
Collaborator
Collaborator

I'll work on these as I have time but from what I can tell GetStringFromValue always takes cm for an input and converts it to another unit... I think in your mind it works more like ConvertUnits where you gave it a unit already converted from cm and want a different unit. 

uom = ThisDoc.Document.UnitsOfMeasure.ConvertUnits(DP_LEN, Parameter.Param("DP_LEN").Units,"ft")
Logger.Debug("uom = {0}", uom)

 first arg is the value from the parameter (181)

second is the unit type (this finds what the parameter is set to (in) you could also just type that in (I like things I don't have to change later)

third is the unit type you want in the end (ft)

If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

Message 6 of 6

clutsa
Collaborator
Collaborator
Accepted solution

Here is a concept to create the Architectural units in feet and inches... I guess there really isn't a metric equivalent because it's really just built into the metric system. I made what fraction it rounds to default to 1/4" but you can overwrite it as a second argument. Try changing DP_LEN to 181.1 and you'll see what I mean. 

Sub Main()
	Logger.Debug(GetArchString(DP_LEN))
	Logger.Debug(GetArchString(DP_LEN, .125))
	Logger.Debug(GetArchString(DP_WID, .125))
End Sub
Function GetArchString(Value As Double, Optional inc As Double = .25) As String
	WholeFeet = Floor(Value/12)
	'Logger.Debug("WholeFeet = {0}", WholeFeet)
	Inches = Value - (WholeFeet*12)
	'Logger.Debug("Inches = {0}", Inches)
	WholeInches = Floor(Inches)
	'Logger.Debug("WholeInches = {0}", WholeInches)
	DecimalInches = Inches - WholeInches
	FractionalInches = Round(Round(DecimalInches,4) / inc) * inc
	'Logger.Debug("FractionalInches = {0}", FractionalInches)
	FractionString = FormatAsFraction(WholeInches + FractionalInches)
	'Logger.Debug("FractionString = {0}", fractionString)
	GetArchString = If(WholeFeet > 0, WholeFeet.ToString & "' " , "") & fractionString & """"
End Function

 And for more fun you can pick the ticks or the letters

Sub Main()
	Logger.Debug(GetArchString(DP_LEN, ,False))
	Logger.Debug(GetArchString(DP_LEN, .125))
	Logger.Debug(GetArchString(DP_WID, .125, False))
End Sub
Function GetArchString(Value As Double, Optional inc As Double = .25, Optional ticks As Boolean = True) As String
	FootTick = "' "
	InchTick = """"
	If Not ticks Then  
		FootTick = " ft. "
		InchTick = " in."
	End If
		
	WholeFeet = Floor(Value / 12)
	'Logger.Debug("WholeFeet = {0}", WholeFeet)
	Inches = Value - (WholeFeet*12)
	'Logger.Debug("Inches = {0}", Inches)
	WholeInches = Floor(Inches)
	'Logger.Debug("WholeInches = {0}", WholeInches)
	DecimalInches = Inches - WholeInches
	FractionalInches = Round(Round(DecimalInches,4) / inc) * inc
	'Logger.Debug("FractionalInches = {0}", FractionalInches)
	FractionString = FormatAsFraction(WholeInches + FractionalInches)
	'Logger.Debug("FractionString = {0}", fractionString)
	GetArchString = If(WholeFeet > 0, WholeFeet.ToString & FootTick , "") & FractionString & InchTick
End Function
If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates