File name of a part/assembly selected in a BOM

Programming and macros
Rojnin
Posts: 2
Joined: Tue Oct 17, 2023 8:19 am
Answers: 0
x 1

File name of a part/assembly selected in a BOM

Unread post 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.
User avatar
JSculley
Posts: 574
Joined: Tue May 04, 2021 7:28 am
Answers: 52
x 7
x 805

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

Unread post 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();
}                                                                                          
Rojnin
Posts: 2
Joined: Tue Oct 17, 2023 8:19 am
Answers: 0
x 1

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

Unread post by Rojnin »

Thanks JSculley,

I will try to use your code for my macro.
Post Reply