Get BOM Tables (C#)
This example shows how to get the BOM tables in a drawing document. This
example also shows how to get each BOM table's name, display status, whether
components with multiple configurations appear as one item in the BOM table, and
the part configuration grouping setting.
//------------------------------------------------------------------
// Preconditions:
// 1. Read the SolidWorks Document Manager API Help "Getting Started"
// topic and register the required DLLs.
// 2. Load the latest SolidWorks.Interop.swdocumentmgr.dll
// interop assembly by:
// a. Right-clicking the project name in the Project Explorer.
// b. Clicking Add Reference.
// c. Selecting the interop assembly on the .NET
// tab or browsing for the DLL in install_dir/api/redist/CLR2.
// 3. Substitute your license key for "your_license_key" in the code.
// 4. Make sure that the drawing document opened by this application
// exists.
// 5. Open the Immediate window.
//
// Postconditions: Printed to the Immediate window:
// * Document's path and file name
// * Date the document was last saved
// * Name of the BOM table
// * Whether the BOM table is hidden
// * Whether the BOM table contains components with multiple configurations,
// and whether to display the configurations as one item
// * Part configuration grouping
//
// NOTE: This sample application was developed using
// Microsoft Visual Studio 2008. If you use another version of
// Microsoft Visual Studio, you might need to add more references to get
// this application to compile.
//------------------------------------------------------------------
using
SolidWorks.Interop.swdocumentmgr;
using
System;
using
System.Diagnostics;
namespace
swDMTableCSharp
{
class
Module1
{
static
void Main()
{
SwDMClassFactory
dmClassFact = default(SwDMClassFactory);
SwDMApplication3
dmDocMgr = default(SwDMApplication3);
SwDMDocument15
dmDoc = default(SwDMDocument15);
ISwDMTable4
dmTable = default(SwDMTable4);
SwDmDocumentOpenError
dmError = default(SwDmDocumentOpenError);
string
nameDrawing = "";
dmClassFact = new
SwDMClassFactory();
dmDocMgr = (SwDMApplication3)dmClassFact.GetApplication("your_license_key");
//Do not distribute
nameDrawing =
"C:\\Program Files\\SolidWorks
Corp\\SolidWorks\\samples\\introsw\\cabinet_bath.slddrw";
//Get the SolidWorks drawing
document
dmDoc = (SwDMDocument15)dmDocMgr.GetDocument(nameDrawing,
SwDmDocumentType.swDmDocumentDrawing,
false,
out dmError);
//Debug.Print("Document's last
saved date: " & dmDoc.LastSavedDate)
Debug.Print("Document's
full name: " + dmDoc.FullName);
Debug.Print("Date
document last saved: " + dmDoc.LastSavedDate);
// Get the names of the BOM
tables in the SolidWorks drawing document
string[]
tableNames = null;
tableNames = (string[])dmDoc.GetTableNames(SwDmTableType.swDmTableTypeBOM);
int
i = 0;
if
((tableNames != null))
{
for
(i = 0; i <= tableNames.Length-1; i++)
{
dmTable = (SwDMTable4)dmDoc.GetTable(tableNames[i]);
// Get the BOM table
names
//
Determine whether:
//
* BOM table is hidden,
//
* Configurations appear with same
//
item number if BOM table contains components
//
with multiple configurations, and
//
Get the component configuration grouping
Debug.Print("
BOM table name: " + dmTable.Name);
Debug.Print("
Is BOM table hidden? " + dmTable.Hidden);
Debug.Print("
If BOM table contains components with multiple configurations, display as
one item? " + dmTable.DisplayAsOneItem);
Debug.Print("
Part configuration grouping? " + dmTable.PartConfigurationGrouping);
}
}
}
}
}