ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
HTHardik Trivedi04/01/2022

The following code adds a custom property 'AutoQty' showing quantity of components from an assembly to all of its components (parts and sub-assemblies). I found this macro a long time back on the forum I think originally created by @Josh Brady. It has been working fine, but the only issue is that it also counts the components that are marked 'Excluded from BOM' or enveloped. Can anyone please help me modify this code so that it does not add the quantity of such components? Thank you.


Option Explicit

Sub UpdateQtys()
Dim swApp As SldWorks.SldWorks 'added this line
Dim Assembly As ModelDoc2 'added this line
Dim myAsy As AssemblyDoc
Dim myCmps
Dim Cfg As String
Dim CmpDoc As ModelDoc2
Dim i As Long
Dim j As Long
Dim cCnt As Long
Dim NoUp As Long
Dim myCmp As Component2
Dim tCmp As Component2
Dim AssyQty As String
Dim tm As Double
tm = Timer
Set swApp = Application.SldWorks 'added this line
Set Assembly = swApp.ActiveDoc 'added this line
Set myAsy = Assembly
If Assembly.ConfigurationManager.ActiveConfiguration.Name <> Assembly.CustomInfo2("", "Cfg4Qty") Then
Assembly.Extension.ShowSmartMessage "Qtys not updated due to config", 1000, True, True
Exit Sub
End If
NoUp = 0
myCmps = myAsy.GetComponents(False)
For i = 0 To UBound(myCmps)
Set myCmp = myCmps(i)
If (myCmp.GetSuppression = 3) Or (myCmp.GetSuppression = 2) Then
cCnt = 0
Set CmpDoc = myCmp.GetModelDoc
Cfg = myCmp.ReferencedConfiguration
For j = 0 To UBound(myCmps)
Set tCmp = myCmps(j)
If tCmp.GetSuppression <> 0 Then
If tCmp.GetModelDoc2 Is CmpDoc Then
If tCmp.ReferencedConfiguration = Cfg Then
cCnt = cCnt + 1
End If
End If
End If
Next j
CmpDoc.AddCustomInfo3 Cfg, "AutoQty", 30, ""
CmpDoc.AddCustomInfo3 Cfg, "QtyIn", 30, ""
AssyQty = Assembly.CustomInfo2("", "TnkQty")
CmpDoc.CustomInfo2(Cfg, "AutoQty") = cCnt * AssyQty
CmpDoc.CustomInfo2(Cfg, "QtyIn") = Assembly.GetTitle & " Cfg " & Assembly.ConfigurationManager.ActiveConfiguration.Name
Else
NoUp = NoUp + 1
End If
Next i
Assembly.Extension.ShowSmartMessage NoUp & " Parts not updated due to lightweight (" & Timer - tm & "s)", 10000, True, True
End Sub