1

这是我在这里的第一个问题。我是 revit api 编程的初学者,所以如果我的问题太蹩脚或方向错误,我很抱歉。希望可以有人帮帮我。我正在尝试在这个简单的学习示例中实现 Iscommand 可用方法,但我无法理解它为什么不起作用,我的意思是该命令在任何情况下仍然可以使用。提前致谢!

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;
using System.Windows.Forms;

namespace PruebasAPI
{
    [Autodesk.Revit.Attributes.Transaction(TransactionMode.Automatic)]
    class IExternalcommand_elements : IExternalCommand
{

    public bool IsCommandAvailable(Autodesk.Revit.UI.UIApplication applicationData,
        CategorySet selectedCategories)
    {
        //allow button click if there is no active selection
        if (selectedCategories.IsEmpty)
            return true;
        //allow button click if there is at least one wall selected
        foreach (Category c in selectedCategories)
            {
                if (c.Id.IntegerValue == (int)BuiltInCategory.OST_Walls)
                    return true;
            }
            return false;
        }


    public Result Execute(
                            ExternalCommandData commandData,
                                     ref string message,
                                     ElementSet elements)
    {
        try
        {
            Document doc = commandData.Application.ActiveUIDocument.Document;
            UIDocument uidoc = commandData.Application.ActiveUIDocument;

            //delete selected elements
            ICollection<Autodesk.Revit.DB.ElementId> ids = doc.Delete(uidoc.Selection.Elements);

            TaskDialog taskdialog = new TaskDialog("Revit");
            taskdialog.MainContent =
                ("click yes to return succeded.Selected members will be deleted. \n" +
                "click no to return failed.Selected members will not be deleted \n" +
                "click cancel to return cancelled. Selected members will not be deleted.");

            TaskDialogCommonButtons buttons = TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No | TaskDialogCommonButtons.Cancel;
            taskdialog.CommonButtons = buttons;
            TaskDialogResult taskdialogresult = taskdialog.Show();

            if (taskdialogresult == TaskDialogResult.Yes)
            {
                return Result.Succeeded;
            }
            else if (taskdialogresult == TaskDialogResult.No)
            {
                elements = uidoc.Selection.Elements;
                message = "failed to delete selection";
                return Result.Failed;
            }
            else
            {
                return Result.Cancelled;
            }
        }
        catch
        {
            message = "unespected dika";
            return Result.Failed;
        }
    }
}

}`

4

1 回答 1

3

IsCommandAvailable 不应在您的命令类中。您实际上需要编写一个实现 IExternalCommandAvailability 的类。以下是 API 指南中的示例:

public class SampleAccessibilityCheck : IExternalCommandAvailability
{
    public bool IsCommandAvailable(Autodesk.Revit.UI.UIApplication applicationData,
         CategorySet selectedCategories)
    {
        // Allow button click if there is no active selection 
        if (selectedCategories.IsEmpty)
            return true;
        // Allow button click if there is at least one wall selected 
        foreach (Category c in selectedCategories)
        {
            if (c.Id.IntegerValue == (int)BuiltInCategory.OST_Walls)
                return true;
        }
        return false;
    }
}

然后,您可以在标签 AvailabilityClassName 内的 Addin 清单文件中指定此类名称,例如:

<AvailabilityClassName>MyNamespace.SampleAccessibilityCheck</AvailabilityClassName>

如果您在功能区上有一个按钮,则 PushButton 类还有一个 PushButton.AvailabilityClassName 属性,您可以在其中设置此类的名称,以便您的命令按钮相应地启用/禁用。

希望这可以帮助。

于 2012-04-01T02:23:16.577 回答