Dynamically enable a control (Become an expert)

Exclusive offer: get 50% off this eBook here
Instant Autodesk Revit 2013 Customization with .NET How-to [Instant]

Instant Autodesk Revit 2013 Customization with .NET How-to [Instant] — Save 50%

A supercharged guide to creating your own plugins, add-ons, and customizations for Revit with .NET book and ebook

$14.99    $7.50
by Don Rudder | May 2013 | Web Graphics & Video

There may come a time when you need to prevent the user from being able to click one of your command buttons. The Revit API supports enabling and disabling commands by implementing the IExternalCommandAvailability interface and applying it to a button class. Your reasons for disabling a command can be just about anything you want. Some examples might include scenarios such as the state of the current model not meeting your application's requirements, the model name may not meet a specific requirement that you specify, or maybe the wrong flavor of Revit was used to open the model.

In this article by Don Rudder, author of Instant Autodesk Revit 2013 Customization with .NET How-to [Instant], we are going to cover recipes for enabling and disabling control for commands.

(For more resources related to this topic, see here.)

Getting ready

We will implement an IExternalCommandAvailability interface where we allow the command to be enabled only when a door element is selected in the current model.

How to do it...

  1. Add a new class named AvailabilityDoorSelected.vb.

  2. Enter the IExternalCommandAvailability code as shown, which returns true only when a door element is selected in the model:

    Imports Autodesk.Revit.UI
    Imports Autodesk.Revit.DB
    ''' <summary>
    ''' Enables a Command Only if a
    ''' Door is Selected in the User Interface
    ''' </summary>
    Public Class AvailabilityDoorSelected
    Implements IExternalCommandAvailability
    ''' <summary>
    ''' Command Availability Implementation
    ''' </summary>
    Public Function IsCommandAvailable(appData As _
    UIApplication,
    selectedCats As CategorySet) _
    As Boolean Implements _
    IExternalCommandAvailability.IsCommandAvailable
    ' Iterate Categories
    For i = 0 To selectedCats.Size
    Try
    ' Get the Category
    Dim m_c As Category = _
    TryCast(selectedCats(i), Category)
    ' Is it a Door?
    If m_c.Name = "Doors" Then Return True
    Catch
    End Try
    Next
    Return False
    End Function
    End Class

  3. Open the ApplicationRibbon class and enter the following code right before the ' Success line in the AddPushButton function that will assign this availability implementation to our sample button:

    ' Availability - Door Selection
    m_pb.AvailabilityClassName = _
    "Revit2013Samples_VB.AvailabilityDoorSelected"

  4. Run the add-in in the debugger and open one of the sample project models that ship with Revit.

  5. Select the door element in the model to enable the sample button. Deselect the door and the button will become disabled again.

How it works...

Our AvailabilityDoorSelected class will execute anytime that a user control that this class is assigned to is either made visible in the Revit user interface or an element in the model is selected. Element selection will only execute this class when an assigned control is visible in the Revit UI. There is no limit to the number of user controls that you can assign to any class that implements IExternalCommandAvailability nor is there a limit to the amount of IExternalCommandAvailability implementations that you can use in your projects. The following illustration shows on the left-hand side how the main button looks without a door selected and on the right-hand side how it becomes enabled when a door is selected.

We could have generated a more complex scenario for our availability implementation, but in an effort to keep it simple we only included door selection as a requirement to enable our command. The possibilities for this functionality are endless and I'm sure you will find plenty of uses for it in your own projects in the future.

Summary

This article explained how to dynamically enable a control.

Resources for Article :


Further resources on this subject:


Instant Autodesk Revit 2013 Customization with .NET How-to [Instant] A supercharged guide to creating your own plugins, add-ons, and customizations for Revit with .NET book and ebook
Published: January 2013
eBook Price: $14.99
See more
Select your format and quantity:

About the Author :


Don Rudder

Don Rudder is the Director of Software Development at CASE and focuses on the creation and management of specialized software and add-ins for various applications developed for client support. With over 16 years of experience in the AEC industry, Don has served well over 10 of those years as an HVAC and electrical designer for various MEP firms. He later began to focus more heavily on software development and related support where he eventually ended up in San Francisco serving as BIM Manager for HOK. He is self-taught in some 14 programming languages and well versed in .NET, web-based AEC tools, and pretty much any kind of automation. Don has also presented at Autodesk University and the Revit Technology Conference of North America.

Don has been the contributing author of the API chapters for Mastering Autodesk Revit Architecture 2011, Mastering Autodesk Revit Architecture 2012, and Mastering Autodesk Revit Architecture 2013.

Books From Packt


Autodesk AutoCAD 2013 Practical 3D Drafting and Design
Autodesk AutoCAD 2013 Practical 3D Drafting and Design

Mudbox 2013 Cookbook
Mudbox 2013 Cookbook

ODP.NET Developer’s Guide: Oracle Database 10g Development with Visual Studio 2005 and the Oracle Data Provider for .NET
ODP.NET Developer’s Guide: Oracle Database 10g Development with Visual Studio 2005 and the Oracle Data Provider for .NET

.Net Framework 4.5 Expert Programming Cookbook
.Net Framework 4.5 Expert Programming Cookbook

FreeCAD [Instant]
FreeCAD [Instant]

SketchUp 7.1 for Architectural Visualization: Beginner's Guide
SketchUp 7.1 for Architectural Visualization: Beginner's Guide

Processing 2: Creative Programming Cookbook
Processing 2: Creative Programming Cookbook

Microsoft .NET Framework 4.5 Quickstart Cookbook
Microsoft .NET Framework 4.5 Quickstart Cookbook


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
M
u
G
m
W
q
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software