Not a bad idea, but maybe not the most efficient. Maybe a better system would be to somehow be able to identify which components belong to different 'groups', by using Custom iProperties (which may 'dirty' those referenced documents), or by using 'Instance Properties' which would only exist at the assembly level that they were created at. This can be applied to the group of components after they are selected. That way, the code can find that same group(s) of components, without needing to specify any specific component names. This would allow for repeatability of getting the sums of their mass, without any specific component names being involved in the code.
The following example code would allow you to either manually pre-select all the components in a group, or manually select them using the 'Pick' method after starting the rule, capturing them into a collection, then iterate through the collection to get their total mass. That whole upper portion of the code which deals with pre-selection and 'Picking' could be replaced with a routine that searches through all components, looking for specific properties, then all with the same property value get grouped together. There could be a two-factor list that pairs a property value with a parameter name, so that the mass of all components in group A gets assigned to parameter A.
At the end of this simple example, it is just asking the user to manually enter the name of a parameter that it should store the group mass value to. That could also be replaced by a few more lines of code that gets the list of all UserParemeters, and lets the user select one of them, then stores the group mass value to that selected parameter.
Just some additional directions/ideas this type of scenario could go.
Sub Main
Dim oApp As Inventor.Application = ThisApplication
Dim oDoc As Document = ThisDoc.Document
Dim oSS As SelectSet = oDoc.SelectSet
Dim oListOfOccs As New List(Of ComponentOccurrence)
If oSS.Count > 0 Then
For i As Integer = 1 To oSS.Count
If TypeOf oSS.Item(i) Is ComponentOccurrence Then
Dim oSSOcc As ComponentOccurrence = oSS.Item(i)
oListOfOccs.Add(oSSOcc)
End If
Next i
End If
If oListOfOccs.Count = 0 Then
Dim oCmdMgr As CommandManager = oApp.CommandManager
PickAgain :
Dim oPickedOcc As ComponentOccurrence = Nothing
oPickedOcc = oCmdMgr.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select A Component.")
If oPickedOcc Is Nothing Then
GoTo AfterPicking
Else
oListOfOccs.Add(oPickedOcc)
GoTo PickAgain
End If
End If
AfterPicking :
If oListOfOccs.Count = 0 Then Return
Dim dGroupMass As Double = 0.0
For Each oOcc In oListOfOccs
dGroupMass += iProperties.Mass(oOcc.Name)
Next oOcc
Logger.Info(vbCrLf & "Group Mass = " & dGroupMass.ToString)
Dim sParamName As String = InputBox("Enter Name of Param to store value in.", "Param Name", "")
Parameter(sParamName) = dGroupMass
End Sub
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)