Revit API无法运行外部命令

时间:2018-08-08 15:25:06

标签: c# autodesk revit-api revit

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

对不起,我是C#和Autodesk Revit的新手

所以我假设需要将IExternalCommand输入到代码中以运行命令。包括IExternalComand在内时,我收到一个Visual Studio错误,提示“ Class1不实现接口成员'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);

        }
    }
}

谢谢您的建议。

1 个答案:

答案 0 :(得分:1)

IExternalCommandExecute方法运行。您需要在类中定义一个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
}

以下是深入介绍IExternalCommand的链接:

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