Can someone help me with code to add up the mass of selected parts within an assembly?

Can someone help me with code to add up the mass of selected parts within an assembly?

chris
Advisor Advisor
192 Views
2 Replies
Message 1 of 3

Can someone help me with code to add up the mass of selected parts within an assembly?

chris
Advisor
Advisor

I have an assembly where I would like to add the masses of selected or listed parts within an assembly. I tried Chat GTP first and this is what it gave me, it's working, as in iLogic is not returning an error when the rule is run, but the rule is not returning a valve?

 

' Get the active assembly document
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oCompDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition

' Define the names of the two parts to include
Dim part1Name As String = "Rectangular Profile Foundation Rebar-Top Mat Radial Bar.ipt" ' Change to match your part name
Dim part2Name As String = "Rectangular Profile Foundation Rebar-Top Mat Radial Bar.ipt" ' Change to match your part name
Dim part3Name As String = "Rectangular Profile Foundation Rebar-Top Mat Hoop Bar.ipt" ' Change to match your part name
Dim part4Name As String = "Rectangular Profile Foundation Rebar-Bottom Mat Hoop Bar.ipt" ' Change to match your part name
Dim part5Name As String = "Rectangular Profile Foundation Rebar-Ring Wall U Bar.ipt" ' Change to match your part name
Dim part6Name As String = "Rectangular Profile Foundation Rebar-Ring Wall Hoop Bar.ipt" ' Change to match your part name
Dim part7Name As String = "Rectangular Profile Foundation Rebar-Footing Dowels.ipt" ' Change to match your part name
Dim part8Name As String = "Rectangular Profile Foundation Rebar-Ring Wall Dowels.ipt" ' Change to match your part name

Dim totalMass As Double = 0

' Loop through the components in the assembly
For Each oOccurrence In oCompDef.Occurrences
    ' Check if the part is one of the two we want to include
    If oOccurrence.Name.Contains(part1Name) Or oOccurrence.Name.Contains(part2Name) Or oOccurrence.Name.Contains(part3Name) Or oOccurrence.Name.Contains(part4Name) Or oOccurrence.Name.Contains(part5Name) Or oOccurrence.Name.Contains(part6Name) Or oOccurrence.Name.Contains(part7Name) Or oOccurrence.Name.Contains(part8Name) Then
        ' Get the physical properties of the part
        Dim oPartProps As PropertySet = oOccurrence.ReferencedDocumentDescriptor.ReferencedDocument.PropertySets.Item("Design Tracking Properties")
        Dim partMass As Double = CDbl(oPartProps.Item("Mass").Value)
        
        ' Add the mass to the total
        totalMass += partMass
    End If
Next

iProperties.Value("Custom", "WOR") = totalMass
0 Likes
193 Views
2 Replies
Replies (2)
Message 2 of 3

petr.meduna
Advocate
Advocate

Hi, and you need what exactly?

The rule cannot return a value, it's just code that runs in the environment of Inventor. The rule, which you are presenting to us, creates user Iproperty "WOR", where the total mass of required parts is stored.

I would recommend you similar approach but from my point of view much easier.

Sub Main()
' Get the active assembly document
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oCompDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition

' Define the names of the two parts to include
Dim pList as New List(of String) From {
"Rectangular Profile Foundation Rebar-Top Mat Radial Bar.ipt",
"Rectangular Profile Foundation Rebar-Top Mat Radial Bar.ipt",
"Rectangular Profile Foundation Rebar-Top Mat Hoop Bar.ipt",
"Rectangular Profile Foundation Rebar-Bottom Mat Hoop Bar.ipt"
"Rectangular Profile Foundation Rebar-Ring Wall U Bar.ipt",
"Rectangular Profile Foundation Rebar-Ring Wall Hoop Bar.ipt",
"Rectangular Profile Foundation Rebar-Footing Dowels.ipt",
"Rectangular Profile Foundation Rebar-Ring Wall Dowels.ipt"}

Dim totalMass As Double = 0

IterateThroughAssembly(oCompDef, pList, totalMass)

iProperties.Value("Custom", "WOR") = totalMass
End Sub

Private Sub IterateThroughAssembly(byVal occs as ComponentOccurrences, byVal pList as List(of String), byRef totalMass as Double)
' Loop through the components in the assembly
For Each oOcc as ComponentOccurrence In occs
    ' Check if the part is one of the two we want to include
    If pList.Contains(IO.Path.GetFileName(oOcc.Definition.Document.FullFileName)) Then
        ' Add the mass to the total
        totalMass += CDbl(oOcc.Definition.Document.PropertySets(3)("Mass").Value)
        If typeOf oOcc.Definition.Document is AssemblyDocument Then
            IterateThroughAssembly(oOcc.Definition.Occurrences, pList, totalMass)
        End If
    End If
Next
End Sub

 

0 Likes
Message 3 of 3

mateusz_baczewski
Advocate
Advocate

Hi,

I’ve corrected your rule and added another option to assign mass by selecting the specific elements that interest you.This rule does not return a value but assigns a value to WOR in the custom properties.

' Get the active assembly document
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oCompDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition


Dim part1Name As String = "Rectangular Profile Foundation Rebar-Top Mat Radial Bar.ipt" ' Change to match your part name
Dim part2Name As String = "Rectangular Profile Foundation Rebar-Top Mat Radial Bar.ipt" ' Change to match your part name
Dim part3Name As String = "Rectangular Profile Foundation Rebar-Top Mat Hoop Bar.ipt" ' Change to match your part name
Dim part4Name As String = "Rectangular Profile Foundation Rebar-Bottom Mat Hoop Bar.ipt" ' Change to match your part name
Dim part5Name As String = "Rectangular Profile Foundation Rebar-Ring Wall U Bar.ipt" ' Change to match your part name
Dim part6Name As String = "Rectangular Profile Foundation Rebar-Ring Wall Hoop Bar.ipt" ' Change to match your part name
Dim part7Name As String = "Rectangular Profile Foundation Rebar-Footing Dowels.ipt" ' Change to match your part name
Dim part8Name As String = "Rectangular Profile Foundation Rebar-Ring Wall Dowels.ipt" ' Change to match your part name

Dim totalMass As Double = 0

' Loop through the components in the assembly
For Each oOccurrence As ComponentOccurrence In oCompDef.Occurrences
    ' Check if the part is one of the two we want to include
    If oOccurrence.Name.Contains(part1Name) Or oOccurrence.Name.Contains(part2Name) Or oOccurrence.Name.Contains(part3Name) Or oOccurrence.Name.Contains(part4Name) Or oOccurrence.Name.Contains(part5Name) Or oOccurrence.Name.Contains(part6Name) Or oOccurrence.Name.Contains(part7Name) Or oOccurrence.Name.Contains(part8Name) Then
        ' Add the mass to the total
        totalMass += oOccurrence.MassProperties.Mass
    End If
Next

iProperties.Value("Custom", "WOR") = totalMass


and the second rule:

Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim totalMass As Double = 0
Dim oSelectSet As SelectSet = oAsmDoc.SelectSet

If oSelectSet.Count = 0
	MsgBox("You have to select a Component Occurrence!",MsgBoxStyle.Information, "Warning!")
End If


For Each oSelect In oSelectSet
	
	If oSelect.Type = kComponentOccurrenceObject
		Dim oCompOcc As ComponentOccurrence = oSelect
		
		totalMass += oCompOcc.MassProperties.Mass
	End If
	
Next

iProperties.Value("Custom", "WOR") = totalMass

  

If you found it helpful, a "Like" would be much appreciated!
If this post solved your problem, please mark it as "Solution.".

0 Likes