AnsweredAssumed Answered

Wonderful Update Quantities Macro (by Josh Brady)- how to improve?

Question asked by Riccardo Mattioli on Dec 14, 2011
Latest reply on Sep 4, 2018 by Wayne Barrett

Hi there,


I'm successfully using this wonderful macro posted by Josh Brady:




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

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 i 'originally was "For j = 0 To UBound(myCmps)", I just changed it to make it faster, and it works (Riccardo)

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, ""

CmpDoc.CustomInfo2(Cfg, "AutoQty") = cCnt

CmpDoc.CustomInfo2(Cfg, "QtyIn") = Assembly.GetTitle & " Cfg " & Assembly.ConfigurationManager.ActiveConfiguration.Name


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



Well, it works great, and I'm using it a lot, since it does what I was looking for from years: it calculates the quantities of parts in an assembly and writes it down into configuration-specific custom properties.


1) Here comes a subtle problem. It writes down the quantities in configuration-specific custom properties, but many of my drawings are already linked to general custom properties or to DefaultFlatPattern custom properties. How can I make it write the same property (AutoQty) into general custom properties too?


2) For large (100+ components) the cycle becomes really slow: is there a way to make it faster? I already made a modification, but it's not fast enough...


Thank you for your collaboration and best regards!