Hi @sei.bun. I changed my code to suit your latest specifications. It now recursively iterates all components, in all levels of the assembly, looking for components that have that custom iProperty with that value, then when it finds them, it gets their Mass and adds it to a running Total Mass value. Then, as before, it converts the units, if needed, then shows you the result in a message. Since I am not sure what the term 'tray' represents, I am not sure if I am using the correct syntax when comparing it to the value of a custom iProperty, so I put a message in there for when it finds that custom iProperty, but the value does not match.
See if this code works better for you.
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.Document
Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
Dim oTotalMass As Double
'run our custom Sub routine
RecurseComponents(oOccs, oTotalMass)
'convert units if needed (retrieved in 'database units', not necessarily document units)
Dim oUOM As UnitsOfMeasure = oADoc.UnitsOfMeasure
Dim oDocMassUnits As UnitsTypeEnum = oUOM.MassUnits
Dim oDBMassUnits As UnitsTypeEnum = UnitsTypeEnum.kDatabaseMassUnits
If oDocMassUnits <> oDBMassUnits Then
oTotalMass = oUOM.ConvertUnits(oTotalMass, oDBMassUnits, oDocMassUnits)
End If
'round value off to 3 decimal places
oTotalMass = Round(oTotalMass, 3)
MsgBox("Total Mass filtered components = " & oTotalMass, vbInformation, "Total Mass")
End Sub
Sub RecurseComponents(oComps As ComponentOccurrences, ByRef oTotalMass As Double)
If IsNothing(oComps) OrElse oComps.Count = 0 Then Exit Sub
For Each oComp As ComponentOccurrence In oComps
If oComp.Suppressed Then Continue For
If TypeOf oComp.Definition Is VirtualComponentDefinition Then Continue For
If TypeOf oComp.Definition Is WeldsComponentDefinition Then Continue For
If TypeOf oComp.Definition Is AssemblyComponentDefinition Then
RecurseComponents(oComp.Definition.Occurrences, oTotalMass)
End If
Dim oCompDoc As Document = oComp.ReferencedDocumentDescriptor.ReferencedDocument
Dim oCProps As PropertySet = oCompDoc.PropertySets.Item("Inventor User Defined Properties")
If oCProps.Count = 0 Then Continue For
Dim oCProp As Inventor.Property = Nothing
For Each oProp As Inventor.Property In oCProps
If oProp.Name = "title" Then
oCProp = oProp
Exit For
End If
Next
If IsNothing(oCProp) Then Continue For
'I don't know what the term 'tray' represents, so I don't know if this is correct syntax
If oCProp.Value = tray Then
Dim oMass As Double = oComp.MassProperties.Mass
oTotalMass = oTotalMass + oMass
Else
MsgBox("The Custom iProperty was found, but it had different value.", , "")
'Logger.Debug("The Custom iProperty was found, but it had different value.")
End If
Next
End Sub
Wesley Crihfield

(Not an Autodesk Employee)