All Resources
Code SnippetSolidWorks API2026-03-10
Export BOM to CSV from SolidWorks
Extract a Bill of Materials from an active SolidWorks assembly document and write it to a CSV file with part number, description, quantity, and material.
csharp
public void ExportBomToCsv(ModelDoc2 model, string outputPath)
{
var feat = model.FirstFeature();
while (feat != null)
{
if (feat.GetTypeName2() == "BomFeat")
{
var bom = (BomFeature)feat.GetSpecificFeature2();
var table = (BomTableAnnotation)bom.GetTableAnnotations()[0];
using var writer = new StreamWriter(outputPath);
writer.WriteLine("Part Number,Description,Qty,Material");
for (int row = 1; row < table.RowCount; row++)
{
var cells = Enumerable.Range(0, table.ColumnCount)
.Select(col => table.Text[row, col]);
writer.WriteLine(string.Join(",", cells));
}
}
feat = feat.GetNextFeature();
}
}How It Works
This method iterates through all features in a SolidWorks model looking for a BOM (Bill of Materials) feature. When found, it:
- Gets the BOM table annotation — the actual table object with rows and columns
- Writes a CSV header with standard BOM columns: Part Number, Description, Quantity, Material
- Iterates rows (starting from row 1 to skip the header) and joins all cell values with commas
When to Use This
- ERP integration: Exporting BOM data to feed into enterprise systems (SAP, Oracle, custom databases)
- Reporting: Generating procurement lists, cost rollups, or material summaries
- Comparison tools: Exporting BOMs from multiple configurations to diff them
Tips
- Row 0 is always the header row in a SolidWorks BOM table — start your data loop from row 1
- For indented BOMs, the first column often contains the item number with dots (e.g., '1.1', '1.2') — parse accordingly
- Always check that GetTableAnnotations() is not null — BOMs can be deleted or hidden
SolidWorks APIC#BOMCSV