Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Reading and saving the mass of parts and assemblies

deak_peter
Contributor

Reading and saving the mass of parts and assemblies

deak_peter
Contributor
Contributor

Hello everyone,

 

I have a question for you: does anyone have a sample code that saves the mass of all parts and subassemblies (they can include additional parts and assemblies) of an assembly in a .txt or .xlsx file?

 

To summarize, the mass of each part and assembly at each level of the assembly should be saved to a .txt or .xlsx file.

 

Thanks in advance

Peter

0 Likes
Reply
Accepted solutions (1)
438 Views
5 Replies
Replies (5)

tyler.warner
Advocate
Advocate

@deak_peter you can try something like this to get you started. It will get the name & mass of every occurrence & all occurrences in the sub-assemblies. You can suppress line 25 if you only want the occurrences from the current assembly.

 

 

Class ThisRule
	
Public oFile As String = "C:/temp/testfile.txt"

	Sub Main

		If System.IO.File.Exists(oFile) Then
			Dim oResult As MsgBoxResult
			oResult = MsgBox("This file already exists. Would you like to overwrite it?", vbYesNo, "Overwrite File?")
			If oResult = vbYes Then
				System.IO.File.Delete(oFile)
			Else
				Exit Sub
			End If
        End If
		
		Dim oCompDef As Inventor.ComponentDefinition
		oCompDef = ThisApplication.ActiveDocument.ComponentDefinition		

		For Each oCompOcc As ComponentOccurrence In oCompDef.Occurrences
			If Not TypeOf oCompOcc.Definition Is VirtualComponentDefinition Then				
				WriteTextToFile(oCompOcc.Name, iProperties.Mass(oCompOcc.Name), oFile, True)	
				
				' Suppress the below line to only get the components in the open assembly.
				ProcessAllSubOcc(oCompOcc)				
			End If
		Next
		
	End Sub	
	
	Private Sub ProcessAllSubOcc(oCompOcc As ComponentOccurrence)
	    
	    Dim oSubCompOcc As ComponentOccurrence
		
	    For Each oSubCompOcc In oCompOcc.SubOccurrences
			If Not TypeOf oSubCompOcc.Definition Is VirtualComponentDefinition Then
				If oSubCompOcc.SubOccurrences.Count = 0 Then					
					WriteTextToFile(oSubCompOcc.Name, iProperties.Mass(oSubCompOcc.Name), oFile, True)
				Else
		        	WriteTextToFile(oSubCompOcc.Name, iProperties.Mass(oSubCompOcc.Name), oFile, True)
		        	ProcessAllSubOcc(oSubCompOcc)
				End If
			End If
	    Next
		
	End Sub

	Private Sub WriteTextToFile(oOccName As String, oMass As Double, oFile As String, oAppend As Boolean)
		
		Dim oText As String
		oText = oOccName & vbTab & vbTab & "Mass: " & oMass.ToString & vbCrLf
		
		My.Computer.FileSystem.WriteAllText(oFile, oText, oAppend)
		
	End Sub

End Class

 

 

If this solved your problem or answered your question, please click ACCEPT SOLUTION.
If this helped you, please click LIKE.
0 Likes

WCrihfield
Mentor
Mentor

Hi @tyler.warner.  Just a quick note...  I would use 'vbCrLf' instead of 'vbNewLine' in any new codes you plan on using long term, because the 'vbNewLine' constant has been 'deprecated'.  Below is the link for that source/reference.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.constants.vbnewline?view=net-7.0 

They still compare equally in iLogic, but its best to make the switch sooner, rather than later.

 

Also, I believe he needs the solution to be in VBA, instead of iLogic/vb.net, because I have also been helping some though Private Messages.  Just so you know.  Nice code though.  

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

deak_peter
Contributor
Contributor

Hi @tyler.warner 

Thanks. I need to vba code. Maybe can you modify code?

0 Likes

tyler.warner
Advocate
Advocate
Accepted solution

@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.

deak_peter
Contributor
Contributor

@tyler.warner 

how to modify the code to write the data to an excel file?

For example:

 

Part name     Weight

Part1              1,563 kg

Part2              2,634 kg

Assembly1    4,642 kg

 

0 Likes