3 Replies Latest reply on Jun 18, 2014 11:39 AM by Chris Mackedanz

    [API] Manipulation of BOM and Excel

    Leon Domercq

      Hello everyone,

       

      I have to make a macro on solidworks to:

       

           1- create a BOM on an existing assemblie (intended number, detail numbering,.. then default config)

                -> easy to do with the macro recorder
                -> there already are BOM into the parts the the BOM is building without any problems

                -> the BOM is not on a drawing but a table of the assembly

          

           2- Save as this BOM on Excel using the name of the assembly (ex: 1995547.sldasm -> 1995547.xls for the BOM) in a specific folder

       

      I wish I could integrate it to a JAVA software because I have do to that for thousands of files. I'm working with solidworks 2011 premium and I never used VBA before. I tried to use API from other forums...nothing has worked.

       

      Best regards

      Leon

        • Re: [API] Manipulation of BOM and Excel
          Adam Hoffman

          I'm still new to programming and started learning vb.net before the old VB6 language used in SolidWorks macros. So all of my programs are written in Visual Studio 2013 Express using windows forms. I had a need to do something very similiar to what you are needing and to my knowledge SolidWorks does not have a method to programatically save a BOM as an Excel workbook traditionally. You have to save it as an .xls in text format. Then if you need to pretty up the Excel document you can open it and then format it as needed. This is the thread I used to get me started:

           

          https://forum.solidworks.com/message/188696

           

          And here is some VB.Net code to get you going. If you need it translated into VB6 I'm sure there are plenty of guys on this forum that can write this in their sleep.

           

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

          Dim swApp As SldWorks.SldWorks = Nothing

          Dim swModel As SldWorks.ModelDoc2 = Nothing

          Dim swModelExt As SldWorks.ModelDocExtension = Nothing

          Dim swBOMAnnotation As SldWorks.BomTableAnnotation = Nothing

          Dim swBOMFeature As SldWorks.BomFeature = Nothing

          Dim swTableAnno As SldWorks.TableAnnotation = Nothing

           

          Dim ModelName As String = ""

          Dim bExcelResult As Boolean = False

           

          Const BOMTemplate As String = "C:\YourBOMTemplatePath\MyBOMTemplate.sldbomtbt"

          Const xlSavePath As String = "C:\YourExcelPathHere\"

           

          swApp = GetObject(, "SldWorks.Application")

           

          If swApp IsNot Nothing Then

           

               swModel = swApp.ActiveDoc

               Dim DocType As Integer = swModel.GetType()

           

                If Not DocType = 2 Then
                     MessageBox.Show("You Must Run This Macro Inside A SolidWorks Assembly!", _
                                                        "SolidWorks Document Type Error", MessageBoxButtons.OK, _
                                                        MessageBoxIcon.Exclamation)
                     Application.Exit()
                End If

           

                If DocType = 2 Then

           

                      ModelName = swModel.GetPathName()

                      ModelName = Path.GetFileNameWithoutExtension(ModelName)

                     

                      swModelExt = swModel.Extension

                      swBOMAnnotation = swModelExt.InsertBomTable2(BOMTemplate, -1.0, 0.0, _

                                                                                                         swBomType_e.swBomType_PartsOnly, _

                                                                                                         "000-000-000", False)

                       swBOMFeature = swBOMAnnotation.BomFeature

                       swTableAnno = swBOMAnnotation

           

                       Dim xlSaveName As String = xlSavePath + ModelName + ".xls"

                      

                       bExcelResult = swTableAnno.SaveAsText(xlSaveName, vbTab)

                       swBOMFeature.GetFeature.Select2(False, 0)

                       swModelExt.DeleteSelection2(0)

           

                End If

           

          End If

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

           

          This will insert a parts only BOM and save it out as .xls file in vbtab format. But not sure if you are wanting it in SolidWorks macro language or not.

          • Re: [API] Manipulation of BOM and Excel
            Chris Mackedanz

            Search http://www.3dcontentcentral.com/macros.aspx for Bill of Materials export.  There should be a bunch of macros that you can look at at and maybe find one that you can just use.