Hi @rafael.barata. I forgot about units conversions. Everything in Inventor having to do with measurements and units of measure, uses what is called 'database units' which is one specific set of all metric units for each measurement type (length, mass, temperature, time), and that is centimeters for length. So most measurements you get from Inventor by code are in those database units by default, instead of in document units. This situation has been a major pain for many years, but it seems to be the only way Autodesk can write the programming for all that numerical stuff and keep it straight. Below is an altered version of the code that uses a built-in units conversion tool.
Sub Main
If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.FactoryDocument
Dim oMSs As ModelStates = oADoc.ComponentDefinition.ModelStates
If oMSs.MemberEditScope <> MemberEditScopeEnum.kEditActiveMember Then
oMSs.MemberEditScope = MemberEditScopeEnum.kEditActiveMember
End If
Dim oOrigMS As ModelState = oMSs.ActiveModelState
Dim UOM As UnitsOfMeasure = oADoc.UnitsOfMeasure
Dim oDocLengthUnits As UnitsTypeEnum = UOM.LengthUnits
Dim oDBLengthUnits As UnitsTypeEnum = UnitsTypeEnum.kDatabaseLengthUnits
For Each oMS As ModelState In oMSs
oMS.Activate
Dim oMSDoc As AssemblyDocument = oMS.FactoryDocument
'oMSDoc.Rebuild2(True) 'helps ensure all updated, but may not be necessary (may take extra time)
Dim oMP As MassProperties = oMSDoc.ComponentDefinition.MassProperties
Dim oCOG As Inventor.Point = oMP.CenterOfMass
Dim oCOC_X As Double = UOM.ConvertUnits(oCOG.X, oDBLengthUnits, oDocLengthUnits)
Dim oCOC_Y As Double = UOM.ConvertUnits(oCOG.Y, oDBLengthUnits, oDocLengthUnits)
Dim oCOC_Z As Double = UOM.ConvertUnits(oCOG.Z, oDBLengthUnits, oDocLengthUnits)
Dim Ixx, Iyy, Izz, Ixy, Iyz, Ixz As Double
oMP.XYZMomentsOfInertia(Ixx, Iyy, Izz, Ixy, Iyz, Ixz)
Ixx = UOM.ConvertUnits(Ixx, oDBLengthUnits, oDocLengthUnits)
Iyy = UOM.ConvertUnits(Iyy, oDBLengthUnits, oDocLengthUnits)
Izz = UOM.ConvertUnits(Izz, oDBLengthUnits, oDocLengthUnits)
TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_Mass", oMP.Mass)
TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG X coordinate", oCOC_X)
TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG Y coordinate", oCOC_Y)
TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG Z coordinate", oCOC_Z)
TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG Ixx", Ixx)
TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG Iyy", Iyy)
TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG Izz", Izz)
Next 'oMS
oOrigMS.Activate
If oADoc.RequiresUpdate Then oADoc.Update2(True)
End Sub
Sub TryCreateUpdateCustomProperty(oDoc As Inventor.Document, sPropName As String, _
oValue As Object, Optional sExpression As String = vbNullString)
If IsNothing(oDoc) Or sPropName = "" Or oValue Is Nothing Then Exit Sub
If oDoc.IsModifiable = False Then Exit Sub
Dim oCProps As Inventor.PropertySet = oDoc.PropertySets.Item(4)
Dim oCProp As Inventor.Property = Nothing
Try
oCProp = oCProps.Item(sPropName)
Catch
oCProp = oCProps.Add(oValue, sPropName)
End Try
If String.IsNullOrEmpty(sExpression) Then
If oCProp.Value <> oValue Then oCProp.Value = oValue
Else
If oCProp.Expression <> sExpression Then oCProp.Expression = sExpression
End If
End Sub
Wesley Crihfield

(Not an Autodesk Employee)