SolidWorks assembly quantity writer (MfgQty custom property)
Quick macro to count every occurrence of each component+configuration in the active assembly, then store that count in the component’s MfgQty custom property. Run it before BOMs, DXF exports, or naming macros that depend on real quantities.
Highlights
- Resolves lightweight components, iterates all instances.
- Counts occurrences per referenced configuration.
- Writes
MfgQtyinto the component’s custom properties (config-specific). - Prints counts to the Immediate window for a quick sanity check.
Macro (VBA)
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swCmp As SldWorks.Component2
Dim tCmp As SldWorks.Component2
Dim CmpDoc As ModelDoc2
Dim i As Integer
Dim j As Integer
Dim Cfg As String
Dim vCmps As Variant
Dim swCustPropMgr As SldWorks.CustomPropertyManager
Dim nstart As Long
Dim nStatus As Long
Dim config As SldWorks.Configuration
Dim lRetVal As Variant
Dim ValOut As String
Dim ResolvedValOut As String
Dim wasResolved As Boolean
Dim cCnt As Integer
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAssy = swModel
' start timer
nstart = Timer
' load all components into array
vCmps = swAssy.GetComponents(False)
' resolve all lightweight components
nStatus = swAssy.ResolveAllLightWeightComponents(False)
'remove duplicates from array
' iterate though array
For i = 0 To UBound(vCmps)
Set swCmp = vCmps(i)
If (swCmp.GetSuppression = 3) Or (swCmp.GetSuppression = 2) Then
cCnt = 0
Set CmpDoc = swCmp.GetModelDoc
Cfg = swCmp.ReferencedConfiguration
' for each component in new array of matching configuration, count instances in original array
For j = 0 To UBound(vCmps)
Set tCmp = vCmps(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
Debug.Print swCmp.Name, swCmp.ReferencedConfiguration, cCnt
' set "MfgQty" custom property to computed value
Set swCustPropMgr = CmpDoc.Extension.CustomPropertyManager(swCmp.ReferencedConfiguration)
lRetVal = swCustPropMgr.Delete2("MfgQty")
lRetVal = swCustPropMgr.Add3("MfgQty", 30, "999", 0)
lRetVal = swCustPropMgr.Get5("MfgQty", False, ValOut, ResolvedValOut, False)
lRetVal = swCustPropMgr.Set2("MfgQty", cCnt)
End If
Next i
' show elapsed time
Debug.Print "Time = " & Timer - nstart & " seconds"
End Sub
Usage
- Open the top-level assembly.
- Run the macro; watch the Immediate window for name/config/count.
MfgQtyis written per configuration for every referenced component.- Re-run whenever assembly content or suppression changes.