Tuesday, March 29, 2022

Inventor API: using UnitsOfMeasure.ConvertUnits

Issue:

You're writing a bit of code that might be used in Inventor documents of varying unit types. For instance maybe this code is sometimes used in inch based parts, other times it's used in millimeter based parts, and sometime centimeter based parts. Or maybe the part has some dimensions entered in inches and some entered in millimeters. In any case you want your code to handle all of this.


Recall that when we work with the API, inventor returns values in its internal units, which is always centimeters. So we often need to have our code apply a conversion factor. 

Because of this you want the code to detect the units and do the conversion automatically.

Note:
If we're working with straight iLogic and no API calls, then the conversion is *often* handled for us internally in the iLogic function.    

*I won't say always*


Solution:
Below is a quick example that you can use to do the units detection and conversion in your code.

Here is the basic function. It uses the document's length units and centimeters to determine the conversion factor, and displays this in a message box to return the conversion factor.


Dim oUOM As UnitsOfMeasure
oUOM = ThisDoc.Document.UnitsOfMeasure
oLenUnits = oUOM.GetStringFromType(UnitsTypeEnum.kDefaultDisplayLengthUnits)
oInvUnits = UnitsTypeEnum.kCentimeterLengthUnits
oInvUnitString = oUOM.GetStringFromType(oInvUnits)
oConversion = oUOM.ConvertUnits(1, oUOM.LengthUnits, oInvUnits)

MsgBox("Document Units = " & oLenUnits & vbLf & _
	"Inventor Internal Units = " & oInvUnitString & vbLf & _
	"Conversion factor = " & oConversion, , "Inventor")





And here is a quick example using it to return the value of a selected sketch dimension.
This part is set to inches (Tools tab > Document Setting button > Units tab > Length setting)



Here a sketch dimension/parameter named length is set to 70 mm, even though the part file is using Inches. And because our code is using the API to get the parameter value, we know that it's going to be returned in centimeters. 








Without the conversion factor, the code returns 7, which is the value in Inventor's internal units of centimeters.










So we use the UnitsOfMeasure.ConvertUnits function to handle all of this and return the expected value in the document units. 






With the conversion code, it returns 2.756, which is the value in inches.


Here's the working example:
Dim oUOM As UnitsOfMeasure
oUOM = ThisDoc.Document.UnitsOfMeasure
oConversion = oUOM.ConvertUnits(1, oUOM.LengthUnits, _
				UnitsTypeEnum.kCentimeterLengthUnits)

oMsg = "Select a sketch dimension(Press Esc to continue)"

While True
	Dim oDimension As DimensionConstraint
	oDimension = ThisApplication.CommandManager.Pick(
	SelectionFilterEnum.kSketchDimConstraintFilter, oMsg)
	
	' If nothing gets selected then we're done	
	If IsNothing(oDimension) Then Exit While
	oParam = oDimension.Parameter	
	oParamVal = Round(oParam.Value() / oConversion, 3)
	
	MsgBox(oParamVal,,"Inventor")	
End While