The Command Enabler project item can be found from the Revit Addins category of the Visual C# Project Items. After the wizard pages have been filled in like:
A new IExternalCommandAvailability derivative will be created and added to the current project:
using System;
using System.Text;
using System.Xml;
using System.Linq;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB.Analysis;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;
using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;
namespace RevitAddinCSProject
{
public class CommandEnabler1 : IExternalCommandAvailability
{
public bool IsCommandAvailable(UIApplication uiApp, CategorySet catSet)
{
if (uiApp.Application.Product == ProductType.Structure) return false;
if (uiApp.Application.Product == ProductType.MEP) return false;
if (catSet.IsEmpty) return true;
Categories allCats = uiApp.ActiveUIDocument.Document.Settings.Categories;
Category cat = allCats.get_Item(BuiltInCategory.OST_Doors);
if (catSet.Contains(cat)) return true;
cat = allCats.get_Item(BuiltInCategory.OST_Stairs);
if (catSet.Contains(cat)) return true;
cat = allCats.get_Item(BuiltInCategory.OST_Walls);
if (catSet.Contains(cat)) return true;
return false;
}
}
}
If the IExternalCommandAvailability derivative is associated with a PushButton like the following:
......
string assemFullName = Assembly.GetExecutingAssembly().Location;
string assemPath = Path.GetDirectoryName(assemFullName);
PushButtonData cnt1Panel_grp0_item1Data = new PushButtonData("cnt1Panel_grp0_item1", @"PushButton1", assemFullName, "RevitAddinCSProject.ExtCmd");
PushButton cnt1Panel_grp0_item1 = panel.AddItem(cnt1Panel_grp0_item1Data) as PushButton;
cnt1Panel_grp0_item1.ToolTip = @"A PushButton with the default image.";
cnt1Panel_grp0_item1.LargeImage = BmpImageSource(@"RevitAddinCSProject.Resources.ExtCmd.bmp");
cnt1Panel_grp0_item1.AvailabilityClassName = "RevitAddinCSProject.CommandEnabler1";
......
The PushButton will be disabled in Revit Structure and Revit MEP. In Revit Architecture, it will be enabled if nothing is selected in the current document. It will also be enabled if some doors, stairs, or walls have been selected in the current document; otherwise, it will be disabled.
With Command Enabler of RevitAddinWizard, an IExternalCommandAvailability interface can be implemented in a moment and it will address all Revit flavors and element categories.
Links to some related articles:
Use RevitAddinWizard to Create IUpdater Derivatives of Revit API
Implement IFailuresPreprocessor of Revit API
Use RevitAddinWizard to Implement IFailuresPreprocessor of Revit API
Implement An IFailuresProcessor of Revit API
Use RevitAddinWizard to Implement IFailuresProcessor of Revit API
Command Availability And Revit Flavors/Categories of Revit API
Use RevitAddinWizard to Implement IExternalCommandavailability of Revit API
Implement ISelectionFilter of Revit API
Use RevitAddinWizard to Implement ISelectionFilter of Revit API
Recent Comments