Api Can´;我得不到;IscommandAvailable“;工作
这是我的第一个问题。我是revit api编程的初学者,如果我的问题太蹩脚或方向错误,我很抱歉。希望有人能帮助我。在这个简单的学习示例中,我正在尝试实现iscommandavailable方法,我无法理解为什么它不起作用,我的意思是该命令在任何情况下都可以使用。提前谢谢Api Can´;我得不到;IscommandAvailable“;工作,api,revit,revit-api,Api,Revit,Revit Api,这是我的第一个问题。我是revit api编程的初学者,如果我的问题太蹩脚或方向错误,我很抱歉。希望有人能帮助我。在这个简单的学习示例中,我正在尝试实现iscommandavailable方法,我无法理解为什么它不起作用,我的意思是该命令在任何情况下都可以使用。提前谢谢 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Autodesk.Revit.UI; us
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Autodesk.Revit.UI;
使用Autodesk.Revit.DB;
使用Autodesk.Revit.Attributes;
使用Autodesk.Revit.UI.Selection;
使用System.Windows.Forms;
名称空间PruebasAPI
{
[Autodesk.Revit.Attributes.Transaction(TransactionMode.Automatic)]
类IExternalcommand_元素:IExternalcommand
{
公共布尔值是通用的(Autodesk.Revit.UI.UIApplicationData,
类别集(选定类别)
{
//如果没有活动选择,则单击“允许”按钮
如果(selectedCategories.IsEmpty)
返回true;
//如果至少选择了一面墙,则单击“允许”按钮
foreach(选定类别中的类别c)
{
if(c.Id.IntegerValue==(int)BuiltInCategory.OST_)
返回true;
}
返回false;
}
公开结果执行(
外部命令数据命令数据,
参考字符串消息,
元素集元素)
{
尝试
{
Document doc=commandData.Application.ActiveUIDocument.Document;
UIDocument uidoc=commandData.Application.ActiveUIDocument;
//删除选定元素
ICollection id=doc.Delete(uidoc.Selection.Elements);
TaskDialog TaskDialog=新建TaskDialog(“Revit”);
taskdialog.MainContent=
(“单击“是”返回成功。所选成员将被删除。\n”+
“单击“否”返回失败。所选成员将不会被删除\n”+
“单击“取消”返回已取消。所选成员将不会被删除。”);
TaskDialogCommonButtons buttons=TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No | TaskDialogCommonButtons.Cancel;
taskdialog.CommonButtons=按钮;
TaskDialogResult TaskDialogResult=taskdialog.Show();
如果(taskdialogresult==taskdialogresult.Yes)
{
返回结果。成功;
}
else if(taskdialogresult==taskdialogresult.No)
{
elements=uidoc.Selection.elements;
message=“删除所选内容失败”;
返回结果,失败;
}
其他的
{
返回结果。已取消;
}
}
抓住
{
message=“未经检查的迪卡”;
返回结果,失败;
}
}
}
}`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内的加载项清单文件中指定此类名,例如:
<AvailabilityClassName>MyNamespace.SampleAccessibilityCheck</AvailabilityClassName>
MyNamespace.SampleAccessibilityCheck
如果功能区上有一个按钮,则button类还具有button.AvailabilityClassName属性,您可以在其中设置此类的名称,以便相应地启用/禁用命令按钮
希望这有帮助。非常感谢!成功了!得到你的回答让我很欣慰,这让我继续学习。谢谢你,布格拉!欢迎来到帕纳诺。如果此答案解决了您的问题,请将其标记为已接受。