Page 1 of 1

File name of a part/assembly selected in a BOM

Posted: Tue Oct 17, 2023 8:30 am
by Rojnin
Hello,

Is it possible to get the file name of a part/assembly selected in a BOM?

I would like to run a macro when a line/case in a BOM is selected. This macro will change some custom properties of the selected file.
However, I can't find a source that tells me how to get the file name.

Thanks in advance.

Re: File name of a part/assembly selected in a BOM

Posted: Tue Oct 17, 2023 11:16 am
by JSculley
TableAnnotation::GetCellRange() in conjunction with BOMTableAnnotation::GetComponents() will do the trick. Here's some sample code in C#:

Code: Select all

ModelDoc2 mDoc = swApp.ActiveDoc as ModelDoc2;       
SelectionMgr selMgr = mDoc.SelectionManager as SelectionMgr;                  
int type = selMgr.GetSelectedObjectType3(1, -1);
if (type != (int)swSelectType_e.swSelANNOTATIONTABLES)
{
	//No table selected
	return;
}
TableAnnotation tAnn = selMgr.GetSelectedObject6(1, -1) as TableAnnotation;
if (tAnn == null)
{
	//Something bad happened
	return;
}
if (tAnn.Type  != (int)swTableAnnotationType_e.swTableAnnotation_BillOfMaterials)
{
	//Not a BOM table
	return;
}
int firstRow = 0;
int lastRow = 0;
int firstColumn = 0;
int lastColumn = 0;
tAnn.GetCellRange(ref firstRow, ref lastRow, ref firstColumn, ref lastColumn);
if (firstRow == lastRow)
{
   //single row selected
   BomTableAnnotation bta = tAnn as BomTableAnnotation;
   object[] compObjArray = bta.GetComponents(firstRow) as object[];
   Component2 comp = compObjArray[0] as Component2;
   ModelDoc2 compDoc = comp.GetModelDoc2() as ModelDoc2;
   string compPath = compDoc.GetPathName();
}                                                                                          

Re: File name of a part/assembly selected in a BOM

Posted: Wed Oct 18, 2023 1:54 am
by Rojnin
Thanks JSculley,

I will try to use your code for my macro.