Macro to determine if a Family is In-Place (with output to Text and Excel Files)

A recent post at whatrevitwants mentions some tools that can be used to find in-place families. Here is a tiny macro to do the same

public void findInPlaceFamilies()
{
    string info = "";
    Document doc = this.ActiveUIDocument.Document;
    foreach (Family family in new FilteredElementCollector(doc).OfClass(typeof(Family)).Cast<Family>())
    {
        if (family.IsInPlace)
            info += family.Name + "\n";
    }
    TaskDialog.Show("In Place Families", info);
}

This is also a good excuse to show how to do something other than show the string inside Revit in a TaskDialog. For example, we might want to write the data to a text file or to an Excel file.

Text File:

public void findInPlaceFamilies()
{
    Document doc = this.ActiveUIDocument.Document;

    // Use the @ before the filename to avoid errors related to the \ character
    // Alternative is to use \\ instead of \
    string filename = @"C:\Users\HP002\Documents\In Place Families.txt";

    // Create a StreamWriter object to write to a file
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filename))
    {
        foreach (Family family in new FilteredElementCollector(doc).OfClass(typeof(Family)).Cast<Family>())
        {
            if (family.IsInPlace)
                writer.WriteLine(family.Name);
        }
    }    
    // open the text file
    System.Diagnostics.Process.Start(filename);
}

Excel File:

  1. Select “Project – Add Reference” from the SharpDevelop menu bar and add a reference to Microsoft.Office.Interop.Excel
    AddExcelReference
  2. Add these lines to the other “using” statements at the top of your file:
    using Microsoft.Office.Interop.Excel;
    using System.Reflection;
  3. Adding “Microsoft.Office.Interop.Excel” may result in errors like this if elsewhere in your macros you are using the Revit Parameter class:
    Ambiguous
    This happens because now you can no longer define variables as “Parameter” because both Autodesk.Revit.DB and Microsoft.Office.Interop.Excel contain definitions for “Parameter”. The compiler can’t guess which one you want to use.
    Go through these errors and replace “Parameter” with  “Autodesk.Revit.DB.Parameter” like this:

    Autodesk.Revit.DB.Parameter parameter = element.get_Parameter("Material");
    
public void findInPlaceFamilies()
{
    Document doc = this.ActiveUIDocument.Document;

    // set up Excel variables
    Application excelApp = new Application();
    Workbook workbook = excelApp.Workbooks.Add(Missing.Value);
    Worksheet worksheet = (Worksheet)workbook.ActiveSheet;

    string filename = @"C:\Users\HP002\Documents\In Place Families.xls";
    int rowCtr = 1;
    foreach (Family family in new FilteredElementCollector(doc).OfClass(typeof(Family)).Cast<Family>())
    {
        if (family.IsInPlace)
        {
            // write data to the next row in the spreadsheet
            worksheet.Cells[rowCtr,1] = family.Name;
            rowCtr++; // increment counter by one
        }
    }

    // Save the file
    workbook.SaveAs(filename,XlFileFormat.xlWorkbookNormal, 
        Missing.Value, Missing.Value, Missing.Value, Missing.Value, 
        XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, 
           Missing.Value, Missing.Value); 
    // Clean up the excel resources that were created
    workbook.Close();
    excelApp.Quit();

    System.Diagnostics.Process.Start(filename);
}

Don’t mind all that crazy “Missing.Value” stuff. The Excel API is strange but it works.

Advertisements

4 thoughts on “Macro to determine if a Family is In-Place (with output to Text and Excel Files)

  1. Harry, Will all of these API posts be combined (organized and published) into a future ExpressTool? Or do you envision a future drop down window on the ribbon?

  2. I have got valuable information of how Revit API communicates with Excel. Even though it was written in C#, I managed to translate to VB. Thanks a lot.
    From Singapore.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s