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

Seems like you've put in a good effort.

 

As it is below, it assumes that there are no Inseparable BOM Structure assemblies as the method to grab the weight for it fails.

 

Even for an "experienced" programmer like me, this still took me about half an hour to piece together, excluding what I had written before.

 

 

Sub Main
    Dim oDoc As AssemblyDocument
    oDoc = ThisApplication.ActiveDocument
    
    Dim oBOM As BOM
    oBOM = oDoc.ComponentDefinition.BOM
    
    Dim oBOMView As BOMView
    oBOMView = oBOM.BOMViews.Item("Parts Only")
    
    Dim Materials As Object
    Materials = CreateObject("Scripting.Dictionary")
    
    Call BOMweightIteration(oBOMView.BOMRows, Materials)

    For Each item In Materials
		If msg = "" Then 
			'msg = "Materials & Mass:" & item & " " & Materials(item) & "kg" & ", "
			msg = item & " " & Materials(item) & "kg" & ", "
		Else
        	msg = msg & item & " " & Materials(item) & "kg" & ","
		End If
    Next
	msg = Left(msg, Len(msg) - 1) 'removes the last comma
	
	Materials = Nothing
	
	'iProperties.Value(System.IO.Path.GetFileName(oDoc.FullFilename), "Custom", "MatlWeights") = msg
	iProperties.Value("Custom", "MatlWeights") = msg
	MsgBox(msg)
End Sub


Private Sub BOMweightIteration(oBOMRows As BOMRowsEnumerator, ByRef Materials As Object)

    Dim i As Long
	Dim oItemWeight As Double
	Dim weight As Double
	
    For i = 1 To oBOMRows.count
        Dim oRow As BOMRow
        oRow = oBOMRows.Item(i)
  
        Dim oCompDef As ComponentDefinition
        oCompDef = oRow.ComponentDefinitions.Item(1)
		
		oRowDoc = oCompDef.Document
		
		Matl = oRowDoc.PropertySets("Design Tracking Properties")("Material").Value
		
		Weight = oCompDef.MassProperties.Mass 'mass in kg as it is database units
		oItemWeight = Round(Weight*oRow.TotalQuantity, 2)
		
		'MsgBox(oRowDoc.FullFileName & vbLf & Matl & vbLf & oItemWeight)
		
        If Materials.Exists(Matl) Then
			Materials(Matl) = Materials(Matl) + oItemWeight 'where 1 = 1 decimal place
		Else
			Materials.Add(Matl, oItemWeight)
		End If  
    Next
End Sub 

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type