There are a couple of options to deal with pre-existing parameters with the wrong units.
- Option 1: Attempt to change its units with a Try...Catch...End Try block of code, just before attempting to update its value.
- Option 2: Just delete the parameter, set the 'Exists' boolean to False, then the lower section will create a new one with the units you specify.
As for the Event Trigger...The AnyModelParameterChange event only works on model parameters, so it won't be triggered when user parameters get changed. That's one of the main drawbacks of using user parameters vs model parameters. However, using user parameters is most often more stable, because they won't disappear on you like model parameters can. Model parameters are created (and deleted) automatically by Inventor when you create (or delete) features, dimensions, etc.
Here is an updated rule code, in which I have changed the mass units to your specifications. It now also includes some code showing you how you can check the units of an existing user parameter, and attempt to change its units. If changing the existing parameters units fails, it will then just delete the parameter, then set the boolean back to False, so the later method will create a new one. I commented out this portion of the Volume parameter check, since I'm unsure how you set your volume units and how you may have done any math for units conversion, but it is fairly easy to follow. Then again in the second method below, where it is to create the Volume parameter, I left that alone too, for the same reasons. You can just use what you did in your last fix to correct the units.
I hope this helps some.
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject And _
ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kPartDocumentObject Then
MsgBox("This rule only works for Assembly or Part documents. Exiting.", , "")
Exit Sub
End If
Parameter.UpdateAfterChange = True
MultiValue.UpdateAfterChange = True
ThisApplication.CommandManager.ControlDefinitions.Item("AppUpdateMassPropertiesCmd").Execute2(True)
Dim oMassProps As MassProperties = ThisApplication.ActiveDocument.ComponentDefinition.MassProperties
Dim oMass As Double = oMassProps.Mass '(will be in database units, not document units)
Dim oVol As Double = oMassProps.Volume '(will be in database units, not document units)
'or
'Dim oDTProps As Inventor.PropertySet = ThisApplication.ActiveDocument.PropertySets.Item("Design Tracking Properties")
'Dim oMass As Double = CDbl(oDTProps.Item("Mass").Value)
'Dim oVol As Double = CDbl(oDTProps.Item("Volume").Value)
Dim oUParams As UserParameters = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
Dim oUParam As UserParameter
Dim oMExists, oVExists As Boolean
For Each oUParam In oUParams
If oUParam.Name = "Mass" Then
oMExists = True
Try
If oUParam.Units <> UnitsTypeEnum.kKilogramMassUnits Then
oUParam.Units = UnitsTypeEnum.kKilogramMassUnits
End If
oUParam.Value = oMass
Catch
oUParam.Delete
oMExists = False
Catch
MsgBox("Failed to change Mass 'Units' to Kilograms and/or delete the parameter.", , "")
End Try
ElseIf oUParam.Name = "Volume" Then
oVExists = True
' Try
' If oUParam.Units <> "m^3" Then
' oUParam.Units = "m^3"
' End If
' oUParam.Value = oVol
' Catch
' oUParam.Delete
' oVExists = False
' Catch
' MsgBox("Failed to change Volume 'Units' to meters cubed.", , "")
' End Try
End If
Next
If oMExists = False Then
oUParam = oUParams.AddByValue("Mass", CDbl(oMass), UnitsTypeEnum.kKilogramMassUnits)
End If
If oVExists = False Then
'<<<< USING OUNCE VOLUME UNITS, CHANGE FOR OTHER UNITS >>>>
oUParam = oUParams.AddByValue("Volume", CDbl(oVol), UnitsTypeEnum.kOunceVolumeUnits)
End If
'Output Parameters values from this rule to the Model. (Use this before Document Update)
RuleParametersOutput()
'Immediately update the current document.
InventorVb.DocumentUpdate()
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield

(Not an Autodesk Employee)