|
加入QQ群
参与讨论和学习
或扫描二维码加入
这是我自己写的统计标准件的代码,请参考
ISldWorks swApp = UtilityForSW.ConnectSW();
ModelDoc2 swModel = default(ModelDoc2);
SelectionMgr swSeleMgr = default(SelectionMgr);
ModelDocExtension swModelDocExt = default(ModelDocExtension);
BomTableAnnotation swBOMAnnotation = default(BomTableAnnotation);
TableAnnotation swTable = default(TableAnnotation);
string TemplateName = null;
bool boolstatus = false;
int nNumCol = 0;//总列数
int nNumRow = 0;//总行数
int BomType = (int)swBomType_e.swBomType_PartsOnly;
string Configuration = "";
if (txtConfigName.Text == "")
{
Configuration = "默认";
}
else
{
Configuration = txtConfigName.Text;
}
TemplateName = @"C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\lang\chinese-simplified\材料明细表.sldbomtbt";
swModel = (ModelDoc2)swApp.ActiveDoc;
if (swModel.GetType() != (int)swDocumentTypes_e.swDocASSEMBLY)
{
MessageBox.Show("当前文档不是装配体!\r\n请打开一个装配体再进行操作。", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
bool result = false;
List<string> tableLst = new List<string>();
result = TableListOfMaterials(swModel, out tableLst);
if (result) //存在表格
{
foreach (var item in tableLst)
{
if (item.Contains ("材料明细表")) //存在材料明细表
{
swSeleMgr = (SelectionMgr)swModel.SelectionManager;
boolstatus = swModel.Extension.SelectByID2(item, "BOMFEATURE", 0, 0, 0, false, 0, null, 0);
BomFeature swBomFeat = (BomFeature)swSeleMgr.GetSelectedObject2(1);
object[] vTableArr = null;
object vTable = null;
vTableArr = (object[])swBomFeat.GetTableAnnotations();
foreach (TableAnnotation vTable_loopVariable in vTableArr)
{
vTable = vTable_loopVariable;
swTable = (TableAnnotation)vTable;
}
nNumCol = swTable.ColumnCount;
nNumRow = swTable.RowCount;
for (int m = 0; m < nNumRow; m++)
{
if (swTable.DisplayedText[m, 1].Contains("GB") || swTable.DisplayedText[m, 1].Contains("JB") || swTable.DisplayedText[m, 1].Contains("HG"))
{
int index = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[0].Value = swTable.DisplayedText[m, 2];//标准件名称
this.dataGridView1.Rows[index].Cells[1].Value = swTable.DisplayedText[m, 1];//标准件代号
this.dataGridView1.Rows[index].Cells[2].Value = swTable.DisplayedText[m, 7];//标准件规格(备注)
this.dataGridView1.Rows[index].Cells[3].Value = swTable.DisplayedText[m, 4];//标准件材料
this.dataGridView1.Rows[index].Cells[4].Value = swTable.DisplayedText[m, 3];//标准件数量
}
}
}
else //不存在材料明细表
{
try
{
swModelDocExt = swModel.Extension;
// 插入BOM表
swBOMAnnotation = swModelDocExt.InsertBomTable2(TemplateName, 400, 300, BomType, Configuration, true);
swTable = (TableAnnotation)swBOMAnnotation;
nNumCol = swTable.ColumnCount;
nNumRow = swTable.RowCount;
for (int m = 0; m < nNumRow; m++)
{
if (swTable.DisplayedText[m, 1].Contains("GB") || swTable.DisplayedText[m, 1].Contains("JB") || swTable.DisplayedText[m, 1].Contains("HG"))
{
int index = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[0].Value = swTable.DisplayedText[m, 2];//标准件名称
this.dataGridView1.Rows[index].Cells[1].Value = swTable.DisplayedText[m, 1];//标准件代号
this.dataGridView1.Rows[index].Cells[2].Value = swTable.DisplayedText[m, 7];//标准件规格(备注)
this.dataGridView1.Rows[index].Cells[3].Value = swTable.DisplayedText[m, 4];//标准件材料
this.dataGridView1.Rows[index].Cells[4].Value = swTable.DisplayedText[m, 3];//标准件数量
}
}
boolstatus = swModel.Extension.SelectByID2("材料明细表1", "BOMFEATURE", 0, 0, 0, false, 0, null, 0);
swModel.EditDelete();
}
catch
{
}
}
}
}
else //不存在表格
{
try
{
swModelDocExt = swModel.Extension;
// 插入BOM表
swBOMAnnotation = swModelDocExt.InsertBomTable2(TemplateName, 400, 300, BomType, Configuration, true);
swTable = (TableAnnotation)swBOMAnnotation;
nNumCol = swTable.ColumnCount;
nNumRow = swTable.RowCount;
for (int m = 0; m < nNumRow; m++)
{
if (swTable.DisplayedText[m, 1].Contains("GB") || swTable.DisplayedText[m, 1].Contains("JB") || swTable.DisplayedText[m, 1].Contains("HG"))
{
int index = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[0].Value = swTable.DisplayedText[m, 2];//标准件名称
this.dataGridView1.Rows[index].Cells[1].Value = swTable.DisplayedText[m, 1];//标准件代号
this.dataGridView1.Rows[index].Cells[2].Value = swTable.DisplayedText[m, 7];//标准件规格(备注)
this.dataGridView1.Rows[index].Cells[3].Value = swTable.DisplayedText[m, 4];//标准件材料
this.dataGridView1.Rows[index].Cells[4].Value = swTable.DisplayedText[m, 3];//标准件数量
}
}
boolstatus = swModel.Extension.SelectByID2("材料明细表1", "BOMFEATURE", 0, 0, 0, false, 0, null, 0);
swModel.EditDelete();
}
catch
{
}
} |
|