BOM export using API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have written code in C# to automatically export the correct fields of an assembly to the BOM, by importing an XML file with the right fields. When I do this manually, it works: the fields appear in the order I placed them. However, when my C# code is doing this, the order is not the same.
Correct is:
The export in Excel shows (with my code) this:
I thought it worked properly before, however I didn't change anything in my code, so most likely it was always like this and I didn't check.
Is there something I need to add to my code, is this a bug in the API? I recently updated to Inventor 2019.5.1. I will check if this also occurs on an older version.
This is the code:
public void XXXContentList()
{
string progId = "Inventor.Application";
Inventor.Application invApp = (Inventor.Application)Marshal.GetActiveObject(progId);
DocumentTypeEnum docType = invApp.ActiveDocumentType;
AssemblyDocument assemblyDoc;
AssemblyComponentDefinition assemblyCompDef;
BOM bom;
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (docType == DocumentTypeEnum.kAssemblyDocumentObject)
{
assemblyDoc = (AssemblyDocument)invApp.ActiveDocument;
assemblyCompDef = assemblyDoc.ComponentDefinition;
}
else
{
return;
}
//templateName = "All avalaible fields";
saveFileDialog.FileName = "Content List.xlsx";
saveFileDialog.Filter = "Excel File (*.xlsx)|*xlsx";
string fullDocName = assemblyDoc.FullDocumentName;
string fileName = assemblyDoc.DisplayName;
try
{
var templateForm = new FormContentListSelect(this);
if (templateForm.ShowDialog() == DialogResult.OK)
{
var contentListTemplateName = string.Empty;
if (TemplateName == "All avalaible fields")
{
contentListTemplateName = "XXX_ContentListAll.xml";
}
else if (TemplateName == "Project Engineering Den Bosch")
{
contentListTemplateName = "XXX_ContentListProjectEngineeringDB.xml";
}
bom = assemblyCompDef.BOM;
bom.PartsOnlyViewEnabled = true;
bom.ImportBOMCustomization(System.IO.Path.GetDirectoryName(Assembly.GetCallingAssembly().Location) + @"\" + contentListTemplateName);
var bomView = bom.BOMViews["Parts Only"];
string initDirectory = fullDocName.Replace(fileName + ".iam", "");
string fileNameExcel = fileName + " Content List.xlsx";
string filePath = initDirectory + fileNameExcel;
saveFileDialog.InitialDirectory = initDirectory;
saveFileDialog.FileName = fileNameExcel;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
bomView.Export(saveFileDialog.FileName, FileFormatEnum.kMicrosoftExcelFormat);
//EditExcel(saveFileDialog.FileName);
DialogResult dialogResult = MessageBox.Show("Would you like to open the created Content List?", "Open Content List", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialogResult == DialogResult.Yes)
{
Process fileOpener = new Process();
fileOpener.StartInfo.FileName = saveFileDialog.FileName;
fileOpener.Start();
}
}
else if (templateForm.ShowDialog() == DialogResult.Cancel)
{
return;
}
}
}
catch
{
MessageBox.Show("Please save the assembly before generating the content list.", "Please save", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}