Categories: Tutorials

Dynamically enable a control (Become an expert)

2 min read

(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:


Packt

Share
Published by
Packt

Recent Posts

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago

Gain Practical Expertise with the Latest Edition of Software Architecture with C# 9 and .NET 5

Software architecture is one of the most discussed topics in the software industry today, and…

3 years ago