Get Unit Abbreviations

Get Unit Abbreviations

Noah.ClarkX5FEF
Contributor Contributor
1,894 Views
18 Replies
Message 1 of 19

Get Unit Abbreviations

Noah.ClarkX5FEF
Contributor
Contributor

Hi there,

I know there is a way to get the default display units such as inch, foot, centimeter in inventor but is there a way to get the default display abbreviated units like in, ft, cm?  It would be nicer than having to code out the case statement for each unit type to get its abbreviation.  I would find it odd if there wasn't because you can use the abbreviated forms in some of the Units Of Measure procedures/functions.

0 Likes
Accepted solutions (4)
1,895 Views
18 Replies
Replies (18)
Message 2 of 19

WCrihfield
Mentor
Mentor

Are you maybe thinking of this method:  UnitsOfMeasure.GetStringFromType()?  There are also lots of other related methods and properties (tools) under the UnitsOfMeasure object.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 19

Noah.ClarkX5FEF
Contributor
Contributor

Sadly no.   UnitsOfMeasure.GetStringFromType() gets the full Length unit type not the type abbreviation.  I know how to do get a string "inch".  I would like to get a string "in".

0 Likes
Message 4 of 19

WCrihfield
Mentor
Mentor

For the most part, you will not find unit abbreviations available in a list for many of the units used for area, volume, & density.  There may be a few, but if you need something specific, you pretty much just have to create it yourself.  Most of the time its just as simple as putting a single space between the unit abbreviations to specify the units for those types of measurements.  For instance, you can use "in in" or "in^2" for square inches area, and "in in in" or "in^3" for inches cubed volume, and so on.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 19

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @Noah.ClarkX5FEF 

 

Give this a try.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

 

Edit to add:

Just noticed you tagged this VBA, so I figured I better check to make sure it didn't return something different in VBA for some reason, but it doesn't. Here is the same for VBA:

 

 

 

    Dim oUOM As UnitsOfMeasure
    Set oUOM = ThisApplication.ActiveDocument.UnitsOfMeasure
    oLenUnits = oUOM.GetStringFromType(UnitsTypeEnum.kDefaultDisplayLengthUnits)
    MsgBox (oLenUnits)

 

 

 

Curtis_Waguespack_1-1649954738191.png

 

 

iLogic version:

	Dim oUOM As UnitsOfMeasure
	oUOM = ThisDoc.Document.UnitsOfMeasure
	oLenUnits = oUOM.GetStringFromType(UnitsTypeEnum.kDefaultDisplayLengthUnits)	
	MsgBox(oLenUnits,, "iLogic")

Curtis_Waguespack_0-1649954360639.png

 

EESignature

Message 6 of 19

Noah.ClarkX5FEF
Contributor
Contributor
Accepted solution

wait how does that work?  every time I use 

GetStringFromType(UnitsTypeEnum.kDefaultDisplayLengthUnits)

I only get "inch".

Message 7 of 19

WCrihfield
Mentor
Mentor

Good point.  It seems like I've seen something like this before, but don't recall where.  I do know that when checking the value of Parameter.Units property, you usually get the abbreviated version of the units string.  And you usually see (and can retrieve) the Expression of a parameter, it will contain the abbreviated version of the units string.  And when viewing the Physical tab of the iProperties dialog, you see the abbreviated units shown after the numerical values.  The Autodesk programmers must be using some sort of behind the scenes routine to generate those abbreviated Strings for internal use.  It would certainly be a pain in the @'s to have to find or create a parameter then retrieve its units string each time though.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 19

WCrihfield
Mentor
Mentor

If using the few "kDefaultDisplayLengthUnits" type specifications, you can only get the units currently set as default.  What about other non-default units abbreviations.  Would you then have to change your default units first, then get the abbreviated version of it using that method, then switch your units back each time?  That would certainly not be ideal.  I'm thinking that a custom Function is likely needed just for returning the abbreviated versions of all the various units we might be working with, because I don't know of another readily available way.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 19

WCrihfield
Mentor
Mentor

You could also just create your own alternate Enum to reference (Link1, Link2), that would return the abbreviated versions of the unit names, instead of the full unit names.  You could even have it in another external iLogic rule that was set to StraightVB, then reference that rule using the AddVbFile system, if needed.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 19

Curtis_Waguespack
Consultant
Consultant

@Noah.ClarkX5FEF wrote:

wait how does that work?  every time I use 

GetStringFromType(UnitsTypeEnum.kDefaultDisplayLengthUnits)

I only get "inch".


hmmm, I'm not sure. I did a couple quick checks to see if I could think of a setting, but didn't see anything.

EESignature

0 Likes
Message 11 of 19

Noah.ClarkX5FEF
Contributor
Contributor
Accepted solution

Ok so my code is different then yours:

Dim oUOM As UnitsOfMeasure
	oUOM = ThisDoc.Document.UnitsOfMeasure
	oLenUnits = oUOM.GetStringFromType(UnitsTypeEnum.kDefaultDisplayLengthUnits)

My code:

