OK so, below you will find my current macro, works nearly flawlessly. alas it is so inefficient it hurts... (as you can see if you watch the debug window).
What it does is it gets all components and configurations of said components in an assembly. It then iterates through them using a duplicate iteration to count the quantities of each configuration of each component. Using this method it overwrites the "MfgQty" custom property again and again every time it encounters a part. Though it still gives the correct result, this is horribly inefficient.
<p>Dim swApp As SldWorks.SldWorks</p><p>Dim swModel As SldWorks.ModelDoc2</p><p>Dim swAssy As SldWorks.AssemblyDoc</p><p>Dim swCmp As SldWorks.Component2</p><p>Dim tCmp As SldWorks.Component2</p><p>Dim CmpDoc As ModelDoc2</p><p>Dim i As Integer</p><p>Dim j As Integer</p><p>Dim Cfg As String</p><p>Dim vCmps As Variant</p><p>Dim swCustPropMgr As SldWorks.CustomPropertyManager</p><p>Dim nstart As Long</p><p>Dim nStatus As Long</p><p>Dim config As SldWorks.Configuration</p><p>Dim lRetVal As Variant</p><p>Dim ValOut As String</p><p>Dim ResolvedValOut As String</p><p>Dim wasResolved As Boolean</p><p>Dim cCnt As Integer</p><p></p><p>Sub main()</p><p> Set swApp = Application.SldWorks</p><p> Set swModel = swApp.ActiveDoc</p><p> Set swAssy = swModel</p><p></p><p>' start timer</p><p> nstart = Timer</p><p></p><p>' load all components into array</p><p> vCmps = swAssy.GetComponents(False)</p><p></p><p>' resolve all lightweight components</p><p> nStatus = swAssy.ResolveAllLightWeightComponents(False)</p><p></p><p>'remove duplicates from array</p><p> </p><p>' iterate though array</p><p> For i = 0 To UBound(vCmps)</p><p> Set swCmp = vCmps(i)</p><p> If (swCmp.GetSuppression = 3) Or (swCmp.GetSuppression = 2) Then</p><p> cCnt = 0</p><p> Set CmpDoc = swCmp.GetModelDoc</p><p> Cfg = swCmp.ReferencedConfiguration</p><p></p><p>' for each component in new array of matching configuration, count instances in original array</p><p> For j = 0 To UBound(vCmps)</p><p> Set tCmp = vCmps(j)</p><p> If tCmp.GetSuppression <> 0 Then</p><p> If tCmp.GetModelDoc2 Is CmpDoc Then</p><p> If tCmp.ReferencedConfiguration = Cfg Then</p><p> cCnt = cCnt + 1</p><p> End If</p><p> End If</p><p> End If</p><p> Next j</p><p> Debug.Print swCmp.Name, swCmp.ReferencedConfiguration, cCnt</p><p></p><p></p><p>' set "MfgQty" custom property to computed value</p><p> Set swCustPropMgr = CmpDoc.Extension.CustomPropertyManager(swCmp.ReferencedConfiguration)</p><p> lRetVal = swCustPropMgr.Delete2("MfgQty")</p><p> lRetVal = swCustPropMgr.Add3("MfgQty", 30, "999", 0)</p><p> lRetVal = swCustPropMgr.Get5("MfgQty", False, ValOut, ResolvedValOut, False)</p><p> lRetVal = swCustPropMgr.Set2("MfgQty", cCnt)</p><p> End If</p><p> Next i</p><p></p><p></p><p>' show elapsed time</p><p> Debug.Print "Time = " & Timer - nstart & " seconds"</p><p></p><p>End Sub</p>What I would like to do is reduce this. I would like to initially build an array of unique components/configurations, then using that array iterate through it using the base array to do the count. this would benefit me at least 2 fold, 1) MUCH more efficient not duplicating counts and 2) it would allow me to add print and dxf commands in (print to .PDF drawings of all parts, and export .DXF drawings of parts that contain a custom property called "ItemCat" with the value of "Laser-Pur" and/or "Router-Pur"), and only have each unique part print out once rather that 24 times for a part that has 24 instances. Below is a sample code of what i think would work... i am just having issues connecting the two.
<p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">Sub Test()</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">Dim aReturn As Variant</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">Dim aArray As Variant</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">Dim swCmp As Variant</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">' build array</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;"></p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">aArray = Array("red", "blue", "green", "blue", "blue", "green", "red", "red", "yellow", "red", "green", "blue")</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">aReturn = ArrayUnique(aArray)</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;"> For i = 0 To UBound(aReturn)</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;"> Debug.Print aReturn(i)</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;"> Next i</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">End Sub</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;"></p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">Function ArrayUnique(ByVal aArrayIn As Variant) As Variant</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;"></p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">'remove duplicated values from a single dimension array</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">Dim aArrayOut() As Variant</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">Dim bFlag As Boolean</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">Dim vIn As Variant</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">Dim vOut As Variant</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">Dim i%, j%, k%</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;"></p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">ReDim aArrayOut(LBound(aArrayIn) To UBound(aArrayIn))</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">i = LBound(aArrayIn)</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">j = i</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;"></p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">For Each vIn In aArrayIn</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;"> For k = j To i - 1</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;"> If vIn = aArrayOut(k) Then bFlag = True: Exit For</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;"> Next</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;"> If Not bFlag Then aArrayOut(i) = vIn: i = i + 1</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;"> bFlag = False</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">Next</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;"></p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">If i <> UBound(aArrayIn) Then ReDim Preserve aArrayOut(LBound(aArrayIn) To i - 1)</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">ArrayUnique = aArrayOut</p><p style="font-size: 14px; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; color: #333333;">End Function</p>the first code has fairly well broken out steps, and works as is to add a custom property called "MfgQty" to each file with the assembly count of said part. Works with multiple configurations of the same part and will add the property to each configuration correctly.
Any help on generating this Array so i can progress this code further would be helpful. as you can see the " 'remove duplicates from array" section is empty
SolidworksApi macros