Tuesday 17 July 2012

Toggle a Display Category in the Active View


There was a question on on the AUGI forum regarding getting a keyboard shortcut to toggle the display of the Space Reference Category in a view, so I figured this would be a good learning experience for me. Its always best to have a goal to aim for with programming so I intended to create a simple add-in that could achieve the request and a keyboard shortcut could then be linked to the add-in.



Here is the code I created:

#Region "Imported Namespaces"
Imports Autodesk.Revit.DB
#End Region
<Transaction(TransactionMode.Manual)> Public Class AdskCommand
    Implements IExternalCommand
    Public Function Execute(ByVal commandData As ExternalCommandData, ByRef message As String, ByVal elements As ElementSet) As Result Implements IExternalCommand.Execute
        'Function to isolate a workset based on a selected element
        Dim uiapp As UIApplication = commandData.Application
        Dim uidoc As UIDocument = uiapp.ActiveUIDocument
        Dim doc As Document = uidoc.Document
        Dim Transaction As New Transaction(doc)
        Dim cat As Category = doc.Settings.Categories.Item(BuiltInCategory.OST_MEPSpaceReferenceVisibility)
        Transaction.Start("Toggle")
        If doc.ActiveView.getVisibility(cat) = False Then
            doc.ActiveView.setVisibility(cat, True)
        Else
            doc.ActiveView.setVisibility(cat, False)
        End If
        Transaction.Commit()
        'Must return some code
        Return Result.Succeeded
    End Function
End Class

So essentially all that happens is the Space Reference category is defined

Dim cat As Category = doc.Settings.Categories.Item(BuiltInCategory.OST_MEPSpaceReferenceVisibility)

then if it is currently on it is hidden, if it is hidden then it is turned on:

        If doc.ActiveView.getVisibility(cat) = False Then
            doc.ActiveView.setVisibility(cat, True)
        Else
            doc.ActiveView.setVisibility(cat, False)
        End If


Not much to it really, so I compiled it into a dll and add-in file that can be downloaded and tested.


Download the AddIn File
Download the DLL


Save the AddIn file to the correct location for your Revit installation and then save the dll to:


C:\Revit\Addins\2012\


you can change the location, but you will have to modify the addin file to point to the correct location.

No comments:

Post a Comment