How to use PCB-Investigator in MS Excel

This example shows how to create a Microsoft Excel plug-in to generate the layerlist with object counts and layertypes in a table.

By opening a new job the Excel table will be filled and all relevant informations are evaluated.

public partial class ThisWorkbook
  {
    IPCBIWindow window;
    private void ThisWorkbook_Startup(object sender, System.EventArgs e)
    {
      window = IAutomation.CreateNewPCBIWindow(true); //create one instance of PCB-Investigator
      window.PCBINewJobOpend += new EventHandler(window_PCBINewJobOpend);
    }
 
    void window_PCBINewJobOpend(object sender, EventArgs e) //on opening an job we fill the excel sheet
    {
      Excel.Sheets sheets;
      Excel._Worksheet sheet;
 
      try
      {
        try
        {
          //Get a reference to the first sheet of the workbook. By changing the index we can write the report in other worksheets
          sheets = this.Worksheets;
          sheet = (Excel._Worksheet)sheets.get_Item(1);
        }
        catch (Exception exSheet)
        {
          MessageBox.Show(exSheet.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
 
          return;
        }
 
        if (sheet != null)
        {
          IStep step = window.GetCurrentStep();
          IMatrix matrix = window.GetMatrix();
          if (step != null)
          {
            int runIndex =2;
            sheet.Cells[1, 1].Value = "Layerlist";
            sheet.Cells[1, 2].Value = "Objectcount";
            sheet.Cells[1, 3].Value = "Layertype";
 
            foreach(string layername in step.GetAllLayerNames()) //all layer
            {
              sheet.Cells[runIndex, 1].Value = layername;
              ILayer layer = step.GetLayer(layername);
              sheet.Cells[runIndex, 2].Value = layer.GetAllLayerObjects().Count; //how much elements on the layer?
              sheet.Cells[runIndex, 3].Value = matrix.GetMatrixLayerType(layername).ToString(); //which type is this layer?
              runIndex++;
            }
            sheet.Activate(); //show the filled list
          }
        }
      }
      catch (Exception exError)
      {
        MessageBox.Show(exError.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
      }
    }
 
    private void ThisWorkbook_Shutdown(object sender, System.EventArgs e)
    {
      if (window != null)
        IAutomation.ClosePCBIWindow(window);
    }