Assembly BOM saved as txt or .xls

Have found some code that will create a BOM Table in an assembly, it then exports it as a text file and then deletes the BOM from the assembly.

It works ok except that it creates a new table each time it is run.

I would like to do something similar, but the BOM Table will already exist in the assembly. How do I get the BOM Table so that I can export it out to a text file or even better a .xls file?

thanks Andrew.

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swBOMTable As SldWorks.BomTableAnnotation
Dim swTable As SldWorks.TableAnnotation
Dim swAnn As SldWorks.Annotation

Const BOMTemplate As String = "C:\\templates\\parts list.sldbomtbt"
Const OutputPath As String = "C:\\"

Sub main()

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swBOMTable = swModel.Extension.InsertBomTable(BOMTemplate, 0, 0, swBomType_e.swBomType_PartsOnly, "")
    Set swTable = swBOMTable
  
    swTable.SaveAsText OutputPath & "BOMTable_" & swModel.GetTitle() & ".txt", "Default" 
    Set swAnn = swTable.GetAnnotation
  
    swAnn.Select3 False, Nothing

    'swModel.EditDelete          'not used in this example.

End Sub

SolidworksApi macros
Answers 18
AW
AW 2009-12-08 Best Answer

Two options for you without a Macro.

Select the BOM so it has focus then go to File > Save As and select the File Type you wish to save to.

Select the BOM in the top left corner cross and RMB > Save As

See attached images....

Cheers,

Anna

AJ
AJ 2009-12-08

Thanks Anna, but I would rather use a one click macro that would then process the file to generate a .xls parts list and manufacturing bom.

I have done a version that is created from a drawing, but I want to try to eliminate the drawing process and create from the assembly if possible.

My plan is to have an indented BOM manually inserted into the assembly. We would then tidy up the appearance (hide some rows & sort the item numbers etc).

By having the bom in the assembly, it will be available to our manufacturing people as well when we save the assembly as an eDrawing.

thanks

Andrew

AW
AW 2009-12-09

Should be very doable.  Just a matter of knowing the code.....

I am not much of a programmer beyond cut and paste myself, so can't offer much help.

Cheers,

Anna

DM
DM 2009-12-09

Try this. It is copied from sections of another program I have so it is not "clean." You will need to change the folder and file paths to your BOM template paths. It creates a BOM in an assembly and copies the table annotation then deletes the BOM from the assembly. The next sub writes the BOM to an excel sheet.

I hope this helps

Dan Miel

PS Anna you are futher that I am in understanding this stuff.

DM
DM 2009-12-09

I missed the part in your note that said the BOM is already in the assembly. This file will find the BOM then delete the BOM copies the bom to an Excel sheet. (It does not delete the BOM.)

Dan Miel

AJ
AJ 2009-12-09

Excellent, thanks Dan.

Works perfectly to create an .xls file for us to process further into a parts list.

cheers

Andrew

SF
SF 2011-08-17

Hi, Dan

I'm realy struggling to do the same in a Drawing document. I don't want to do a savas .txt or xls of the BOM. I want to use bomtable.GetEntryText

Any thougts? I tried changing your code but I get lost between BomFeature, BomTable and BomTableAnnotation.

Thanks.

DM
DM 2011-08-18

Martin,

     I took a look and I'm a bit lost on this. I can get the Bom and the values of the cell but I cannot get the Attach3 to work which must be set before the GetEntryText. Have you tried using the SetEntryText and see if that gives a clue how it works.

     What is the difference between getting the actual cell text and the GetEntryText?

Dan Miel

SF
SF 2011-08-19

Dan,

You said you were able to get the values in the BOM? What command did you use?

I still  havn't figured out the differece between BomFeature, BomTable and BomTableAnnotation. I find the help files so NOT helpful sometimes. They should really give more samples or explanation on HOW TO use the functions. AARRGGGHH!!!

Martin

FV
FV 2011-08-19

Hi Dan,

what happens when you call the Attach3 method?  You only need to call it once, run all your code and Detach when your done.

As for the difference between the GetEntryText and GetEntryValue...

