Revit API 无法 运行 外部命令

Revit API cannot run the external command

我目前正在尝试创建一个 Autodesk Revit 插件,用于检查房间的几何形状。由于错误提示“Revit 无法 运行 外部命令。AutodeskRevit.Exceptions.InvalidOperationException。HelloWorld.Class1 不继承 IExternalCommand。

抱歉,我是 C# 和 Autodesk Revit 的新手

所以我假设需要将 IExternalCommand 输入到 运行 命令的代码中。当我这样做时包括 IExternalComand 我收到一个 visual studio 错误说 "Class1 does not implement interface member 'IExternalCommand.Execute(ExternalCommandData, ref string, ElementSet)".

这是我的代码:

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

        }
    }
}

感谢您的建议。

IExternalCommandExecute 方法运行。您需要在 class 中定义一个 Execute 方法。从那里你可以调用你的 GetRoomDimensions 方法

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
}

这里是 link 对 IExternalCommand 的深入解释:

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