38 Replies Latest reply on May 25, 2016 3:04 PM by Robert Adair

    Qty custom property in part, from assembly

    Seth Renigar

      I know this has been asked several times before.  I think I even asked this 6-7 years ago, to no avail.  But I just wanted to put it out here again just in case someone has developed a macro or method to do this recently.

       

      Basically, I need a automated way to add a Quantity custom property to parts, transversed from the assembly quantity.  I have to show the quantity required on each part drawing, that's used in an assembly.  Right now I have to do this manually.  I understand that generally this idea doesn't make any sense.  However, when designing molds, each of the custom parts are unique to the mold assembly they were designed for anyway.  So having a Quantity custom property in the parts, driven from the assembly, is not that big of a deal in this case.  This custom property would only be linked to, from the part drawing, not the assembly drawing.

       

      Ideally, if I had a macro that would traverse through an assembly, and write a Quantity custom property to the part files based on the quantity in the assembly, that would be perfect.  It would be even better if this could be a macro feature.  However, that may put too much on the system for every rebuild though.

       

      The only caveat would be the common parts that ARE used in multiple assemblies, such as fasteners.  This is not that big of a deal for me.  First off, those libraries are set to read only.  So no property could be added.  But even if they were not set to read only, the only place this custom property will be linked to, is from a part drawing.  And I'll never create a part drawing of common purchased parts.  So what if these common parts contain a quantity custom property that might change with every assembly.  That wouldn't hurt my feelings none.

       

      So, has anyone come up with a solution to this in the last few years?

        • Re: Qty custom property in part, from assembly
          Josh Brady

          Have a look at this assembly.  It has embedded code in an equation and custom property that updates its components' custom properties.

           

          If you want, you can delete all the components that are currently in that assembly and then use that as a template.

            • Re: Qty custom property in part, from assembly
              Seth Renigar

              Josh,

               

              That is AWESOME .  This is EXACTLY what I needed.  There's not even a macro you have to run....  I even tested it to see if it would capture parts in sub-assemblies.  And it DOES!!!!!  Thank you so very much...

               

              I've got a question though.  Where is the code?  The only equation I see is "Dummy"= "code""code2" which doesn't make sense to me....  I've just gotta know how its done!!! 

               

              Seth

                • Re: Qty custom property in part, from assembly
                  Josh Brady

                  "code" and "code2" are custom properties in the assembly.  Those custom properties each contain quite a few lines of VBA code.

                   

                  One of the little-known aspects of equations is that VBA can be used.  Any VBA, as long as you follow some rules.  The rules are finicky and unpublished. 

                   

                  With SW 2009, the values of custom properties can be included in equations by simply enclosing the name in quotes.

                   

                  So, when SW sees this equation, it replaces "code" and "code2" with the properties' values (which is a bunch of VBA code) and passes it off to the VBA interpreter, which runs all that code.

                   

                  All that's a big oversimplification of what I'll be presenting at SW World this year...

                    • Re: Qty custom property in part, from assembly
                      Seth Renigar

                      Oky-Doky then...  Maybe I shouldn't have asked... That flew right over my head...

                       

                      Well, I kinda get the basic at least though...

                       

                      I do so wish I knew VBA.  But I am completely uneducated in that.  I keep saying year after year that I am going to start learning VBA...  But I never can seem to make it happen...

                       

                      Anyway, you are the man.  And thank you again..  I hate I'm going to miss SWW this year.  I would definately take your class...

                       

                      Seth

                      • Re: Auto-Create "Stock Size" custom property in part
                        Seth Renigar

                        Josh,

                         

                        I'm going to change the thread subject a little bit.  Since you were so kind and knowledgable helping me with the the Quantity to part custom property thingy,  I was wondering if you knew of a similar solution to another thing I've always needed.

                         

                        On my mold assembly BOM, the purchasing people wants a column for "Stock Size" for the manufactured inserts, so that they know what size raw material to purchase, without having to look at every single part print.  To accomplish this, I create 3 reference dimensions in the part model, and link a custom property to them (i.e. "RD1@Annotations@6158-203.moPart_c" x "RD2@Annotations@6158-203.moPart_c" x "RD3@Annotations@6158-203.moPart_c").  This works fairly well for flat plate type parts.  However, there are several limitations to this method.  First, when dealing with a part that does not have a flat side in one direction for example, there is nothing to apply a reference dimension to.  Second, These reference dimension only tell the size of the faces that you dimension to (presumably at the largest possible size on each axis).  If the part "grows" on a different face than your dimension is attached, there is failure to this method.  And third, these reference dimensions are notorius for breaking, with modifications to the part.  The problem here is, you will not know this unless you are constantly turning your your reference dimension display to check these from time to time.

                         

                        There is a macro thats been out there for a long time from Wayne Tiffany called BoundingBox that does a pretty good job already.  It doesn't care what the shape is either.  So it takes care of 2 of the problems I have with the reference dimension method.  If this was a macro feature, it would be great, as I was all the time forgetting to re-run the macro after sizes changed.  Therefore, I quit using it.

                         

                        So I was wondering, do you know of a way to make this automated as well?  Possibly using some of the code from the BoundingBox macro into one of your fancy VBA custom properties perhaps?  Or, simply turning the BoundingBox macro into a macro feature?  You'd just have to make sure it stayed at the bottom of the feature tree.

                         

                        I've never understood why I never hear about people using macro features.  When this was introduced years ago, I thought this was going to be an awesome tool that VBA people would go nuts over..  But to date, I've only ever seen 1 macro feature anywhere.  And you never hear anyone discussing them.  hmmmmm....

                        • Re: Qty custom property in part, from assembly
                          Riccardo Mattioli

                          Hi Josh,

                           

                          applied your method, and I consider it simple genius.

                           

                          One simple question: since it is really slow with "big" assemblies (100 parts) with some equations (10+), do you foresee a method to make it run more quickly? Maybe eliminating the messages and/or verification of suspended parts?

                           

                          Thanks and regards

                           

                          Riccardo

                      • Re: Qty custom property in part, from assembly
                        Ward Davis

                        "Negative values are not supported for dimensions driven by equations."

                         

                        I'm recieving this error while entering "Dummy" = "code" "code2" into the Global Variables. The sample file above works as advertised but I recieve the same error if I open and update the equations. Has anyone encountered this?

                         

                        Running 2012 x64

                        Regards,

                          • Re: Qty custom property in part, from assembly
                            Josh Brady

                            The newfangled equation editor in 2012 broke a lot of stuff (undocumented and unsupported stuff, so I can't really complain) I had spent a lot of time investigating.  I haven't messed with this one to see what has to be done with it to fix it.

                              • Re: Qty custom property in part, from assembly
                                Seth Renigar

                                Josh,

                                 

                                I've been using your code for quite a while now and still absolutely love it.  I owe you many drinks for this, hehe...

                                 

                                Since you are the "master" of this code, if you ever get the time, please please take a look at this.  I don't know if I can go on living without it. 

                                 

                                I am currently still on SW2010.  So I haven't had any problems with it yet.  But I will be moving up to SW2012 in the near future and can't fathom the thoughts of losing this functionality. 

                                  • Re: Qty custom property in part, from assembly
                                    Ward Davis

                                    Seth,

                                    Below is a macro built from the above. I'm using it in 2012 and it works well, just need to re-run it after updating your drawings.

                                     

                                    ''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

                                    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 UBound(myCmps)

                                    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

                                     

                                    There is some redundancy in the variables but it works... and that is a success for me.

                                    Regards,

                            • Re: Qty custom property in part, from assembly
                              Mikael Bolduc

                              Thanks for the code but,

                               

                              Just a litle question . If my assembly quantity is 2, how I can do to multiply 2 on my quantity custom property ?

                              Exemple if I have 2 wheelassembly and each assembly have 4 wheels = 2*4=  the autoqty should be 8 .

                              I think that i can write the quantity in the file property of the assembly and add getcustominfo in the macro.

                              Does it a good way to proceed ? And how I can integrate the code in the macro

                               

                              Thanks

                              Mikael

                              • Re: Qty custom property in part, from assembly
                                Mikael Bolduc

                                I dont need assembly under the 2 wheelassembly because I make the drawing of one wheelassembly but I would produice it two time.

                                • Re: Qty custom property in part, from assembly
                                  Mikael Bolduc

                                  yess its good thank you

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

                                  the 2 can be a variable ?

                                  Can i write the quantity (the variable) in the file property of the assembly ?

                                  • Re: Qty custom property in part, from assembly
                                    Mikael Bolduc

                                    How i can integrate the variable in your code ?

                                    • Re: Qty custom property in part, from assembly
                                      Mikael Bolduc

                                      Ok thank you i will try it !

                                      • Re: Qty custom property in part, from assembly
                                        Mikael Bolduc

                                        Why It dosent work?

                                         

                                        Dim swApp As Object
                                        Sub main()
                                        ''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

                                        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 UBound(myCmps)

                                        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 * Assembly.CustomInfo2("", "Qteass")

                                        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

                                         

                                        Sorry im a beginner with prog i try to understand the VBA code.

                                        Thank for your help !

                                        • Re: Qty custom property in part, from assembly
                                          Mikael Bolduc

                                          Do you have idea for my problem ?

                                            • Re: Qty custom property in part, from assembly
                                              Ravi Teja Tadepalli

                                              Hi Alin,

                                              As you said, the equation method is not working.

                                              But below is the macro posted by Ward Davis in a previous reply and its working fine in Soliworks 2015 SP5.

                                              I just needed some more functionality. While it adds the custom properties to each and every part, I want it to read a property, say, "Spare QTY." which will already be there in the part and add it to the "AutoQty" and then create another property called "Total QTY."

                                              It would be great if u could help me with that.

                                              Thanks in advance.

                                               

                                               

                                              ''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 PROPERTIES 'Cfg4Qty'-'Text'-'Default'  & 'Sets'-'Number'-"some integer" TO TOP LEVEL ASSEMBLY BEFORE RUNNING

                                              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 UBound(myCmps)

                                              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 * Assembly.CustomInfo2("", "Sets")

                                              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

                                            • Re: Qty custom property in part, from assembly
                                              Robert Adair

                                              Has anyone gotten this to work in newer versions?  Running 2016 sp3.  I get the same "Dummy" error as Ward. The SampleAssembly works, but saving as a template, doesn't seem to work.