20 Replies Latest reply on Sep 4, 2018 11:17 AM by Wayne Barrett

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

    Riccardo Mattioli

      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

      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

      -------------------------------------------------------------------------------------------

       

      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!

       

      Riccardo

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

          Riccardo,

          Are you using this macro as an equation or as a standalone macro?  The way it is written is not particularly efficient.  I wrote it to be simple and short code to fit into an equation.  I must confess that I don't actually use it myself or I might have optimized it better.  If you are using it as a standalone macro, it could probably be made considerably more efficient by using other methods or algorithms.

           

          The change you made is good.  It certainly increases efficiency. 

           

          To make it set general custom properties instead of configuration-specific, just replace Cfg with "" in each CustomInfo command.

            • Re: Wonderful Update Quantities Macro (by Josh Brady)- how to improve?
              Riccardo Mattioli

              Josh,

               

              thank you so much for your answer, it just works! I'm really pleased you replied so quickly, I always read your posts and admire your programming skills. Thanks again.

               

              About the macro.

               

              We're using it extensively as a standalone macro, not as an equation. I really think the "equation trick" is great, and it should be seriously considered in some cases, but we're dealing with 500+ component assemblies, and it slows down the design process a lot, since it calculates at every rebuild...

              We used to apply Lenny's "PropertyEditorGlobal" macro to propagate custom properties to all the components in the active directory, and it worked, but it doesn't calculate part quantities. Then I discovered your macro, modified it in many ways (adding some SendMsgToUser2, some controls here and there, basically to meet our factory habits), and began to use it in everyday work. It solved an old problem we had: insert the correct quantities in part drawings.

               

              And it's true, the way it is written can be a problem for 1500+ components assemblies. In these cases, the execution of the macro can be really time-consuming (10+ mins). Maybe a traverse through the assembly could work, but here I stop, my programming skills are really poor.

               

              Any ideas?

               

              Thanks

               

              Riccardo

                • Re: Wonderful Update Quantities Macro (by Josh Brady)- how to improve?
                  Markku Lehtola

                  Hi,

                   

                  I'm using Excel to calculate large assemblies. I export indented BOM and run a Excel-macro to get the full quantities for every component...not that many lines to write.

                   

                  br

                  Markku

                  www.sloworks.fi

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

                    I believe what is taking the longest is probably all of the calls to the SW API.  All of the comparisons in the loop should just be string comparisons rather than multiple SW calls.  This approach would be like:

                     

                    1. Load a 2-column array with the path and referenced configuration name of each component, something like:

                     

                    (this code is off-the-cuff, no debug)

                     

                    dim CompArray(0,1) as string

                    for i = 0 to ubound(mycmps)

                      if (suppression state stuff) then

                        redim preserve comparray(i,1) as string

                        set mycmp = mycmps(i)

                        comparray(i,0) = mycmp.getmodeldoc.getpathname

                        comparray(i,1) = mycmp.referencedconfiguration

                      end if

                    next i

                     

                    Now you have all the components in an array. 

                     

                    2. Create a new 3-column array.  Iterate through CompArray and compare to the new array.  If CompArray member is found in the new array, increment the quantity in the third column of the new array.  If it's not found, add it to the end of the array with qty 1.

                     

                    Now you have an array containing the full path, configuration name, and qty of each component.

                     

                    3. Iterate through the new array.  Use OpenDoc6  with appropriate option arguments to return a pointer to the ModelDoc2 of each element of the array and set the custom property.

                     

                    In your case, you may want to add some code to check and make sure that you don't have two different configurations of the same SW file in the assembly since you are wanting to set general custom properties.

                      • Re: Wonderful Update Quantities Macro (by Josh Brady)- how to improve?
                        Riccardo Mattioli

                        Hi Josh,

                         

                        I think your approach is brilliant, I understood how it works but I can't figure out how to write it down.

                        I think I'll spend some nightly hours with that, 'cause it really interests me, and possibly publish some results.

                        Two things I didn't catch at all:

                        - How do I use OpenDoc6??? What I use it for???

                        - Why do you retrieve full path name? Isn't component name enough?

                         

                        Thank you for your support.

                         

                        Riccardo

                          • Re: Wonderful Update Quantities Macro (by Josh Brady)- how to improve?
                            Riccardo Mattioli

                            OK Josh, here's some weekend code:

                             

                            Dim CompArray() As String

                            Dim N As Long

                            Dim k As Long

                             

                            N = UBound(myCmps)

                            ReDim Preserve CompArray(N, 1) As String

                             

                            For k = 0 To UBound(myCmps)

                            Set myCmp = myCmps(k)

                            If (myCmp.GetSuppression = 3) Or (myCmp.GetSuppression = 2) Then

                                Set myCmp = myCmps(k)

                                CompArray(k, 0) = myCmp.GetModelDoc.GetPathName

                                CompArray(k, 1) = myCmp.ReferencedConfiguration

                            End If

                            Next k

                             

                            ....

                             

                            And it works, it gives me an array with the desired data. I was forced to define the static array dimension because "redim preserve comparray(i,1) as string" doesn't work. Redim Preserve lets you change the right side of an array...

                             

                            And this becomes a problem for the definition of the new 3-dimensional array, right?

                             

                            Thank you

                             

                            Riccardo

                      • Re: Wonderful Update Quantities Macro (by Josh Brady)- how to improve?
                        Wayne Barrett

                        Hi, relatively new to solidworks here.  I know this is an old thread, but man oh man it is exactly the topic I am looking for a solution for.  I have this same macro that I pulled from another thread, these "AutoQTY" macros seem to be about the same from post to post on this forum.  It works great, but I have one small issue.  If I have mirrored parts in an assembly, I make those mirrored parts as a mirrored configuration in the same part file.  I like to set up my BOMs as "display all configs of the same part as one item", because I consider a left hand and right hand set of parts to be only one part with quantity of 2, the shop can use the same drawing to make both, so, yeah.  Mostly that's just the way we have always done it and I need to preserve it.

                         

                        Problem is, the "AutoQTY" property seems to only count the configuration instances instead of the filename instances.  So I may have a BOM read qty=2 and AutoQTY=1, because AutoQTY is only counting 1 in "Default"" and only 1 "MirrorDefault".

                         

                        I just tried "To make it set general custom properties instead of configuration-specific, just replace Cfg with "" in each CustomInfo command"

                        as Josh Brady suggested in the first response above.  I thought that may write the AutyQTY as 2 in custom properties tab of all configs since I don't think those are "configuration specific".  That didn't work though.

                         

                        So my solution so far is to set up my BOMs as "display configs of the same part as separate items".  I don't like that, but it works to

                      • Re: Wonderful Update Quantities Macro (by Josh Brady)- how to improve?
                        Sam King

                        Josh,

                         

                        What is the purpose of the coding:

                         

                        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

                         

                        I've just started learning about programming macros, but whenever I run this macro I get the error "Qtys not updated due to config"

                         

                        From what I understand, this is just checking to see that the name of the active configuration is not equal to the "Cfg4Qty" in the custom properties of the assembly. 

                         

                        So i was just curious as to its purpose, the macro will run if I remove that section. Then after a while Solidworks will tell me there is an error in the code and the system may be Unstable now.

                        • Re: Wonderful Update Quantities Macro (by Josh Brady)- how to improve?
                          Sai Pistule

                          HI RICCARDO

                           

                          I ALSO FACING THE SAME PROBLEM, I GET THIS MACRO AS SOLUTION AND I PASTE THE CODE MAKE NEW MACRO, BUT CANT GET ANY QUANTITY IN DETAIL DRAWING.

                           

                           

                          CAN ANYBODY PLEASE ELABORATE THE WHOLE CONCEPT, AS HOW TO USE THIS MACRO AND WHAT ARE THW CUSTOM PROPERTY REQUIRED IN BOM, ASSEMBLY, PART AND DETAIL DRAWING.

                           

                           

                          PLEASE PROVIDE THE SOLUTION..