@deak_peter this will work for VBA.
Sub SumMassPropsToTXT()
Dim Path As String
Path = "C:/temp/testfile.txt"
If Not Dir(Path, vbDirectory) = vbNullString Then
Dim oResult As VbMsgBoxResult
oResult = MsgBox("This file already exists. Would you like to overwrite it?", vbYesNo, "Overwrite File?")
If oResult = vbNo Then
Exit Sub
End If
End If
Dim FileNumber As Integer
FileNumber = FreeFile
Open Path For Output As FileNumber
Dim oCompDef As Inventor.ComponentDefinition
Set oCompDef = ThisApplication.ActiveDocument.ComponentDefinition
For Each oCompOcc In oCompDef.Occurrences
If Not TypeOf oCompOcc.Definition Is VirtualComponentDefinition Then
Dim oMassProps As MassProperties
Set oMassProps = oCompDef.MassProperties
Call WriteTextToFile(Path, FileNumber, oCompOcc.Name, oMassProps.Mass)
' Suppress the below line to only get the components in the open assembly.
Call ProcessAllSubOcc(Path, FileNumber, oCompOcc)
End If
Next
Close FileNumber
End Sub
Private Sub ProcessAllSubOcc(Path As String, FileNumber As Integer, ByVal oCompOcc As ComponentOccurrence)
Dim oSubCompOcc As ComponentOccurrence
For Each oSubCompOcc In oCompOcc.SubOccurrences
If Not TypeOf oSubCompOcc.Definition Is VirtualComponentDefinition Then
Dim oMassProps As MassProperties
Set oMassProps = oSubCompOcc.MassProperties
If oSubCompOcc.SubOccurrences.Count = 0 Then
Call WriteTextToFile(Path, FileNumber, oSubCompOcc.Name, oMassProps.Mass)
Else
Call WriteTextToFile(Path, FileNumber, oSubCompOcc.Name, oMassProps.Mass)
Call ProcessAllSubOcc(Path, FileNumber, oSubCompOcc)
End If
End If
Next
End Sub
Private Sub WriteTextToFile(Path As String, FileNumber As Integer, oOccName As String, oMass As Double)
Dim oText As String
oText = oOccName & vbTab & vbTab & "Mass: " & oMass & vbCrLf
Print #FileNumber, oText
End Sub
If this solved your problem or answered your question, please click ACCEPT SOLUTION.
If this helped you, please click LIKE.