Get Table Cell Text Example (C#)
This example shows how to get all of the cell text
from a BOM table using the SolidWorks Document Manager API.
// -------------------------------------------------------------------------
// Preconditions:
// 1. Read the SolidWorks Document Manager API Help
// Getting
Started topic and ensure that the
// required DLLs are registered.
// 2. Ensure that the latest SolidWorks.Interop.swdocumentmgr.dll
//
interop assembly is loaded in the project
// (right-click
on the project in Project Explorer, click
// Add Reference, select
the interop assembly from the .NET
// tab or browse for the DLL in <SolidWorks_install_dir>/api/redist
// directory).
// 3. In SolidWorks, create a part document with a BOM
table.
// 4. Save the part and close it.
// 5. Substitute your license key for "your_license_key"
in the code.
// 6. Substitute the new file name for sFile in the code.
//
// Postconditions: The Immediate Window displays the BOM
// table row and
column count and all of the table's cell
// text in row order.
//---------------------------------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.swdocumentmgr;
using System;
using System.Diagnostics;
partial
class SolidWorksMacro
{
SwDMClassFactory
classfac;
SwDMApplication
tapp;
SwDMDocument13
swDoc;
SwDMTable3
swTable;
SwDmDocumentOpenError
e;
string
sFile;
String[]
vTables;
String[]
vTabArr;
SwDmTableError
err;
int
row;
int
col;
int
j;
public
void Main()
{
classfac
= new SwDMClassFactory();
tapp
= classfac.GetApplication("your_license_key");
//license
needed please do not distribute this
sFile =
"C:\\temp\\Part1.SLDPRT";
//get
the SW document file
swDoc = (SwDMDocument13)tapp.GetDocument(sFile,
SwDmDocumentType.swDmDocumentPart, false, out e);
//swDoc
= (SwDMDocument13)tapp.GetDocument(sFile, SwDmDocumentType.swDmDocumentAssembly,
false, out e)
//swDoc
= (SwDMDocument13)tapp.GetDocument(sFile, SwDmDocumentType.swDmDocumentDrawing,
false, out e)
Debug.Print("Doc
Version is " + swDoc.GetVersion());
Debug.Print("Doc
name is " + swDoc.FullName);
vTables
= (String[])swDoc.GetTableNames(SwDmTableType.swDmTableTypeBOM);
if
((vTables != null))
{
swTable
= (SwDMTable3)swDoc.GetTable((String)vTables[0]);
if
((swTable != null))
{
Debug.Print("Table
retrieved is " + vTables[0]);
vTabArr
= (String[])swTable.GetTableCellText(out
err, out row, out col);
Debug.Print("Row
count is " + row);
Debug.Print("Column
count is " + col);
Debug.Print("Table
cell text:");
for
(j = 0; j <= vTabArr.GetUpperBound(0); j++)
{
Debug.Print("
" + vTabArr[j]);
}
}
}
swDoc.CloseDoc();
}
public
SldWorks swApp;
}