34 Replies Latest reply on Jun 9, 2018 11:59 AM by Deepak Gupta

    Creating BOM, either BOMtype (top level only) is correct or Configuration, not both.

    Robert Voogt

      Important update.

       

      If I set
      Set swBomAnn = swView.InsertBomTable4(True, 0, 0, 4, 2, configuration, bomTemplate, False, 0, False)

       

      to something other than Top-level only, then the configuration is correct!!!

       

      It seems that either the configuration is correct, or the bomtype...

       

      Now I need to find out if it is possible to set the bomtype to Top-level only after creating the BOM.

       

       

      Hi all,

       

      when I use this macro, see link, solidworks opens an example model and shows the configurations in the model (immediate window).

      2015 SOLIDWORKS API Help - Get List of Configurations Example (VBA)

       

      When I apply this to the current open file, I change the following:

       

      I remove "filename = "..." "and change "Set swModel = swApp.OpenDoc6 (etc)" to "Set swModel = swApp.ActiveDoc"

       

      Now, when I run it, I get an error here:

      Debug.Print "  Name of active configuration = " & swConfig.Name

      error is Object variable or With block variable not set.

       

      This macro does print swModel.GetPathName correctly.

       

      What could be the problem?

        • Re: swConfig.name doesn't work on activedoc
          Nilesh Patel

          Hi Robert,

           

          Below codes works perfect for me:

           

          Option Explicit
          Sub main()
              Dim swApp As SldWorks.SldWorks
              Dim swModel As SldWorks.ModelDoc2
              Dim configNames()  As String
              Dim configName As String
              Dim swConfig As SldWorks.Configuration
              Dim i As Long
              Dim errors As Long
              Dim warnings As Long
              Dim fileName As String
              
              Set swApp = Application.SldWorks
              Set swModel = swApp.ActiveDoc
              'Get and print active model path and name
              Debug.Print "File = " & swModel.GetPathName
              Debug.Print ""
              'Get and print name of active configuration
              Set swConfig = swModel.GetActiveConfiguration
              Debug.Print "  Name of active configuration = " & swConfig.Name
              Debug.Print ""
             
              'Get and print names of all configurations
              configNames = swModel.GetConfigurationNames
              For i = 0 To UBound(configNames)
                  configName = configNames(i)
                  Set swConfig = swModel.GetConfigurationByName(configName)
                  Debug.Print "  Name of configuration(" & i & ") = " & configName
                  Debug.Print "    Use alternate name in BOM  = " & swConfig.UseAlternateNameInBOM
                  Debug.Print "    Alternate name             = " + swConfig.AlternateName
                  Debug.Print "    Comment                    = " + swConfig.Comment
                  Debug.Print ""
              Next i
          End Sub
          

           

          Are you sure you are not running the macro with drawing document active?

           

          Regards,

          Nilesh

          • Re: swConfig.name doesn't work on activedoc
            Josh Brady

            Please paste your code.  Then we won't have to guess what the problem might be.

              • Re: swConfig.name doesn't work on activedoc
                Robert Voogt

                What Nilesh posted.

                  • Re: swConfig.name doesn't work on activedoc
                    Josh Brady

                    OK... Your problem is that drawings do not have configurations.  Parts and assemblies have configurations.  You need to get a pointer to the ModelDoc2 referenced by one of the Views in the drawing. 

                      • Re: swConfig.name doesn't work on activedoc
                        Robert Voogt

                        Is that also the reason why it is so hard to find the configuration used by the BOM in the drawing? I only need that name so I can use it as input when I replace that BOM with the new one. (Is going to be a lot of drawings, so I do not want to select those configurations by hand...)

                          • Re: swConfig.name doesn't work on activedoc
                            Deepak Gupta

                            You need to use swBomFeat.GetConfigurations method. Here are two post with examples (you would have to modify the codes to suit your needs)

                             

                            API/Macro for updating a BOM

                            Set configuration when inserting BOM into drawing

                             

                             

                              • Re: swConfig.name doesn't work on activedoc
                                Robert Voogt

                                Dear all,

                                 

                                I have this at the moment:

                                 

                                 

                                        boolstatus = Part.Extension.SelectByID2("Sheet1", "SHEET", 0, 0, 0, False, 0, Nothing, 0)
                                        Part.ClearSelection2 True
                                        Set swView = Part.GetCurrentSheet.GetViews()(1)
                                        configuration = swView.ReferencedConfiguration
                                        bomTemplate = "D:\Spare Part Generator\System Files\BOMMANUALS.sldbomtbt"
                                              
                                        Set swBomAnn = swView.InsertBomTable4(True, 0, 0, 4, 2, configuration, bomTemplate, False, 1, True)
                                        swModel.ClearSelection2 True
                                        Set swBomFeat = swBomAnn.BomFeature
                                        Names = swBomFeat.GetConfigurations(False, Visible)
                                        Visible(0) = True
                                        boolstatus = swBomFeat.SetConfigurations(True, Visible, Names)
                                

                                This adds the new BOMtemplate, but does not use the old BOMs' configuration. I would like to have the 'GetConfigurations' before I insert the new BOM. Is that possible?

                                 

                                I guess what I need is this:

                                        Set swBomFeat = swBomAnn.BomFeature

                                        Names = swBomFeat.GetConfigurations(False, Visible)

                                somewhere before deleting the old BOM and inserting the new BOM, and point swBomAnn to the old BOM. I do not know how to do that. (Our BOM names differ, so it should select the exsisting BOM in the drawing and there is always just 1 visible in our drawings, sometimes a few BOM's are hidden.)

                                 

                                Then, the configuration that comes out of the "Getconfigurations" should be inserted in the "Configuration = ". Because "configuration = swView.ReferencedConfiguration" is not always the same as the configuration being used by the old BOM. (for example, we have a configuration 'default' for when engineers work with larger assemblies (the bolts, etc are missing) and a configuration 'with fasteners' for the drawing. This makes the loading time when working on the machine a bit better)

                                  • Re: swConfig.name doesn't work on activedoc
                                    Robert Voogt

                                    I got the used configuration by using this:

                                     

                                            Set swView = Part.GetCurrentSheet.GetViews()(1)

                                            configuration = swView.ReferencedConfiguration

                                     

                                    and using this:

                                            Set swBomAnn = swView.InsertBomTable4(True, 0, 0, 4, 2, configuration, bomTemplate, False, 1, True)

                                     

                                    creates a BOM, but still not with the correct configuration...

                                    (when I debug, I can see that in the line with "Set swBomAnn"  'configuration' does show the correct configuration name.)

                                    • Re: swConfig.name doesn't work on activedoc
                                      Deepak Gupta

                                      Robert, you can refer to the macro codes in this post API/Macro for updating a BOM to either find the BOM configuration or update it later.

                                      • Re: swConfig.name doesn't work on activedoc
                                        Josh Brady

                                        Robert,

                                         

                                        Let's think about this logically.  On your sheet, you have one BOM that is visible.  You many have multiple BOMs, but the others are hidden.  The ONLY way to find out the configuration referenced by the visible BOM is to find the visible BOM and read its configuration.

                                         

                                        There are a number of ways to find BOM tables through the API.  One way is shown in the example for "Get components in Each BOM Table Row" in the API help under the IBomTableAnnotation Interface topic.  Read that macro line by line.  Understand what each line does, then you can implement it in your code.  Note that the starting point of that macro will be Sub main().

                                         

                                        Once you find the BOM, you can read its configurations using the code you mentioned already. 

                                         

                                        The code to set configurations is indeed on the page referenced.  Read all the posts, understand what's going on, and you will find it. 

                                          • Re: swConfig.name doesn't work on activedoc
                                            Robert Voogt

                                            Hi Josh,

                                             

                                            I can find the used BOM configuration now:

                                             

                                                    swModel.ClearSelection2 True

                                                    Set swView = Part.GetCurrentSheet.GetViews()(0)

                                                    configuration = swView.ReferencedConfiguration

                                             

                                            But whatever I try to get 'configuration' set as the configuration doesn't work. Not setting the configuration in the InsertBomTable4 or after this adding:

                                             

                                                    Dim BOMFeat As BomFeature

                                                    Set BOMFeat = swModel.SelectionManager.GetSelectedObject6(1, -1)

                                                    Dim confNme(0) As String

                                                    confNme(0) = configuration

                                                    Visible = True

                                                    boolstatus = BOMFeat.SetConfigurations(True, Visible, confNme)

                                             

                                            (I get an error on boolstatus; object variable of with block variable not set)

                                              • Re: swConfig.name doesn't work on activedoc
                                                Deepak Gupta

                                                Not on my machine but this line

                                                boolstatus = BOMFeat.SetConfigurations(True, Visible, confNme)

                                                should be

                                                boolstatus = BOMFeat.SetConfigurations(True, Visible, confNme(0))
                                                  • Re: swConfig.name doesn't work on activedoc
                                                    Robert Voogt

                                                    Hi Deepak,

                                                     

                                                    Tried that as well. Still get the error:

                                                     

                                                     

                                                    '   Retrieving BOM Configuration

                                                            swModel.ClearSelection2 True

                                                            Set swView = Part.GetCurrentSheet.GetViews()(0)

                                                            configuration = swView.ReferencedConfiguration           'shows the correct configuration

                                                    '   Insert new BOM

                                                            boolstatus = Part.Extension.SelectByID2("Sheet1", "SHEET", 0, 0, 0, False, 0, Nothing, 0)

                                                            Part.ClearSelection2 True

                                                            bomTemplate = "D:\Spare Part Generator\System Files\BOMMANUALS.sldbomtbt"

                                                               

                                                            Set swBomAnn = swView.InsertBomTable4(True, 0, 0, 4, 2, configuration, bomTemplate, False, 0, False)

                                                            swModel.ClearSelection2 True

                                                        

                                                     

                                                     

                                                            Dim BOMFeat As BomFeature

                                                            Set BOMFeat = swModel.SelectionManager.GetSelectedObject6(1, -1)

                                                            Dim confNme(0) As String

                                                            confNme(0) = configuration

                                                            Visible = True

                                                            boolstatus = BOMFeat.SetConfigurations(True, Visible, confNme(0))   ' gives an error

                                                            Part.ForceRebuild3 True

                                                     

                                                    It still uses the default BOM and not the value of configuration.

                                                    ps I use SW 2015

                                  • Re: swConfig.name doesn't work on activedoc
                                    Ivana Kolin

                                    where is set swConfig = .....