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.
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")
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