Revit Macro Programming in C# and Python. Create your own Revit add-in or macros.

Revit Macro – List electrical panel schedules and the sheets that they are placed on

Revit Macro Programming in C# and Python. Create your own Revit add-in or macros.

We found a quick-and-dirty way to show what sheets your electrical panel schedules are on. Essentially, it is a schedule of schedules. We haven’t found any other way to accomplish this as of yet and we welcome any improvements to this code.

What this does is simply create a dialog box with all of your electrical panel schedules in your model and what sheets they are on. With the box open do a ctrl-c (copy to clipboard) and then paste into excel. Not very elegant but gets the job done.

For all you advanced coders out there, we could use some help in getting this to become a more elegant solution!

To create a Revit macro:

  1. Open up the macro manager and create a new C# module – name it whatever you want.
  2. With the module that you have just created selected, click the Macro button and name your macro “SchedulingSchedules”.
  3. Revit will then open “SharpDevelop”, an IDE for coding Revit macros. Look for the code block that SharpDevelop generated:
    public void SchedulingSchedules()
         {
         }
    

    and paste the code below in between the curly braces { }.

  4. Then you need to add “using System.Text;” at the top with the other using statements.
  5. Click ‘Build’ menu and then ‘Build Solution (F8)’.
  6. Switch back to Revit and you will see the “SchedulingSchedules” macro. Select the macro and click the Run button.

Code:

public void SchedulingSchedules()
         {
             UIApplication uiApp = this.Application;    
             Application app = uiApp.Application;
             UIDocument uiDoc = uiApp.ActiveUIDocument; 
             Document Doc = uiDoc.Document;
             
             StringBuilder sb = new StringBuilder(); 
             
             ElementCategoryFilter sheetcf = new ElementCategoryFilter(BuiltInCategory.OST_Sheets);
             FilteredElementCollector sheetCollector = new FilteredElementCollector(Doc); 
             IList<Element> sheetlist = sheetCollector.WherePasses(sheetcf).WhereElementIsNotElementType().ToElements(); 
         
             foreach(Element sheet in sheetlist) { 
                 ElementCategoryFilter panelcf = new ElementCategoryFilter(BuiltInCategory.OST_PanelScheduleGraphics); 
                 FilteredElementCollector panelCollector = new FilteredElementCollector(Doc, sheet.Id); 
                 IList<Element> panellist = panelCollector.WherePasses(panelcf).WhereElementIsNotElementType().ToElements(); 
                 foreach(Element panel in panellist) { 
                     sb.AppendLine(panel.Name+" \t"+sheet.get_Parameter(BuiltInParameter.SHEET_NUMBER).AsString()); 
                 } 
             }
         
             TaskDialog.Show("Revit", sb.ToString()); 
         } 

  • kidlatj

    What would you like it to do? How about writing what sheet they are on to a project parameter on the panel itself and then creating an electrical equipment schedule that had the panel name and what sheet it shows on?

    • kidlatj

      public void scheduling_schedules2()
      {
      string projectparameter = “Whatever your new project parameter is named”;//add a new instance text project parameter to the electrical equipment category.
      UIDocument UiDoc = this.ActiveUIDocument;
      Document Doc = UiDoc.Document;
      Transaction tranny = new Transaction(Doc, “updating Panel Names”);
      StringBuilder sb = new StringBuilder();
      ElementCategoryFilter sheetcf = new ElementCategoryFilter(BuiltInCategory.OST_Sheets);
      FilteredElementCollector sheetCollector = new FilteredElementCollector(Doc);
      IList sheetlist = sheetCollector.WherePasses(sheetcf).WhereElementIsNotElementType().ToElements();

      ElementCategoryFilter equipcf = new ElementCategoryFilter(BuiltInCategory.OST_ElectricalEquipment);
      FilteredElementCollector equipCollector = new FilteredElementCollector(Doc);
      IList equiplist = equipCollector.WherePasses(equipcf).WhereElementIsNotElementType().ToElements();
      tranny.Start();
      foreach(Element sheet in sheetlist)
      {
      ElementCategoryFilter panelcf = new ElementCategoryFilter(BuiltInCategory.OST_PanelScheduleGraphics);
      FilteredElementCollector panelCollector = new FilteredElementCollector(Doc, sheet.Id);
      IList panellist = panelCollector.WherePasses(panelcf).WhereElementIsNotElementType().ToElements();
                      foreach(Element panelsched in panellist)
                      {
                      try
                      {
                      Element panel = equiplist.FirstOrDefault(p=>p.Name == panelsched.Name);//this is also assuming that all your equipment have unique names
                      panel.get_Parameter(projectparameter).Set(sheet.get_Parameter(BuiltInParameter.SHEET_NUMBER).AsString());//if this is for 2016, change the ‘get_Parameter’ to ‘LookupParameter’
                      //sb.AppendLine(panel.Name+” \t”+sheet.get_Parameter(BuiltInParameter.SHEET_NUMBER).AsString());
                      }
                      catch{}
                      }
      }
      tranny.Commit();
      }

      • BIM Extension

        Thank you for the code, I will try it out.

      • Chris Rich

        I’ve been trying to implement this code on Revit 2016 with no luck. The conversion to LookupParameter from get_Parameter only brought up errors. Can anyone confirm this working on Revit 2016?

        • BIM Extension, LLC

          Chris, I am finally getting the chance to dive deeper into this. Did you ever figure it out?

          I know the “get_” methods were deprecated in 2016, so I believe you need to use the new method provided by the 2016 API.

          • Chris Rich

            I never did figure it out, which was disappointing. Have you had any luck? I’m not very familiar with the any of the API’s, more of a macro tinkerer. Can you help me out?

          • BIM Extension, LLC

            Hey there, I finally got around to fixing the code. I’ve tested in Revit 2016 and it is working!

          • Chris Rich

            Awesome! Where can I view it?

          • BIM Extension, LLC

            Sorry that I wasn’t more clear. I updated the code snippet in the original post. I also revised the instructions to be more descriptive. Hope this helps!

    • BIM Extension

      This is a great idea!

  • Paul Sommer

    Hello,
    This found this post. I tried this code but it does not work. And I am very interested with what Kidlatj ask about writing what sheet they are on.

    • BIM Extension, LLC

      Did the macro throw an error when you tried to run it?

      • Paul Sommer

        Yes..But let me first say that I am a newbie with macros. But I think I followed the instructions correctly. Please see the attached image.

        • BIM Extension, LLC

          Sorry for the delayed response on this. What version of Revit are you using?

        • BIM Extension, LLC

          Hey there, I finally updated the post with the fixed code. It is working on my end in Revit 2016.

    • Paul Sommer

      Hello,
      I think my reply did not go thru earlier. Let me try this again. Yes but I had some error messages.