I did try the Assembly visualization (works great for a manual work around !) Thank you!
This is what I am trying to do by macro.
- Accurately sums the mass of specific materials,
- Skips suppressed and hidden components,
- Displays a pop-up with the result.
This is what I have so far … although I am getting an error.
I am receiving and error. “Object doesn’t support this property or method”
Const swComponentHidden = 0
Const swThisConfiguration = 1
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim totalMass As Double
Dim targetMaterials As Variant
Sub main()
On Error GoTo ErrorHandler
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then
MsgBox "No active document. Please open an assembly file.", vbExclamation, "Missing File"
Exit Sub
End If
If swModel.GetType <> swDocASSEMBLY Then
MsgBox "This macro only works on assembly documents.", vbExclamation, "Incorrect File Type"
Exit Sub
End If
Set swAssy = swModel
' Define your specific material list here
targetMaterials = Array( _
"Aluminum - 6061", _
"Aluminum Hex - 6061", _
"Aluminum Angle - 6061", _
"Aluminum - Tooling Plate", _
"Aluminum Hex - Extrusion" _
)
totalMass = 0
Call AccumulateMass(swAssy.GetRootComponent)
MsgBox "✅ Total mass of specified materials: " & Format(totalMass, "0.000") & " kg", vbInformation, "Mass Summary"
Exit Sub
ErrorHandler:
MsgBox "Error encountered: " & Err.Description, vbCritical, “Macro Error”
End Sub
Sub AccumulateMass(comp As SldWorks.Component2)
Dim visibility As Long
Dim model As SldWorks.ModelDoc2
Dim matName As String
Dim massProps As SldWorks.MassProperty
Dim children As Variant
Dim i As Integer, k As Integer
If comp.IsSuppressed Then Exit Sub
visibility = comp.GetVisibility(swThisConfiguration)
If visibility = swComponentHidden Then Exit Sub
Set model = comp.GetModelDoc2
If Not model Is Nothing Then
matName = model.GetMaterialPropertyName2("", "")
For k = LBound(targetMaterials) To UBound(targetMaterials)
If StrComp(matName, targetMaterials(k), vbTextCompare) = 0 Then
Set massProps = model.Extension.CreateMassProperty
totalMass = totalMass + massProps.Mass
Exit For
End If
Next k
End If
children = comp.GetChildren
If Not IsEmpty(children) Then
For i = 0 To UBound(children)
Call AccumulateMass(children(i))
Next i
End If
End Sub