Search code examples
c#autodeskrevit-apirevit

Revit API cannot run the external command


I am currently trying to create a Autodesk Revit add in, which check the geometry of a room. I am struggling to do this due to an error which says "Revit cannot run the external command. AutodeskRevit.Exceptions.InvalidOperationException. HelloWorld.Class1 does not inherit IExternalCommand.

Sorry I'm new to C# and Autodesk Revit

So I am assuming the IExternalCommand needs to be inputted into the code to run the command. When I do so include the IExternalComand I receive a visual studio error saying "Class1 does not implement interface member 'IExternalCommand.Execute(ExternalCommandData, ref string, ElementSet)".

Here is my code:

    using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.Creation;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.DB.Architecture;


namespace HelloWorld
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
    class Class1 :IExternalCommand
    {



        public void GetRoomDimensions(Autodesk.Revit.DB.Document doc, Room room)
        {
            String roominfo = "Room dimensions:\n";
            // turn on volume calculations:
            using (Transaction t = new Transaction(doc, "Turn on volume calculation"))
            {
                t.Start();
                AreaVolumeSettings settings = AreaVolumeSettings.GetAreaVolumeSettings(doc);
                settings.ComputeVolumes = true;
                t.Commit();
            }

            roominfo += "Vol: " + room.Volume + "\n";
            roominfo += "Area: " + room.Area + "\n";
            roominfo += "Perimeter: " + room.Perimeter + "\n";
            roominfo += "Unbounded height: " + room.UnboundedHeight + "\n";
            TaskDialog.Show("Revit", roominfo);

        }
    }
}

Thanks for any advise.


Solution

  • An IExternalCommand runs from the Execute method. You need to have an Execute method defined in your class. From there you can call your GetRoomDimensions method

    public Result Execute(
      ExternalCommandData commandData, 
      ref string message, 
      ElementSet elements)
    {
        UIApplication application = commandData.Application;
        Document mainDocument = application.ActiveUIDocument.Document;
    
        if(elements.Size > 0)
        {
            //Only 1 room should be selected
            return Result.Failed;
        }
    
        Room room = null;
    
        foreach(Element element in elements)
        {
            room = element as Room;
        }
    
        if(room == null)
        {
            //A non-room element was selected
            return Result.Failed;
        }
    
        GetRoomDimensions(mainDocument, room);
    
        return Result.Success
    }
    

    Here is a link explaining the IExternalCommand in depth:

    https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/Revit-API/files/GUID-797F9E50-08C4-4E58-8CF0-8B4C68035409-htm.html