User Hidden Categories

User Hidden Categories

MattKincaid
Advocate Advocate
2,079 Views
2 Replies
Message 1 of 3

User Hidden Categories

MattKincaid
Advocate
Advocate

We're trying to write a routine that takes a View3D and hides everything except the walls.  I know it's easy enough to do that in the Revit UI, but the routine is part of a larger project.  I've run into an exception that I'm not sure what to do about.  My code looks like the sample code below.

Snippet

Public Sub HideEverythingExceptWalls(view As view3d)
 
    Dim categories = view.Document.Settings.Categories
 
    For Each cat As Category In categories
 
        If cat.Id.IntegerValue = BuiltInCategory.OST_Walls Then
            cat.Visible(view) = True
        Else
            cat.Visible(view) = False
        End If
 
    Next
 
End Sub

 

When the loop gets to the "Piping Systems" category, an exception gets thrown that states ,

 

Autodesk.Revit.Exceptions.InvalidOperationException was unhandled by user code
  HResult=-2146233088
  Message=Cannot set the visible attribute of the category xyz because it is user hidden

 

I couldn't find any documentation on the exception, but I'm guessing that the problem is related to the fact that Piping Systems are not exposed in the visibility graphics UI (my guess could be wrong).  Has anyone ever encountered this exception before or know of a way to prevent it?

 

Many thanks.

 

0 Likes
Accepted solutions (1)
2,080 Views
2 Replies
Replies (2)
Message 2 of 3

Mustafa.Salaheldin
Collaborator
Collaborator
Accepted solution

Please try this code:

 

#Region "Namespaces"

Imports System
Imports System.Text
Imports System.Linq
Imports System.Xml
Imports System.Reflection
Imports System.ComponentModel
Imports System.Collections
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Media.Imaging
Imports System.Windows.Forms
Imports System.IO

Imports Autodesk.Revit.ApplicationServices
Imports Autodesk.Revit.Attributes

Imports Autodesk.Revit.DB
Imports Autodesk.Revit.DB.Events
Imports Autodesk.Revit.DB.Architecture
Imports Autodesk.Revit.DB.Structure
Imports Autodesk.Revit.DB.Mechanical
Imports Autodesk.Revit.DB.Electrical
Imports Autodesk.Revit.DB.Plumbing

Imports Autodesk.Revit.UI
Imports Autodesk.Revit.UI.Selection
Imports Autodesk.Revit.UI.Events

'Imports Autodesk.Revit.Collections
Imports Autodesk.Revit.Exceptions
Imports Autodesk.Revit.Utility

Imports RvtApplication = Autodesk.Revit.ApplicationServices.Application
Imports RvtDocument = Autodesk.Revit.DB.Document

#End Region

'Namespace RevitAddinVB1

    <Transaction(TransactionMode.Manual)> _
    <Regeneration(RegenerationOption.Manual)> _
    Public Class ExtCmd
    Implements IExternalCommand

#Region "Cached Variables"

    Shared _cachedCmdData As ExternalCommandData

    Public Shared ReadOnly Property CachedUiApp() As UIApplication
        Get
            Return _cachedCmdData.Application
        End Get
    End Property

    Public Shared ReadOnly Property CachedApp() As RvtApplication
        Get
            Return CachedUiApp.Application
        End Get
    End Property

    Public Shared ReadOnly Property CachedDoc() As RvtDocument
        Get
            Return CachedUiApp.ActiveUIDocument.Document
        End Get
    End Property

#End Region

#Region "IExternalCommand Members"

    Public Function Execute(ByVal cmdData As ExternalCommandData, ByRef msg As String, ByVal elemSet As ElementSet) As Result Implements IExternalCommand.Execute

        _cachedCmdData = cmdData

        Try
            Using trans As New Transaction(CachedDoc, "Hide")
                trans.Start()
                HideEverythingExceptWalls(CachedDoc.ActiveView)
                trans.Commit()
            End Using

            Return Result.Succeeded
        Catch ex As Exception

            msg = ex.ToString()
            Return Result.Failed
        End Try
    End Function
    Public Sub HideEverythingExceptWalls(view As View3D)

        Dim categories = view.Document.Settings.Categories

        For Each cat As Category In categories

            If cat.Id.IntegerValue = BuiltInCategory.OST_Walls Then
                cat.Visible(view) = True
            Else
                If cat.AllowsVisibilityControl(view) Then
                    cat.Visible(view) = False

                End If
            End If

        Next

    End Sub
#End Region

End Class
'End Namespace

Please if this satisfies your needs don't forget to mark the reply as an answer.


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

Message 3 of 3

MattKincaid
Advocate
Advocate
Thanks buddy. I missed the AllowsVisibilityControl property in the Category class. Appreciate the help.
0 Likes