Tag Archives: Revit Macro

[Solved] Revit can’t create a new module in the macro manager

This morning, I ran into an interesting bug in Revit 2016. I opened up my Macro Manager per the norm, but when attempting to create a new module Revit wouldn’t save the new module. It successfully opened the dialog box to let me choose a name, language, and description, however once I clicked “OK” nothing happened. Sharp Develop did not open nor did the new module appear in the Macro Manager window.

I found the suggested fix on Revitforum.org, however it did not work for me. Perhaps because this post was for an earlier version of Revit.

Here’s actually what ended up working for me:

  1. Close all Revit instances.
  2. Navigate to your Macros folder on your C drive:
    C:\ProgramData\Autodesk\Revit\Macros
  3. You may see multiple years in this folder, one per Revit version installed on your computer (e.g., 2014, 2015, 2016, 2017). Move the year of Revit that you are working in to a temporary folder like your desktop.
  4. Restart Revit.
  5. Go to your Macro Manager and create a new module.

By creating a new module, Revit recreates this directory and is able to successfully create a new module for your use.

IronPython UI Generator and Script Library for Autodesk Revit

I just stumbled on this script library for Revit users who wish to use Python with the Revit API. In it’s simplest form, it’s a folder of IronPython .py scripts for Autodesk Revit.

Check it out on GitHub: https://github.com/eirannejad/pyRevit

There is plenty of example code written in Python that you can refer to when learning to create macros or add-ins for Revit.

To use Python with Revit, you will need to install a few things:

I don’t want to get into too much detail on how to use Python with Revit in this post, but if you need help getting started, there is a very helpful guide on Productspec.

  1. Iron Python: http://ironpython.net/download/
  2. RevitPythonShell: https://code.google.com/p/revitpythonshell/
  3. Revit Software Development Kit: http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=2484975

Copy a Legend to Multiple Sheets in Revit

The good folks over at Boost Your BIM have posted a macro for copying a legend to multiple sheets. It works really well, but I wonder if anyone would like to take on the challenge of doing something similar in Dynamo.

public void legendOnSheets()
{
    Document doc = this.ActiveUIDocument.Document;
    
    // create list of element ids for the sheets that will get the legends
    List<ElementId> sheetIds = new List<ElementId>();
        
    // use SheetNumber to find the desired sheets
    // add each sheet to the list of sheet ids
    sheetIds.Add(new FilteredElementCollector(doc)
        .OfClass(typeof(ViewSheet))
        .Cast<ViewSheet>()
        .FirstOrDefault(q => q.SheetNumber == "A101").Id);
    sheetIds.Add(new FilteredElementCollector(doc)
        .OfClass(typeof(ViewSheet))
        .Cast<ViewSheet>()
        .FirstOrDefault(q => q.SheetNumber == "A102").Id);                
    sheetIds.Add(new FilteredElementCollector(doc)
        .OfClass(typeof(ViewSheet))
        .Cast<ViewSheet>()
        .FirstOrDefault(q => q.SheetNumber == "A103").Id);    
    
    // find the legend to put on the sheets
    // use ViewType.Legend and the view name to find the legend view
    Element legend = new FilteredElementCollector(doc)
        .OfClass(typeof(View))
        .Cast<View>()
        .FirstOrDefault(q => q.ViewType == ViewType.Legend && q.Name == "Legend 1");
    
    // create a transaction so that the document can be modified
    using (Transaction t = new Transaction(doc, "Legends on Sheets"))
    {
        // start the transaction
        t.Start();
        
        // loop through the list of sheet ids
        foreach (ElementId sheetid in sheetIds)
        {
            // create a new viewport for the legend on each sheet
            // place the legend view at the 0,0,0 location on each sheet
            // user will need to move the legend to the desired location
            Viewport.Create(doc, sheetid, legend.Id, new XYZ(0,0,0));
        }
        
        // commit the changes
        t.Commit();
    }
}

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()); 
         }