GetEntryText: The actual string of text input by the user or "solidworks rule"...

GetEntryValue: Evaluated string...that reflects the output of the "solidworks rule"

When I say solidworks rule, I'm reffering to internal dynamic links, such as custom properties, global variables, file info, etc.

Cheers,

Filipe

SF
SF 2011-08-19

We're gettin somewhere. Thanks Filipe.

I was bitchin' earlier about the help files... .. and then i found this VBA example......

http://help.solidworks.com/2011/English/api/sldworksapi/Get_Components_in_Each_BOM_Table_Row_VB.htm

But I am on 2009 so the command GetComponentsCount2 was replaced with GetComponentsCount.. (which has less arguments).

I put a lot of comments to try to understand everything.

I made it into a .swp.

To test it make sure you have:

1. A drawing open with a BOM. This drawing also has to be the active document... (not the assembly)

2. Your immediate window is visible

It doesn't get the values like I really want to, but it does use the "BomFeature" and "BomTableAnnotation" commands. It's a good start to understaning all of this.

DM
DM 2011-08-19

This should find the bom then print it to the immediate window. It finds only the bom on the active page. To read all bom's on all pages you need to loop through all sheets.

I hope this helps

Dan

SF
SF 2011-08-21

Dan,

This is beautiful!!! It's simple and elegant! Exactly what I need.

It even omits rows that are hidden.  It takes a picture of what you see. Perfect!

You rock.

Martin

SF
SF 2011-08-21

General question:

If we can get the information from the BOM, like in Dan's example, why do the commands GetEntryText , GetEntryValue, Attach3 ..... exist?

Martin

WM
WM 2011-08-21

They are newer mothods that are typically more powerful.

DM
DM 2011-08-21

It has been a year or so since I wrote something to get information from a BOM. I can see why it would be a benefit to use GetEntryValue if a person wants to see what creates the BOM output but if a person just wants the BOM’s output I believe what I wrote is all that is needed. I was not able to find an example of how to use Attach3 and I don’t have time right now to explore how to use it, so yes, I agree that the SW API help is sometimes lacking and in 2009, 2010 I believe it was worse than before. I have just started using 2011.  As Wayne says, the newer codes are normally more powerful but without examples on how to use them a person can waste a lot of time trying to use code which may not benefit the project they are working on.

Dan Miel

YN
YN 2012-02-26

Thanks, your code.

'Written 08-18-2011 Dan Miel
'Loops though tables on active sheet to find BOM
'Prints text to Immediate window


Dim swApp As SldWorks.SldWorks
Dim DrawDoc As SldWorks.DrawingDoc
Dim TableAnn As SldWorks.TableAnnotation
Dim BOM As SldWorks.BomTableAnnotation '.TableAnnotation ' This can be either type
Dim View As SldWorks.View

Sub main()

Set swApp = Application.SldWorks
Set DrawDoc = swApp.ActiveDoc
Set View = DrawDoc.GetFirstView ' this is the sheet
Set TableAnn = View.GetFirstTableAnnotation

Do While Not TableAnn Is Nothing
    If TableAnn.Type = 2 Then '2 is the bom table type
        Set BOM = TableAnn
        Exit Do
    End If
    Set TableAnn = TableAnn.GetNext
Loop

If BOM Is Nothing Then
    Debug.Print ("BOM was not found")
Else
    Dim Txt As String
    Dim Row As Long
    Dim Col As Long
    For Row = 0 To BOM.RowCount - 1
    Txt = ""
        For Col = 0 To BOM.ColumnCount - 1
            Txt = Txt & BOM.Text(Row, Col) & vbTab
        Next Col
        Debug.Print Txt
    Next Row
End If
Debug.Print "Done"
End Sub

------

debug.print Result

6           1   Q345R       49.14      
5           2   20  4.58    9.16       
4   HG/T20592-2009   法兰 WN250-25(B) RF    2   16MnII  11.33   22.66      
3           1   Q345R       73.85      
2           1   Q345R       200.58     
1           1   Q345R       118.72     
序号  标 准 号    名        称     数量  材    料    质量(单)    质量(合)    备 注  
Done