Dim oUOM As UnitsOfMeasure
Set oUOM = ThisApplication.ActiveDocument.UnitsOfMeasure
Dim eLengthUnit As UnitsTypeEnum
eLengthUnit = oUOM.LengthUnits
DocUnits = oUOM.GetStringFromType(eLengthUnit)

 The difference is I used length units vs you used display units.  For some reason the display units show as the abbreviated form.  I thought they were the same but I guess they are different.

Message 12 of 19

Maxim-CADman77
Advisor
Advisor

I wish this discussion ends with workflow for getting abbreviation for any (not just default) unit (i mean something better than @WCrihfield's idea to change the default unit and then undo the action).

For me select case seems too clumsy from the point of developing global solutions (with indefinite Inventor UI-language).

 

PS:
I don't understand why three messages are marked as answers (for me second and third are just repeatings of the Curtis's answer).

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Message 13 of 19

Maxim-CADman77
Advisor
Advisor

I wonder whether @JelteDeJong knows the solution for this?

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Message 14 of 19

JelteDeJong
Mentor
Mentor
Accepted solution

I could not find a method for getting the abbreviations of units. But there is another workaround. Have a look at this:

 

Dim doc As PartDocument = ThisDoc.Document
Dim uom As UnitsOfMeasure = doc.UnitsOfMeasure

Dim tempString = uom.GetStringFromValue(123, UnitsTypeEnum.kKilogramMassUnits)
Dim unitAbbreviation = tempString.Split(" ")(1)

MsgBox(unitAbbreviation)

 

the value string uses the format "<value> <abbreviation of the units>" So if you create a "value string" and give some random number.  And split on the space between the random number and the "abbreviation of the units", and you get an array. The 2e element in the array is your abbreviation.

 

That is the best I could think of but still a bit of a hack

 

Edit:

Changed code to give a full example

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 15 of 19

Maxim-CADman77
Advisor
Advisor

@JelteDeJong  IMHO this is more elegant than anything that was mentioned above.
If I were the thread host I would definitely mark it a solution. Many thanks!

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Message 16 of 19

Maxim-CADman77
Advisor
Advisor

@JelteDeJong 

Yet this trick doesn't provide ability to get local "Each" abbreviation:

MaximAnatoljevich_0-1693826113970.pngMaximAnatoljevich_1-1693826280884.png

 

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Message 17 of 19

Maxim-CADman77
Advisor
Advisor

@JelteDeJong 

Seems like units abbreviation (at least LengthUnit) returned by the trick you've suggested depends on the localization of Inventor the document was created (or may be saved) with ... which is not what me expected.

 

I then tried to use UnitsOfMeasure property of Application instead of Document's property but got an exception (see the code-snippet below).

 

Dim oUOM As UnitsOfMeasure

logger.info("1:" & UnitsTypeEnum.kDefaultDisplayLengthUnits) ' 11260


oUOM = ThisDoc.Document.UnitsOfMeasure ' +
logger.info("2:" & oUOM.GetStringFromValue(1, UnitsTypeEnum.kDefaultDisplayLengthUnits).Split(" ")(1))


oUOM = ThisApplication.UnitsOfMeasure
logger.info("3:" & oUOM.GetStringFromValue(1, UnitsTypeEnum.kDefaultDisplayLengthUnits).Split(" ")(1)) ' Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

 

 

Thus I'd like to know:

1. What I'm missing regarding the exception?

2. How do I get Unit Abbreviations relevant to current Inventor localization?

 

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Message 18 of 19

WCrihfield
Mentor
Mentor

Interesting issue.  I too got an error with that code.  I was able to get rid of that error by using a different method (GetPriciseStringFromValue vs GetStringFromValue).  I don't know why that would make a difference though.

Dim oUOM As Inventor.UnitsOfMeasure = ThisDoc.Document.UnitsOfMeasure
Logger.Info("1:  " & oUOM.GetPreciseStringFromValue(1, UnitsTypeEnum.kInchLengthUnits).Split()(1))
oUOM = ThisApplication.UnitsOfMeasure
Logger.Info("2:  " & oUOM.GetPreciseStringFromValue(1, UnitsTypeEnum.kInchLengthUnits).Split()(1))

  I do see that there is a method called "GetLocaleCorrectedExpression", but the documentation for it sort of suggests that it may only correct decimal place character changes.  It does not mention units string abbreviation conversion.  The only 'localization' related tool I have seen within iLogic or Inventor API is a tool I found maybe a year ago, but it seems to be specific to the names of ModelStates, not just general verbiage.

Dim oMSName As String = "Master"
Dim oMSNUtils As New Autodesk.iLogic.Runtime.ModelStateNameUtils
Dim sLocalizedName As String = oMSNUtils.GetLocalizedName(oMSName)
MsgBox("The 'Localized' version of " & oMSName & " is:  " & sLocalizedName, vbInformation, "iLogic")

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 19 of 19

Maxim-CADman77
Advisor
Advisor

@WCrihfield 
Using GetPriciseStringFromValue and Application's UnitsOfMeasure property I still get English LengthUnits Abbreviation while using non-English Inventor localization ... which is not what I need.

Please vote for Inventor-Idea Text Search within Option Names

0 Likes