Hello,
I am trying to work with the inventor API, trying to export parts lists from .idw drawings for assemblies using C#. I looked through the documentation and found the 'Parts List Query' sample program in the Autodesk Inventor 2014 API Help document. This is in VBA and also only works on active drawing sheets. I want my program to take in a list of .idw files and output excel or csv files of the parts list. I would like this program to be in C# becuase I already have code in C# interfacing with the Vault. Could someone please point me in the direction of where to find the answer to this question?
Below is my C# code that is a modified version of 'AssemblyTree' that is included as a C# sample with the SDK. This allows the user to point to an .iam file and it outputs a .csv file of the files on which it depends. This is close to what I need, but many of our drawings have parts on the BOM that do not have files and some filenames are not the same as their part numbers
Thank you so much
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using Inventor.Document;
namespace AssemblyTree
{
/// <summary>
/// AssemblyTree Form.
/// </summary>
public class frmAssemblyTree : System.Windows.Forms.Form
{
internal System.Windows.Forms.Button cmdClose;
internal System.Windows.Forms.OpenFileDialog OpenFileDialog;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ImageList imageList;
internal System.Windows.Forms.TextBox txtFileName;
internal System.Windows.Forms.Button cmdBrowse;
private System.Windows.Forms.GroupBox grpBoxAssemblyFile;
private Inventor.ApprenticeServerComponent objapprenticeServerApp = new Inventor.ApprenticeServerComponentClass();
public frmAssemblyTree()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows Form Designer generated code
...
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmAssemblyTree());
}
private void cmdBrowse_Click(object sender, System.EventArgs e)
{
//setup parameters for the open dialog
OpenFileDialog openFileDlg = new OpenFileDialog();
openFileDlg.InitialDirectory = objapprenticeServerApp.FileLocations.Workspace;
openFileDlg.Title = "Select Assembly file";
openFileDlg.DefaultExt = ".iam";
openFileDlg.Filter = "Inventor Assembly File (*.iam) | *.iam";
openFileDlg.ShowDialog();
//write the filename to the text box
txtFileName.Text = openFileDlg.FileName;
//check to see if a filename is not empty and call the BuildTree function
if (txtFileName.Text != "")
{
BuildTree();
}
}
private void BuildTree()
{
try
{
Inventor.ApprenticeServerDocument objapprenticeServerDocument = objapprenticeServerApp.Open(txtFileName.Text);
if (objapprenticeServerDocument == null)
{
MessageBox.Show("Unable to open the specified file", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
IEnumerator objoccsEnumerator = objapprenticeServerDocument.ComponentDefinition.Occurrences.GetEnumerator();
string csv = "";
Inventor.ComponentOccurrence objcompOccurrence;
while (objoccsEnumerator.MoveNext())
{
objcompOccurrence = (Inventor.ComponentOccurrence)objoccsEnumerator.Current;
csv += objcompOccurrence.Name + "\n";
}
File.WriteAllText(@"C:\test.csv", csv);
}
catch (Exception)
{
MessageBox.Show("Unable to open and get the assembly heirarchy of the specified file", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
Hi
Post you question in Inventor Customization Group, you might have more luck. 😉
Can't find what you're looking for? Ask the community or share your knowledge.