主题:按类别控制可见性。

摘要此示例演示了如何按类别控制可见性,以及API支持选择单个元素或选择多个元素的选项。

相关类:

Autodesk.Revit.DB.View

Autodesk.Revit.Document

Autodesk.Revit.UI.Selection

Autodesk.Revit.DB.Document.Settings

Autodesk.Revit.DB.Categories

项目文件:

Command.cs

它包含实现接口IExternalCommandCommand类;它是此外部命令的入口。

VisibilityCtrl.cs

它包含控制可见性的VisibilityCtrl类。

和一个枚举类型IsolateMode列出元素选择模式的类型。

描述:

功能:

-按类别控制活动视图中元素的可见性。选中分类选框将允许用户选择要可见的类别。之后,活动视图将显示属于所选类别的元素,属于未选类别的元素将不可见。

-用户可以选择一个元素或多个元素,然后选择与所选元素属于相同类别的元素将在活动视图中被隔离显示。

实现:

-Document.Settings属性提供对Revit应用程序的常规应用程序设置(例如类别)的访问,其属性类别检索提供对Revit应用程序和项目中的所有类别的访问。

-View对象的setVisibility方法可以设置指定类别的可见性。

-SelectionPickOne方法选择最多一个元素。如果用户选择与选择之前模块范围内的元素不同的元素,则会将一个或多个元素添加到模块范围中。

-SelectionWindowSelect方法通过窗口选择元素。如果可能,将添加元素到模块范围中。

说明:

1. 运行Revit 2009

2. 运行此外部命令。

3. 您可以获得并更改活动视图中所有类别的可见性。

4. 您可以选择PickOneWindowSelect模式来隔离元素。

完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码

VisibilityCtrl.cs

//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//

using System;
using System.Collections;

using Autodesk.Revit.DB;

namespace Revit.SDK.Samples.VisibilityControl.CS
{
    /// <summary>
    /// An enumerate type listing the types of element select mode.
    /// </summary>
    public enum IsolateMode
    {
        None,
        PickOne,
        WindowSelect
    }

    /// <summary>
    /// An object control visibility by category
    /// </summary>
    public class VisibilityCtrl
    {
        private Autodesk.Revit.UI.UIDocument m_document;    // the active document
        private Hashtable m_allCategories; // all categories name with its visibility
        private Hashtable m_categoriesWithName; // all categories with its name
        private IsolateMode m_isolateMode;  // the mode to select element(s)
        
        /// <summary>
        /// get all categories name with its visibility
        /// </summary>
        public Hashtable AllCategories
        {
            get
            {
                return m_allCategories;
            }
        }

        /// <summary>
        /// get and set the mode to select element(s)
        /// </summary>
        public IsolateMode IsolateMode
        {
            get
            {
                return m_isolateMode;
            }
            set
            {
                m_isolateMode = value;
            }
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <remarks>get all categories of this document. check its visibility for 
        /// the active document, and initialize a hashtable.</remarks>
        public VisibilityCtrl(Autodesk.Revit.UI.UIDocument document)
        {
            if (null == document)
            {
                throw new ArgumentNullException("document");
            }
            else
            {
                m_document = document;
            }

            // initialize the two table
            m_allCategories = new Hashtable();
            m_categoriesWithName = new Hashtable();

            // fill out the two table
            foreach (Category category in m_document.Document.Settings.Categories)
            {
                if(category.get_AllowsVisibilityControl(m_document.Document.ActiveView))
                {
                    m_allCategories.Add(category.Name, category.get_Visible(m_document.Document.ActiveView));
                    m_categoriesWithName.Add(category.Name, category);
                }                
            }
        }

        /// <summary>
        /// Set the visibility for the active view
        /// </summary>
        /// <returns>Return true if operation successed, or else, return false.</returns>
        public bool SetVisibility(bool visible, string name)
        {
            try
            {
                Category cat = m_categoriesWithName[name] as Category;                
                m_document.Document.ActiveView.SetCategoryHidden(cat.Id, !visible);
                //or cat.set_Visible(m_document.ActiveView, visible);
                m_allCategories[cat.Name] = visible;
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Isolate elements with the same categories that the selected elements belong to
        /// using PickOne or WindowSelect.
        /// </summary>
        public void Isolate()
        {
            //m_document.Selection.Elements.Clear();
            System.Collections.Generic.ICollection<Reference> elements = null;
            switch (m_isolateMode)
            {
                case IsolateMode.PickOne: 
                    // One more element will be added to modscope 
                    // if user really selects an element which differs from the elements in 
                    // modscope before user's pick one operation.
                    elements.Add(m_document.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element));
                    break;
                case IsolateMode.WindowSelect:
                    // Elements will be added to modscope if possilbe.
                    elements = m_document.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element);
                    break;
                default:
                    break;
            }

            // ElementSet elements = m_document.Selection.Elements;

            // hide all categories elements
            foreach (Category cat in m_document.Document.Settings.Categories)
            {
                SetVisibility(false, cat.Name);
            }

            // set the visibility for the selection elements
            foreach (Reference reference in elements)
            {
                Category cat = m_document.Document.GetElement(reference).Category;
                if (null != cat && !string.IsNullOrEmpty(cat.Name))
                {
                    SetVisibility(true, cat.Name);
                }
            }
        }
    }
}