Beginner's guide to adding tab and button with Revit API


This video is a beginner's guide to how to add a tab and button to Revit through the API.  The documentation can be found on Autodesk website.  I've also provided the sample code I used in the video.



Tips:
  • Add "References" by right clicking and going to "Add References" under "Framework" you'll find the "PresentationCore", "System.Xaml", "System.Xml", and "WindowsBase".
  • MyTest.addin remove the <AddIn Type="Command"> and it's entire setup.  This is for the drop down Add-in for Revit. 

Sample Code:

#region Namespaces
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Media.Imaging;
#endregion

namespace MyTest
{
    class App : IExternalApplication
    {
        public Result OnStartup(UIControlledApplication a)
        {
    // Method to add Tab and Panel 
            RibbonPanel panel = ribbonPanel(a);
    // Reflection to look for this assembly path 
            string thisAssemblyPath = Assembly.GetExecutingAssembly().Location;
    // Add button to panel 
            PushButton button = panel.AddItem(new PushButtonData("Button", "Test Button", thisAssemblyPath, "MyTest.Command")) as PushButton;
    // Add tool tip 
            button.ToolTip = "this is a sample";
    // Reflection of path to image 
            var globePath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "LACMA DOORS.png");
            Uri uriImage = new Uri(globePath);
    // Apply image to bitmap
            BitmapImage largeImage = new BitmapImage(uriImage);
    // Apply image to button 
            button.LargeImage = largeImage;

            a.ApplicationClosing += a_ApplicationClosing;

            //Set Application to Idling
            a.Idling += a_Idling;

            return Result.Succeeded;
        }
//*****************************a_Idling()*****************************
        void a_Idling(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e)
        {

        }
//*****************************a_ApplicationClosing()*****************************
        void a_ApplicationClosing(object sender, Autodesk.Revit.UI.Events.ApplicationClosingEventArgs e)
        {
            throw new NotImplementedException();
        }
//*****************************ribbonPanel()*****************************
        public RibbonPanel ribbonPanel(UIControlledApplication a)
        {
    // Tab name 
            string tab = "My Test Tab";
    // Empty ribbon panel 
            RibbonPanel ribbonPanel = null;
// Try to create ribbon tab. 
            try
            {
                //a.CreateRibbonPanel("My Test Tools");
                a.CreateRibbonTab(tab);
            }
            catch { }
    // Try to create ribbon panel. 
            try
            {
                RibbonPanel panel = a.CreateRibbonPanel(tab , "test");
            }
            catch { }
    // Search existing tab for your panel. 
            List<RibbonPanel> panels = a.GetRibbonPanels(tab);
            foreach(RibbonPanel p in panels)
            {
                if (p.Name == "test")
                {
                    ribbonPanel = p;
                }
            }
    //return panel 
            return ribbonPanel;
        }

        public Result OnShutdown(UIControlledApplication a)
        {
            return Result.Succeeded;
        }
    }
}

Comments

  1. Hi Danny,

    we have a similar story :) I got into Revit, then Dynamo, then Python, C# and the API. Like you, I've found there's a lot of self-study necessary and the barriers to entry seem pretty high, so I appreciate sites like yours and the fact you took the time to spread some of your API knowledge :)

    Many thanks,

    Ollie

    ReplyDelete
    Replies
    1. Thank you Ollie, Yes learning all these additional items can be a big task. I know how frustrating it can be when someone is first starting. I'm hopping to add a little to the community and help when I can.

      Delete
  2. Hey Danny,
    Thanks for posting this!
    I finished "my first plug-in training" (Autodesk training)
    Where is the "New Tab/Ribbon" code supposed to go?
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Autodesk.Revit.DB;
    using Autodesk.Revit.DB.Architecture;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.UI.Selection;
    using Autodesk.Revit.ApplicationServices;
    using Autodesk.Revit.Attributes;

    [TransactionAttribute(TransactionMode.Manual)]
    [RegenerationAttribute(RegenerationOption.Manual)]
    public class Lab1PlaceGroup : IExternalCommand
    {
    public Result Execute(
    ExternalCommandData commandData,
    ref string message,
    ElementSet elements)
    {
    //Get application and document objects
    UIApplication uiApp = commandData.Application;
    Document doc = uiApp.ActiveUIDocument.Document;

    //Define a Reference object to accept the pick result.
    Reference pickedRef = null;

    //Pick a group
    Selection sel = uiApp.ActiveUIDocument.Selection;
    pickedRef = sel.PickObject(ObjectType.Element, "Please select a group");
    Element elem = doc.GetElement(pickedRef);
    Group group = elem as Group;

    //Pick a point
    XYZ point = sel.PickPoint("Please pick a point to place group");

    //Place the group
    Transaction trans = new Transaction(doc);
    trans.Start("Lab");
    doc.Create.PlaceGroup(point, group.GroupType);
    trans.Commit();

    return Result.Succeeded;
    }
    }

    ReplyDelete
    Replies
    1. Kristofer,
      The sample code should be added the App.cs file. Revit reads this file when it starts up and closes. This file "App.cs" tells Revit to add your button and ribbon to Revit. It also tells it that if someone pushes your button to run your Command.cs file. These files should be in the Addin Wizard.

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Nice blog you have provided unique information I haven’t seen ever, by seeing this blog I came to know lots of new things those are very useful to me.

    JOB Oriented AutoCAD Training Course

    ReplyDelete

Post a Comment

Popular Posts