We have been using the Auto Qty Count Script written by Josh Brady for a while now, and I need to see if I can add another field to the count. In counting all of the parts in a Master assembly, we need to add SPARES which don't appear in the model, but which are manually entered into a SPARES field. I also was wondering if there was a way to only count the SPARES in a single entry and not the SPARES # in each of the same parts (ie. if part 001 needs 12 spares, but part 001 is used 12 times, spares will still be counted as 12 in the final count and not 144).
Here is the VBA file that we have been using. Thank you!
''Originally an equation built by Josh Brady of the Solidworks Forums
''WILL AUTO SUM THE QUANTITY OF UNIQUE PARTS IN ALL ASSEMBLIES WHEN RAN FROM THE TOP ASSEMBLY.
''CURRENTLY NEED TO MANUALLY ADD CUSTOM PROPERY 'Cfg4Qty'-'Text'-'Default' TO TOP LEVEL ASSEMBLY BEFORE RUNNING
'' custom property automatically added Cfg4Qty configuration automatically
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 tm As Double
tm = Timer
Set swApp = Application.SldWorks 'added this line
Set Assembly = swApp.ActiveDoc 'added this line
Set myAsy = Assembly
Dim AssConfig As String
Dim AssDoc As Component2
'added for envelope check
'Dim DMcomp As SwDMComponent5
Dim swConfigMgr As SldWorks.ConfigurationManager
Dim swConfig As SldWorks.Configuration
Dim swCustPropMgr As SldWorks.CustomPropertyManager
Set swConfigMgr = Assembly.ConfigurationManager
Set swConfig = swConfigMgr.ActiveConfiguration
Set swCustPropMgr = swConfig.CustomPropertyManager
'Runs only if configuration is "*"
'Assembly.AddCustomInfo3 "", "Cfg4Qty", swCustomInfoText, "*"
'Runs for any active configuration
AssConfig = swConfig.Name
Assembly.AddCustomInfo3 "", "Cfg4Qty", swCustomInfoText, "AssConfig"
Assembly.CustomInfo2("", "Cfg4Qty") = AssConfig
'---------------
If Assembly.ConfigurationManager.ActiveConfiguration.Name <> Assembly.CustomInfo2("", "Cfg4Qty") Then
Assembly.Extension.ShowSmartMessage "Qtys not updated due to custom properties need Cfg4Qty added", 1000, True, True
MsgBox "Qtys not updated due to custom properties need Cfg4Qty added"
Exit Sub
End If
'---------------
MsgBox ("Initiating Count for Assembly: " & Assembly.GetTitle & vbNewLine & "Configuration: " & Assembly.ConfigurationManager.ActiveConfiguration.Name)
tm = Timer
NoUp = 0
myCmps = myAsy.GetComponents(False)
For i = 0 To UBound(myCmps)
Set myCmp = myCmps(i)
If (myCmp.GetSuppression = 3) Or (myCmp.GetSuppression = 2) And (myCmp.IsEnvelope = False) Then
cCnt = 0
Set CmpDoc = myCmp.GetModelDoc
Cfg = myCmp.ReferencedConfiguration
For j = 0 To UBound(myCmps)
Set tCmp = myCmps(j)
If tCmp.GetSuppression <> 0 And (tCmp.IsEnvelope = False) 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, ""
CmpDoc.CustomInfo2(Cfg, "AutoQty") = cCnt
CmpDoc.CustomInfo2(Cfg, "QtyIn") = Assembly.GetTitle & " Cfg " & Assembly.ConfigurationManager.ActiveConfiguration.Name
Else
NoUp = NoUp + 1
End If
Next i
MsgBox ("Auto Quantity Count Assembly: " & Assembly.GetTitle & vbNewLine & " Configuration: " & Assembly.ConfigurationManager.ActiveConfiguration.Name & vbNewLine & NoUp & " Parts not updated due to being lightweight, suppressed, or an envelope component" & vbNewLine & vbNewLine & " Procedure Ran in: " & Timer - tm & " seconds")
End Sub
