这是我的第一个问题。我是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;
}
}
}
}`
答案 0 :(得分:2)
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属性,您可以在其中设置此类的名称,以便您的命令按钮相应地启用/禁用。
希望这有帮助。