8 Replies Latest reply on Nov 20, 2014 4:15 AM by Nick Hayman

    Quantity in Part from Assembly: Excluded from BOM

    Jared Huffman

      I am using the macro from https://forum.solidworks.com/thread/39750 to count quantities in an assembly and write them to a custom property in each part.  This code skips any lightweight or suppressed components. 

       

      Is there any way to determine if a component is set to "Excluded from BOM" and skip it during the count? 

       

      Thanks for any help! 

        • Re: Quantity in Part from Assembly: Excluded from BOM
          Matt Martens

          The IComponent2 object has a boolean property called ExcludeFromBOM that indicates this setting.

            • Re: Quantity in Part from Assembly: Excluded from BOM
              Jared Huffman

              Thanks for the good news!  I apologize for my inexperience, but I'm still having trouble implementing it.  I've tried to add this with no success.  Why doesn't the macro skip Excluded from BOM? 

               

              Here's the section where the instances are counted:

               

              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.ExcludeFromBOM <> True) Then

              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

              End If

              Next j

                • Re: Quantity in Part from Assembly: Excluded from BOM
                  Santosh Pawar

                  Jared,

                   

                  In the macro instead of

                  If (tCmp.ExcludeFromBOM <> True) Then

                  use

                  Boolstatus =  tCmp.ExcludeFromBOM

                  If Not boolstatus Then

                  ...

                  End If

                    • Re: Quantity in Part from Assembly: Excluded from BOM
                      Jared Huffman

                      I updated that and it still counts my excluded instances.  Here's the whole macro:

                       

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

                      '' Added several custom properties to each subcomponent -Jared Huffman

                      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 ProjTitle As String 'The variable for storing the assembly Project Title

                      Dim StatTitle As String 'The variable for storing the assembly Station Title

                      Dim Description As String ' The variable for setting the description custom property

                      Dim StockDescription As String ' The variable for reading the stock description custom property

                      Dim Weldment As String ' The variable for setting the weldment custom property

                      Dim DescriptionOverride As String ' The variable for reading the description override custom property

                      Dim Boolstatus As Boolean

                      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

                      ProjTitle = myAsy.CustomInfo2(Cfg, "ProjectTitle") 'Read the project title from the assembly

                      StatTitle = myAsy.CustomInfo2(Cfg, "StationTitle") 'Read the station title from the assembly

                      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)

                      Boolstatus = tCmp.ExcludeFromBOM

                      If Not Boolstatus Then

                      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

                      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

                      ''Define Stock Description for machined parts

                      StockDescription = "$PRP:" & Chr(34) & "HasDiameter" & Chr(34) & "$PRP:" & Chr(34) & "StockThickness" & Chr(34) & "$PRP:" & Chr(34) & "HasWidth" & Chr(34) & "$PRP:" & Chr(34) & "StockWidth" & Chr(34) & "$PRP:" & Chr(34) & "HasWall" & Chr(34) & "$PRP:" & Chr(34) & "StockWall" & Chr(34) & " x $PRP:" & Chr(34) & "StockLength" & Chr(34) & " LG"

                      ''Figure out which type of description to show

                      Weldment = CmpDoc.CustomInfo2(Cfg, "Weldment")

                      DescriptionOverride = CmpDoc.CustomInfo2(Cfg, "DescriptionOverride")

                      If DescriptionOverride <> "" Then

                      Description = DescriptionOverride

                      Else

                      If Weldment <> "" Then

                      Description = Weldment

                      Else

                      Description = StockDescription

                      End If

                      End If

                      ''Add Description custom property and assign it

                      CmpDoc.AddCustomInfo3 Cfg, "Description", 30, ""

                      CmpDoc.CustomInfo2(Cfg, "Description") = Description

                      ''Added Project Title Custom Property as read from assembly

                      CmpDoc.AddCustomInfo3 Cfg, "ProjectTitle", 30, ""

                      CmpDoc.CustomInfo2(Cfg, "ProjectTitle") = ProjTitle

                      ''Added Station Title Custom Property as read from assembly

                      CmpDoc.AddCustomInfo3 Cfg, "StationTitle", 30, ""

                      CmpDoc.CustomInfo2(Cfg, "StationTitle") = StatTitle

                      ''Add Part Number property from filename

                      CmpDoc.AddCustomInfo3 Cfg, "PartNo", 30, ""

                      CmpDoc.CustomInfo2(Cfg, "PartNo") = Right(CmpDoc.GetTitle, 4)

                      ''Add Weldment Part Number property from filename

                      CmpDoc.AddCustomInfo3 Cfg, "WeldmentNo", 30, ""

                      CmpDoc.CustomInfo2(Cfg, "WeldmentNo") = Right(CmpDoc.GetTitle, 1)

                      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: Quantity in Part from Assembly: Excluded from BOM
                          Santosh Pawar

                          try this

                           

                          Boolstatus = tCmp.ExcludeFromBOM

                          If Boolstatus Then

                          GoTo nextiteration
                          Else
                          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

                          End If
                          nextiteration:
                          Next j

                            • Re: Quantity in Part from Assembly: Excluded from BOM
                              Jared Huffman

                              That fixed it.  Thanks for the help. 

                                • Re: Quantity in Part from Assembly: Excluded from BOM
                                  Nick Hayman

                                  Hi Jared,

                                   

                                  Very happy you've managed to work this out. Helps me a lot! Thank you.

                                   

                                  I have different configurations (linked to display states) where each configuration has different sub assemblies excluded from BOM. If I was to active configuration 1 would the drawings automatically change quantities? So If I was to click on configuration 2 would the drawing quantities then automatically update to that configuration?

                                   

                                  Basically if I activated one of my configurations would the drawing quantities update according to the configuration?

                                    • Re: Quantity in Part from Assembly: Excluded from BOM
                                      Nick Hayman

                                      Santosh,

                                       

                                      Do I replace the code shown below in green with the code shown below in red/orange? Is this the idea?

                                       

                                       

                                      Code in the attached file on previous forum:

                                       

                                      Suppression = 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

                                       

                                       

                                       

                                      New Code:

                                       

                                      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 ProjTitle As String 'The variable for storing the assembly Project Title

                                      Dim StatTitle As String 'The variable for storing the assembly Station Title

                                      Dim Description As String ' The variable for setting the description custom property

                                      Dim StockDescription As String ' The variable for reading the stock description custom property

                                      Dim Weldment As String ' The variable for setting the weldment custom property

                                      Dim DescriptionOverride As String ' The variable for reading the description override custom property

                                      Dim Boolstatus As Boolean

                                      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

                                      ProjTitle = myAsy.CustomInfo2(Cfg, "ProjectTitle") 'Read the project title from the assembly

                                      StatTitle = myAsy.CustomInfo2(Cfg, "StationTitle") 'Read the station title from the assembly

                                      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)

                                      Boolstatus = tCmp.ExcludeFromBOM

                                      If Boolstatus Then

                                      GoTo nextiteration
                                      Else
                                      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

                                      End If
                                      nextiteration:
                                      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

                                      ''Define Stock Description for machined parts

                                      StockDescription = "$PRP:" & Chr(34) & "HasDiameter" & Chr(34) & "$PRP:" & Chr(34) & "StockThickness" & Chr(34) & "$PRP:" & Chr(34) & "HasWidth" & Chr(34) & "$PRP:" & Chr(34) & "StockWidth" & Chr(34) & "$PRP:" & Chr(34) & "HasWall" & Chr(34) & "$PRP:" & Chr(34) & "StockWall" & Chr(34) & " x $PRP:" & Chr(34) & "StockLength" & Chr(34) & " LG"

                                      ''Figure out which type of description to show

                                      Weldment = CmpDoc.CustomInfo2(Cfg, "Weldment")

                                      DescriptionOverride = CmpDoc.CustomInfo2(Cfg, "DescriptionOverride")

                                      If DescriptionOverride <> "" Then

                                      Description = DescriptionOverride

                                      Else

                                      If Weldment <> "" Then

                                      Description = Weldment

                                      Else

                                      Description = StockDescription

                                      End If

                                      End If

                                      ''Add Description custom property and assign it

                                      CmpDoc.AddCustomInfo3 Cfg, "Description", 30, ""

                                      CmpDoc.CustomInfo2(Cfg, "Description") = Description

                                      ''Added Project Title Custom Property as read from assembly

                                      CmpDoc.AddCustomInfo3 Cfg, "ProjectTitle", 30, ""

                                      CmpDoc.CustomInfo2(Cfg, "ProjectTitle") = ProjTitle

                                      ''Added Station Title Custom Property as read from assembly

                                      CmpDoc.AddCustomInfo3 Cfg, "StationTitle", 30, ""

                                      CmpDoc.CustomInfo2(Cfg, "StationTitle") = StatTitle

                                      ''Add Part Number property from filename

                                      CmpDoc.AddCustomInfo3 Cfg, "PartNo", 30, ""

                                      CmpDoc.CustomInfo2(Cfg, "PartNo") = Right(CmpDoc.GetTitle, 4)

                                      ''Add Weldment Part Number property from filename

                                      CmpDoc.AddCustomInfo3 Cfg, "WeldmentNo", 30, ""

                                      CmpDoc.CustomInfo2(Cfg, "WeldmentNo") = Right(CmpDoc.GetTitle, 1)

                                      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

                                       

                                      Many Thanks

                                      Nick